diff --git a/internal/dto/in.go b/internal/dto/in.go index 651ffa7..ab37a73 100644 --- a/internal/dto/in.go +++ b/internal/dto/in.go @@ -1,5 +1,36 @@ package dto +type AuthorGet struct { + Name string `json:"name" binding:"required,max=100"` + Description string `json:"description"` +} + type BookSearchGetParam struct { 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"` +} diff --git a/internal/dto/out.go b/internal/dto/out.go index ed81739..86c96cd 100644 --- a/internal/dto/out.go +++ b/internal/dto/out.go @@ -1,5 +1,30 @@ 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 { Count int64 `json:"count"` Books []BookSearchGetBook `json:"books"` diff --git a/internal/query/query.go b/internal/query/query.go index ba57cd9..a7dda92 100644 --- a/internal/query/query.go +++ b/internal/query/query.go @@ -1,28 +1,14 @@ package query import ( + "git.artlef.fr/PersonalLibraryManager/internal/dto" "git.artlef.fr/PersonalLibraryManager/internal/fileutils" "git.artlef.fr/PersonalLibraryManager/internal/model" "gorm.io/gorm" ) -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"` -} - -func FetchBookGet(db *gorm.DB, userId uint, bookId uint64) (BookGet, error) { - var book BookGet +func FetchBookGet(db *gorm.DB, userId uint, bookId uint64) (dto.BookGet, error) { + var book dto.BookGet query := db.Model(&model.Book{}) 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, " + @@ -38,18 +24,8 @@ func FetchBookGet(db *gorm.DB, userId uint, bookId uint64) (BookGet, error) { return book, res.Error } -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"` -} - -func FetchReadUserBook(db *gorm.DB, userId uint, limit int, offset int) ([]BookUserGet, error) { - var books []BookUserGet +func FetchReadUserBook(db *gorm.DB, userId uint, limit int, offset int) ([]dto.BookUserGet, error) { + var books []dto.BookUserGet query := fetchReadUserBookQuery(db, userId) query = query.Limit(limit) query = query.Offset(offset) @@ -64,8 +40,8 @@ func FetchReadUserBookCount(db *gorm.DB, userId uint) (int64, error) { return count, res.Error } -func FetchReadingUserBook(db *gorm.DB, userId uint, limit int, offset int) ([]BookUserGet, error) { - var books []BookUserGet +func FetchReadingUserBook(db *gorm.DB, userId uint, limit int, offset int) ([]dto.BookUserGet, error) { + var books []dto.BookUserGet query := fetchReadingUserBookQuery(db, userId) query = query.Limit(limit) query = query.Offset(offset) @@ -91,8 +67,8 @@ func fetchReadingUserBookQuery(db *gorm.DB, userId uint) *gorm.DB { return query } -func FetchWantReadUserBook(db *gorm.DB, userId uint, limit int, offset int) ([]BookUserGet, error) { - var books []BookUserGet +func FetchWantReadUserBook(db *gorm.DB, userId uint, limit int, offset int) ([]dto.BookUserGet, error) { + var books []dto.BookUserGet query := fetchWantReadUserBookQuery(db, userId) query = query.Limit(limit) diff --git a/internal/routes/authorget.go b/internal/routes/authorget.go index eaba881..0a16323 100644 --- a/internal/routes/authorget.go +++ b/internal/routes/authorget.go @@ -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}) } diff --git a/internal/routes/bookpostcreate.go b/internal/routes/bookpostcreate.go index 65bd777..1db304a 100644 --- a/internal/routes/bookpostcreate.go +++ b/internal/routes/bookpostcreate.go @@ -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 diff --git a/internal/routes/bookpostimport.go b/internal/routes/bookpostimport.go index 5f25fba..90bcdd6 100644 --- a/internal/routes/bookpostimport.go +++ b/internal/routes/bookpostimport.go @@ -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) diff --git a/internal/routes/bookputupdate.go b/internal/routes/bookputupdate.go deleted file mode 100644 index 9ebd52e..0000000 --- a/internal/routes/bookputupdate.go +++ /dev/null @@ -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) { - -} diff --git a/internal/routes/uploadbookcover.go b/internal/routes/uploadbookcover.go index 89e41c1..0e4bfd9 100644 --- a/internal/routes/uploadbookcover.go +++ b/internal/routes/uploadbookcover.go @@ -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, } diff --git a/internal/routes/userlogin.go b/internal/routes/userlogin.go index 6d49c55..4c724a8 100644 --- a/internal/routes/userlogin.go +++ b/internal/routes/userlogin.go @@ -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) diff --git a/internal/routes/usersignup.go b/internal/routes/usersignup.go index cb55c4a..d5fb115 100644 --- a/internal/routes/usersignup.go +++ b/internal/routes/usersignup.go @@ -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: "",