55 lines
1.3 KiB
Go
55 lines
1.3 KiB
Go
package models
|
|
|
|
import (
|
|
"GoMembership/pkg/logger"
|
|
"time"
|
|
|
|
"gorm.io/gorm"
|
|
)
|
|
|
|
type Location struct {
|
|
ID uint `gorm:"primaryKey" json:"id"`
|
|
CreatedAt time.Time
|
|
UpdatedAt time.Time
|
|
DeletedAt *time.Time
|
|
CarID uint `gorm:"index" json:"car_id"`
|
|
Latitude float32 `json:"latitude"`
|
|
Longitude float32 `json:"longitude"`
|
|
}
|
|
|
|
func (l *Location) Create(db *gorm.DB) error {
|
|
return db.Transaction(func(tx *gorm.DB) error {
|
|
// Create the base User record (omit associations to handle them separately)
|
|
if err := tx.Create(l).Error; err != nil {
|
|
return err
|
|
}
|
|
logger.Info.Printf("Location created: %#v", l)
|
|
// Preload all associations to return the fully populated User
|
|
return tx.
|
|
First(l, l.ID).Error // Refresh the user object with all associations
|
|
})
|
|
}
|
|
|
|
func (l *Location) Update(db *gorm.DB) error {
|
|
|
|
return db.Transaction(func(tx *gorm.DB) error {
|
|
// Check if the user exists in the database
|
|
var existingLocation Location
|
|
|
|
logger.Info.Printf("updating Location: %#v", l)
|
|
if err := tx.First(&existingLocation, l.ID).Error; err != nil {
|
|
return err
|
|
}
|
|
|
|
if err := tx.Model(&existingLocation).Updates(l).Error; err != nil {
|
|
return err
|
|
}
|
|
return tx.First(l, l.ID).Error
|
|
})
|
|
|
|
}
|
|
|
|
func (l *Location) Delete(db *gorm.DB) error {
|
|
return db.Delete(&l).Error
|
|
}
|