refactor: improve handling of optional CustomerID in project models and DTOs
Gitea Actions Demo / Explore-Gitea-Actions (push) Has been cancelled

This commit is contained in:
2025-04-01 15:48:43 +00:00
parent a9c7598862
commit 4b47da3673
13 changed files with 1446 additions and 28 deletions
+1 -1
View File
@@ -90,7 +90,7 @@ func UpdateModel(ctx context.Context, model any, updates any) error {
updateMap := make(map[string]any)
// Iterate through all fields
for i := 0; i < updateValue.NumField(); i++ {
for i := range updateValue.NumField() {
field := updateValue.Field(i)
fieldType := updateType.Field(i)
+18 -13
View File
@@ -14,7 +14,7 @@ import (
type Project struct {
EntityBase
Name string `gorm:"column:name;not null"`
CustomerID *types.ULID `gorm:"column:customer_id;type:bytea;not null"`
CustomerID *types.ULID `gorm:"column:customer_id;type:bytea;index"`
// Relationships (for Eager Loading)
Customer *Customer `gorm:"foreignKey:CustomerID"`
@@ -33,9 +33,9 @@ type ProjectCreate struct {
// ProjectUpdate contains the updatable fields of a project
type ProjectUpdate struct {
ID types.ULID `gorm:"-"` // Exclude from updates
Name *string `gorm:"column:name"`
CustomerID *types.ULID `gorm:"column:customer_id"`
ID types.ULID `gorm:"-"` // Exclude from updates
Name *string `gorm:"column:name"`
CustomerID types.Nullable[types.ULID] `gorm:"column:customer_id"`
}
// Validate checks if the Create struct contains valid data
@@ -44,7 +44,7 @@ func (pc *ProjectCreate) Validate() error {
return errors.New("project name cannot be empty")
}
// Check for valid CustomerID
if pc.CustomerID.Compare(types.ULID{}) == 0 {
if pc.CustomerID != nil && pc.CustomerID.Compare(types.ULID{}) == 0 {
return errors.New("customerID cannot be empty")
}
return nil
@@ -122,7 +122,7 @@ func CreateProject(ctx context.Context, create ProjectCreate) (*Project, error)
}
// Check if the customer exists
if create.CustomerID == nil {
if create.CustomerID != nil {
customer, err := GetCustomerByID(ctx, *create.CustomerID)
if err != nil {
return nil, fmt.Errorf("error checking the customer: %w", err)
@@ -160,13 +160,18 @@ func UpdateProject(ctx context.Context, update ProjectUpdate) (*Project, error)
}
// If CustomerID is updated, check if the customer exists
if update.CustomerID != nil {
customer, err := GetCustomerByID(ctx, *update.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 update.CustomerID.Valid {
if update.CustomerID.Value != nil {
customer, err := GetCustomerByID(ctx, *update.CustomerID.Value)
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")
}
} else {
// If CustomerID is nil, set it to nil in the project
project.CustomerID = nil
}
}
+10 -8
View File
@@ -187,7 +187,7 @@ func (uc *UserCreate) Validate() error {
}
}
if uc.CompanyID.Compare(types.ULID{}) == 0 {
if uc.CompanyID != nil && uc.CompanyID.Compare(types.ULID{}) == 0 {
return errors.New("companyID cannot be empty")
}
@@ -363,13 +363,15 @@ func CreateUser(ctx context.Context, create UserCreate) (*User, error) {
return errors.New("email is already in use")
}
// Check if company exists
var companyCount int64
if err := tx.Model(&Company{}).Where("id = ?", create.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 create.CompanyID != nil {
// Check if company exists
var companyCount int64
if err := tx.Model(&Company{}).Where("id = ?", create.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")
}
}
// Hash password with unique salt