Initial test, but meh

This commit is contained in:
Pablu23
2024-05-21 11:56:14 +02:00
parent 0904a1214e
commit 20ad56b155
6 changed files with 63 additions and 36 deletions

View File

@@ -1,7 +1,8 @@
package database package database
type Chapter struct { type Chapter struct {
Id int `gorm:"primary_key;AUTO_INCREMENT"` Id int `gorm:"primary_key;autoIncrement;"`
ChapterId int
Url string Url string
Name string Name string
Number string Number string
@@ -11,7 +12,7 @@ type Chapter struct {
func NewChapter(id int, mangaId int, url string, name string, number string, timeStampUnix int64) Chapter { func NewChapter(id int, mangaId int, url string, name string, number string, timeStampUnix int64) Chapter {
return Chapter{ return Chapter{
Id: id, ChapterId: id,
Url: url, Url: url,
Name: name, Name: name,
Number: number, Number: number,

View File

@@ -54,6 +54,6 @@ func (dbMgr *Manager) Delete(mangaId int) {
} }
func (dbMgr *Manager) createDatabaseIfNotExists() error { func (dbMgr *Manager) createDatabaseIfNotExists() error {
err := dbMgr.Db.AutoMigrate(&Manga{}, &Chapter{}, &Setting{}) err := dbMgr.Db.AutoMigrate(&MangaDefinition{}, &User{}, &Manga{}, &Chapter{}, &Setting{})
return err return err
} }

View File

@@ -1,17 +1,26 @@
package database package database
type Manga struct { type MangaDefinition struct {
Id int `gorm:"primary_key;AUTO_INCREMENT"` Id int `gorm:"primary_key;AUTO_INCREMENT"`
Title string Title string
TimeStampUnix int64 TimeStampUnix int64
Thumbnail []byte Thumbnail []byte
LastChapterNum string LastChapterNum string
Chapters []Chapter // Chapters []Chapter
//`gorm:"foreignkey:MangaID"` //`gorm:"foreignkey:MangaID"`
} }
func NewManga(id int, title string, timeStampUnix int64) Manga { type Manga struct {
return Manga{ Id int `gorm:"primary_key;AUTO_INCREMENT"`
MangaDefinitionId int
Definition MangaDefinition `gorm:"foreignKey:MangaDefinitionId"`
UserId int
TimeStampUnix int64
Chapters []Chapter `gorm:"foreignKey:MangaId"`
}
func NewMangaDefinition(id int, title string, timeStampUnix int64) MangaDefinition {
return MangaDefinition{
Id: id, Id: id,
Title: title, Title: title,
TimeStampUnix: timeStampUnix, TimeStampUnix: timeStampUnix,
@@ -21,20 +30,20 @@ func NewManga(id int, title string, timeStampUnix int64) Manga {
// GetLatestChapter TODO: Cache this somehow // GetLatestChapter TODO: Cache this somehow
func (m *Manga) GetLatestChapter() (*Chapter, bool) { func (m *Manga) GetLatestChapter() (*Chapter, bool) {
highest := int64(0) // highest := int64(0)
index := 0 // index := 0
for i, chapter := range m.Chapters { // for i, chapter := range m.Chapters {
if chapter.MangaId == m.Id && highest < chapter.TimeStampUnix { // if chapter.MangaId == m.Manga.Id && highest < chapter.TimeStampUnix {
highest = chapter.TimeStampUnix // highest = chapter.TimeStampUnix
index = i // index = i
} // }
} // }
if highest == 0 { // if highest == 0 {
return nil, false return nil, false
} // }
return &m.Chapters[index], true // return &m.Chapters[index], true
//result := db.Where("manga.id = ?", m.Id).Order("TimeStampUnix desc").Take(&chapter) //result := db.Where("manga.id = ?", m.Id).Order("TimeStampUnix desc").Take(&chapter)
//if result.Error != nil && !errors.Is(result.Error, gorm.ErrRecordNotFound) { //if result.Error != nil && !errors.Is(result.Error, gorm.ErrRecordNotFound) {

18
internal/database/user.go Normal file
View File

@@ -0,0 +1,18 @@
package database
type User struct {
Id int `gorm:"primary_key;AUTO_INCREMENT"`
DisplayName string
LoginName string
PwdHash []byte
Salt []byte
Mangas []Manga `gorm:"foreignKey:UserId"`
}
// type UserManga struct {
// Id int `gorm:"primary_key;AUTO_INCREMENT"`
// DisplayName string
// Manga Manga
// User User
// // Chapters []Chapter `gorm:"ForeignKey:ChapterId,UserId;References:Id,UserId"`
// }

View File

@@ -38,7 +38,7 @@ func (s *Server) HandleNew(w http.ResponseWriter, r *http.Request) {
func (s *Server) HandleMenu(w http.ResponseWriter, _ *http.Request) { func (s *Server) HandleMenu(w http.ResponseWriter, _ *http.Request) {
tmpl := template.Must(view.GetViewTemplate(view.Menu)) tmpl := template.Must(view.GetViewTemplate(view.Menu))
var all []*database.Manga var all []*database.Manga
_ = s.DbMgr.Db.Preload("Chapters").Find(&all) _ = s.DbMgr.Db.Preload("Chapters").Where("user_id = ?", 1).Find(&all)
l := len(all) l := len(all)
mangaViewModels := make([]view.MangaViewModel, l) mangaViewModels := make([]view.MangaViewModel, l)
counter := 0 counter := 0
@@ -57,7 +57,7 @@ func (s *Server) HandleMenu(w http.ResponseWriter, _ *http.Request) {
//TODO: Change all this to be more performant //TODO: Change all this to be more performant
for _, manga := range all { for _, manga := range all {
title := cases.Title(language.English, cases.Compact).String(strings.Replace(manga.Title, "-", " ", -1)) title := cases.Title(language.English, cases.Compact).String(strings.Replace(manga.Definition.Title, "-", " ", -1))
t1 := time.Now().UnixNano() t1 := time.Now().UnixNano()
@@ -78,30 +78,29 @@ func (s *Server) HandleMenu(w http.ResponseWriter, _ *http.Request) {
// This is very slow // This is very slow
// TODO: put this into own Method // TODO: put this into own Method
if manga.LastChapterNum == "" { if manga.Definition.LastChapterNum == "" {
err, updated := s.UpdateLatestAvailableChapter(manga) err, updated := s.UpdateLatestAvailableChapter(manga)
if err != nil { if err != nil {
fmt.Println(err) fmt.Println(err)
} }
if updated { if updated {
s.DbMgr.Db.Save(manga) s.DbMgr.Db.Save(manga.Definition)
} }
} }
t2 = time.Now().UnixNano() t2 = time.Now().UnixNano()
titNs += t2 - t1 titNs += t2 - t1
latestChapter, ok := manga.GetLatestChapter() latestChapter, ok := manga.GetLatestChapter()
if !ok { if !ok {
continue continue
} }
mangaViewModels[counter] = view.MangaViewModel{ mangaViewModels[counter] = view.MangaViewModel{
ID: manga.Id, ID: manga.Definition.Id,
Title: title, Title: title,
Number: latestChapter.Number, Number: latestChapter.Number,
LastNumber: manga.LastChapterNum, LastNumber: manga.Definition.LastChapterNum,
// I Hate this time Format... 15 = hh, 04 = mm, 02 = DD, 01 = MM, 06 == YY // I Hate this time Format... 15 = hh, 04 = mm, 02 = DD, 01 = MM, 06 == YY
LastTime: time.Unix(manga.TimeStampUnix, 0).Format("15:04 (02-01-06)"), LastTime: time.Unix(manga.TimeStampUnix, 0).Format("15:04 (02-01-06)"),
Url: latestChapter.Url, Url: latestChapter.Url,
@@ -214,10 +213,10 @@ func (s *Server) HandleCurrent(w http.ResponseWriter, _ *http.Request) {
fmt.Println(err) fmt.Println(err)
} }
var manga database.Manga var manga database.MangaDefinition
result := s.DbMgr.Db.First(&manga, mangaId) result := s.DbMgr.Db.First(&manga, mangaId)
if result.Error != nil && errors.Is(result.Error, gorm.ErrRecordNotFound) { if result.Error != nil && errors.Is(result.Error, gorm.ErrRecordNotFound) {
manga = database.NewManga(mangaId, title, time.Now().Unix()) manga = database.NewMangaDefinition(mangaId, title, time.Now().Unix())
} else { } else {
manga.TimeStampUnix = time.Now().Unix() manga.TimeStampUnix = time.Now().Unix()
} }

View File

@@ -212,9 +212,9 @@ func (s *Server) LoadCurr() {
} }
func (s *Server) UpdateLatestAvailableChapter(manga *database.Manga) (error, bool) { func (s *Server) UpdateLatestAvailableChapter(manga *database.Manga) (error, bool) {
fmt.Printf("Updating Manga: %s\n", manga.Title) fmt.Printf("Updating Manga: %s\n", manga.Definition.Title)
l, err := s.Provider.GetChapterList("/title/" + strconv.Itoa(manga.Id)) l, err := s.Provider.GetChapterList("/title/" + strconv.Itoa(manga.Definition.Id))
if err != nil { if err != nil {
return err, false return err, false
} }
@@ -227,16 +227,16 @@ func (s *Server) UpdateLatestAvailableChapter(manga *database.Manga) (error, boo
chapterNumberStr := strings.Replace(c, "ch_", "", 1) chapterNumberStr := strings.Replace(c, "ch_", "", 1)
if manga.LastChapterNum == chapterNumberStr { if manga.Definition.LastChapterNum == chapterNumberStr {
return nil, false return nil, false
} else { } else {
manga.LastChapterNum = chapterNumberStr manga.Definition.LastChapterNum = chapterNumberStr
return nil, true return nil, true
} }
} }
func (s *Server) LoadThumbnail(manga *database.Manga) (path string, updated bool, err error) { func (s *Server) LoadThumbnail(manga *database.Manga) (path string, updated bool, err error) {
strId := strconv.Itoa(manga.Id) strId := strconv.Itoa(manga.Definition.Id)
s.Mutex.Lock() s.Mutex.Lock()
defer s.Mutex.Unlock() defer s.Mutex.Unlock()
@@ -244,8 +244,8 @@ func (s *Server) LoadThumbnail(manga *database.Manga) (path string, updated bool
return strId, false, nil return strId, false, nil
} }
if manga.Thumbnail != nil { if manga.Definition.Thumbnail != nil {
s.ImageBuffers[strId] = manga.Thumbnail s.ImageBuffers[strId] = manga.Definition.Thumbnail
return strId, false, nil return strId, false, nil
} }
@@ -257,7 +257,7 @@ func (s *Server) LoadThumbnail(manga *database.Manga) (path string, updated bool
if err != nil { if err != nil {
return "", false, err return "", false, err
} }
manga.Thumbnail = ram manga.Definition.Thumbnail = ram
s.ImageBuffers[strId] = ram s.ImageBuffers[strId] = ram
return strId, true, nil return strId, true, nil
} }