implement graphQL and init postgres
This commit is contained in:
95
local/model/type.go
Normal file
95
local/model/type.go
Normal file
@@ -0,0 +1,95 @@
|
||||
package model
|
||||
|
||||
import (
|
||||
"errors"
|
||||
"strings"
|
||||
|
||||
"gorm.io/gorm"
|
||||
)
|
||||
|
||||
// Type represents a project type in the system
|
||||
type Type struct {
|
||||
BaseModel
|
||||
UserID *string `json:"user_id" gorm:"type:uuid;index;references:users(id);onDelete:SET NULL"`
|
||||
Name string `json:"name" gorm:"not null;type:varchar(100)"`
|
||||
Description string `json:"description" gorm:"type:text"`
|
||||
User *User `json:"user,omitempty" gorm:"foreignKey:UserID"`
|
||||
}
|
||||
|
||||
// TypeCreateRequest represents the request to create a new type
|
||||
type TypeCreateRequest struct {
|
||||
UserID *string `json:"user_id"`
|
||||
Name string `json:"name" validate:"required,min=1,max=100"`
|
||||
Description string `json:"description" validate:"max=1000"`
|
||||
}
|
||||
|
||||
// TypeUpdateRequest represents the request to update a type
|
||||
type TypeUpdateRequest struct {
|
||||
Name *string `json:"name,omitempty" validate:"omitempty,min=1,max=100"`
|
||||
Description *string `json:"description,omitempty" validate:"omitempty,max=1000"`
|
||||
}
|
||||
|
||||
// TypeInfo represents public type information
|
||||
type TypeInfo struct {
|
||||
ID string `json:"id"`
|
||||
UserID *string `json:"user_id"`
|
||||
Name string `json:"name"`
|
||||
Description string `json:"description"`
|
||||
CreatedAt string `json:"created_at"`
|
||||
UpdatedAt string `json:"updated_at"`
|
||||
}
|
||||
|
||||
// BeforeCreate is called before creating a type
|
||||
func (t *Type) BeforeCreate(tx *gorm.DB) error {
|
||||
t.BaseModel.BeforeCreate()
|
||||
|
||||
// Normalize name
|
||||
t.Name = strings.TrimSpace(t.Name)
|
||||
t.Description = strings.TrimSpace(t.Description)
|
||||
|
||||
return t.Validate()
|
||||
}
|
||||
|
||||
// BeforeUpdate is called before updating a type
|
||||
func (t *Type) BeforeUpdate(tx *gorm.DB) error {
|
||||
t.BaseModel.BeforeUpdate()
|
||||
|
||||
// Normalize fields if they're being updated
|
||||
if t.Name != "" {
|
||||
t.Name = strings.TrimSpace(t.Name)
|
||||
}
|
||||
if t.Description != "" {
|
||||
t.Description = strings.TrimSpace(t.Description)
|
||||
}
|
||||
|
||||
return t.Validate()
|
||||
}
|
||||
|
||||
// Validate validates type data
|
||||
func (t *Type) Validate() error {
|
||||
if t.Name == "" {
|
||||
return errors.New("name is required")
|
||||
}
|
||||
|
||||
if len(t.Name) > 100 {
|
||||
return errors.New("name must not exceed 100 characters")
|
||||
}
|
||||
|
||||
if len(t.Description) > 1000 {
|
||||
return errors.New("description must not exceed 1000 characters")
|
||||
}
|
||||
|
||||
return nil
|
||||
}
|
||||
|
||||
// ToTypeInfo converts Type to TypeInfo (public information)
|
||||
func (t *Type) ToTypeInfo() TypeInfo {
|
||||
return TypeInfo{
|
||||
ID: t.ID,
|
||||
UserID: t.UserID,
|
||||
Name: t.Name,
|
||||
Description: t.Description,
|
||||
CreatedAt: t.CreatedAt.Format("2006-01-02T15:04:05Z07:00"),
|
||||
UpdatedAt: t.UpdatedAt.Format("2006-01-02T15:04:05Z07:00"),
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user