Multiple implementations of the same back-end application. The aim is to provide quick, side-by-side comparisons of different technologies (languages, frameworks, libraries) while preserving consistent business logic across all implementations.
You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.
 
 
 
 
 
 

118 lines
2.7 KiB

package entities
import (
"encoding/json"
"fmt"
"time"
"autostore/internal/domain/value_objects"
)
type itemEntityJSON struct {
ID string `json:"id"`
Name string `json:"name"`
ExpirationDate time.Time `json:"expirationDate"`
OrderURL string `json:"orderUrl"`
UserID string `json:"userId"`
CreatedAt time.Time `json:"createdAt"`
}
type ItemEntity struct {
id value_objects.ItemID
name string
expirationDate value_objects.ExpirationDate
orderURL string
userID value_objects.UserID
createdAt time.Time
}
func NewItem(id value_objects.ItemID, name string, expirationDate value_objects.ExpirationDate, orderURL string, userID value_objects.UserID) (*ItemEntity, error) {
if name == "" {
return nil, ErrInvalidItemName
}
if orderURL == "" {
return nil, ErrInvalidOrderURL
}
return &ItemEntity{
id: id,
name: name,
expirationDate: expirationDate,
orderURL: orderURL,
userID: userID,
createdAt: time.Now(),
}, nil
}
func (i *ItemEntity) GetID() value_objects.ItemID {
return i.id
}
func (i *ItemEntity) GetName() string {
return i.name
}
func (i *ItemEntity) GetExpirationDate() value_objects.ExpirationDate {
return i.expirationDate
}
func (i *ItemEntity) ExpirationDate() time.Time {
return i.expirationDate.Time()
}
func (i *ItemEntity) GetOrderURL() string {
return i.orderURL
}
func (i *ItemEntity) GetUserID() value_objects.UserID {
return i.userID
}
func (i *ItemEntity) GetCreatedAt() time.Time {
return i.createdAt
}
// MarshalJSON implements json.Marshaler interface
func (i *ItemEntity) MarshalJSON() ([]byte, error) {
return json.Marshal(&itemEntityJSON{
ID: i.id.String(),
Name: i.name,
ExpirationDate: i.expirationDate.Time(),
OrderURL: i.orderURL,
UserID: i.userID.String(),
CreatedAt: i.createdAt,
})
}
// UnmarshalJSON implements json.Unmarshaler interface
func (i *ItemEntity) UnmarshalJSON(data []byte) error {
var aux itemEntityJSON
if err := json.Unmarshal(data, &aux); err != nil {
return err
}
id, err := value_objects.NewItemIDFromString(aux.ID)
if err != nil {
return fmt.Errorf("invalid item ID: %w", err)
}
userID, err := value_objects.NewUserIDFromString(aux.UserID)
if err != nil {
return fmt.Errorf("invalid user ID: %w", err)
}
expirationDate, err := value_objects.NewExpirationDate(aux.ExpirationDate)
if err != nil {
return fmt.Errorf("invalid expiration date: %w", err)
}
i.id = id
i.name = aux.Name
i.expirationDate = expirationDate
i.orderURL = aux.OrderURL
i.userID = userID
i.createdAt = aux.CreatedAt
return nil
}