feat: Refactor DTOs to use types.ULID and update companyId fields to be optional

This commit is contained in:
2025-03-12 09:32:29 +00:00
parent 233f3cdb5c
commit 4170eb5fbd
21 changed files with 269 additions and 264 deletions
+13 -9
View File
@@ -2,6 +2,8 @@ package dto
import (
"time"
"github.com/timetracker/backend/internal/types"
)
type CustomerDto struct {
@@ -10,19 +12,21 @@ type CustomerDto struct {
UpdatedAt time.Time `json:"updatedAt" example:"2024-01-01T00:00:00Z"`
LastEditorID string `json:"lastEditorID" example:"01HGW2BBG0000000000000000"`
Name string `json:"name" example:"John Doe"`
CompanyID string `json:"companyId" example:"01HGW2BBG0000000000000000"`
CompanyID *string `json:"companyId" example:"01HGW2BBG0000000000000000"`
OwnerUserID *string `json:"owningUserID" example:"01HGW2BBG0000000000000000"`
}
type CustomerCreateDto struct {
Name string `json:"name" example:"John Doe"`
CompanyID string `json:"companyId" example:"01HGW2BBG0000000000000000"`
Name string `json:"name" example:"John Doe"`
CompanyID *string `json:"companyId" example:"01HGW2BBG0000000000000000"`
}
type CustomerUpdateDto struct {
ID string `json:"id" example:"01HGW2BBG0000000000000000"`
CreatedAt *time.Time `json:"createdAt" example:"2024-01-01T00:00:00Z"`
UpdatedAt *time.Time `json:"updatedAt" example:"2024-01-01T00:00:00Z"`
LastEditorID *string `json:"lastEditorID" example:"01HGW2BBG0000000000000000"`
Name *string `json:"name" example:"John Doe"`
CompanyID *string `json:"companyId" example:"01HGW2BBG0000000000000000"`
ID string `json:"id" example:"01HGW2BBG0000000000000000"`
CreatedAt *time.Time `json:"createdAt" example:"2024-01-01T00:00:00Z"`
UpdatedAt *time.Time `json:"updatedAt" example:"2024-01-01T00:00:00Z"`
LastEditorID *string `json:"lastEditorID" example:"01HGW2BBG0000000000000000"`
Name *string `json:"name" example:"John Doe"`
CompanyID *types.Nullable[string] `json:"companyId" example:"01HGW2BBG0000000000000000"`
OwnerUserID *types.Nullable[string] `json:"owningUserID" example:"01HGW2BBG0000000000000000"`
}
@@ -1,33 +0,0 @@
package helper
import "encoding/json"
type NullString struct {
String string
IsNull bool
}
// Serialization
func (ns NullString) MarshalJSON() ([]byte, error) {
// If Valid is true, return the JSON serialization result of String.
if ns.IsNull {
return []byte(`"` + ns.String + `"`), nil
}
// If Valid is false, return the serialization result of null.
return []byte("null"), nil
}
// Deserialization
func (ns *NullString) UnmarshalJSON(data []byte) error {
// If data is null, set Valid to false and String to an empty string.
if string(data) == "null" {
ns.String, ns.IsNull = "", false
return nil
}
// Otherwise, deserialize data to String and set Valid to true.
if err := json.Unmarshal(data, &ns.String); err != nil {
return err
}
ns.IsNull = true
return nil
}
+5 -5
View File
@@ -18,11 +18,11 @@ type UserDto struct {
}
type UserCreateDto struct {
Email string `json:"email" example:"test@example.com"`
Password string `json:"password" example:"password123"`
Role string `json:"role" example:"admin"`
CompanyID *types.Nullable[string] `json:"companyId" example:"01HGW2BBG0000000000000000"`
HourlyRate float64 `json:"hourlyRate" example:"50.00"`
Email string `json:"email" example:"test@example.com"`
Password string `json:"password" example:"password123"`
Role string `json:"role" example:"admin"`
CompanyID *string `json:"companyId" example:"01HGW2BBG0000000000000000"`
HourlyRate float64 `json:"hourlyRate" example:"50.00"`
}
type UserUpdateDto struct {