diff options
author | Mo Tarbin <mhed.t91@gmail.com> | 2024-06-30 21:41:41 -0400 |
---|---|---|
committer | Mo Tarbin <mhed.t91@gmail.com> | 2024-06-30 21:41:41 -0400 |
commit | c13dd9addbf89f716e4ef5cfdf1d673139ffcb68 (patch) | |
tree | bc09646ce1d6d3a402abb4694e19da51b57204f6 /internal/thing/repo | |
download | donetick-c13dd9addbf89f716e4ef5cfdf1d673139ffcb68.tar.gz donetick-c13dd9addbf89f716e4ef5cfdf1d673139ffcb68.tar.bz2 donetick-c13dd9addbf89f716e4ef5cfdf1d673139ffcb68.zip |
Move to Donetick Org, first commit
Diffstat (limited to 'internal/thing/repo')
-rw-r--r-- | internal/thing/repo/repository.go | 117 |
1 files changed, 117 insertions, 0 deletions
diff --git a/internal/thing/repo/repository.go b/internal/thing/repo/repository.go new file mode 100644 index 0000000..8b2dbaa --- /dev/null +++ b/internal/thing/repo/repository.go @@ -0,0 +1,117 @@ +package chore + +import ( + "context" + "time" + + config "donetick.com/core/config" + tModel "donetick.com/core/internal/thing/model" + "gorm.io/gorm" +) + +type ThingRepository struct { + db *gorm.DB + dbType string +} + +func NewThingRepository(db *gorm.DB, cfg *config.Config) *ThingRepository { + return &ThingRepository{db: db, dbType: cfg.Database.Type} +} + +func (r *ThingRepository) UpsertThing(c context.Context, thing *tModel.Thing) error { + return r.db.WithContext(c).Model(&thing).Save(thing).Error +} + +func (r *ThingRepository) UpdateThingState(c context.Context, thing *tModel.Thing) error { + // update the state of the thing where the id is the same: + if err := r.db.WithContext(c).Model(&thing).Where("id = ?", thing.ID).Updates(map[string]interface{}{ + "state": thing.State, + "updated_at": time.Now().UTC(), + }).Error; err != nil { + return err + } + // Create history Record of the thing : + createdAt := time.Now().UTC() + thingHistory := &tModel.ThingHistory{ + ThingID: thing.ID, + State: thing.State, + CreatedAt: &createdAt, + UpdatedAt: &createdAt, + } + + if err := r.db.WithContext(c).Create(thingHistory).Error; err != nil { + return err + } + + return nil +} +func (r *ThingRepository) GetThingByID(c context.Context, thingID int) (*tModel.Thing, error) { + var thing tModel.Thing + if err := r.db.WithContext(c).Model(&tModel.Thing{}).Preload("ThingChores").First(&thing, thingID).Error; err != nil { + return nil, err + } + return &thing, nil +} + +func (r *ThingRepository) GetThingByChoreID(c context.Context, choreID int) (*tModel.Thing, error) { + var thing tModel.Thing + if err := r.db.WithContext(c).Model(&tModel.Thing{}).Joins("left join thing_chores on things.id = thing_chores.thing_id").First(&thing, "thing_chores.chore_id = ?", choreID).Error; err != nil { + return nil, err + } + return &thing, nil +} + +func (r *ThingRepository) AssociateThingWithChore(c context.Context, thingID int, choreID int, triggerState string, condition string) error { + + return r.db.WithContext(c).Save(&tModel.ThingChore{ThingID: thingID, ChoreID: choreID, TriggerState: triggerState, Condition: condition}).Error +} + +func (r *ThingRepository) DissociateThingWithChore(c context.Context, thingID int, choreID int) error { + return r.db.WithContext(c).Where("thing_id = ? AND chore_id = ?", thingID, choreID).Delete(&tModel.ThingChore{}).Error +} + +func (r *ThingRepository) GetThingHistoryWithOffset(c context.Context, thingID int, offset int, limit int) ([]*tModel.ThingHistory, error) { + var thingHistory []*tModel.ThingHistory + if err := r.db.WithContext(c).Model(&tModel.ThingHistory{}).Where("thing_id = ?", thingID).Order("created_at desc").Offset(offset).Limit(limit).Find(&thingHistory).Error; err != nil { + return nil, err + } + return thingHistory, nil +} + +func (r *ThingRepository) GetUserThings(c context.Context, userID int) ([]*tModel.Thing, error) { + var things []*tModel.Thing + if err := r.db.WithContext(c).Model(&tModel.Thing{}).Where("user_id = ?", userID).Find(&things).Error; err != nil { + return nil, err + } + return things, nil +} + +func (r *ThingRepository) DeleteThing(c context.Context, thingID int) error { + // one transaction to delete the thing and its history : + return r.db.WithContext(c).Transaction(func(tx *gorm.DB) error { + if err := r.db.WithContext(c).Where("thing_id = ?", thingID).Delete(&tModel.ThingHistory{}).Error; err != nil { + return err + } + if err := r.db.WithContext(c).Delete(&tModel.Thing{}, thingID).Error; err != nil { + return err + } + return nil + }) +} + +// get ThingChores by thingID: +func (r *ThingRepository) GetThingChoresByThingId(c context.Context, thingID int) ([]*tModel.ThingChore, error) { + var thingChores []*tModel.ThingChore + if err := r.db.WithContext(c).Model(&tModel.ThingChore{}).Where("thing_id = ?", thingID).Find(&thingChores).Error; err != nil { + return nil, err + } + return thingChores, nil +} + +// func (r *ThingRepository) GetChoresByThingId(c context.Context, thingID int) ([]*chModel.Chore, error) { +// var chores []*chModel.Chore +// if err := r.db.WithContext(c).Model(&chModel.Chore{}).Joins("left join thing_chores on chores.id = thing_chores.chore_id").Where("thing_chores.thing_id = ?", thingID).Find(&chores).Error; err != nil { +// return nil, err +// } +// return chores, nil +// } |