Add basic author form
This commit is contained in:
38
internal/routes/authorget.go
Normal file
38
internal/routes/authorget.go
Normal file
@@ -0,0 +1,38 @@
|
||||
package routes
|
||||
|
||||
import (
|
||||
"net/http"
|
||||
"strconv"
|
||||
|
||||
"git.artlef.fr/PersonalLibraryManager/internal/appcontext"
|
||||
"git.artlef.fr/PersonalLibraryManager/internal/model"
|
||||
"git.artlef.fr/PersonalLibraryManager/internal/myvalidator"
|
||||
"github.com/gin-gonic/gin"
|
||||
)
|
||||
|
||||
type AuthorGet struct {
|
||||
Name string `json:"name" binding:"required,max=100"`
|
||||
Description string `json:"description"`
|
||||
}
|
||||
|
||||
func GetAuthorHandler(ac appcontext.AppContext) {
|
||||
authorId, err := strconv.ParseUint(ac.C.Param("id"), 10, 64)
|
||||
if err != nil {
|
||||
ac.C.JSON(http.StatusBadRequest, gin.H{"error": err})
|
||||
return
|
||||
}
|
||||
err = myvalidator.ValidateId(ac.Db, uint(authorId), &model.Author{})
|
||||
if err != nil {
|
||||
myvalidator.ReturnErrorsAsJsonResponse(&ac, err)
|
||||
return
|
||||
}
|
||||
var author model.Author
|
||||
res := ac.Db.First(&author, authorId)
|
||||
if res.Error != nil {
|
||||
myvalidator.ReturnErrorsAsJsonResponse(&ac, res.Error)
|
||||
return
|
||||
}
|
||||
ac.C.JSON(http.StatusOK, AuthorGet{
|
||||
Name: author.Name,
|
||||
Description: author.Description})
|
||||
}
|
||||
@@ -29,7 +29,7 @@ func GetBookHandler(ac appcontext.AppContext) {
|
||||
}
|
||||
book, queryErr := query.FetchBookGet(ac.Db, user.ID, bookId)
|
||||
if queryErr != nil {
|
||||
myvalidator.ReturnErrorsAsJsonResponse(&ac, err)
|
||||
myvalidator.ReturnErrorsAsJsonResponse(&ac, queryErr)
|
||||
return
|
||||
}
|
||||
ac.C.JSON(http.StatusOK, book)
|
||||
|
||||
Reference in New Issue
Block a user