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
@@ -12,7 +12,7 @@ type ProjectDto struct {
UpdatedAt time.Time `json:"updatedAt" example:"2024-01-01T00:00:00Z"`
LastEditorID string `json:"lastEditorID" example:"01HGW2BBG0000000000000000"`
Name string `json:"name" example:"Time Tracking App"`
CustomerID *string `json:"customerId" example:"01HGW2BBG0000000000000000"`
CustomerID *string `json:"customerId,omitempty" example:"01HGW2BBG0000000000000000"`
}
type ProjectCreateDto struct {
@@ -148,13 +148,17 @@ func (h *ProjectHandler) DeleteProject(c *gin.Context) {
// Helper functions for DTO conversion
func convertProjectToDTO(project *models.Project) dto.ProjectDto {
customerId := project.CustomerID.String()
var customerIdPtr *string
if project.CustomerID != nil {
customerIdStr := project.CustomerID.String()
customerIdPtr = &customerIdStr
}
return dto.ProjectDto{
ID: project.ID.String(),
CreatedAt: project.CreatedAt,
UpdatedAt: project.UpdatedAt,
Name: project.Name,
CustomerID: &customerId,
CustomerID: customerIdPtr,
}
}
@@ -182,13 +186,13 @@ func convertUpdateProjectDTOToModel(dto dto.ProjectUpdateDto, id types.ULID) (mo
if dto.CustomerID.Valid {
if dto.CustomerID.Value == nil {
update.CustomerID = nil
update.CustomerID = types.Null[types.ULID]()
} else {
customerID, err := types.ULIDFromString(*dto.CustomerID.Value)
if err != nil {
return models.ProjectUpdate{}, fmt.Errorf("invalid customer ID: %w", err)
}
update.CustomerID = &customerID
update.CustomerID = types.NewNullable(customerID)
}
}