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

@@ -1,5 +1,36 @@
package dto package dto
type AuthorGet struct {
Name string `json:"name" binding:"required,max=100"`
Description string `json:"description"`
}
type BookSearchGetParam struct { type BookSearchGetParam struct {
Lang string `form:"lang" binding:"max=5"` Lang string `form:"lang" binding:"max=5"`
} }
type BookPostCreate struct {
Title string `json:"title" binding:"required,max=300"`
Author string `json:"author" binding:"max=100"`
CoverID uint `json:"coverId"`
}
type BookPostImport struct {
InventaireID string `json:"inventaireid" binding:"required,max=50"`
Lang string `json:"lang" binding:"required,max=5"`
}
type FileInfoPost struct {
FileID uint `json:"fileId"`
FilePath string `json:"filepath"`
}
type UserLogin struct {
Username string `json:"username" binding:"required,min=2,max=20"`
Password string `json:"password" binding:"required,min=6,max=100"`
}
type UserSignup struct {
Username string `json:"username" binding:"required,min=2,max=20"`
Password string `json:"password" binding:"required,min=6,max=100"`
}

View File

@@ -1,5 +1,30 @@
package dto package dto
type BookGet struct {
Title string `json:"title" binding:"required,max=300"`
Author string `json:"author" binding:"max=100"`
ISBN string `json:"isbn"`
InventaireId string `json:"inventaireid"`
OpenLibraryId string `json:"openlibraryid"`
Summary string `json:"summary"`
Rating int `json:"rating"`
Read bool `json:"read"`
WantRead bool `json:"wantread"`
StartReadDate string `json:"startReadDate"`
EndReadDate string `json:"endReadDate"`
CoverPath string `json:"coverPath"`
}
type BookUserGet struct {
ID uint `json:"id"`
Title string `json:"title" binding:"required,max=300"`
Author string `json:"author" binding:"max=100"`
Rating int `json:"rating" binding:"min=0,max=10"`
Read bool `json:"read" binding:"boolean"`
WantRead bool `json:"wantread" binding:"boolean"`
CoverPath string `json:"coverPath"`
}
type BookSearchGet struct { type BookSearchGet struct {
Count int64 `json:"count"` Count int64 `json:"count"`
Books []BookSearchGetBook `json:"books"` Books []BookSearchGetBook `json:"books"`

View File

@@ -1,28 +1,14 @@
package query package query
import ( import (
"git.artlef.fr/PersonalLibraryManager/internal/dto"
"git.artlef.fr/PersonalLibraryManager/internal/fileutils" "git.artlef.fr/PersonalLibraryManager/internal/fileutils"
"git.artlef.fr/PersonalLibraryManager/internal/model" "git.artlef.fr/PersonalLibraryManager/internal/model"
"gorm.io/gorm" "gorm.io/gorm"
) )
type BookGet struct { func FetchBookGet(db *gorm.DB, userId uint, bookId uint64) (dto.BookGet, error) {
Title string `json:"title" binding:"required,max=300"` var book dto.BookGet
Author string `json:"author" binding:"max=100"`
ISBN string `json:"isbn"`
InventaireId string `json:"inventaireid"`
OpenLibraryId string `json:"openlibraryid"`
Summary string `json:"summary"`
Rating int `json:"rating"`
Read bool `json:"read"`
WantRead bool `json:"wantread"`
StartReadDate string `json:"startReadDate"`
EndReadDate string `json:"endReadDate"`
CoverPath string `json:"coverPath"`
}
func FetchBookGet(db *gorm.DB, userId uint, bookId uint64) (BookGet, error) {
var book BookGet
query := db.Model(&model.Book{}) query := db.Model(&model.Book{})
selectQueryString := "books.title, authors.name as author, books.isbn, books.inventaire_id, books.open_library_id, books.summary, " + selectQueryString := "books.title, authors.name as author, books.isbn, books.inventaire_id, books.open_library_id, books.summary, " +
"user_books.rating, user_books.read, user_books.want_read, " + "user_books.rating, user_books.read, user_books.want_read, " +
@@ -38,18 +24,8 @@ func FetchBookGet(db *gorm.DB, userId uint, bookId uint64) (BookGet, error) {
return book, res.Error return book, res.Error
} }
type BookUserGet struct { func FetchReadUserBook(db *gorm.DB, userId uint, limit int, offset int) ([]dto.BookUserGet, error) {
ID uint `json:"id"` var books []dto.BookUserGet
Title string `json:"title" binding:"required,max=300"`
Author string `json:"author" binding:"max=100"`
Rating int `json:"rating" binding:"min=0,max=10"`
Read bool `json:"read" binding:"boolean"`
WantRead bool `json:"wantread" binding:"boolean"`
CoverPath string `json:"coverPath"`
}
func FetchReadUserBook(db *gorm.DB, userId uint, limit int, offset int) ([]BookUserGet, error) {
var books []BookUserGet
query := fetchReadUserBookQuery(db, userId) query := fetchReadUserBookQuery(db, userId)
query = query.Limit(limit) query = query.Limit(limit)
query = query.Offset(offset) query = query.Offset(offset)
@@ -64,8 +40,8 @@ func FetchReadUserBookCount(db *gorm.DB, userId uint) (int64, error) {
return count, res.Error return count, res.Error
} }
func FetchReadingUserBook(db *gorm.DB, userId uint, limit int, offset int) ([]BookUserGet, error) { func FetchReadingUserBook(db *gorm.DB, userId uint, limit int, offset int) ([]dto.BookUserGet, error) {
var books []BookUserGet var books []dto.BookUserGet
query := fetchReadingUserBookQuery(db, userId) query := fetchReadingUserBookQuery(db, userId)
query = query.Limit(limit) query = query.Limit(limit)
query = query.Offset(offset) query = query.Offset(offset)
@@ -91,8 +67,8 @@ func fetchReadingUserBookQuery(db *gorm.DB, userId uint) *gorm.DB {
return query return query
} }
func FetchWantReadUserBook(db *gorm.DB, userId uint, limit int, offset int) ([]BookUserGet, error) { func FetchWantReadUserBook(db *gorm.DB, userId uint, limit int, offset int) ([]dto.BookUserGet, error) {
var books []BookUserGet var books []dto.BookUserGet
query := fetchWantReadUserBookQuery(db, userId) query := fetchWantReadUserBookQuery(db, userId)
query = query.Limit(limit) query = query.Limit(limit)

View File

@@ -5,16 +5,12 @@ import (
"strconv" "strconv"
"git.artlef.fr/PersonalLibraryManager/internal/appcontext" "git.artlef.fr/PersonalLibraryManager/internal/appcontext"
"git.artlef.fr/PersonalLibraryManager/internal/dto"
"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" "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) { func GetAuthorHandler(ac appcontext.AppContext) {
authorId, err := strconv.ParseUint(ac.C.Param("id"), 10, 64) authorId, err := strconv.ParseUint(ac.C.Param("id"), 10, 64)
if err != nil { if err != nil {
@@ -32,7 +28,7 @@ func GetAuthorHandler(ac appcontext.AppContext) {
myvalidator.ReturnErrorsAsJsonResponse(&ac, res.Error) myvalidator.ReturnErrorsAsJsonResponse(&ac, res.Error)
return return
} }
ac.C.JSON(http.StatusOK, AuthorGet{ ac.C.JSON(http.StatusOK, dto.AuthorGet{
Name: author.Name, Name: author.Name,
Description: author.Description}) Description: author.Description})
} }

View File

@@ -4,19 +4,14 @@ import (
"errors" "errors"
"git.artlef.fr/PersonalLibraryManager/internal/appcontext" "git.artlef.fr/PersonalLibraryManager/internal/appcontext"
"git.artlef.fr/PersonalLibraryManager/internal/dto"
"git.artlef.fr/PersonalLibraryManager/internal/model" "git.artlef.fr/PersonalLibraryManager/internal/model"
"git.artlef.fr/PersonalLibraryManager/internal/myvalidator" "git.artlef.fr/PersonalLibraryManager/internal/myvalidator"
"gorm.io/gorm" "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) { func PostBookHandler(ac appcontext.AppContext) {
var book bookPostCreate var book dto.BookPostCreate
err := ac.C.ShouldBindJSON(&book) err := ac.C.ShouldBindJSON(&book)
if err != nil { if err != nil {
myvalidator.ReturnErrorsAsJsonResponse(&ac, err) myvalidator.ReturnErrorsAsJsonResponse(&ac, err)
@@ -41,7 +36,7 @@ func PostBookHandler(ac appcontext.AppContext) {
ac.C.String(200, "Success") 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) author, err := fetchOrCreateAuthor(ac, b.Author)
if err != nil { if err != nil {
return err return err

View File

@@ -4,6 +4,7 @@ import (
"errors" "errors"
"git.artlef.fr/PersonalLibraryManager/internal/appcontext" "git.artlef.fr/PersonalLibraryManager/internal/appcontext"
"git.artlef.fr/PersonalLibraryManager/internal/dto"
"git.artlef.fr/PersonalLibraryManager/internal/inventaire" "git.artlef.fr/PersonalLibraryManager/internal/inventaire"
"git.artlef.fr/PersonalLibraryManager/internal/model" "git.artlef.fr/PersonalLibraryManager/internal/model"
"git.artlef.fr/PersonalLibraryManager/internal/myvalidator" "git.artlef.fr/PersonalLibraryManager/internal/myvalidator"
@@ -12,13 +13,8 @@ import (
"gorm.io/gorm" "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) { func PostImportBookHandler(ac appcontext.AppContext) {
var request bookPostImport var request dto.BookPostImport
err := ac.C.ShouldBindJSON(&request) err := ac.C.ShouldBindJSON(&request)
if err != nil { if err != nil {
myvalidator.ReturnErrorsAsJsonResponse(&ac, err) 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" "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/fileutils" "git.artlef.fr/PersonalLibraryManager/internal/fileutils"
"git.artlef.fr/PersonalLibraryManager/internal/model" "git.artlef.fr/PersonalLibraryManager/internal/model"
"git.artlef.fr/PersonalLibraryManager/internal/myvalidator" "git.artlef.fr/PersonalLibraryManager/internal/myvalidator"
) )
type fileInfoPost struct {
FileID uint `json:"fileId"`
FilePath string `json:"filepath"`
}
func PostUploadBookCoverHandler(ac appcontext.AppContext) { func PostUploadBookCoverHandler(ac appcontext.AppContext) {
file, err := ac.C.FormFile("file") file, err := ac.C.FormFile("file")
if err != nil { if err != nil {
@@ -28,8 +24,8 @@ func PostUploadBookCoverHandler(ac appcontext.AppContext) {
ac.C.JSON(http.StatusOK, staticFileDbToWs(&staticFile)) ac.C.JSON(http.StatusOK, staticFileDbToWs(&staticFile))
} }
func staticFileDbToWs(f *model.StaticFile) fileInfoPost { func staticFileDbToWs(f *model.StaticFile) dto.FileInfoPost {
return fileInfoPost{ return dto.FileInfoPost{
FileID: f.ID, FileID: f.ID,
FilePath: fileutils.GetWsLinkPrefix() + f.Path, FilePath: fileutils.GetWsLinkPrefix() + f.Path,
} }

View File

@@ -5,6 +5,7 @@ import (
"net/http" "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/i18nresource" "git.artlef.fr/PersonalLibraryManager/internal/i18nresource"
"git.artlef.fr/PersonalLibraryManager/internal/jwtauth" "git.artlef.fr/PersonalLibraryManager/internal/jwtauth"
"git.artlef.fr/PersonalLibraryManager/internal/model" "git.artlef.fr/PersonalLibraryManager/internal/model"
@@ -14,13 +15,8 @@ import (
"gorm.io/gorm" "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) { func PostLoginHandler(ac appcontext.AppContext) {
var user userLogin var user dto.UserLogin
err := ac.C.ShouldBindJSON(&user) err := ac.C.ShouldBindJSON(&user)
if err != nil { if err != nil {
myvalidator.ReturnErrorsAsJsonResponse(&ac, err) myvalidator.ReturnErrorsAsJsonResponse(&ac, err)

View File

@@ -2,18 +2,14 @@ package routes
import ( import (
"git.artlef.fr/PersonalLibraryManager/internal/appcontext" "git.artlef.fr/PersonalLibraryManager/internal/appcontext"
"git.artlef.fr/PersonalLibraryManager/internal/dto"
"git.artlef.fr/PersonalLibraryManager/internal/model" "git.artlef.fr/PersonalLibraryManager/internal/model"
"git.artlef.fr/PersonalLibraryManager/internal/myvalidator" "git.artlef.fr/PersonalLibraryManager/internal/myvalidator"
"golang.org/x/crypto/bcrypt" "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) { func PostSignupHandler(ac appcontext.AppContext) {
var user userSignup var user dto.UserSignup
err := ac.C.ShouldBindJSON(&user) err := ac.C.ShouldBindJSON(&user)
if err != nil { if err != nil {
myvalidator.ReturnErrorsAsJsonResponse(&ac, err) myvalidator.ReturnErrorsAsJsonResponse(&ac, err)
@@ -32,7 +28,7 @@ func PostSignupHandler(ac appcontext.AppContext) {
ac.C.String(200, "Success") ac.C.String(200, "Success")
} }
func userWsToDb(u userSignup) (model.User, error) { func userWsToDb(u dto.UserSignup) (model.User, error) {
user := model.User{ user := model.User{
Name: u.Username, Name: u.Username,
Password: "", Password: "",