Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add new db models #25

Draft
wants to merge 4 commits into
base: goqu-rework
Choose a base branch
from
Draft
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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
117 changes: 94 additions & 23 deletions pkg/db/library.go
Original file line number Diff line number Diff line change
Expand Up @@ -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, "")
Expand Down Expand Up @@ -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.
Expand Down
3 changes: 2 additions & 1 deletion pkg/metadata/title.go
Original file line number Diff line number Diff line change
Expand Up @@ -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)
}
Expand Down
27 changes: 27 additions & 0 deletions pkg/model/gallery.go
Original file line number Diff line number Diff line change
@@ -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"`
}
13 changes: 13 additions & 0 deletions pkg/model/gallery_pref.go
Original file line number Diff line number Diff line change
@@ -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"`
}
6 changes: 6 additions & 0 deletions pkg/model/gallery_tag.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
package model

type GalleryTag struct {
GalleryUUID string `db:"gallery_uuid"`
TagID int32 `db:"tag_id"`
}
12 changes: 12 additions & 0 deletions pkg/model/library.go
Original file line number Diff line number Diff line change
@@ -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"`
}
12 changes: 12 additions & 0 deletions pkg/model/reference.go
Original file line number Diff line number Diff line change
@@ -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"`
}
12 changes: 12 additions & 0 deletions pkg/model/session.go
Original file line number Diff line number Diff line change
@@ -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"`
}
7 changes: 7 additions & 0 deletions pkg/model/tag.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
package model

type Tag struct {
ID int32 `db:"id"`
Namespace string `db:"namespace"`
Name string `db:"name"`
}
14 changes: 14 additions & 0 deletions pkg/model/user.go
Original file line number Diff line number Diff line change
@@ -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"`
}