implement graphQL and init postgres
This commit is contained in:
@@ -8,22 +8,22 @@ import (
|
||||
|
||||
// BaseModel provides common fields for all database models
|
||||
type BaseModel struct {
|
||||
ID string `json:"id" gorm:"primary_key;type:varchar(36)"`
|
||||
DateCreated time.Time `json:"dateCreated" gorm:"not null"`
|
||||
DateUpdated time.Time `json:"dateUpdated" gorm:"not null"`
|
||||
ID string `json:"id" gorm:"type:uuid;primary_key;default:gen_random_uuid()"`
|
||||
CreatedAt time.Time `json:"created_at" gorm:"not null;default:now()"`
|
||||
UpdatedAt time.Time `json:"updated_at" gorm:"not null;default:now()"`
|
||||
}
|
||||
|
||||
// Init initializes base model with DateCreated, DateUpdated, and ID values
|
||||
// Init initializes base model with CreatedAt, UpdatedAt, and ID values
|
||||
func (bm *BaseModel) Init() {
|
||||
now := time.Now().UTC()
|
||||
bm.ID = uuid.NewString()
|
||||
bm.DateCreated = now
|
||||
bm.DateUpdated = now
|
||||
bm.CreatedAt = now
|
||||
bm.UpdatedAt = now
|
||||
}
|
||||
|
||||
// UpdateTimestamp updates the DateUpdated field
|
||||
// UpdateTimestamp updates the UpdatedAt field
|
||||
func (bm *BaseModel) UpdateTimestamp() {
|
||||
bm.DateUpdated = time.Now().UTC()
|
||||
bm.UpdatedAt = time.Now().UTC()
|
||||
}
|
||||
|
||||
// BeforeCreate is a GORM hook that runs before creating a record
|
||||
@@ -76,7 +76,7 @@ func DefaultParams() Params {
|
||||
return Params{
|
||||
Page: 1,
|
||||
Limit: 10,
|
||||
SortBy: "dateCreated",
|
||||
SortBy: "created_at",
|
||||
SortOrder: "desc",
|
||||
}
|
||||
}
|
||||
@@ -90,7 +90,7 @@ func (p *Params) Validate() {
|
||||
p.Limit = 10
|
||||
}
|
||||
if p.SortBy == "" {
|
||||
p.SortBy = "dateCreated"
|
||||
p.SortBy = "created_at"
|
||||
}
|
||||
if p.SortOrder != "asc" && p.SortOrder != "desc" {
|
||||
p.SortOrder = "desc"
|
||||
|
||||
Reference in New Issue
Block a user