diff --git a/routes/file/fileEndpoints.go b/routes/file/fileEndpoints.go index b691ba5..2628e76 100644 --- a/routes/file/fileEndpoints.go +++ b/routes/file/fileEndpoints.go @@ -1 +1,48 @@ package file + +import ( + "github.com/gin-gonic/gin" + "net/http" +) + +func FilesRegister(r *gin.RouterGroup) { + r.GET("/", filesReadEp) + //r.POST("/", fileRegistrationEp) // TODO to be added to API + //r.PUT("/:fileID", fileUpdateEp) // TODO to be added to API + r.GET("/:fileID", fileReadEp) + r.DELETE("/:fileID", fileDeleteEp) +} + +func filesReadEp(c *gin.Context) { + allFiles, _, _ := FindAllFiles() + serializer := FilesSerializer{c, allFiles} + c.JSON(http.StatusOK, gin.H{ + "users": serializer.Response(), + }) +} + +// TODO to be added to API +//func fileRegistrationEp(c *gin.Context) { +// c.JSON(http.StatusOK, gin.H{ +// "message": "NOT implemented", +// }) +//} + +// TODO to be added to API +//func fileUpdateEp(c *gin.Context) { +// c.JSON(http.StatusOK, gin.H{ +// "message": "NOT implemented", +// }) +//} + +func fileReadEp(c *gin.Context) { + c.JSON(http.StatusOK, gin.H{ + "message": "NOT implemented", + }) +} + +func fileDeleteEp(c *gin.Context) { + c.JSON(http.StatusOK, gin.H{ + "message": "NOT implemented", + }) +} diff --git a/routes/file/fileQueries.go b/routes/file/fileQueries.go index b691ba5..44e7f17 100644 --- a/routes/file/fileQueries.go +++ b/routes/file/fileQueries.go @@ -1 +1,12 @@ package file + +import ( + "git.rwth-aachen.de/acs/public/villas/villasweb-backend-go/common" +) + +func FindAllFiles() ([]common.File, int, error) { + db := common.GetDB() + var files []common.File + err := db.Find(&files).Error + return files, len(files), err +} diff --git a/routes/file/fileSerializer.go b/routes/file/fileSerializer.go index b691ba5..754d8e5 100644 --- a/routes/file/fileSerializer.go +++ b/routes/file/fileSerializer.go @@ -1 +1,53 @@ package file + +import ( + "time" + + "github.com/gin-gonic/gin" + + "git.rwth-aachen.de/acs/public/villas/villasweb-backend-go/common" +) + +type FilesSerializer struct { + Ctx *gin.Context + Files []common.File +} + +func (self *FilesSerializer) Response() []FileResponse { + response := []FileResponse{} + for _, File := range self.Files { + serializer := FileSerializer{self.Ctx, File} + response = append(response, serializer.Response()) + } + return response +} + +type FileSerializer struct { + Ctx *gin.Context + common.File +} + +type FileResponse struct { + Name string `json:"Name"` + ID uint `json:"FileID"` + Path string `json:"Path"` + Type string `json:"Type"` //MIME type? + Size uint `json:"Size"` + H uint `json:"ImageHeight"` + W uint `json:"ImageWidth"` + Date time.Time `json:"Date"` +} + +func (self *FileSerializer) Response() FileResponse { + + response := FileResponse{ + Name: self.Name, + Path: self.Path, + Type: self.Type, + Size: self.Size, + Date: self.Date, + H: self.ImageHeight, + W: self.ImageWidth, + } + return response +} diff --git a/routes/user/userQueries.go b/routes/user/userQueries.go index 2356505..031331d 100644 --- a/routes/user/userQueries.go +++ b/routes/user/userQueries.go @@ -2,8 +2,6 @@ package user import ( "git.rwth-aachen.de/acs/public/villas/villasweb-backend-go/common" - //"github.com/jinzhu/gorm" - //"github.com/jinzhu/gorm/dialects/postgres" ) func FindAllUsers() ([]common.User, int, error) {