diff --git a/pkg/api/api.go b/pkg/api/api.go index 139e199..5d09928 100644 --- a/pkg/api/api.go +++ b/pkg/api/api.go @@ -98,6 +98,14 @@ func errorHandler(w http.ResponseWriter, status int, msg string) { } } +func returnTest(w http.ResponseWriter, r *http.Request) { + libraries, err := db.GetLibraries() + if handleResult(w, libraries, err, true) { + return + } + resultToJSON(w, libraries) +} + // Handles HTTP(S) requests. func handleRequests() { baseURL := "/api/v1" @@ -122,6 +130,7 @@ func handleRequests() { r.HandleFunc(baseURL+"/scan", scanLibraries).Methods("GET") r.HandleFunc(baseURL+"/thumbnails", generateThumbnails).Methods("GET") r.HandleFunc(baseURL+"/meta", findMetadata).Methods("GET") + r.HandleFunc(baseURL+"/test", returnTest).Methods("GET") r.HandleFunc(baseURL+"/categories", returnCategories).Methods("GET") r.HandleFunc(baseURL+"/series", returnSeries).Methods("GET") diff --git a/pkg/db/library.go b/pkg/db/library.go index 10af56c..b3bb991 100644 --- a/pkg/db/library.go +++ b/pkg/db/library.go @@ -2,24 +2,13 @@ package db import ( "github.com/Mangatsu/server/internal/config" - "github.com/Mangatsu/server/pkg/types/model" + "github.com/Mangatsu/server/pkg/model" "github.com/doug-martin/goqu/v9" + "github.com/doug-martin/goqu/v9/exec" log "github.com/sirupsen/logrus" + "time" ) -// FIXME: it won't work yet!! CombinedLibrary has to be a plain struct, -// something like this: -// -// type User struct { -// FirstName string `db:"first_name"` -// LastName string `db:"last_name"` -// } - -type CombinedLibrary struct { - model.Library - Galleries []model.Gallery -} - func StorePaths(givenLibraries []config.Library) error { for _, library := range givenLibraries { libraries, err := getLibrary(library.ID, "") @@ -54,19 +43,101 @@ func GetOnlyLibraries() ([]model.Library, error) { return libraries, err } -func GetLibraries() ([]CombinedLibrary, error) { - var libraries []CombinedLibrary - err := database.QB(). +type LibraryRow struct { + ID int32 `db:"id"` + Path string `db:"path"` + Layout string `db:"layout"` + UUID string `db:"uuid"` + LibraryID int32 `db:"library_id"` + ArchivePath string `db:"archive_path"` + Title string `db:"title"` + TitleNative *string `db:"title_native"` + TitleTranslated *string `db:"title_translated"` + Category *string `db:"category"` + Series *string `db:"series"` + Released *string `db:"released"` + Language *string `db:"language"` + Translated *bool `db:"translated"` + Nsfw bool `db:"nsfw"` + Hidden bool `db:"hidden"` + ImageCount *int32 `db:"image_count"` + ArchiveSize *int32 `db:"archive_size"` + ArchiveHash *string `db:"archive_hash"` + Thumbnail *string `db:"thumbnail"` + CreatedAt time.Time `db:"created_at"` + UpdatedAt time.Time `db:"updated_at"` +} + +func GetLibraries() ([]model.CombinedLibrary, error) { + scanner, err := database.QB(). From("library"). - LeftJoin( + Join( goqu.T("gallery"), - goqu.On(goqu.Ex{ - "gallery.id": goqu.I("library.id"), - }), + goqu.On(goqu.I("gallery.library_id").Eq(goqu.I("library.id"))), ). - ScanStructs(&libraries) + Executor(). + Scanner() - return libraries, err + if err != nil { + log.Error(err) + return nil, err + } + + defer func(scanner exec.Scanner) { + if err := scanner.Close(); err != nil { + log.Error(err) + } + }(scanner) + + librariesMap := make(map[int32]model.CombinedLibrary) + for scanner.Next() { + lr := LibraryRow{} + if err = scanner.ScanStruct(&lr); err != nil { + log.Error(err) + return nil, err + } + + var gallery = model.Gallery{UUID: lr.UUID, + Title: lr.Title, + TitleNative: lr.TitleNative, + TitleTranslated: lr.TitleTranslated, + Category: lr.Category, + Series: lr.Series, + Released: lr.Released, + Language: lr.Language, + Translated: lr.Translated, + Nsfw: lr.Nsfw, + Hidden: lr.Hidden, + ImageCount: lr.ImageCount, + ArchiveSize: lr.ArchiveSize, + ArchiveHash: lr.ArchiveHash, + Thumbnail: lr.Thumbnail, + CreatedAt: lr.CreatedAt, + UpdatedAt: lr.UpdatedAt, + } + + value, ok := librariesMap[lr.ID] + if ok { + value.Galleries = append(value.Galleries, gallery) + librariesMap[lr.ID] = value + } else { + librariesMap[lr.ID] = model.CombinedLibrary{ + Library: model.Library{ + ID: lr.ID, + Path: lr.Path, + Layout: lr.Layout, + }, + Galleries: []model.Gallery{gallery}, + } + } + } + + librariesSlice := make([]model.CombinedLibrary, 0, len(librariesMap)) + for _, val := range librariesMap { + librariesSlice = append(librariesSlice, val) + } + + return librariesSlice, nil } // getLibrary returns the library from the database based on the ID or path. diff --git a/pkg/metadata/title.go b/pkg/metadata/title.go index 7e97d8e..f3a1870 100644 --- a/pkg/metadata/title.go +++ b/pkg/metadata/title.go @@ -133,7 +133,8 @@ func ParseTitles(tryNative bool, overwrite bool) { gallery.Category = &manga } - err = db.UpdateGallery(gallery, currentTags, currentReference, true) + // FIXME: Remove the type assertion after UpdateGallery is reworked. + err = db.UpdateGallery(model.Gallery(gallery), currentTags, currentReference, true) if err != nil { log.Errorf("Error updating gallery %s based on its title: %s", gallery.UUID, err) } diff --git a/pkg/model/gallery.go b/pkg/model/gallery.go new file mode 100644 index 0000000..54e9800 --- /dev/null +++ b/pkg/model/gallery.go @@ -0,0 +1,27 @@ +package model + +import ( + "time" +) + +type Gallery struct { + UUID string `db:"uuid"` + LibraryID int32 `db:"library_id"` + ArchivePath string `db:"archive_path"` + Title string `db:"title"` + TitleNative *string `db:"title_native"` + TitleTranslated *string `db:"title_translated"` + Category *string `db:"category"` + Series *string `db:"series"` + Released *string `db:"released"` + Language *string `db:"language"` + Translated *bool `db:"translated"` + Nsfw bool `db:"nsfw"` + Hidden bool `db:"hidden"` + ImageCount *int32 `db:"image_count"` + ArchiveSize *int32 `db:"archive_size"` + ArchiveHash *string `db:"archive_hash"` + Thumbnail *string `db:"thumbnail"` + CreatedAt time.Time `db:"created_at"` + UpdatedAt time.Time `db:"updated_at"` +} diff --git a/pkg/model/gallery_pref.go b/pkg/model/gallery_pref.go new file mode 100644 index 0000000..1fe8088 --- /dev/null +++ b/pkg/model/gallery_pref.go @@ -0,0 +1,13 @@ +package model + +import ( + "time" +) + +type GalleryPref struct { + UserUUID string `db:"user_uuid"` + GalleryUUID string `db:"gallery_uuid"` + Progress int32 `db:"progress"` + FavoriteGroup *string `db:"favorite_group"` + UpdatedAt time.Time `db:"updated_at"` +} diff --git a/pkg/model/gallery_tag.go b/pkg/model/gallery_tag.go new file mode 100644 index 0000000..c63a6cd --- /dev/null +++ b/pkg/model/gallery_tag.go @@ -0,0 +1,6 @@ +package model + +type GalleryTag struct { + GalleryUUID string `db:"gallery_uuid"` + TagID int32 `db:"tag_id"` +} diff --git a/pkg/model/library.go b/pkg/model/library.go new file mode 100644 index 0000000..5511ba0 --- /dev/null +++ b/pkg/model/library.go @@ -0,0 +1,12 @@ +package model + +type Library struct { + ID int32 `db:"id"` + Path string `db:"path"` + Layout string `db:"layout"` +} + +type CombinedLibrary struct { + Library + Galleries []Gallery `db:"gallery"` +} diff --git a/pkg/model/reference.go b/pkg/model/reference.go new file mode 100644 index 0000000..5941503 --- /dev/null +++ b/pkg/model/reference.go @@ -0,0 +1,12 @@ +package model + +type Reference struct { + GalleryUUID string `db:"gallery_uuid"` + MetaInternal bool `db:"meta_internal"` + MetaPath *string `db:"meta_path"` + MetaMatch *int32 `db:"meta_match"` + Urls *string `db:"urls"` + ExhGid *int32 `db:"exh_gid"` + ExhToken *string `db:"exh_token"` + AnilistID *int32 `db:"anilist_id"` +} diff --git a/pkg/model/session.go b/pkg/model/session.go new file mode 100644 index 0000000..6d28c47 --- /dev/null +++ b/pkg/model/session.go @@ -0,0 +1,12 @@ +package model + +import ( + "time" +) + +type Session struct { + ID string `db:"id"` + UserUUID string `db:"user_uuid"` + Name *string `db:"name"` + ExpiresAt *time.Time `db:"expires_at"` +} diff --git a/pkg/model/tag.go b/pkg/model/tag.go new file mode 100644 index 0000000..85b7d5f --- /dev/null +++ b/pkg/model/tag.go @@ -0,0 +1,7 @@ +package model + +type Tag struct { + ID int32 `db:"id"` + Namespace string `db:"namespace"` + Name string `db:"name"` +} diff --git a/pkg/model/user.go b/pkg/model/user.go new file mode 100644 index 0000000..409a335 --- /dev/null +++ b/pkg/model/user.go @@ -0,0 +1,14 @@ +package model + +import ( + "time" +) + +type User struct { + UUID string `db:"uuid"` + Username string `db:"username"` + Password string `db:"password"` + Role int32 `db:"role"` + CreatedAt time.Time `db:"created_at"` + UpdatedAt time.Time `db:"updated_at"` +}