mirror of
https://git.rwth-aachen.de/acs/public/villas/web-backend-go/
synced 2025-03-30 00:00:12 +01:00
work on file endpoints (WIP, not working)
This commit is contained in:
parent
5a6ec9108c
commit
473abac8f4
3 changed files with 118 additions and 13 deletions
24
common/dberrorhandling.go
Normal file
24
common/dberrorhandling.go
Normal 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
|
||||||
|
}
|
|
@ -1,8 +1,14 @@
|
||||||
package file
|
package file
|
||||||
|
|
||||||
import (
|
import (
|
||||||
"github.com/gin-gonic/gin"
|
"strconv"
|
||||||
|
|
||||||
"net/http"
|
"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) {
|
func FilesRegister(r *gin.RouterGroup) {
|
||||||
|
@ -13,18 +19,40 @@ func FilesRegister(r *gin.RouterGroup) {
|
||||||
r.DELETE("/:FileID", fileDeleteEp)
|
r.DELETE("/:FileID", fileDeleteEp)
|
||||||
}
|
}
|
||||||
|
|
||||||
func filesReadEp(c *gin.Context) {
|
func filesReadEp(c *gin.Context) {
|
||||||
allFiles, _, _ := FindAllFiles()
|
// Database query
|
||||||
serializer := FilesSerializerNoAssoc{c, allFiles}
|
allFiles, _, err := FindAllFiles()
|
||||||
c.JSON(http.StatusOK, gin.H{
|
|
||||||
"files": serializer.Response(),
|
if common.ProvideErrorResponse(c, err) == false {
|
||||||
})
|
serializer := FilesSerializerNoAssoc{c, allFiles}
|
||||||
|
c.JSON(http.StatusOK, gin.H{
|
||||||
|
"files": serializer.Response(),
|
||||||
|
})
|
||||||
|
}
|
||||||
|
|
||||||
}
|
}
|
||||||
|
|
||||||
func fileRegistrationEp(c *gin.Context) {
|
func fileRegistrationEp(c *gin.Context) {
|
||||||
c.JSON(http.StatusOK, gin.H{
|
var m map[string]interface{}
|
||||||
"message": "NOT implemented",
|
|
||||||
})
|
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) {
|
func fileUpdateEp(c *gin.Context) {
|
||||||
|
@ -34,9 +62,28 @@ func fileUpdateEp(c *gin.Context) {
|
||||||
}
|
}
|
||||||
|
|
||||||
func fileReadEp(c *gin.Context) {
|
func fileReadEp(c *gin.Context) {
|
||||||
c.JSON(http.StatusOK, gin.H{
|
var err error
|
||||||
"message": "NOT implemented",
|
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) {
|
func fileDeleteEp(c *gin.Context) {
|
||||||
|
|
|
@ -1,6 +1,11 @@
|
||||||
package file
|
package file
|
||||||
|
|
||||||
import (
|
import (
|
||||||
|
"fmt"
|
||||||
|
"strconv"
|
||||||
|
|
||||||
|
_ "github.com/gin-gonic/gin"
|
||||||
|
|
||||||
"git.rwth-aachen.de/acs/public/villas/villasweb-backend-go/common"
|
"git.rwth-aachen.de/acs/public/villas/villasweb-backend-go/common"
|
||||||
)
|
)
|
||||||
|
|
||||||
|
@ -8,6 +13,10 @@ func FindAllFiles() ([]common.File, int, error) {
|
||||||
db := common.GetDB()
|
db := common.GetDB()
|
||||||
var files []common.File
|
var files []common.File
|
||||||
err := db.Find(&files).Error
|
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
|
return files, len(files), err
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@ -16,4 +25,29 @@ func FindUserFiles(user *common.User) ([]common.File, int, error) {
|
||||||
var files []common.File
|
var files []common.File
|
||||||
err := db.Model(user).Related(&files, "Files").Error
|
err := db.Model(user).Related(&files, "Files").Error
|
||||||
return files, len(files), err
|
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
|
||||||
|
|
||||||
}
|
}
|
Loading…
Add table
Reference in a new issue