Skip to content

Commit

Permalink
feat: Temp library db changes
Browse files Browse the repository at this point in the history
  • Loading branch information
CrescentKohana committed Apr 14, 2022
1 parent 4a05722 commit 8652b80
Show file tree
Hide file tree
Showing 2 changed files with 101 additions and 8 deletions.
9 changes: 9 additions & 0 deletions pkg/api/api.go
Original file line number Diff line number Diff line change
Expand Up @@ -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"
Expand All @@ -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")
Expand Down
100 changes: 92 additions & 8 deletions pkg/db/library.go
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,9 @@ import (
"github.com/Mangatsu/server/internal/config"
"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"
)

func StorePaths(givenLibraries []config.Library) error {
Expand Down Expand Up @@ -41,19 +43,101 @@ func GetOnlyLibraries() ([]model.Library, error) {
return libraries, err
}

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) {
var libraries []model.CombinedLibrary
err := database.QB().
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.
Expand Down

0 comments on commit 8652b80

Please sign in to comment.