aboutsummaryrefslogtreecommitdiffstats
path: root/internal/thing/repo/repository.go
blob: ad5a92f929051734df144d2e2bdb6a86b4876c57 (plain) (blame)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
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) DissociateChoreWithThing(c context.Context, choreID int) error {
	return r.db.WithContext(c).Where("chore_id = ?", choreID).Delete(&tModel.ThingChore{}).Error
}

func (r *ThingRepository) GetThingHistoryWithOffset(c context.Context, thingID int, offset 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(10).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
// }