wip
This commit is contained in:
@@ -1,123 +1,151 @@
|
||||
package models
|
||||
|
||||
import (
|
||||
"GoMembership/pkg/errors"
|
||||
"GoMembership/pkg/logger"
|
||||
"time"
|
||||
|
||||
"gorm.io/gorm"
|
||||
"gorm.io/gorm/clause"
|
||||
)
|
||||
|
||||
type Car struct {
|
||||
ID uint `gorm:"primarykey" json:"id"`
|
||||
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 `json:"price"`
|
||||
Rate float32 `json:"rate"`
|
||||
StartDate time.Time `json:"start_date"`
|
||||
EndDate time.Time `json:"end_date"`
|
||||
Location Location `gorm:"constraint:OnUpdate:CASCADE,OnDelete:CASCADE;" json:"location"`
|
||||
LocationID uint
|
||||
Damages *[]Damage `gorm:"constraint:OnUpdate:CASCADE,OnDelete:CASCADE;" json:"damages"`
|
||||
Insurances *[]Insurance `gorm:"foreignkey:OwnerID;constraint:OnUpdate:CASCADE,OnDelete:CASCADE;" json:"insurance"`
|
||||
Notes string `json:"notes"`
|
||||
}
|
||||
|
||||
type Location struct {
|
||||
ID uint `gorm:"primarykey" json:"id"`
|
||||
CreatedAt time.Time
|
||||
UpdatedAt time.Time
|
||||
DeletedAt *time.Time
|
||||
Latitude float32 `json:"latitude"`
|
||||
Longitude float32 `json:"longitude"`
|
||||
}
|
||||
|
||||
type Damage struct {
|
||||
ID uint `gorm:"primarykey" json:"id"`
|
||||
CarID uint `json:"car_id"`
|
||||
CreatedAt time.Time
|
||||
UpdatedAt time.Time
|
||||
DeletedAt *time.Time
|
||||
Opponent *User `gorm:"constraint:OnUpdate:CASCADE,OnDelete:CASCADE;" json:"opponent"`
|
||||
OpponentID uint
|
||||
Insurance *Insurance `gorm:"foreignkey:OwnerID;constraint:OnUpdate:CASCADE,OnDelete:CASCADE;" json:"insurance"`
|
||||
InsuranceID uint
|
||||
Notes string `json:"notes"`
|
||||
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 {
|
||||
// Create the base User record (omit associations to handle them separately)
|
||||
if err := tx.Create(c).Error; err != nil {
|
||||
if err := tx.Preload(clause.Associations).Create(c).Error; err != nil {
|
||||
return err
|
||||
}
|
||||
|
||||
// Replace associated Categories (assumes Categories already exist)
|
||||
if c.Insurances != nil {
|
||||
if err := tx.Model(c).Association("Insurances").Replace(c.Insurances); err != nil {
|
||||
return err
|
||||
}
|
||||
}
|
||||
logger.Info.Printf("car created: %#v", c)
|
||||
// Preload all associations to return the fully populated User
|
||||
return tx.
|
||||
Preload("Insurances").
|
||||
First(c, c.ID).Error // Refresh the user object with all associations
|
||||
Preload(clause.Associations).
|
||||
First(c, c.ID).Error
|
||||
})
|
||||
}
|
||||
|
||||
func (c *Car) Update(db *gorm.DB) error {
|
||||
|
||||
err := db.Transaction(func(tx *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("Insurances").
|
||||
if err := tx.Preload("Damages.Insurance").
|
||||
Preload("Damages.Opponent").
|
||||
First(&existingCar, c.ID).Error; err != nil {
|
||||
return err
|
||||
}
|
||||
result := tx.Session(&gorm.Session{FullSaveAssociations: true}).Updates(c)
|
||||
if result.Error != nil {
|
||||
logger.Error.Printf("car update error: %#v", result.Error)
|
||||
return result.Error
|
||||
}
|
||||
if result.RowsAffected == 0 {
|
||||
return errors.ErrNoRowsAffected
|
||||
}
|
||||
|
||||
if c.Insurances != nil {
|
||||
if err := tx.Save(*c.Insurances).Error; err != nil {
|
||||
return err
|
||||
}
|
||||
if err := tx.Session(&gorm.Session{FullSaveAssociations: true}).Updates(c).Error; err != nil {
|
||||
return err
|
||||
}
|
||||
return nil
|
||||
// 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
|
||||
})
|
||||
|
||||
if err != nil {
|
||||
return err
|
||||
}
|
||||
|
||||
return db.
|
||||
Preload("Insurances").
|
||||
First(&c, c.ID).Error
|
||||
}
|
||||
|
||||
func (c *Car) Delete(db *gorm.DB) error {
|
||||
return db.Delete(&c).Error
|
||||
return db.Select(clause.Associations).Delete(&c).Error
|
||||
}
|
||||
|
||||
func GetAllCars(db *gorm.DB) ([]Car, error) {
|
||||
var cars []Car
|
||||
if err := db.Find(&cars).Error; err != nil {
|
||||
if err := db.
|
||||
Preload(clause.Associations).
|
||||
Preload("Damages").
|
||||
Preload("Insurances").
|
||||
Find(&cars).Error; err != nil {
|
||||
return nil, err
|
||||
}
|
||||
return cars, nil
|
||||
@@ -125,7 +153,11 @@ func GetAllCars(db *gorm.DB) ([]Car, error) {
|
||||
|
||||
func (c *Car) FromID(db *gorm.DB, id uint) error {
|
||||
var car Car
|
||||
if err := db.Preload("Insurances").First(&car, id).Error; err != nil {
|
||||
if err := db.
|
||||
Preload(clause.Associations).
|
||||
Preload("Damages").
|
||||
Preload("Insurances").
|
||||
First(&car, id).Error; err != nil {
|
||||
return err
|
||||
}
|
||||
*c = car
|
||||
|
||||
Reference in New Issue
Block a user