feat: Introduce Undefined function for Nullable type and refactor DTOs to use Nullable directly
This commit is contained in:
@@ -1,7 +1,6 @@
|
||||
package models
|
||||
|
||||
import (
|
||||
"fmt"
|
||||
"math/rand"
|
||||
"time"
|
||||
|
||||
@@ -24,7 +23,6 @@ func (eb *EntityBase) BeforeCreate(tx *gorm.DB) error {
|
||||
entropy := ulid.Monotonic(rand.New(rand.NewSource(time.Now().UnixNano())), 0)
|
||||
newID := ulid.MustNew(ulid.Timestamp(time.Now()), entropy)
|
||||
eb.ID = types.ULID{ULID: newID}
|
||||
fmt.Println("Generated ID:", eb.ID)
|
||||
}
|
||||
return nil
|
||||
}
|
||||
|
||||
@@ -13,8 +13,8 @@ import (
|
||||
// Project represents a project in the system
|
||||
type Project struct {
|
||||
EntityBase
|
||||
Name string `gorm:"column:name;not null"`
|
||||
CustomerID types.ULID `gorm:"column:customer_id;type:bytea;not null"`
|
||||
Name string `gorm:"column:name;not null"`
|
||||
CustomerID *types.ULID `gorm:"column:customer_id;type:bytea;not null"`
|
||||
|
||||
// Relationships (for Eager Loading)
|
||||
Customer *Customer `gorm:"foreignKey:CustomerID"`
|
||||
@@ -28,7 +28,7 @@ func (Project) TableName() string {
|
||||
// ProjectCreate contains the fields for creating a new project
|
||||
type ProjectCreate struct {
|
||||
Name string
|
||||
CustomerID types.ULID
|
||||
CustomerID *types.ULID
|
||||
}
|
||||
|
||||
// ProjectUpdate contains the updatable fields of a project
|
||||
@@ -122,12 +122,14 @@ func CreateProject(ctx context.Context, create ProjectCreate) (*Project, error)
|
||||
}
|
||||
|
||||
// Check if the customer exists
|
||||
customer, err := GetCustomerByID(ctx, create.CustomerID)
|
||||
if err != nil {
|
||||
return nil, fmt.Errorf("error checking the customer: %w", err)
|
||||
}
|
||||
if customer == nil {
|
||||
return nil, errors.New("the specified customer does not exist")
|
||||
if create.CustomerID == nil {
|
||||
customer, err := GetCustomerByID(ctx, *create.CustomerID)
|
||||
if err != nil {
|
||||
return nil, fmt.Errorf("error checking the customer: %w", err)
|
||||
}
|
||||
if customer == nil {
|
||||
return nil, errors.New("the specified customer does not exist")
|
||||
}
|
||||
}
|
||||
|
||||
project := Project{
|
||||
|
||||
@@ -63,12 +63,12 @@ type UserCreate struct {
|
||||
|
||||
// UserUpdate contains the updatable fields of a user
|
||||
type UserUpdate struct {
|
||||
ID types.ULID `gorm:"-"` // Exclude from updates
|
||||
Email *string `gorm:"column:email"`
|
||||
Password *string `gorm:"-"` // Not stored directly in DB
|
||||
Role *string `gorm:"column:role"`
|
||||
CompanyID *types.ULID `gorm:"column:company_id"`
|
||||
HourlyRate *float64 `gorm:"column:hourly_rate"`
|
||||
ID types.ULID `gorm:"-"` // Exclude from updates
|
||||
Email *string `gorm:"column:email"`
|
||||
Password *string `gorm:"-"` // Not stored directly in DB
|
||||
Role *string `gorm:"column:role"`
|
||||
CompanyID types.Nullable[types.ULID] `gorm:"column:company_id"`
|
||||
HourlyRate *float64 `gorm:"column:hourly_rate"`
|
||||
}
|
||||
|
||||
// PasswordData contains the data for password hash and salt
|
||||
@@ -448,13 +448,15 @@ func UpdateUser(ctx context.Context, update UserUpdate) (*User, error) {
|
||||
}
|
||||
|
||||
// If CompanyID is updated, check if it exists
|
||||
if update.CompanyID != nil && (user.CompanyID == nil || update.CompanyID.Compare(*user.CompanyID) != 0) {
|
||||
var companyCount int64
|
||||
if err := tx.Model(&Company{}).Where("id = ?", *update.CompanyID).Count(&companyCount).Error; err != nil {
|
||||
return fmt.Errorf("error checking company: %w", err)
|
||||
}
|
||||
if companyCount == 0 {
|
||||
return errors.New("the specified company does not exist")
|
||||
if update.CompanyID.Valid && update.CompanyID.Value != nil {
|
||||
if user.CompanyID == nil || *update.CompanyID.Value != *user.CompanyID {
|
||||
var companyCount int64
|
||||
if err := tx.Model(&Company{}).Where("id = ?", *update.CompanyID.Value).Count(&companyCount).Error; err != nil {
|
||||
return fmt.Errorf("error checking company: %w", err)
|
||||
}
|
||||
if companyCount == 0 {
|
||||
return errors.New("the specified company does not exist")
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
@@ -484,8 +486,13 @@ func UpdateUser(ctx context.Context, update UserUpdate) (*User, error) {
|
||||
if update.Role != nil {
|
||||
updates["role"] = *update.Role
|
||||
}
|
||||
if update.CompanyID != nil {
|
||||
updates["company_id"] = *update.CompanyID
|
||||
if update.CompanyID.Valid {
|
||||
if update.CompanyID.Value == nil {
|
||||
updates["company_id"] = nil
|
||||
} else {
|
||||
updates["company_id"] = *update.CompanyID.Value
|
||||
}
|
||||
|
||||
}
|
||||
if update.HourlyRate != nil {
|
||||
updates["hourly_rate"] = *update.HourlyRate
|
||||
|
||||
Reference in New Issue
Block a user