35 lines
871 B
Go
35 lines
871 B
Go
package routes
|
|
|
|
import (
|
|
"net/http"
|
|
"strconv"
|
|
|
|
"git.artlef.fr/bibliomane/internal/appcontext"
|
|
"git.artlef.fr/bibliomane/internal/dto"
|
|
"git.artlef.fr/bibliomane/internal/model"
|
|
"git.artlef.fr/bibliomane/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})
|
|
}
|