Add basic author form

This commit is contained in:
2025-11-25 14:14:24 +01:00
parent 3cbe9f909e
commit 624dfe0faa
10 changed files with 138 additions and 3 deletions

View 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})
}

View File

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