Skip to content

Commit 8652b80

Browse files
feat: Temp library db changes
1 parent 4a05722 commit 8652b80

File tree

2 files changed

+101
-8
lines changed

2 files changed

+101
-8
lines changed

pkg/api/api.go

+9
Original file line numberDiff line numberDiff line change
@@ -98,6 +98,14 @@ func errorHandler(w http.ResponseWriter, status int, msg string) {
9898
}
9999
}
100100

101+
func returnTest(w http.ResponseWriter, r *http.Request) {
102+
libraries, err := db.GetLibraries()
103+
if handleResult(w, libraries, err, true) {
104+
return
105+
}
106+
resultToJSON(w, libraries)
107+
}
108+
101109
// Handles HTTP(S) requests.
102110
func handleRequests() {
103111
baseURL := "/api/v1"
@@ -122,6 +130,7 @@ func handleRequests() {
122130
r.HandleFunc(baseURL+"/scan", scanLibraries).Methods("GET")
123131
r.HandleFunc(baseURL+"/thumbnails", generateThumbnails).Methods("GET")
124132
r.HandleFunc(baseURL+"/meta", findMetadata).Methods("GET")
133+
r.HandleFunc(baseURL+"/test", returnTest).Methods("GET")
125134

126135
r.HandleFunc(baseURL+"/categories", returnCategories).Methods("GET")
127136
r.HandleFunc(baseURL+"/series", returnSeries).Methods("GET")

pkg/db/library.go

+92-8
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,9 @@ import (
44
"github.com/Mangatsu/server/internal/config"
55
"github.com/Mangatsu/server/pkg/model"
66
"github.com/doug-martin/goqu/v9"
7+
"github.com/doug-martin/goqu/v9/exec"
78
log "github.com/sirupsen/logrus"
9+
"time"
810
)
911

1012
func StorePaths(givenLibraries []config.Library) error {
@@ -41,19 +43,101 @@ func GetOnlyLibraries() ([]model.Library, error) {
4143
return libraries, err
4244
}
4345

46+
type LibraryRow struct {
47+
ID int32 `db:"id"`
48+
Path string `db:"path"`
49+
Layout string `db:"layout"`
50+
UUID string `db:"uuid"`
51+
LibraryID int32 `db:"library_id"`
52+
ArchivePath string `db:"archive_path"`
53+
Title string `db:"title"`
54+
TitleNative *string `db:"title_native"`
55+
TitleTranslated *string `db:"title_translated"`
56+
Category *string `db:"category"`
57+
Series *string `db:"series"`
58+
Released *string `db:"released"`
59+
Language *string `db:"language"`
60+
Translated *bool `db:"translated"`
61+
Nsfw bool `db:"nsfw"`
62+
Hidden bool `db:"hidden"`
63+
ImageCount *int32 `db:"image_count"`
64+
ArchiveSize *int32 `db:"archive_size"`
65+
ArchiveHash *string `db:"archive_hash"`
66+
Thumbnail *string `db:"thumbnail"`
67+
CreatedAt time.Time `db:"created_at"`
68+
UpdatedAt time.Time `db:"updated_at"`
69+
}
70+
4471
func GetLibraries() ([]model.CombinedLibrary, error) {
45-
var libraries []model.CombinedLibrary
46-
err := database.QB().
72+
scanner, err := database.QB().
4773
From("library").
48-
LeftJoin(
74+
Join(
4975
goqu.T("gallery"),
50-
goqu.On(goqu.Ex{
51-
"gallery.id": goqu.I("library.id"),
52-
}),
76+
goqu.On(goqu.I("gallery.library_id").Eq(goqu.I("library.id"))),
5377
).
54-
ScanStructs(&libraries)
78+
Executor().
79+
Scanner()
5580

56-
return libraries, err
81+
if err != nil {
82+
log.Error(err)
83+
return nil, err
84+
}
85+
86+
defer func(scanner exec.Scanner) {
87+
if err := scanner.Close(); err != nil {
88+
log.Error(err)
89+
}
90+
}(scanner)
91+
92+
librariesMap := make(map[int32]model.CombinedLibrary)
93+
for scanner.Next() {
94+
lr := LibraryRow{}
95+
if err = scanner.ScanStruct(&lr); err != nil {
96+
log.Error(err)
97+
return nil, err
98+
}
99+
100+
var gallery = model.Gallery{UUID: lr.UUID,
101+
Title: lr.Title,
102+
TitleNative: lr.TitleNative,
103+
TitleTranslated: lr.TitleTranslated,
104+
Category: lr.Category,
105+
Series: lr.Series,
106+
Released: lr.Released,
107+
Language: lr.Language,
108+
Translated: lr.Translated,
109+
Nsfw: lr.Nsfw,
110+
Hidden: lr.Hidden,
111+
ImageCount: lr.ImageCount,
112+
ArchiveSize: lr.ArchiveSize,
113+
ArchiveHash: lr.ArchiveHash,
114+
Thumbnail: lr.Thumbnail,
115+
CreatedAt: lr.CreatedAt,
116+
UpdatedAt: lr.UpdatedAt,
117+
}
118+
119+
value, ok := librariesMap[lr.ID]
120+
if ok {
121+
value.Galleries = append(value.Galleries, gallery)
122+
librariesMap[lr.ID] = value
123+
} else {
124+
librariesMap[lr.ID] = model.CombinedLibrary{
125+
Library: model.Library{
126+
ID: lr.ID,
127+
Path: lr.Path,
128+
Layout: lr.Layout,
129+
},
130+
Galleries: []model.Gallery{gallery},
131+
}
132+
}
133+
}
134+
135+
librariesSlice := make([]model.CombinedLibrary, 0, len(librariesMap))
136+
for _, val := range librariesMap {
137+
librariesSlice = append(librariesSlice, val)
138+
}
139+
140+
return librariesSlice, nil
57141
}
58142

59143
// getLibrary returns the library from the database based on the ID or path.

0 commit comments

Comments
 (0)