feat: Introduce Undefined function for Nullable type and refactor DTOs to use Nullable directly
This commit is contained in:
@@ -147,7 +147,7 @@ func (h *CompanyHandler) CreateCompany(c *gin.Context) {
|
||||
func (h *CompanyHandler) UpdateCompany(c *gin.Context) {
|
||||
// Parse ID from URL
|
||||
idStr := c.Param("id")
|
||||
id, err := ulid.Parse(idStr)
|
||||
id, err := types.ULIDFromString(idStr)
|
||||
if err != nil {
|
||||
utils.BadRequestResponse(c, "Invalid company ID format")
|
||||
return
|
||||
@@ -160,11 +160,8 @@ func (h *CompanyHandler) UpdateCompany(c *gin.Context) {
|
||||
return
|
||||
}
|
||||
|
||||
// Set ID from URL
|
||||
companyUpdateDTO.ID = id.String()
|
||||
|
||||
// Convert DTO to model
|
||||
companyUpdate := convertUpdateCompanyDTOToModel(companyUpdateDTO)
|
||||
companyUpdate := convertUpdateCompanyDTOToModel(companyUpdateDTO, id)
|
||||
|
||||
// Update company in the database
|
||||
company, err := models.UpdateCompany(c.Request.Context(), companyUpdate)
|
||||
@@ -234,10 +231,9 @@ func convertCreateCompanyDTOToModel(dto dto.CompanyCreateDto) models.CompanyCrea
|
||||
}
|
||||
}
|
||||
|
||||
func convertUpdateCompanyDTOToModel(dto dto.CompanyUpdateDto) models.CompanyUpdate {
|
||||
id, _ := ulid.Parse(dto.ID)
|
||||
func convertUpdateCompanyDTOToModel(dto dto.CompanyUpdateDto, id types.ULID) models.CompanyUpdate {
|
||||
update := models.CompanyUpdate{
|
||||
ID: types.FromULID(id),
|
||||
ID: id,
|
||||
}
|
||||
|
||||
if dto.Name != nil {
|
||||
|
||||
@@ -319,16 +319,14 @@ func convertUpdateCustomerDTOToModel(dto dto.CustomerUpdateDto) (models.Customer
|
||||
update.Name = dto.Name
|
||||
}
|
||||
|
||||
if dto.CompanyID != nil {
|
||||
if dto.CompanyID.Valid {
|
||||
companyID, err := types.ULIDFromString(*dto.CompanyID.Value)
|
||||
if err != nil {
|
||||
return models.CustomerUpdate{}, fmt.Errorf("invalid company ID: %w", err)
|
||||
}
|
||||
update.CompanyID = &companyID
|
||||
} else {
|
||||
update.CompanyID = nil
|
||||
if dto.CompanyID.Valid {
|
||||
companyID, err := types.ULIDFromString(*dto.CompanyID.Value)
|
||||
if err != nil {
|
||||
return models.CustomerUpdate{}, fmt.Errorf("invalid company ID: %w", err)
|
||||
}
|
||||
update.CompanyID = &companyID
|
||||
} else {
|
||||
update.CompanyID = nil
|
||||
}
|
||||
|
||||
return update, nil
|
||||
|
||||
@@ -220,7 +220,7 @@ func (h *ProjectHandler) CreateProject(c *gin.Context) {
|
||||
func (h *ProjectHandler) UpdateProject(c *gin.Context) {
|
||||
// Parse ID from URL
|
||||
idStr := c.Param("id")
|
||||
id, err := ulid.Parse(idStr)
|
||||
id, err := types.ULIDFromString(idStr)
|
||||
if err != nil {
|
||||
utils.BadRequestResponse(c, "Invalid project ID format")
|
||||
return
|
||||
@@ -233,11 +233,8 @@ func (h *ProjectHandler) UpdateProject(c *gin.Context) {
|
||||
return
|
||||
}
|
||||
|
||||
// Set ID from URL
|
||||
projectUpdateDTO.ID = id.String()
|
||||
|
||||
// Convert DTO to model
|
||||
projectUpdate, err := convertUpdateProjectDTOToModel(projectUpdateDTO)
|
||||
projectUpdate, err := convertUpdateProjectDTOToModel(projectUpdateDTO, id)
|
||||
if err != nil {
|
||||
utils.BadRequestResponse(c, err.Error())
|
||||
return
|
||||
@@ -297,49 +294,51 @@ func (h *ProjectHandler) DeleteProject(c *gin.Context) {
|
||||
// Helper functions for DTO conversion
|
||||
|
||||
func convertProjectToDTO(project *models.Project) dto.ProjectDto {
|
||||
|
||||
customerId := project.CustomerID.String()
|
||||
return dto.ProjectDto{
|
||||
ID: project.ID.String(),
|
||||
CreatedAt: project.CreatedAt,
|
||||
UpdatedAt: project.UpdatedAt,
|
||||
Name: project.Name,
|
||||
CustomerID: project.CustomerID.String(),
|
||||
CustomerID: &customerId,
|
||||
}
|
||||
}
|
||||
|
||||
func convertCreateProjectDTOToModel(dto dto.ProjectCreateDto) (models.ProjectCreate, error) {
|
||||
create := models.ProjectCreate{Name: dto.Name}
|
||||
// Convert CustomerID from int to ULID (this is a simplification, adjust as needed)
|
||||
customerID, err := types.ULIDFromString(dto.CustomerID)
|
||||
if err != nil {
|
||||
return models.ProjectCreate{}, fmt.Errorf("invalid customer ID: %w", err)
|
||||
}
|
||||
if dto.CustomerID != nil {
|
||||
|
||||
return models.ProjectCreate{
|
||||
Name: dto.Name,
|
||||
CustomerID: customerID,
|
||||
}, nil
|
||||
customerID, err := types.ULIDFromString(*dto.CustomerID)
|
||||
if err != nil {
|
||||
return models.ProjectCreate{}, fmt.Errorf("invalid customer ID: %w", err)
|
||||
}
|
||||
create.CustomerID = &customerID
|
||||
}
|
||||
return create, nil
|
||||
}
|
||||
|
||||
func convertUpdateProjectDTOToModel(dto dto.ProjectUpdateDto) (models.ProjectUpdate, error) {
|
||||
id, err := ulid.Parse(dto.ID)
|
||||
if err != nil {
|
||||
return models.ProjectUpdate{}, fmt.Errorf("invalid project ID: %w", err)
|
||||
}
|
||||
func convertUpdateProjectDTOToModel(dto dto.ProjectUpdateDto, id types.ULID) (models.ProjectUpdate, error) {
|
||||
update := models.ProjectUpdate{
|
||||
ID: types.FromULID(id),
|
||||
ID: id,
|
||||
}
|
||||
|
||||
if dto.Name != nil {
|
||||
update.Name = dto.Name
|
||||
}
|
||||
|
||||
if dto.CustomerID != nil {
|
||||
// Convert CustomerID from int to ULID (this is a simplification, adjust as needed)
|
||||
customerID, err := types.ULIDFromString(*dto.CustomerID)
|
||||
if err != nil {
|
||||
return models.ProjectUpdate{}, fmt.Errorf("invalid customer ID: %w", err)
|
||||
if dto.CustomerID.Valid {
|
||||
if dto.CustomerID.Value == nil {
|
||||
update.CustomerID = nil
|
||||
|
||||
} else {
|
||||
// Convert CustomerID from int to ULID (this is a simplification, adjust as needed)
|
||||
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 = &customerID
|
||||
}
|
||||
|
||||
return update, nil
|
||||
|
||||
@@ -326,7 +326,7 @@ func (h *TimeEntryHandler) CreateTimeEntry(c *gin.Context) {
|
||||
func (h *TimeEntryHandler) UpdateTimeEntry(c *gin.Context) {
|
||||
// Parse ID from URL
|
||||
idStr := c.Param("id")
|
||||
id, err := ulid.Parse(idStr)
|
||||
id, err := types.ULIDFromString(idStr)
|
||||
if err != nil {
|
||||
utils.BadRequestResponse(c, "Invalid time entry ID format")
|
||||
return
|
||||
@@ -339,11 +339,8 @@ func (h *TimeEntryHandler) UpdateTimeEntry(c *gin.Context) {
|
||||
return
|
||||
}
|
||||
|
||||
// Set ID from URL
|
||||
timeEntryUpdateDTO.ID = id.String()
|
||||
|
||||
// Convert DTO to model
|
||||
timeEntryUpdate, err := convertUpdateTimeEntryDTOToModel(timeEntryUpdateDTO)
|
||||
timeEntryUpdate, err := convertUpdateTimeEntryDTOToModel(timeEntryUpdateDTO, id)
|
||||
if err != nil {
|
||||
utils.BadRequestResponse(c, err.Error())
|
||||
return
|
||||
@@ -445,13 +442,10 @@ func convertCreateTimeEntryDTOToModel(dto dto.TimeEntryCreateDto) (models.TimeEn
|
||||
}, nil
|
||||
}
|
||||
|
||||
func convertUpdateTimeEntryDTOToModel(dto dto.TimeEntryUpdateDto) (models.TimeEntryUpdate, error) {
|
||||
id, err := ulid.Parse(dto.ID)
|
||||
if err != nil {
|
||||
return models.TimeEntryUpdate{}, fmt.Errorf("invalid time entry ID: %w", err)
|
||||
}
|
||||
func convertUpdateTimeEntryDTOToModel(dto dto.TimeEntryUpdateDto, id types.ULID) (models.TimeEntryUpdate, error) {
|
||||
|
||||
update := models.TimeEntryUpdate{
|
||||
ID: types.FromULID(id),
|
||||
ID: id,
|
||||
}
|
||||
|
||||
if dto.UserID != nil {
|
||||
|
||||
@@ -148,7 +148,7 @@ func (h *UserHandler) CreateUser(c *gin.Context) {
|
||||
func (h *UserHandler) UpdateUser(c *gin.Context) {
|
||||
// Parse ID from URL
|
||||
idStr := c.Param("id")
|
||||
id, err := ulid.Parse(idStr)
|
||||
id, err := types.ULIDFromString(idStr)
|
||||
if err != nil {
|
||||
utils.BadRequestResponse(c, "Invalid user ID format")
|
||||
return
|
||||
@@ -161,13 +161,9 @@ func (h *UserHandler) UpdateUser(c *gin.Context) {
|
||||
return
|
||||
}
|
||||
|
||||
// Set ID from URL
|
||||
userUpdateDTO.ID = id.String()
|
||||
|
||||
// Convert DTO to Model
|
||||
idWrapper := types.FromULID(id)
|
||||
update := models.UserUpdate{
|
||||
ID: idWrapper,
|
||||
ID: id,
|
||||
}
|
||||
|
||||
if userUpdateDTO.Email != nil {
|
||||
@@ -179,22 +175,23 @@ func (h *UserHandler) UpdateUser(c *gin.Context) {
|
||||
if userUpdateDTO.Role != nil {
|
||||
update.Role = userUpdateDTO.Role
|
||||
}
|
||||
if userUpdateDTO.CompanyID != nil {
|
||||
if userUpdateDTO.CompanyID.Valid {
|
||||
|
||||
if userUpdateDTO.CompanyID.Valid {
|
||||
if userUpdateDTO.CompanyID.Value != nil {
|
||||
companyID, err := types.ULIDFromString(*userUpdateDTO.CompanyID.Value)
|
||||
if err != nil {
|
||||
utils.BadRequestResponse(c, "Invalid company ID format")
|
||||
return
|
||||
}
|
||||
update.CompanyID = &companyID
|
||||
update.CompanyID = types.NewNullable(companyID)
|
||||
} else {
|
||||
update.CompanyID = nil
|
||||
update.CompanyID = types.Null[types.ULID]()
|
||||
}
|
||||
}
|
||||
|
||||
if userUpdateDTO.HourlyRate != nil {
|
||||
update.HourlyRate = userUpdateDTO.HourlyRate
|
||||
}
|
||||
|
||||
// Update user in the database
|
||||
user, err := models.UpdateUser(c.Request.Context(), update)
|
||||
if err != nil {
|
||||
|
||||
Reference in New Issue
Block a user