Files
bibliomane/internal/routes/authorget.go

35 lines
919 B
Go

package routes
import (
"net/http"
"strconv"
"git.artlef.fr/PersonalLibraryManager/internal/appcontext"
"git.artlef.fr/PersonalLibraryManager/internal/dto"
"git.artlef.fr/PersonalLibraryManager/internal/model"
"git.artlef.fr/PersonalLibraryManager/internal/myvalidator"
"github.com/gin-gonic/gin"
)
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, dto.AuthorGet{
Name: author.Name,
Description: author.Description})
}