This repository has been archived on 2023-11-13. You can view files and clone it, but cannot push or open issues or pull requests.
imagini/internal/models/db.go
2021-02-01 18:24:09 -05:00

97 lines
3.8 KiB
Go

package models
import (
"time"
"strings"
"reflect"
"gorm.io/gorm"
"github.com/google/uuid"
)
type Base struct {
UUID uuid.UUID `json:"uuid" gorm:"type:uuid;primarykey"`
CreatedAt time.Time `json:"created_at"`
UpdatedAt time.Time `json:"updated_at"`
DeletedAt gorm.DeletedAt `json:"-" gorm:"index"`
}
func (base *Base) BeforeCreate(tx *gorm.DB) (err error) {
base.UUID = uuid.New()
return
}
type ServerSetting struct {
Base
Name string `json:"name" gorm:"not null"`
Description string `json:"description" gorm:"not null"`
Value string `json:"value" gorm:"not null"`
}
type Device struct {
Base
UserUUID uuid.UUID `json:"-" gorm:"not null"`
User User `json:"user" gorm:"ForeignKey:UUID;References:UserUUID;not null"` // User
Name string `json:"name" gorm:"not null"` // Name of Device
Type string `json:"type" gorm:"not null"` // Android, iOS, Chrome, FireFox, Edge
RefreshKey string `json:"-"` // Device Specific Refresh Key
}
type User struct {
Base
Email string `json:"email" gorm:"unique"` // Email
Username string `json:"username" gorm:"unique"` // Username
FirstName string `json:"first_name"` // First Name
LastName string `json:"last_name"` // Last Name
Role string `json:"role"` // Role
AuthType string `json:"auth_type" gorm:"default:Local;not null"` // Auth Type (E.g. Local, LDAP)
Password string `json:"-"` // Hased & Salted Password
}
type MediaItem struct {
Base
UserUUID uuid.UUID `json:"-" gorm:"not null"`
User User `json:"-" gorm:"ForeignKey:UUID;References:UserUUID;not null"` // User
EXIFDate time.Time `json:"exif_date"` // EXIF Date
Latitude *float32 `json:"latitude" gorm:"type:decimal(10,2)"` // Decimal Latitude
Longitude *float32 `json:"longitude" gorm:"type:decimal(10,2)"` // Decimal Longitude
MediaType string `json:"media_type" gorm:"default:Image;not null"` // Image, Video
OrigName string `json:"orig_name" gorm:"not null"` // Original Name
FileName string `json:"file_name" gorm:"not null"` // File Name
Tags []Tag `json:"tags" gorm:"many2many:media_tags;"` // Associated Tag UUIDs
Albums []Album `json:"albums" gorm:"many2many:media_albums;"` // Associated Album UUIDs
}
type Tag struct {
Base
Name string `json:"name" gorm:"not null"` // Tag Name
}
type Album struct {
Base
Name string `json:"name" gorm:"not null"` // Album Name
}
func JSONFields(model interface{}) map[string]struct{} {
jsonFields := make(map[string]struct{})
val := reflect.ValueOf(model)
t := val.Type()
for i := 0; i < t.NumField(); i++ {
jsonField := strings.TrimSpace(t.Field(i).Tag.Get("json"))
if jsonField == "" {
continue
}
jsonSplit := strings.Split(jsonField, ",")
fieldVal := strings.TrimSpace(jsonSplit[0])
if fieldVal == "" || fieldVal == "-" {
continue
}
jsonFields[fieldVal] = struct{}{}
}
return jsonFields
}