init bootstrap
This commit is contained in:
110
local/model/base.go
Normal file
110
local/model/base.go
Normal file
@@ -0,0 +1,110 @@
|
||||
package model
|
||||
|
||||
import (
|
||||
"time"
|
||||
|
||||
"github.com/google/uuid"
|
||||
)
|
||||
|
||||
// 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"`
|
||||
}
|
||||
|
||||
// Init initializes base model with DateCreated, DateUpdated, and ID values
|
||||
func (bm *BaseModel) Init() {
|
||||
now := time.Now().UTC()
|
||||
bm.ID = uuid.NewString()
|
||||
bm.DateCreated = now
|
||||
bm.DateUpdated = now
|
||||
}
|
||||
|
||||
// UpdateTimestamp updates the DateUpdated field
|
||||
func (bm *BaseModel) UpdateTimestamp() {
|
||||
bm.DateUpdated = time.Now().UTC()
|
||||
}
|
||||
|
||||
// BeforeCreate is a GORM hook that runs before creating a record
|
||||
func (bm *BaseModel) BeforeCreate() error {
|
||||
if bm.ID == "" {
|
||||
bm.Init()
|
||||
}
|
||||
return nil
|
||||
}
|
||||
|
||||
// BeforeUpdate is a GORM hook that runs before updating a record
|
||||
func (bm *BaseModel) BeforeUpdate() error {
|
||||
bm.UpdateTimestamp()
|
||||
return nil
|
||||
}
|
||||
|
||||
// FilteredResponse represents a paginated response
|
||||
type FilteredResponse struct {
|
||||
Items interface{} `json:"items"`
|
||||
Params
|
||||
}
|
||||
|
||||
// MessageResponse represents a simple message response
|
||||
type MessageResponse struct {
|
||||
Message string `json:"message"`
|
||||
Success bool `json:"success"`
|
||||
}
|
||||
|
||||
// ErrorResponse represents an error response
|
||||
type ErrorResponse struct {
|
||||
Error string `json:"error"`
|
||||
Details map[string]interface{} `json:"details,omitempty"`
|
||||
Code int `json:"code,omitempty"`
|
||||
}
|
||||
|
||||
// Params represents pagination and filtering parameters
|
||||
type Params struct {
|
||||
SortBy string `json:"sortBy" query:"sortBy"`
|
||||
SortOrder string `json:"sortOrder" query:"sortOrder"`
|
||||
Page int `json:"page" query:"page"`
|
||||
Limit int `json:"limit" query:"limit"`
|
||||
Search string `json:"search" query:"search"`
|
||||
Filter string `json:"filter" query:"filter"`
|
||||
TotalRecords int64 `json:"totalRecords"`
|
||||
TotalPages int `json:"totalPages"`
|
||||
}
|
||||
|
||||
// DefaultParams returns default pagination parameters
|
||||
func DefaultParams() Params {
|
||||
return Params{
|
||||
Page: 1,
|
||||
Limit: 10,
|
||||
SortBy: "dateCreated",
|
||||
SortOrder: "desc",
|
||||
}
|
||||
}
|
||||
|
||||
// Validate validates and sets default values for parameters
|
||||
func (p *Params) Validate() {
|
||||
if p.Page < 1 {
|
||||
p.Page = 1
|
||||
}
|
||||
if p.Limit < 1 || p.Limit > 100 {
|
||||
p.Limit = 10
|
||||
}
|
||||
if p.SortBy == "" {
|
||||
p.SortBy = "dateCreated"
|
||||
}
|
||||
if p.SortOrder != "asc" && p.SortOrder != "desc" {
|
||||
p.SortOrder = "desc"
|
||||
}
|
||||
}
|
||||
|
||||
// Offset calculates the offset for database queries
|
||||
func (p *Params) Offset() int {
|
||||
return (p.Page - 1) * p.Limit
|
||||
}
|
||||
|
||||
// CalculateTotalPages calculates total pages based on total records
|
||||
func (p *Params) CalculateTotalPages() {
|
||||
if p.Limit > 0 {
|
||||
p.TotalPages = int((p.TotalRecords + int64(p.Limit) - 1) / int64(p.Limit))
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user