Added a route to get all tasks sorted desc by their due dates (#14)

This commit is contained in:
konrad 2018-11-02 16:59:49 +00:00 committed by Gitea
parent 14207a0fa8
commit 31a4a1dd00
6 changed files with 99 additions and 24 deletions

1
.gitignore vendored
View file

@ -1,4 +1,5 @@
.idea/ .idea/
.idea/*
.idea/*/* .idea/*/*
config.yml config.yml
config.yaml config.yaml

View file

@ -4,7 +4,7 @@ Content-Type: application/json
{ {
"username": "user", "username": "user",
"password": "1234" "password": "12345"
} }
> {% client.global.set("auth_token", response.body.token); %} > {% client.global.set("auth_token", response.body.token); %}

View file

@ -99,3 +99,9 @@ DELETE http://localhost:8080/api/v1/lists/28/users/3
Authorization: Bearer {{auth_token}} Authorization: Bearer {{auth_token}}
### ###
# Get all pending tasks
GET http://localhost:8080/api/v1/tasks
Authorization: Bearer {{auth_token}}
###

View file

@ -1,5 +1,7 @@
package models package models
import "sort"
// List represents a list of tasks // List represents a list of tasks
type List struct { type List struct {
ID int64 `xorm:"int(11) autoincr not null unique pk" json:"id" param:"list"` ID int64 `xorm:"int(11) autoincr not null unique pk" json:"id" param:"list"`
@ -26,32 +28,11 @@ func GetListsByNamespaceID(nID int64) (lists []*List, err error) {
// ReadAll gets all lists a user has access to // ReadAll gets all lists a user has access to
func (l *List) ReadAll(u *User) (interface{}, error) { func (l *List) ReadAll(u *User) (interface{}, error) {
lists := []*List{} lists, err := getRawListsForUser(u)
fullUser, err := GetUserByID(u.ID)
if err != nil { if err != nil {
return lists, err return nil, err
} }
// Gets all Lists where the user is either owner or in a team which has access to the list
// Or in a team which has namespace read access
err = x.Select("l.*").
Table("list").
Alias("l").
Join("INNER", []string{"namespaces", "n"}, "l.namespace_id = n.id").
Join("LEFT", []string{"team_namespaces", "tn"}, "tn.namespace_id = n.id").
Join("LEFT", []string{"team_members", "tm"}, "tm.team_id = tn.team_id").
Join("LEFT", []string{"team_list", "tl"}, "l.id = tl.list_id").
Join("LEFT", []string{"team_members", "tm2"}, "tm2.team_id = tl.team_id").
Join("LEFT", []string{"users_list", "ul"}, "ul.list_id = l.id").
Join("LEFT", []string{"users_namespace", "un"}, "un.namespace_id = l.namespace_id").
Where("tm.user_id = ?", fullUser.ID).
Or("tm2.user_id = ?", fullUser.ID).
Or("l.owner_id = ?", fullUser.ID).
Or("ul.user_id = ?", fullUser.ID).
Or("un.user_id = ?", fullUser.ID).
GroupBy("l.id").
Find(&lists)
// Add more list details // Add more list details
AddListDetails(lists) AddListDetails(lists)
@ -98,6 +79,36 @@ func (l *List) GetSimpleByID() (err error) {
return return
} }
// Gets the lists only, without any tasks or so
func getRawListsForUser(u *User) (lists []*List, err error) {
fullUser, err := GetUserByID(u.ID)
if err != nil {
return lists, err
}
// Gets all Lists where the user is either owner or in a team which has access to the list
// Or in a team which has namespace read access
err = x.Select("l.*").
Table("list").
Alias("l").
Join("INNER", []string{"namespaces", "n"}, "l.namespace_id = n.id").
Join("LEFT", []string{"team_namespaces", "tn"}, "tn.namespace_id = n.id").
Join("LEFT", []string{"team_members", "tm"}, "tm.team_id = tn.team_id").
Join("LEFT", []string{"team_list", "tl"}, "l.id = tl.list_id").
Join("LEFT", []string{"team_members", "tm2"}, "tm2.team_id = tl.team_id").
Join("LEFT", []string{"users_list", "ul"}, "ul.list_id = l.id").
Join("LEFT", []string{"users_namespace", "un"}, "un.namespace_id = l.namespace_id").
Where("tm.user_id = ?", fullUser.ID).
Or("tm2.user_id = ?", fullUser.ID).
Or("l.owner_id = ?", fullUser.ID).
Or("ul.user_id = ?", fullUser.ID).
Or("un.user_id = ?", fullUser.ID).
GroupBy("l.id").
Find(&lists)
return lists, err
}
// AddListDetails adds owner user objects and list tasks to all lists in the slice // AddListDetails adds owner user objects and list tasks to all lists in the slice
func AddListDetails(lists []*List) (err error) { func AddListDetails(lists []*List) (err error) {
var listIDs []int64 var listIDs []int64
@ -141,3 +152,38 @@ func AddListDetails(lists []*List) (err error) {
return return
} }
// ListTasksDummy is a dummy we use to be able to use the crud handler
type ListTasksDummy struct {
CRUDable
Rights
}
// ReadAll gets all tasks for a user
func (lt *ListTasksDummy) ReadAll(u *User) (interface{}, error) {
// Get all lists
lists, err := getRawListsForUser(u)
if err != nil {
return nil, err
}
// Get all list IDs and get the tasks
var listIDs []int64
for _, l := range lists {
listIDs = append(listIDs, l.ID)
}
// Then return all tasks for that lists
var tasks []*ListTask
if err := x.In("list_id", listIDs).Find(&tasks); err != nil {
return nil, err
}
// Sort it by due date
sort.Slice(tasks, func(i, j int) bool {
return tasks[i].DueDateUnix > tasks[j].DueDateUnix
})
return tasks, err
}

View file

@ -913,3 +913,18 @@ package v1
// "$ref": "#/responses/Message" // "$ref": "#/responses/Message"
// "500": // "500":
// "$ref": "#/responses/Message" // "$ref": "#/responses/Message"
// swagger:operation GET /tasks lists getPendingTasks
// ---
// summary: gets all tasks for the currently authenticated user
// consumes:
// - application/json
// produces:
// - application/json
// responses:
// "200":
// "$ref": "#/responses/ListTask"
// "400":
// "$ref": "#/responses/Message"
// "500":
// "$ref": "#/responses/Message"

View file

@ -99,6 +99,13 @@ func RegisterRoutes(e *echo.Echo) {
a.DELETE("/tasks/:listtask", taskHandler.DeleteWeb) a.DELETE("/tasks/:listtask", taskHandler.DeleteWeb)
a.POST("/tasks/:listtask", taskHandler.UpdateWeb) a.POST("/tasks/:listtask", taskHandler.UpdateWeb)
listTaskHandler := &crud.WebHandler{
EmptyStruct: func() crud.CObject {
return &models.ListTasksDummy{}
},
}
a.GET("/tasks", listTaskHandler.ReadAllWeb)
listTeamHandler := &crud.WebHandler{ listTeamHandler := &crud.WebHandler{
EmptyStruct: func() crud.CObject { EmptyStruct: func() crud.CObject {
return &models.TeamList{} return &models.TeamList{}