/
tasks.go
165 lines (136 loc) · 4.34 KB
/
tasks.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
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
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
package services
import (
"time"
"github.com/architectv/networking-course-project/backend/pkg/models"
"github.com/architectv/networking-course-project/backend/pkg/repositories"
)
type TaskService struct {
repo repositories.Task
boardRepo repositories.Board
projectRepo repositories.Project
}
func NewTaskService(repo repositories.Task, boardRepo repositories.Board, projectRepo repositories.Project) *TaskService {
return &TaskService{repo: repo, boardRepo: boardRepo, projectRepo: projectRepo}
}
func (s *TaskService) GetAll(userId, projectId, boardId, listId int) *models.ApiResponse {
r := &models.ApiResponse{}
projectPermissions, err := s.projectRepo.GetPermissions(userId, projectId)
if err != nil || projectPermissions.Read == false {
r.Error(StatusForbidden, "Forbidden")
return r
}
boardPermissions, err := s.boardRepo.GetPermissions(userId, boardId)
if err != nil || boardPermissions.Read == false {
r.Error(StatusForbidden, "Forbidden")
return r
}
tasks, err := s.repo.GetAll(listId)
if err != nil {
r.Error(StatusInternalServerError, err.Error())
return r
}
r.Set(StatusOK, "OK", Map{"tasks": tasks})
return r
}
func (s *TaskService) GetById(userId, projectId, boardId, listId, taksId int) *models.ApiResponse {
r := &models.ApiResponse{}
projectPermissions, err := s.projectRepo.GetPermissions(userId, projectId)
if err != nil || projectPermissions.Read == false {
r.Error(StatusForbidden, "Forbidden")
return r
}
boardPermissions, err := s.boardRepo.GetPermissions(userId, boardId)
if err != nil || boardPermissions.Read == false {
r.Error(StatusForbidden, "Forbidden")
return r
}
task, err := s.repo.GetById(taksId)
if err != nil {
r.Error(StatusInternalServerError, err.Error())
return r
}
r.Set(StatusOK, "OK", Map{"task": task})
return r
}
func (s *TaskService) Create(userId, projectId, boardId, listId int, task *models.Task) *models.ApiResponse {
r := &models.ApiResponse{}
projectPermissions, err := s.projectRepo.GetPermissions(userId, projectId)
if err != nil || projectPermissions.Read == false {
r.Error(StatusForbidden, "Forbidden")
return r
}
boardPermissions, err := s.boardRepo.GetPermissions(userId, boardId)
if err != nil || boardPermissions.Read == false {
r.Error(StatusForbidden, "Forbidden")
return r
}
task.ListId = listId
curTime := time.Now().Unix()
datetimes := &models.Datetimes{
Created: curTime,
Updated: curTime,
Accessed: curTime,
}
task.Datetimes = datetimes
taskId, err := s.repo.Create(task)
if err != nil {
r.Error(StatusInternalServerError, err.Error())
return r
}
r.Set(StatusOK, "OK", Map{"taskId": taskId})
return r
}
func (s *TaskService) Update(userId, projectId, boardId, listId, taskId int, task *models.UpdateTask) *models.ApiResponse {
r := &models.ApiResponse{}
if task.Position != nil && *task.Position < 0 {
r.Error(StatusBadRequest, "Task position out of bounds")
return r
}
if task.ListId != nil && *task.ListId < 1 {
r.Error(StatusBadRequest, "New list id out of bounds")
return r
}
// TODO: права projectId = read ?
projectPermissions, err := s.projectRepo.GetPermissions(userId, projectId)
if err != nil || projectPermissions.Read == false {
r.Error(StatusForbidden, "Forbidden")
return r
}
boardPermissions, err := s.boardRepo.GetPermissions(userId, boardId)
if err != nil || boardPermissions.Write == false {
r.Error(StatusForbidden, "Forbidden")
return r
}
curTime := time.Now().Unix()
task.Datetimes = &models.UpdateDatetimes{
Updated: &curTime,
Accessed: &curTime,
}
if err = s.repo.Update(taskId, task); err != nil {
r.Error(StatusInternalServerError, err.Error())
return r
}
r.Set(StatusOK, "OK", Map{})
return r
}
func (s *TaskService) Delete(userId, projectId, boardId, listId, taskId int) *models.ApiResponse {
r := &models.ApiResponse{}
// TODO: права projectId = read ?
projectPermissions, err := s.projectRepo.GetPermissions(userId, projectId)
if err != nil || projectPermissions.Read == false {
r.Error(StatusForbidden, "Forbidden")
return r
}
boardPermissions, err := s.boardRepo.GetPermissions(userId, boardId)
if err != nil || boardPermissions.Write == false {
r.Error(StatusForbidden, "Forbidden")
return r
}
err = s.repo.Delete(taskId)
if err != nil {
r.Error(StatusInternalServerError, err.Error())
return r
}
r.Set(StatusOK, "OK", Map{})
return r
}