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
+117 -25
View File
@@ -1,23 +1,42 @@
package schemas
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,42 +151,72 @@ 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
}
type Share struct {
ID uint `gorm:"primaryKey"`
Token string `gorm:"uniqueIndex"`
ID uuid.UUID `gorm:"primaryKey"`
Token string `gorm:"uniqueIndex"`
CreatedAt time.Time
ExpiredAt *time.Time
BookID *uint
BookID *uuid.UUID
Book *Book
UserID uint
UserID uuid.UUID
User User
}
func (b *Share) BeforeCreate(tx *gorm.DB) (err error) {
if b.ID == uuid.Nil {
b.ID = uuid.New()
}
return
}
type Permission struct {
ID uint `gorm:"primaryKey"`
ID uuid.UUID `gorm:"primaryKey"`
User User `gorm:"foreignKey:ID"`
EditBooks []Book `gorm:"many2many:EditableBook;"`
BooksWhitelist *[]Book `gorm:"many2many:WhitelistedBook;"`
@@ -143,6 +227,14 @@ type Permission struct {
CanUpload bool
CanCreateCollections bool
}
func (b *Permission) BeforeCreate(tx *gorm.DB) (err error) {
if b.ID == uuid.Nil {
b.ID = uuid.New()
}
return
}
type Settings struct {
LibPath string
ReadOnly bool