diff --git a/common/dberrorhandling.go b/common/dberrorhandling.go new file mode 100644 index 0000000..31c2f5c --- /dev/null +++ b/common/dberrorhandling.go @@ -0,0 +1,24 @@ +package common + +import ( + "net/http" + + "github.com/gin-gonic/gin" + "github.com/jinzhu/gorm" +) + +func ProvideErrorResponse(c *gin.Context, err error) bool { + if err != nil { + if err == gorm.ErrRecordNotFound { + c.JSON(http.StatusNotFound, gin.H{ + "error": "No files found in DB", + }) + } else { + c.JSON(http.StatusInternalServerError, gin.H{ + "error": "Error on DB Query or transaction", + }) + } + return true // Error + } + return false // No error +} diff --git a/routes/file/fileEndpoints.go b/routes/file/fileEndpoints.go index afdaa41..65a1938 100644 --- a/routes/file/fileEndpoints.go +++ b/routes/file/fileEndpoints.go @@ -1,8 +1,14 @@ package file import ( - "github.com/gin-gonic/gin" + "strconv" + "net/http" + + "github.com/gin-gonic/gin" + "github.com/gin-gonic/gin/json" + + "git.rwth-aachen.de/acs/public/villas/villasweb-backend-go/common" ) func FilesRegister(r *gin.RouterGroup) { @@ -13,18 +19,40 @@ func FilesRegister(r *gin.RouterGroup) { r.DELETE("/:FileID", fileDeleteEp) } -func filesReadEp(c *gin.Context) { - allFiles, _, _ := FindAllFiles() - serializer := FilesSerializerNoAssoc{c, allFiles} - c.JSON(http.StatusOK, gin.H{ - "files": serializer.Response(), - }) +func filesReadEp(c *gin.Context) { + // Database query + allFiles, _, err := FindAllFiles() + + if common.ProvideErrorResponse(c, err) == false { + serializer := FilesSerializerNoAssoc{c, allFiles} + c.JSON(http.StatusOK, gin.H{ + "files": serializer.Response(), + }) + } + } func fileRegistrationEp(c *gin.Context) { - c.JSON(http.StatusOK, gin.H{ - "message": "NOT implemented", - }) + var m map[string]interface{} + + decoder := json.NewDecoder(c.Request.Body) + defer c.Request.Body.Close() + + if err := decoder.Decode(&m); err != nil { + c.JSON(http.StatusBadRequest, gin.H{ + "error": "Bad request. Invalid body.", + }) + return; + } + + // Database query + err := AddFile(m) + + if common.ProvideErrorResponse(c, err) == false { + c.JSON(http.StatusOK, gin.H{ + "message": "OK.", + }) + } } func fileUpdateEp(c *gin.Context) { @@ -34,9 +62,28 @@ func fileUpdateEp(c *gin.Context) { } func fileReadEp(c *gin.Context) { - c.JSON(http.StatusOK, gin.H{ - "message": "NOT implemented", - }) + var err error + var file common.File + fileID := c.Param("FileID") + desc := c.GetHeader("X-Request-FileDesc") + desc_b, _ := strconv.ParseBool(desc) + + userID := 1 // TODO obtain ID of user making the request + + //check if description of file or file itself shall be returned + if desc_b { + file, err = FindFile(userID, fileID) + if common.ProvideErrorResponse(c, err) == false { + serializer := FileSerializerNoAssoc{c, file} + c.JSON(http.StatusOK, gin.H{ + "file": serializer.Response(), + }) + } + + + } else { + //TODO: return file itself + } } func fileDeleteEp(c *gin.Context) { diff --git a/routes/file/fileQueries.go b/routes/file/fileQueries.go index 6c977ed..8efaab5 100644 --- a/routes/file/fileQueries.go +++ b/routes/file/fileQueries.go @@ -1,6 +1,11 @@ package file import ( + "fmt" + "strconv" + + _ "github.com/gin-gonic/gin" + "git.rwth-aachen.de/acs/public/villas/villasweb-backend-go/common" ) @@ -8,6 +13,10 @@ func FindAllFiles() ([]common.File, int, error) { db := common.GetDB() var files []common.File err := db.Find(&files).Error + if err != nil { + // print error message to screen + fmt.Println(fmt.Errorf("DB Error in FindAllFiles(): %q", err).Error()) + } return files, len(files), err } @@ -16,4 +25,29 @@ func FindUserFiles(user *common.User) ([]common.File, int, error) { var files []common.File err := db.Model(user).Related(&files, "Files").Error return files, len(files), err +} + +func FindFile(userID int, fileID string) ( common.File, error) { + //TODO Check here if user owns the file + var file common.File + db := common.GetDB() + fileID_i, _ := strconv.Atoi(fileID) + + err := db.First(&file, fileID_i).Error + + return file, err + +} + +func AddFile(m map[string]interface{}) error { + + // TODO deserialize m (JSON file object) to data struct File + + // TODO we need the user here as well to be able to create the association in the DB + + // TODO add deserialized File to DB + + var err error + return err + } \ No newline at end of file