Pre graphql

This commit is contained in:
2021-02-01 18:24:09 -05:00
parent dc56899b8b
commit ecf981495e
14 changed files with 414 additions and 156 deletions

View File

@@ -5,18 +5,20 @@ type APICredentials struct {
Password string `json:"password"`
}
type APIData interface{}
type APIMeta struct {
Count int `json:"count"`
Page int `json:"page"`
Count int64 `json:"count"`
Page int64 `json:"page"`
}
type APIError struct {
Message string `json:"message"`
Code int `json:"code"`
Code int64 `json:"code"`
}
type APIResponse struct {
Data []interface{} `json:"data"`
Meta APIMeta `json:"meta"`
Error APIError `json:"error"`
Data APIData `json:"data,omitempty"`
Meta *APIMeta `json:"meta,omitempty"`
Error *APIError `json:"error,omitempty"`
}

View File

@@ -2,16 +2,17 @@ package models
import (
"time"
"strings"
"reflect"
"gorm.io/gorm"
"github.com/google/uuid"
)
// Base contains common columns for all tables.
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:"deleted_at" gorm:"index"`
DeletedAt gorm.DeletedAt `json:"-" gorm:"index"`
}
func (base *Base) BeforeCreate(tx *gorm.DB) (err error) {
@@ -28,10 +29,11 @@ type ServerSetting struct {
type Device struct {
Base
User User `json:"user" gorm:"ForeignKey:UUID;not null"` // User UUID
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
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 {
@@ -47,15 +49,16 @@ type User struct {
type MediaItem struct {
Base
User User `json:"user" gorm:"ForeignKey:UUID;not null"` // User UUID
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
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 {
@@ -67,3 +70,27 @@ 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
}