add post userbook
needed a refactor to check the id in db during dto validation
This commit is contained in:
5
internal/dto/userbookpostcreate.go
Normal file
5
internal/dto/userbookpostcreate.go
Normal file
@@ -0,0 +1,5 @@
|
|||||||
|
package dto
|
||||||
|
|
||||||
|
type UserBookPostCreate struct {
|
||||||
|
BookID uint `json:"bookId" binding:"required"`
|
||||||
|
}
|
||||||
@@ -20,7 +20,7 @@ func InitializeI18n() *i18n.Bundle {
|
|||||||
return bundle
|
return bundle
|
||||||
}
|
}
|
||||||
|
|
||||||
func GetTranslatedMessage(ac appcontext.AppContext, messageID string) string {
|
func GetTranslatedMessage(ac *appcontext.AppContext, messageID string) string {
|
||||||
localizer := i18n.NewLocalizer(ac.I18n, ac.C.GetHeader("Accept-Language"))
|
localizer := i18n.NewLocalizer(ac.I18n, ac.C.GetHeader("Accept-Language"))
|
||||||
message, err := localizer.LocalizeMessage(&i18n.Message{
|
message, err := localizer.LocalizeMessage(&i18n.Message{
|
||||||
ID: messageID,
|
ID: messageID,
|
||||||
|
|||||||
13
internal/mapper/userbookpostcreate.go
Normal file
13
internal/mapper/userbookpostcreate.go
Normal file
@@ -0,0 +1,13 @@
|
|||||||
|
package mapper
|
||||||
|
|
||||||
|
import (
|
||||||
|
"git.artlef.fr/PersonalLibraryManager/internal/dto"
|
||||||
|
"git.artlef.fr/PersonalLibraryManager/internal/model"
|
||||||
|
)
|
||||||
|
|
||||||
|
func UserBookWsToDb(ub dto.UserBookPostCreate, user *model.User) model.UserBook {
|
||||||
|
return model.UserBook{
|
||||||
|
UserID: user.ID,
|
||||||
|
BookID: ub.BookID,
|
||||||
|
}
|
||||||
|
}
|
||||||
@@ -9,23 +9,55 @@ import (
|
|||||||
"git.artlef.fr/PersonalLibraryManager/internal/i18nresource"
|
"git.artlef.fr/PersonalLibraryManager/internal/i18nresource"
|
||||||
"github.com/gin-gonic/gin"
|
"github.com/gin-gonic/gin"
|
||||||
"github.com/go-playground/validator/v10"
|
"github.com/go-playground/validator/v10"
|
||||||
|
"gorm.io/gorm"
|
||||||
)
|
)
|
||||||
|
|
||||||
|
type HttpError struct {
|
||||||
|
StatusCode int
|
||||||
|
Err error
|
||||||
|
}
|
||||||
|
|
||||||
|
func ValidateId(db *gorm.DB, id uint, value any) error {
|
||||||
|
record := map[string]any{}
|
||||||
|
result := db.Model(value).First(&record, id)
|
||||||
|
if result.Error == nil {
|
||||||
|
return nil
|
||||||
|
}
|
||||||
|
if errors.Is(result.Error, gorm.ErrRecordNotFound) {
|
||||||
|
return HttpError{
|
||||||
|
StatusCode: http.StatusBadRequest,
|
||||||
|
Err: fmt.Errorf("Id %d could not be found for model %s", id, value),
|
||||||
|
}
|
||||||
|
} else {
|
||||||
|
return HttpError{
|
||||||
|
StatusCode: http.StatusInternalServerError,
|
||||||
|
Err: result.Error,
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
func ReturnErrorsAsJsonResponse(ac *appcontext.AppContext, err error) {
|
||||||
|
var httpError HttpError
|
||||||
|
var ve validator.ValidationErrors
|
||||||
|
if errors.As(err, &ve) {
|
||||||
|
ac.C.JSON(http.StatusBadRequest, gin.H{"error": getValidationErrors(ac, &ve)})
|
||||||
|
} else if errors.As(err, &httpError) {
|
||||||
|
ac.C.JSON(httpError.StatusCode, gin.H{"error": httpError.Err})
|
||||||
|
} else {
|
||||||
|
ac.C.JSON(http.StatusInternalServerError, gin.H{"error": err})
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
func (h HttpError) Error() string {
|
||||||
|
return fmt.Sprintf("%d: err %v", h.StatusCode, h.Err)
|
||||||
|
}
|
||||||
|
|
||||||
type apiValidationError struct {
|
type apiValidationError struct {
|
||||||
Field string `json:"field"`
|
Field string `json:"field"`
|
||||||
Err string `json:"error"`
|
Err string `json:"error"`
|
||||||
}
|
}
|
||||||
|
|
||||||
func ManageBindingError(ac appcontext.AppContext, err error) {
|
func getValidationErrors(ac *appcontext.AppContext, ve *validator.ValidationErrors) []apiValidationError {
|
||||||
var ve validator.ValidationErrors
|
|
||||||
if errors.As(err, &ve) {
|
|
||||||
ac.C.JSON(http.StatusBadRequest, getValidationErrors(ac, &ve))
|
|
||||||
} else {
|
|
||||||
ac.C.JSON(http.StatusBadRequest, gin.H{"error": err.Error()})
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
func getValidationErrors(ac appcontext.AppContext, ve *validator.ValidationErrors) []apiValidationError {
|
|
||||||
errors := make([]apiValidationError, len(*ve))
|
errors := make([]apiValidationError, len(*ve))
|
||||||
for i, fe := range *ve {
|
for i, fe := range *ve {
|
||||||
errors[i] = apiValidationError{
|
errors[i] = apiValidationError{
|
||||||
@@ -36,7 +68,7 @@ func getValidationErrors(ac appcontext.AppContext, ve *validator.ValidationError
|
|||||||
return errors
|
return errors
|
||||||
}
|
}
|
||||||
|
|
||||||
func computeValidationMessage(ac appcontext.AppContext, fe *validator.FieldError) string {
|
func computeValidationMessage(ac *appcontext.AppContext, fe *validator.FieldError) string {
|
||||||
tag := (*fe).Tag()
|
tag := (*fe).Tag()
|
||||||
switch tag {
|
switch tag {
|
||||||
case "required":
|
case "required":
|
||||||
|
|||||||
@@ -1,32 +1,29 @@
|
|||||||
package routes
|
package routes
|
||||||
|
|
||||||
import (
|
import (
|
||||||
"net/http"
|
|
||||||
|
|
||||||
"git.artlef.fr/PersonalLibraryManager/internal/appcontext"
|
"git.artlef.fr/PersonalLibraryManager/internal/appcontext"
|
||||||
"git.artlef.fr/PersonalLibraryManager/internal/dto"
|
"git.artlef.fr/PersonalLibraryManager/internal/dto"
|
||||||
"git.artlef.fr/PersonalLibraryManager/internal/mapper"
|
"git.artlef.fr/PersonalLibraryManager/internal/mapper"
|
||||||
"git.artlef.fr/PersonalLibraryManager/internal/model"
|
"git.artlef.fr/PersonalLibraryManager/internal/model"
|
||||||
"git.artlef.fr/PersonalLibraryManager/internal/myvalidator"
|
"git.artlef.fr/PersonalLibraryManager/internal/myvalidator"
|
||||||
"github.com/gin-gonic/gin"
|
|
||||||
)
|
)
|
||||||
|
|
||||||
func PostBookHandler(ac appcontext.AppContext) {
|
func PostBookHandler(ac appcontext.AppContext) {
|
||||||
var book dto.BookPostCreate
|
var book dto.BookPostCreate
|
||||||
err := ac.C.ShouldBindJSON(&book)
|
err := ac.C.ShouldBindJSON(&book)
|
||||||
if err != nil {
|
if err != nil {
|
||||||
myvalidator.ManageBindingError(ac, err)
|
myvalidator.ReturnErrorsAsJsonResponse(&ac, err)
|
||||||
return
|
return
|
||||||
}
|
}
|
||||||
user, fetchUserErr := ac.GetAuthenticatedUser()
|
user, fetchUserErr := ac.GetAuthenticatedUser()
|
||||||
if fetchUserErr != nil {
|
if fetchUserErr != nil {
|
||||||
ac.C.JSON(http.StatusBadRequest, gin.H{"error": err.Error()})
|
myvalidator.ReturnErrorsAsJsonResponse(&ac, err)
|
||||||
return
|
return
|
||||||
}
|
}
|
||||||
bookDb := mapper.BookWsToDb(book, &user)
|
bookDb := mapper.BookWsToDb(book, &user)
|
||||||
err = ac.Db.Model(&model.Book{}).Save(&bookDb).Error
|
err = ac.Db.Model(&model.Book{}).Save(&bookDb).Error
|
||||||
if err != nil {
|
if err != nil {
|
||||||
ac.C.JSON(http.StatusBadRequest, gin.H{"error": err.Error()})
|
myvalidator.ReturnErrorsAsJsonResponse(&ac, err)
|
||||||
return
|
return
|
||||||
}
|
}
|
||||||
ac.C.String(200, "Success")
|
ac.C.String(200, "Success")
|
||||||
|
|||||||
@@ -7,14 +7,14 @@ import (
|
|||||||
"git.artlef.fr/PersonalLibraryManager/internal/dto"
|
"git.artlef.fr/PersonalLibraryManager/internal/dto"
|
||||||
"git.artlef.fr/PersonalLibraryManager/internal/mapper"
|
"git.artlef.fr/PersonalLibraryManager/internal/mapper"
|
||||||
"git.artlef.fr/PersonalLibraryManager/internal/model"
|
"git.artlef.fr/PersonalLibraryManager/internal/model"
|
||||||
"github.com/gin-gonic/gin"
|
"git.artlef.fr/PersonalLibraryManager/internal/myvalidator"
|
||||||
)
|
)
|
||||||
|
|
||||||
func GetMyBooksHanderl(ac appcontext.AppContext) {
|
func GetMyBooksHanderl(ac appcontext.AppContext) {
|
||||||
var userbooks []model.UserBook
|
var userbooks []model.UserBook
|
||||||
user, err := ac.GetAuthenticatedUser()
|
user, err := ac.GetAuthenticatedUser()
|
||||||
if err != nil {
|
if err != nil {
|
||||||
ac.C.JSON(http.StatusBadRequest, gin.H{"error": err.Error()})
|
myvalidator.ReturnErrorsAsJsonResponse(&ac, err)
|
||||||
return
|
return
|
||||||
}
|
}
|
||||||
ac.Db.Preload("Book").Where("user_id = ?", user.ID).Find(&userbooks)
|
ac.Db.Preload("Book").Where("user_id = ?", user.ID).Find(&userbooks)
|
||||||
|
|||||||
37
internal/routes/userbookpostcreate.go
Normal file
37
internal/routes/userbookpostcreate.go
Normal file
@@ -0,0 +1,37 @@
|
|||||||
|
package routes
|
||||||
|
|
||||||
|
import (
|
||||||
|
"net/http"
|
||||||
|
|
||||||
|
"git.artlef.fr/PersonalLibraryManager/internal/appcontext"
|
||||||
|
"git.artlef.fr/PersonalLibraryManager/internal/dto"
|
||||||
|
"git.artlef.fr/PersonalLibraryManager/internal/mapper"
|
||||||
|
"git.artlef.fr/PersonalLibraryManager/internal/model"
|
||||||
|
"git.artlef.fr/PersonalLibraryManager/internal/myvalidator"
|
||||||
|
)
|
||||||
|
|
||||||
|
func PostUserBookHandler(ac appcontext.AppContext) {
|
||||||
|
var userbook dto.UserBookPostCreate
|
||||||
|
err := ac.C.ShouldBindJSON(&userbook)
|
||||||
|
if err != nil {
|
||||||
|
myvalidator.ReturnErrorsAsJsonResponse(&ac, err)
|
||||||
|
return
|
||||||
|
}
|
||||||
|
err = myvalidator.ValidateId(ac.Db, userbook.BookID, &model.Book{})
|
||||||
|
if err != nil {
|
||||||
|
myvalidator.ReturnErrorsAsJsonResponse(&ac, err)
|
||||||
|
return
|
||||||
|
}
|
||||||
|
user, fetchUserErr := ac.GetAuthenticatedUser()
|
||||||
|
if fetchUserErr != nil {
|
||||||
|
myvalidator.ReturnErrorsAsJsonResponse(&ac, err)
|
||||||
|
return
|
||||||
|
}
|
||||||
|
userbookDb := mapper.UserBookWsToDb(userbook, &user)
|
||||||
|
err = ac.Db.Save(&userbookDb).Error
|
||||||
|
if err != nil {
|
||||||
|
myvalidator.ReturnErrorsAsJsonResponse(&ac, err)
|
||||||
|
return
|
||||||
|
}
|
||||||
|
ac.C.String(http.StatusOK, "Success")
|
||||||
|
}
|
||||||
@@ -19,13 +19,13 @@ func PostLoginHandler(ac appcontext.AppContext) {
|
|||||||
var user dto.UserLogin
|
var user dto.UserLogin
|
||||||
err := ac.C.ShouldBindJSON(&user)
|
err := ac.C.ShouldBindJSON(&user)
|
||||||
if err != nil {
|
if err != nil {
|
||||||
myvalidator.ManageBindingError(ac, err)
|
myvalidator.ReturnErrorsAsJsonResponse(&ac, err)
|
||||||
return
|
return
|
||||||
}
|
}
|
||||||
|
|
||||||
if !isUserAndPasswordOk(ac.Db, user.Username, user.Password) {
|
if !isUserAndPasswordOk(ac.Db, user.Username, user.Password) {
|
||||||
ac.C.JSON(http.StatusInternalServerError,
|
ac.C.JSON(http.StatusUnauthorized,
|
||||||
gin.H{"error": i18nresource.GetTranslatedMessage(ac, "InvalidCredentials")})
|
gin.H{"error": i18nresource.GetTranslatedMessage(&ac, "InvalidCredentials")})
|
||||||
return
|
return
|
||||||
}
|
}
|
||||||
|
|
||||||
@@ -36,7 +36,7 @@ func PostLoginHandler(ac appcontext.AppContext) {
|
|||||||
gin.H{"error": fmt.Errorf("Error when generating JWT token: %w", err)})
|
gin.H{"error": fmt.Errorf("Error when generating JWT token: %w", err)})
|
||||||
return
|
return
|
||||||
}
|
}
|
||||||
ac.C.JSON(200, gin.H{"message": i18nresource.GetTranslatedMessage(ac, "AuthenticationSuccess"), "token": jwtToken})
|
ac.C.JSON(http.StatusOK, gin.H{"message": i18nresource.GetTranslatedMessage(&ac, "AuthenticationSuccess"), "token": jwtToken})
|
||||||
}
|
}
|
||||||
|
|
||||||
func isUserAndPasswordOk(db *gorm.DB, username string, password string) bool {
|
func isUserAndPasswordOk(db *gorm.DB, username string, password string) bool {
|
||||||
|
|||||||
@@ -1,31 +1,28 @@
|
|||||||
package routes
|
package routes
|
||||||
|
|
||||||
import (
|
import (
|
||||||
"net/http"
|
|
||||||
|
|
||||||
"git.artlef.fr/PersonalLibraryManager/internal/appcontext"
|
"git.artlef.fr/PersonalLibraryManager/internal/appcontext"
|
||||||
"git.artlef.fr/PersonalLibraryManager/internal/dto"
|
"git.artlef.fr/PersonalLibraryManager/internal/dto"
|
||||||
"git.artlef.fr/PersonalLibraryManager/internal/mapper"
|
"git.artlef.fr/PersonalLibraryManager/internal/mapper"
|
||||||
"git.artlef.fr/PersonalLibraryManager/internal/model"
|
"git.artlef.fr/PersonalLibraryManager/internal/model"
|
||||||
"git.artlef.fr/PersonalLibraryManager/internal/myvalidator"
|
"git.artlef.fr/PersonalLibraryManager/internal/myvalidator"
|
||||||
"github.com/gin-gonic/gin"
|
|
||||||
)
|
)
|
||||||
|
|
||||||
func PostSignupHandler(ac appcontext.AppContext) {
|
func PostSignupHandler(ac appcontext.AppContext) {
|
||||||
var user dto.UserSignup
|
var user dto.UserSignup
|
||||||
err := ac.C.ShouldBindJSON(&user)
|
err := ac.C.ShouldBindJSON(&user)
|
||||||
if err != nil {
|
if err != nil {
|
||||||
myvalidator.ManageBindingError(ac, err)
|
myvalidator.ReturnErrorsAsJsonResponse(&ac, err)
|
||||||
return
|
return
|
||||||
}
|
}
|
||||||
userDb, err := mapper.UserWsToDb(user)
|
userDb, err := mapper.UserWsToDb(user)
|
||||||
if err != nil {
|
if err != nil {
|
||||||
ac.C.JSON(http.StatusBadRequest, gin.H{"error": err.Error()})
|
myvalidator.ReturnErrorsAsJsonResponse(&ac, err)
|
||||||
return
|
return
|
||||||
}
|
}
|
||||||
err = ac.Db.Model(&model.User{}).Save(&userDb).Error
|
err = ac.Db.Model(&model.User{}).Save(&userDb).Error
|
||||||
if err != nil {
|
if err != nil {
|
||||||
ac.C.JSON(http.StatusBadRequest, gin.H{"error": err.Error()})
|
myvalidator.ReturnErrorsAsJsonResponse(&ac, err)
|
||||||
return
|
return
|
||||||
}
|
}
|
||||||
ac.C.String(200, "Success")
|
ac.C.String(200, "Success")
|
||||||
|
|||||||
@@ -32,6 +32,9 @@ func Setup(config *config.Config) *gin.Engine {
|
|||||||
r.POST("/book", func(c *gin.Context) {
|
r.POST("/book", func(c *gin.Context) {
|
||||||
routes.PostBookHandler(appcontext.AppContext{C: c, Db: db, I18n: bundle})
|
routes.PostBookHandler(appcontext.AppContext{C: c, Db: db, I18n: bundle})
|
||||||
})
|
})
|
||||||
|
r.POST("/userbook", func(c *gin.Context) {
|
||||||
|
routes.PostUserBookHandler(appcontext.AppContext{C: c, Db: db, I18n: bundle})
|
||||||
|
})
|
||||||
r.POST("/auth/signup", func(c *gin.Context) {
|
r.POST("/auth/signup", func(c *gin.Context) {
|
||||||
routes.PostSignupHandler(appcontext.AppContext{C: c, Db: db, I18n: bundle})
|
routes.PostSignupHandler(appcontext.AppContext{C: c, Db: db, I18n: bundle})
|
||||||
})
|
})
|
||||||
|
|||||||
43
post_userbook_test.go
Normal file
43
post_userbook_test.go
Normal file
@@ -0,0 +1,43 @@
|
|||||||
|
package main
|
||||||
|
|
||||||
|
import (
|
||||||
|
"fmt"
|
||||||
|
"log"
|
||||||
|
"net/http"
|
||||||
|
"net/http/httptest"
|
||||||
|
"strings"
|
||||||
|
"testing"
|
||||||
|
|
||||||
|
"git.artlef.fr/PersonalLibraryManager/internal/testutils"
|
||||||
|
"github.com/stretchr/testify/assert"
|
||||||
|
)
|
||||||
|
|
||||||
|
func TestPostUserBookHandler_Ok(t *testing.T) {
|
||||||
|
userBookJson :=
|
||||||
|
`{
|
||||||
|
"bookId": 6
|
||||||
|
}`
|
||||||
|
testPostUserBookHandler(t, userBookJson, http.StatusOK)
|
||||||
|
}
|
||||||
|
|
||||||
|
func TestPostUserBookHandler_IDDoesNotExist(t *testing.T) {
|
||||||
|
userBookJson :=
|
||||||
|
`{
|
||||||
|
"bookId": 46546
|
||||||
|
}`
|
||||||
|
testPostUserBookHandler(t, userBookJson, http.StatusBadRequest)
|
||||||
|
}
|
||||||
|
|
||||||
|
func testPostUserBookHandler(t *testing.T, userBookJson string, expectedCode int) {
|
||||||
|
router := testutils.TestSetup()
|
||||||
|
w := httptest.NewRecorder()
|
||||||
|
|
||||||
|
token := testutils.ConnectDemo2User(router)
|
||||||
|
req, _ := http.NewRequest("POST", "/userbook",
|
||||||
|
strings.NewReader(string(userBookJson)))
|
||||||
|
req.Header.Add("Authorization", fmt.Sprintf("Bearer %s", token))
|
||||||
|
router.ServeHTTP(w, req)
|
||||||
|
log.Printf("%s\n", w.Body.String())
|
||||||
|
assert.Equal(t, expectedCode, w.Code)
|
||||||
|
|
||||||
|
}
|
||||||
Reference in New Issue
Block a user