init project
This commit is contained in:
@@ -0,0 +1,158 @@
|
||||
package entity
|
||||
|
||||
import (
|
||||
"database/sql/driver"
|
||||
"errors"
|
||||
"strings"
|
||||
"time"
|
||||
)
|
||||
|
||||
type Status string
|
||||
|
||||
type Event struct {
|
||||
ID int64
|
||||
Name string
|
||||
Description string
|
||||
StartDate time.Time
|
||||
EndDate time.Time
|
||||
Location string
|
||||
Level string
|
||||
Included StringArray `gorm:"type:text[]"`
|
||||
Price float64
|
||||
Paid bool
|
||||
LocationID *int64
|
||||
Status Status
|
||||
CreatedAt time.Time
|
||||
UpdatedAt time.Time
|
||||
DeletedAt *time.Time
|
||||
}
|
||||
|
||||
type StringArray []string
|
||||
|
||||
func (a StringArray) Value() (driver.Value, error) {
|
||||
if a == nil {
|
||||
return nil, nil
|
||||
}
|
||||
|
||||
joined := "{" + strings.Join(a, ",") + "}"
|
||||
|
||||
return []byte(joined), nil
|
||||
}
|
||||
|
||||
func (a *StringArray) Scan(src interface{}) error {
|
||||
if src == nil {
|
||||
*a = nil
|
||||
return nil
|
||||
}
|
||||
|
||||
srcStr, ok := src.(string)
|
||||
if !ok {
|
||||
return errors.New("failed to scan StringArray")
|
||||
}
|
||||
|
||||
// Remove the curly braces and split the string into elements
|
||||
if len(srcStr) < 2 || srcStr[0] != '{' || srcStr[len(srcStr)-1] != '}' {
|
||||
return errors.New("invalid format for StringArray")
|
||||
}
|
||||
srcStr = srcStr[1 : len(srcStr)-1]
|
||||
|
||||
*a = strings.Split(srcStr, ",")
|
||||
|
||||
return nil
|
||||
}
|
||||
|
||||
type EventSearch struct {
|
||||
Name string
|
||||
Limit int
|
||||
Offset int
|
||||
}
|
||||
|
||||
type EventList []*EventDB
|
||||
|
||||
type EventDB struct {
|
||||
Event
|
||||
}
|
||||
|
||||
func (e *Event) ToEventDB() *EventDB {
|
||||
return &EventDB{
|
||||
Event: *e,
|
||||
}
|
||||
}
|
||||
|
||||
func (e *EventDB) ToEvent() *Event {
|
||||
return &Event{
|
||||
ID: e.ID,
|
||||
Name: e.Name,
|
||||
Description: e.Description,
|
||||
StartDate: e.StartDate,
|
||||
EndDate: e.EndDate,
|
||||
Location: e.Location,
|
||||
Level: e.Level,
|
||||
Included: e.Included,
|
||||
Price: e.Price,
|
||||
Paid: e.Paid,
|
||||
LocationID: e.LocationID,
|
||||
CreatedAt: e.CreatedAt,
|
||||
UpdatedAt: e.UpdatedAt,
|
||||
Status: e.Status,
|
||||
}
|
||||
}
|
||||
|
||||
func (e *EventList) ToEventList() []*Event {
|
||||
var events []*Event
|
||||
for _, event := range *e {
|
||||
events = append(events, event.ToEvent())
|
||||
}
|
||||
return events
|
||||
}
|
||||
|
||||
func (EventDB) TableName() string {
|
||||
return "events"
|
||||
}
|
||||
|
||||
func (o *EventDB) ToUpdatedEvent(req Event) {
|
||||
if req.Name != "" {
|
||||
o.Name = req.Name
|
||||
}
|
||||
|
||||
if req.Description != "" {
|
||||
o.Description = req.Description
|
||||
}
|
||||
|
||||
if !req.StartDate.IsZero() {
|
||||
o.StartDate = req.StartDate
|
||||
}
|
||||
|
||||
if !req.EndDate.IsZero() {
|
||||
o.EndDate = req.EndDate
|
||||
}
|
||||
|
||||
if req.Location != "" {
|
||||
o.Location = req.Location
|
||||
}
|
||||
|
||||
if req.Level != "" {
|
||||
o.Level = req.Level
|
||||
}
|
||||
|
||||
if req.Included != nil && len(req.Included) > 0 {
|
||||
o.Included = req.Included
|
||||
}
|
||||
|
||||
if req.Price != 0 {
|
||||
o.Price = req.Price
|
||||
}
|
||||
|
||||
if req.LocationID != nil {
|
||||
o.LocationID = req.LocationID
|
||||
}
|
||||
|
||||
if req.Status != "" {
|
||||
o.Status = req.Status
|
||||
}
|
||||
}
|
||||
|
||||
func (o *EventDB) SetDeleted() {
|
||||
currentTime := time.Now()
|
||||
o.DeletedAt = ¤tTime
|
||||
}
|
||||
Reference in New Issue
Block a user