feat: Enhance user update handling and introduce NullString type for optional fields

This commit is contained in:
2025-03-12 07:54:00 +00:00
parent 0379ea4ae4
commit da115dc3f6
8 changed files with 111 additions and 54 deletions
+33
View File
@@ -0,0 +1,33 @@
package dto
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
}
+2 -2
View File
@@ -11,7 +11,7 @@ type UserDto struct {
LastEditorID string `json:"lastEditorID" example:"01HGW2BBG0000000000000000"`
Email string `json:"email" example:"test@example.com"`
Role string `json:"role" example:"admin"`
CompanyID string `json:"companyId" example:"01HGW2BBG0000000000000000"`
CompanyID *string `json:"companyId" example:"01HGW2BBG0000000000000000"`
HourlyRate float64 `json:"hourlyRate" example:"50.00"`
}
@@ -19,7 +19,7 @@ type UserCreateDto struct {
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"`
CompanyID *string `json:"companyId" example:"01HGW2BBG0000000000000000"`
HourlyRate float64 `json:"hourlyRate" example:"50.00"`
}