-
Notifications
You must be signed in to change notification settings - Fork 1
/
task.go
93 lines (73 loc) · 1.86 KB
/
task.go
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
package repo
import (
"errors"
"github.com/gogap/isolator"
"github.com/gogap/isolator/example/todo/models"
"github.com/gogap/isolator/extension/xorm"
)
type TaskRepository interface {
isolator.Object
AddTask(task models.Task) (id string, err error)
GetTaskByID(id string) (task models.Task, exist bool, err error)
GetUserTasks(userID string) (task []models.Task, err error)
AllTasks() (task []models.Task, err error)
}
type TaskRepo struct {
session *isolator.Session
}
func NewTaskRepo() TaskRepository {
return &TaskRepo{}
}
func (p *TaskRepo) Derive(session *isolator.Session) (obj isolator.Object, err error) {
return &TaskRepo{
session: session,
}, nil
}
func (p *TaskRepo) AddTask(task models.Task) (id string, err error) {
xormSession := xorm.GetXORMSession(p.session, "todo")
if xormSession == nil {
err = errors.New("xorm session is nil")
return
}
if _, err = xormSession.InsertOne(&task); err != nil {
return
}
return
}
func (p *TaskRepo) GetTaskByID(id string) (task models.Task, exist bool, err error) {
xormSession := xorm.GetXORMSession(p.session, "todo")
if xormSession == nil {
err = errors.New("xorm session is nil")
return
}
t := models.Task{ID: id}
if exist, err = xormSession.Get(&t); err != nil {
return
}
if exist {
task = t
}
return
}
func (p *TaskRepo) GetUserTasks(userID string) (task []models.Task, err error) {
xormSession := xorm.GetXORMSession(p.session, "todo")
if xormSession == nil {
err = errors.New("xorm session is nil")
return
}
if err = xormSession.Where("`owner_id` = ?", userID).Find(&task); err != nil {
return
}
return
}
func (p *TaskRepo) AllTasks() (task []models.Task, err error) {
xormSession := xorm.GetXORMSession(p.session, "todo")
if xormSession == nil {
err = errors.New("xorm session is nil")
return
}
if err = xormSession.Find(&task); err != nil {
return
}
return
}