fuelly api complete

This commit is contained in:
Akhil Gupta
2021-06-26 09:48:29 +05:30
parent 2bd8481670
commit 5f96345828
6 changed files with 207 additions and 0 deletions

View File

@@ -1,6 +1,7 @@
package controllers
import (
"io/ioutil"
"net/http"
"os"
@@ -116,6 +117,18 @@ func getAttachmentFile(c *gin.Context) {
}
}
func getFileBytes(c *gin.Context, fileVariable string) ([]byte, error) {
if fileVariable == "" {
fileVariable = "file"
}
formFile, err := c.FormFile(fileVariable)
if err != nil {
return nil, err
}
openedFile, _ := formFile.Open()
return ioutil.ReadAll(openedFile)
}
func saveUploadedFile(c *gin.Context, fileVariable string) (*db.Attachment, error) {
if fileVariable == "" {
fileVariable = "file"

View File

@@ -0,0 +1,26 @@
package controllers
import (
"net/http"
"github.com/akhilrex/hammond/service"
"github.com/gin-gonic/gin"
)
func RegisteImportController(router *gin.RouterGroup) {
router.POST("/import/fuelly", fuellyImport)
}
func fuellyImport(c *gin.Context) {
bytes, err := getFileBytes(c, "file")
if err != nil {
c.JSON(http.StatusUnprocessableEntity, err)
return
}
err = service.FuellyImport(bytes, c.MustGet("userId").(string))
if err != nil {
c.JSON(http.StatusUnprocessableEntity, gin.H{"errors": err.Error()})
return
}
c.JSON(http.StatusOK, gin.H{})
}