94 lines
2.7 KiB
Go
94 lines
2.7 KiB
Go
// internal/models/lost_item.go
|
|
package models
|
|
|
|
import (
|
|
"time"
|
|
|
|
"gorm.io/gorm"
|
|
)
|
|
|
|
// LostItem represents a lost item report
|
|
type LostItem struct {
|
|
ID uint `gorm:"primaryKey" json:"id"`
|
|
UserID uint `gorm:"not null" json:"user_id"`
|
|
User User `gorm:"foreignKey:UserID" json:"user,omitempty"`
|
|
Name string `gorm:"type:varchar(100);not null" json:"name"`
|
|
CategoryID uint `gorm:"not null" json:"category_id"`
|
|
Category Category `gorm:"foreignKey:CategoryID" json:"category,omitempty"`
|
|
Color string `gorm:"type:varchar(50)" json:"color"`
|
|
Location string `gorm:"type:varchar(200)" json:"location"` // Optional
|
|
Description string `gorm:"type:text;not null" json:"description"`
|
|
DateLost time.Time `gorm:"not null" json:"date_lost"`
|
|
Status string `gorm:"type:varchar(50);default:'active'" json:"status"` // active, found, expired
|
|
CreatedAt time.Time `json:"created_at"`
|
|
UpdatedAt time.Time `json:"updated_at"`
|
|
DeletedAt gorm.DeletedAt `gorm:"index" json:"-"`
|
|
|
|
// Relationships
|
|
MatchResults []MatchResult `gorm:"foreignKey:LostItemID" json:"match_results,omitempty"`
|
|
}
|
|
|
|
// TableName specifies the table name for LostItem model
|
|
func (LostItem) TableName() string {
|
|
return "lost_items"
|
|
}
|
|
|
|
// LostItem status constants
|
|
const (
|
|
LostItemStatusActive = "active"
|
|
LostItemStatusFound = "found"
|
|
LostItemStatusExpired = "expired"
|
|
)
|
|
|
|
// BeforeCreate hook
|
|
func (l *LostItem) BeforeCreate(tx *gorm.DB) error {
|
|
if l.Status == "" {
|
|
l.Status = LostItemStatusActive
|
|
}
|
|
return nil
|
|
}
|
|
|
|
// IsActive checks if lost item is still active
|
|
func (l *LostItem) IsActive() bool {
|
|
return l.Status == LostItemStatusActive
|
|
}
|
|
|
|
// LostItemResponse represents lost item data for API responses
|
|
type LostItemResponse struct {
|
|
ID uint `json:"id"`
|
|
UserName string `json:"user_name"`
|
|
Name string `json:"name"`
|
|
Category string `json:"category"`
|
|
Color string `json:"color"`
|
|
Location string `json:"location"`
|
|
Description string `json:"description"`
|
|
DateLost time.Time `json:"date_lost"`
|
|
Status string `json:"status"`
|
|
CreatedAt time.Time `json:"created_at"`
|
|
}
|
|
|
|
// ToResponse converts LostItem to LostItemResponse
|
|
func (l *LostItem) ToResponse() LostItemResponse {
|
|
userName := ""
|
|
if l.User.ID != 0 {
|
|
userName = l.User.Name
|
|
}
|
|
|
|
categoryName := ""
|
|
if l.Category.ID != 0 {
|
|
categoryName = l.Category.Name
|
|
}
|
|
|
|
return LostItemResponse{
|
|
ID: l.ID,
|
|
UserName: userName,
|
|
Name: l.Name,
|
|
Category: categoryName,
|
|
Color: l.Color,
|
|
Location: l.Location,
|
|
Description: l.Description,
|
|
DateLost: l.DateLost,
|
|
Status: l.Status,
|
|
CreatedAt: l.CreatedAt,
|
|
}
|
|
} |