feat (api): migrate to UUIDs. Not fully debugged, may contain bugs

This commit is contained in:
2025-08-18 16:05:30 +03:00
parent 7ff759947a
commit 2666b76b44
9 changed files with 306 additions and 121 deletions
+1
View File
@@ -3,6 +3,7 @@ module book-tools
go 1.24.4
require (
github.com/google/uuid v1.6.0
github.com/jinzhu/inflection v1.0.0 // indirect
github.com/jinzhu/now v1.1.5 // indirect
github.com/joho/godotenv v1.5.1
+2
View File
@@ -1,3 +1,5 @@
github.com/google/uuid v1.6.0 h1:NIvaJDMOsjHA8n1jAhLSgzrAzy1Hgr+hNrb57e+94F0=
github.com/google/uuid v1.6.0/go.mod h1:TIyPZe4MgqvfeYDBFedMoGGpEw/LqOeaOT+nhxU+yHo=
github.com/jinzhu/inflection v1.0.0 h1:K317FqzuhWc8YvSVlFMCCUb36O/S9MCKRDI7QkRKD/E=
github.com/jinzhu/inflection v1.0.0/go.mod h1:h+uFLlag+Qp1Va5pdKtLDYj+kHp5pxUVkryuEj+Srlc=
github.com/jinzhu/now v1.1.5 h1:/o9tlHleP7gOFmsnYNz3RGnqzefHA47wQpKrrdTIwXQ=
+2 -1
View File
@@ -13,6 +13,7 @@ import (
"sync/atomic"
"time"
"github.com/google/uuid"
"gorm.io/gorm"
)
@@ -63,7 +64,7 @@ func (a *App) Run() error {
log.Printf("Failed add book to transaction: %v", tx.Error)
return
}
if bookID > 0 {
if bookID != uuid.Nil {
addedBooks++
}
}
+2 -1
View File
@@ -4,6 +4,7 @@ import (
"encoding/xml"
"io"
"github.com/google/uuid"
"golang.org/x/net/html/charset"
"gorm.io/gorm"
)
@@ -65,7 +66,7 @@ func nilCheck(nilString *string) string {
return *nilString
}
func FB2toDB(tx *gorm.DB, book FB2) uint64 {
func FB2toDB(tx *gorm.DB, book FB2) uuid.UUID {
var genres *[]Genre
//book.Genres = nil
if book.Genres != nil {
+96 -20
View File
@@ -1,23 +1,42 @@
package reaper
import "time"
import (
"time"
"github.com/google/uuid"
"gorm.io/gorm"
)
type Language struct {
ID uint `gorm:",unique;autoIncrement:true"`
ID uuid.UUID
Code string `gorm:"primaryKey;index:,unique"`
ISO *string
}
func (b *Language) BeforeCreate(tx *gorm.DB) (err error) {
if b.ID == uuid.Nil {
b.ID = uuid.New()
}
return
}
type Genre struct {
ID uint `gorm:",unique;autoIncrement:true"`
ID uuid.UUID
RawTag string `gorm:"index:,unique"`
Tag *string
Name *string
}
func (b *Genre) BeforeCreate(tx *gorm.DB) (err error) {
if b.ID == uuid.Nil {
b.ID = uuid.New()
}
return
}
type Author struct {
ID uint `gorm:"primaryKey;index:,unique;autoIncrement:true"`
Key string `gorm:",unique"`
ID uuid.UUID `gorm:"primaryKey;index:,unique;autoIncrement:true"`
Key string `gorm:",unique"`
FirstName string
MiddleName *string
LastName *string
@@ -26,9 +45,16 @@ type Author struct {
Books []Book `gorm:"many2many:BookAuthor"`
}
func (b *Author) BeforeCreate(tx *gorm.DB) (err error) {
if b.ID == uuid.Nil {
b.ID = uuid.New()
}
return
}
type User struct {
ID uint `gorm:"primaryKey"`
Username string `gorm:"autoIncrement:false;index:,unique"`
ID uuid.UUID `gorm:"primaryKey"`
Username string `gorm:"autoIncrement:false;index:,unique"`
Avatar *string
Name *string
Admin bool
@@ -42,26 +68,54 @@ type User struct {
BookShelf []ReaderBook
}
func (b *User) BeforeCreate(tx *gorm.DB) (err error) {
if b.ID == uuid.Nil {
b.ID = uuid.New()
}
return
}
type Translator struct {
ID uint `gorm:"primaryKey;index:,unique;autoIncrement:true"`
Key string `gorm:",unique"`
ID uuid.UUID `gorm:"primaryKey;index:,unique"`
Key string `gorm:",unique"`
FirstName string
MiddleName *string
LastName *string
}
func (b *Translator) BeforeCreate(tx *gorm.DB) (err error) {
if b.ID == uuid.Nil {
b.ID = uuid.New()
}
return
}
type Sequence struct {
ID uint `gorm:",unique;autoIncrement:true"`
Name string `gorm:"primaryKey;index:,unique"`
ID uuid.UUID `gorm:",unique"`
Name string `gorm:"primaryKey;index:,unique"`
}
func (b *Sequence) BeforeCreate(tx *gorm.DB) (err error) {
if b.ID == uuid.Nil {
b.ID = uuid.New()
}
return
}
type Publisher struct {
ID uint `gorm:",unique;autoIncrement:true"`
Name string `gorm:"primaryKey;index:,unique"`
ID uuid.UUID `gorm:",unique;autoIncrement:true"`
Name string `gorm:"primaryKey;index:,unique"`
}
func (b *Publisher) BeforeCreate(tx *gorm.DB) (err error) {
if b.ID == uuid.Nil {
b.ID = uuid.New()
}
return
}
type Book struct {
ID uint64 `gorm:"primaryKey"`
ID uuid.UUID `gorm:"primaryKey"`
Title string
Authors []Author `gorm:"many2many:BookAuthor;References:ID"`
Lang *string
@@ -97,26 +151,48 @@ type Book struct {
ExternalCover *string
}
func (b *Book) BeforeCreate(tx *gorm.DB) (err error) {
if b.ID == uuid.Nil {
b.ID = uuid.New()
}
return
}
// type Filetype struct {
// ID uint `gorm:"primaryKey"`
// Filetype string `gorm:"uniqueIndex"`
// Name string
// }
type ReaderBook struct {
ID uint `gorm:"primaryKey"`
UserID uint
BookID uint
ID uuid.UUID `gorm:"primaryKey"`
UserID uuid.UUID
BookID uuid.UUID
Book Book `gorm:"foreignKey:BookID"`
Progress float64
LastRead time.Time
}
func (b *ReaderBook) BeforeCreate(tx *gorm.DB) (err error) {
if b.ID == uuid.Nil {
b.ID = uuid.New()
}
return
}
type Collection struct {
ID uint `gorm:"primaryKey"`
Link string `gorm:"uniqueIndex"`
ID uuid.UUID `gorm:"primaryKey"`
Link string `gorm:"uniqueIndex"`
Name string
UserID uint
UserID uuid.UUID
Creator User `gorm:"foreignKey:UserID"`
Books []Book `gorm:"many2many:CollectionBook;"`
Created time.Time
Modified time.Time
}
func (b *Collection) BeforeCreate(tx *gorm.DB) (err error) {
if b.ID == uuid.Nil {
b.ID = uuid.New()
}
return
}