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