Move json struct in a new module

This commit is contained in:
2026-01-23 18:26:47 +01:00
parent e1fd12e233
commit 4ac0f4243d
10 changed files with 80 additions and 86 deletions

View File

@@ -5,16 +5,12 @@ import (
"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"
)
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 {
@@ -32,7 +28,7 @@ func GetAuthorHandler(ac appcontext.AppContext) {
myvalidator.ReturnErrorsAsJsonResponse(&ac, res.Error)
return
}
ac.C.JSON(http.StatusOK, AuthorGet{
ac.C.JSON(http.StatusOK, dto.AuthorGet{
Name: author.Name,
Description: author.Description})
}

View File

@@ -4,19 +4,14 @@ import (
"errors"
"git.artlef.fr/PersonalLibraryManager/internal/appcontext"
"git.artlef.fr/PersonalLibraryManager/internal/dto"
"git.artlef.fr/PersonalLibraryManager/internal/model"
"git.artlef.fr/PersonalLibraryManager/internal/myvalidator"
"gorm.io/gorm"
)
type bookPostCreate struct {
Title string `json:"title" binding:"required,max=300"`
Author string `json:"author" binding:"max=100"`
CoverID uint `json:"coverId"`
}
func PostBookHandler(ac appcontext.AppContext) {
var book bookPostCreate
var book dto.BookPostCreate
err := ac.C.ShouldBindJSON(&book)
if err != nil {
myvalidator.ReturnErrorsAsJsonResponse(&ac, err)
@@ -41,7 +36,7 @@ func PostBookHandler(ac appcontext.AppContext) {
ac.C.String(200, "Success")
}
func saveBookToDb(ac appcontext.AppContext, b bookPostCreate, user *model.User) error {
func saveBookToDb(ac appcontext.AppContext, b dto.BookPostCreate, user *model.User) error {
author, err := fetchOrCreateAuthor(ac, b.Author)
if err != nil {
return err

View File

@@ -4,6 +4,7 @@ import (
"errors"
"git.artlef.fr/PersonalLibraryManager/internal/appcontext"
"git.artlef.fr/PersonalLibraryManager/internal/dto"
"git.artlef.fr/PersonalLibraryManager/internal/inventaire"
"git.artlef.fr/PersonalLibraryManager/internal/model"
"git.artlef.fr/PersonalLibraryManager/internal/myvalidator"
@@ -12,13 +13,8 @@ import (
"gorm.io/gorm"
)
type bookPostImport struct {
InventaireID string `json:"inventaireid" binding:"required,max=50"`
Lang string `json:"lang" binding:"required,max=5"`
}
func PostImportBookHandler(ac appcontext.AppContext) {
var request bookPostImport
var request dto.BookPostImport
err := ac.C.ShouldBindJSON(&request)
if err != nil {
myvalidator.ReturnErrorsAsJsonResponse(&ac, err)

View File

@@ -1,13 +0,0 @@
package routes
import "git.artlef.fr/PersonalLibraryManager/internal/appcontext"
type bookPutUpdate struct {
Title string `json:"title" binding:"required,max=300"`
Author string `json:"author" binding:"max=100"`
CoverID uint `json:"coverId"`
}
func PutBookHandler(ac appcontext.AppContext) {
}

View File

@@ -4,16 +4,12 @@ import (
"net/http"
"git.artlef.fr/PersonalLibraryManager/internal/appcontext"
"git.artlef.fr/PersonalLibraryManager/internal/dto"
"git.artlef.fr/PersonalLibraryManager/internal/fileutils"
"git.artlef.fr/PersonalLibraryManager/internal/model"
"git.artlef.fr/PersonalLibraryManager/internal/myvalidator"
)
type fileInfoPost struct {
FileID uint `json:"fileId"`
FilePath string `json:"filepath"`
}
func PostUploadBookCoverHandler(ac appcontext.AppContext) {
file, err := ac.C.FormFile("file")
if err != nil {
@@ -28,8 +24,8 @@ func PostUploadBookCoverHandler(ac appcontext.AppContext) {
ac.C.JSON(http.StatusOK, staticFileDbToWs(&staticFile))
}
func staticFileDbToWs(f *model.StaticFile) fileInfoPost {
return fileInfoPost{
func staticFileDbToWs(f *model.StaticFile) dto.FileInfoPost {
return dto.FileInfoPost{
FileID: f.ID,
FilePath: fileutils.GetWsLinkPrefix() + f.Path,
}

View File

@@ -5,6 +5,7 @@ import (
"net/http"
"git.artlef.fr/PersonalLibraryManager/internal/appcontext"
"git.artlef.fr/PersonalLibraryManager/internal/dto"
"git.artlef.fr/PersonalLibraryManager/internal/i18nresource"
"git.artlef.fr/PersonalLibraryManager/internal/jwtauth"
"git.artlef.fr/PersonalLibraryManager/internal/model"
@@ -14,13 +15,8 @@ import (
"gorm.io/gorm"
)
type userLogin struct {
Username string `json:"username" binding:"required,min=2,max=20"`
Password string `json:"password" binding:"required,min=6,max=100"`
}
func PostLoginHandler(ac appcontext.AppContext) {
var user userLogin
var user dto.UserLogin
err := ac.C.ShouldBindJSON(&user)
if err != nil {
myvalidator.ReturnErrorsAsJsonResponse(&ac, err)

View File

@@ -2,18 +2,14 @@ package routes
import (
"git.artlef.fr/PersonalLibraryManager/internal/appcontext"
"git.artlef.fr/PersonalLibraryManager/internal/dto"
"git.artlef.fr/PersonalLibraryManager/internal/model"
"git.artlef.fr/PersonalLibraryManager/internal/myvalidator"
"golang.org/x/crypto/bcrypt"
)
type userSignup struct {
Username string `json:"username" binding:"required,min=2,max=20"`
Password string `json:"password" binding:"required,min=6,max=100"`
}
func PostSignupHandler(ac appcontext.AppContext) {
var user userSignup
var user dto.UserSignup
err := ac.C.ShouldBindJSON(&user)
if err != nil {
myvalidator.ReturnErrorsAsJsonResponse(&ac, err)
@@ -32,7 +28,7 @@ func PostSignupHandler(ac appcontext.AppContext) {
ac.C.String(200, "Success")
}
func userWsToDb(u userSignup) (model.User, error) {
func userWsToDb(u dto.UserSignup) (model.User, error) {
user := model.User{
Name: u.Username,
Password: "",