work on file endpoints (WIP, not working)

This commit is contained in:
Sonja Happ 2019-05-16 16:58:53 +02:00
parent 5a6ec9108c
commit 473abac8f4
3 changed files with 118 additions and 13 deletions

24
common/dberrorhandling.go Normal file
View file

@ -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
}

View file

@ -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) {
@ -14,17 +20,39 @@ func FilesRegister(r *gin.RouterGroup) {
}
func filesReadEp(c *gin.Context) {
allFiles, _, _ := FindAllFiles()
// 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) {
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{
"message": "NOT implemented",
"file": serializer.Response(),
})
}
} else {
//TODO: return file itself
}
}
func fileDeleteEp(c *gin.Context) {

View file

@ -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
}
@ -17,3 +26,28 @@ func FindUserFiles(user *common.User) ([]common.File, int, error) {
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
}