166 lines
4.4 KiB
Go
166 lines
4.4 KiB
Go
package models
|
|
|
|
import (
|
|
"GoMembership/pkg/logger"
|
|
"time"
|
|
|
|
"gorm.io/gorm"
|
|
"gorm.io/gorm/clause"
|
|
)
|
|
|
|
type Car struct {
|
|
ID uint `gorm:"primaryKey" json:"id"`
|
|
CreatedAt time.Time
|
|
UpdatedAt time.Time
|
|
DeletedAt *time.Time
|
|
Status uint `json:"status"`
|
|
Name string `json:"name"`
|
|
Brand string `gorm:"not null" json:"brand"`
|
|
Model string `gorm:"not null" json:"model"`
|
|
Color string `gorm:"not null" json:"color"`
|
|
LicencePlate string `gorm:"not null,unique" json:"licence_plate"`
|
|
Price float32 `gorm:"type:decimal(10,2)" json:"price"`
|
|
Rate float32 `gorm:"type:decimal(10,2)" json:"rate"`
|
|
StartDate time.Time `json:"start_date"`
|
|
EndDate time.Time `json:"end_date"`
|
|
Location *Location `gorm:"constraint:OnUpdate:CASCADE,OnDelete:CASCADE;" json:"location"`
|
|
Damages []Damage `gorm:"constraint:OnUpdate:CASCADE,OnDelete:CASCADE;" json:"damages"`
|
|
Insurances []Insurance `gorm:"constraint:OnUpdate:CASCADE,OnDelete:CASCADE;many2many:car_insurances" json:"insurances"`
|
|
Notes string `json:"notes"`
|
|
}
|
|
|
|
func (c *Car) Create(db *gorm.DB) error {
|
|
return db.Transaction(func(tx *gorm.DB) error {
|
|
if err := tx.Preload(clause.Associations).Create(c).Error; err != nil {
|
|
return err
|
|
}
|
|
|
|
logger.Info.Printf("car created: %#v", c)
|
|
return tx.
|
|
Preload(clause.Associations).
|
|
First(c, c.ID).Error
|
|
})
|
|
}
|
|
|
|
func (c *Car) Update(db *gorm.DB) error {
|
|
|
|
return db.Transaction(func(tx *gorm.DB) error {
|
|
// Check if the user exists in the database
|
|
var existingCar Car
|
|
|
|
logger.Info.Printf("updating car: %#v", c)
|
|
if err := tx.Preload("Damages.Insurance").
|
|
Preload("Damages.Opponent").
|
|
First(&existingCar, c.ID).Error; err != nil {
|
|
return err
|
|
}
|
|
|
|
if err := tx.Session(&gorm.Session{FullSaveAssociations: true}).Updates(c).Error; err != nil {
|
|
return err
|
|
}
|
|
// if err := tx.Model(c).Association("Damages").Replace(c.Damages); err != nil {
|
|
// return err
|
|
// }
|
|
|
|
// if err := tx.Model(c).Association("Insurances").Replace(c.Insurances); err != nil {
|
|
// return err
|
|
// }
|
|
// Calculate damage IDs to delete
|
|
// existingDamageIDs := make(map[uint]bool)
|
|
// for _, d := range existingCar.Damages {
|
|
// existingDamageIDs[d.ID] = true
|
|
// }
|
|
|
|
// newDamageIDs := make(map[uint]bool)
|
|
// for _, d := range c.Damages {
|
|
// if d.ID != 0 {
|
|
// newDamageIDs[d.ID] = true
|
|
// }
|
|
// }
|
|
|
|
// // Find IDs to delete
|
|
// var toDelete []uint
|
|
// for id := range existingDamageIDs {
|
|
// if !newDamageIDs[id] {
|
|
// toDelete = append(toDelete, id)
|
|
// }
|
|
// }
|
|
|
|
// // Batch delete orphaned damages
|
|
// if len(toDelete) > 0 {
|
|
// if err := tx.Where("id IN ?", toDelete).Delete(&Damage{}).Error; err != nil {
|
|
// return err
|
|
// }
|
|
// }
|
|
// if len(c.Insurances) > 0 {
|
|
// logger.Info.Printf("updating insurances: %#v", c.Insurances)
|
|
// if err := tx.Model(&existingCar).Association("Insurances").Replace(c.Insurances); err != nil {
|
|
// return err
|
|
// }
|
|
// }
|
|
|
|
// // Upsert new damages
|
|
// for _, damage := range c.Damages {
|
|
// // Process relationships
|
|
// if damage.Opponent != nil {
|
|
// if err := tx.Save(damage.Opponent).Error; err != nil {
|
|
// return err
|
|
// }
|
|
// damage.OpponentID = damage.Opponent.ID
|
|
// }
|
|
|
|
// if damage.Insurance != nil {
|
|
// if err := tx.Save(damage.Insurance).Error; err != nil {
|
|
// return err
|
|
// }
|
|
// damage.InsuranceID = damage.Insurance.ID
|
|
// }
|
|
|
|
// // Create or update damage
|
|
// if err := tx.Save(damage).Error; err != nil {
|
|
// return err
|
|
// }
|
|
// }
|
|
|
|
// // Update associations
|
|
// if err := tx.Model(&existingCar).Association("Damages").Replace(c.Damages); err != nil {
|
|
// return err
|
|
// }
|
|
return tx.
|
|
Preload(clause.Associations).
|
|
Preload("Damages").
|
|
Preload("Insurances").
|
|
First(c, c.ID).Error
|
|
})
|
|
|
|
}
|
|
|
|
func (c *Car) Delete(db *gorm.DB) error {
|
|
return db.Select(clause.Associations).Delete(&c).Error
|
|
}
|
|
|
|
func GetAllCars(db *gorm.DB) ([]Car, error) {
|
|
var cars []Car
|
|
if err := db.
|
|
Preload(clause.Associations).
|
|
Preload("Damages").
|
|
Preload("Insurances").
|
|
Find(&cars).Error; err != nil {
|
|
return nil, err
|
|
}
|
|
return cars, nil
|
|
}
|
|
|
|
func (c *Car) FromID(db *gorm.DB, id uint) error {
|
|
var car Car
|
|
if err := db.
|
|
Preload(clause.Associations).
|
|
Preload("Damages").
|
|
Preload("Insurances").
|
|
First(&car, id).Error; err != nil {
|
|
return err
|
|
}
|
|
*c = car
|
|
return nil
|
|
}
|