/
schedule.go
165 lines (143 loc) · 5.37 KB
/
schedule.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
/*GRP-GNU-AGPL******************************************************************
File: schedule.go
Copyright (C) 2021 Team Georepublic <info@georepublic.de>
Developer(s):
Copyright (C) 2021 Ashish Kumar <ashishkr23438@gmail.com>
-----
This file is part of pg_scheduleserv.
pg_scheduleserv is free software: you can redistribute it and/or modify
it under the terms of the GNU Affero General Public License as published
by the Free Software Foundation, either version 3 of the License, or
(at your option) any later version.
pg_scheduleserv is distributed in the hope that it will be useful,
but WITHOUT ANY WARRANTY; without even the implied warranty of
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
GNU Affero General Public License for more details.
You should have received a copy of the GNU Affero General Public License
along with pg_scheduleserv. If not, see <https://www.gnu.org/licenses/>.
******************************************************************GRP-GNU-AGPL*/
package api
import (
"net/http"
"strconv"
"github.com/Georepublic/pg_scheduleserv/internal/util"
"github.com/gorilla/mux"
)
// CreateSchedule godoc
// @Summary Schedule the tasks
// @Description Schedule the tasks present in a project, deleting any previous schedule and return the new schedule.
// @Description
// @Description When fresh = true, the old schedule is ignored and a fresh schedule is created. Otherwise, the old schedule of each task is altered such that it remains in the "max_shift" interval. Default value is false.
// @Description **For JSON content type**: When overview = true, only the metadata is returned. Default value is false, which also returns the summary object.
// @Tags Schedule
// @Accept application/json
// @Produce application/json
// @Param project_id path int true "Project ID"
// @Param fresh query bool false "Fresh"
// @Param overview query bool false "Overview"
// @Success 201 {object} util.SuccessResponse{data=util.ScheduleData}
// @Failure 400 {object} util.ErrorResponse
// @Router /projects/{project_id}/schedule [post]
func (server *Server) CreateSchedule(w http.ResponseWriter, r *http.Request) {
// Add the project_id path variable
vars := mux.Vars(r)
projectID, err := strconv.ParseInt(vars["project_id"], 10, 64)
if err != nil {
server.FormatJSON(w, http.StatusBadRequest, err)
return
}
ctx := r.Context()
fresh := r.URL.Query().Get("fresh")
err = server.DBCreateSchedule(ctx, projectID, fresh)
if err != nil {
server.FormatJSON(w, http.StatusBadRequest, err)
return
}
// Fetch the schedule
schedule, err := server.DBGetSchedule(ctx, projectID)
if err != nil {
server.FormatJSON(w, http.StatusBadRequest, err)
return
}
overview := r.URL.Query().Get("overview")
if overview == "true" {
server.FormatJSON(w, http.StatusCreated, util.ScheduleDataOverview{
Metadata: schedule.Metadata,
ProjectID: schedule.ProjectID,
})
} else {
server.FormatJSON(w, http.StatusCreated, schedule)
}
}
// GetSchedule godoc
// @Summary Get the schedule
// @Description Get the schedule for a project.
// @Description
// @Description **For JSON content type**: When overview = true, only the metadata is returned. Default value is false, which also returns the summary object.
// @Tags Schedule
// @Accept application/json
// @Produce text/calendar,application/json
// @Param project_id path int true "Project ID"
// @Param overview query bool false "Overview"
// @Success 200 {object} util.SuccessResponse{data=util.ScheduleData}
// @Failure 400 {object} util.ErrorResponse
// @Failure 404 {object} util.NotFound
// @Router /projects/{project_id}/schedule [get]
func (server *Server) GetSchedule(w http.ResponseWriter, r *http.Request) {
vars := mux.Vars(r)
projectID, err := strconv.ParseInt(vars["project_id"], 10, 64)
if err != nil {
server.FormatJSON(w, http.StatusBadRequest, err)
return
}
ctx := r.Context()
schedule, err := server.DBGetSchedule(ctx, projectID)
if err != nil {
server.FormatJSON(w, http.StatusBadRequest, err)
return
}
switch r.Header.Get("Accept") {
case "text/calendar":
calendar, filename := server.GetScheduleICal(schedule)
server.FormatICAL(w, http.StatusOK, calendar, filename)
case "application/json":
overview := r.URL.Query().Get("overview")
if overview == "true" {
server.FormatJSON(w, http.StatusOK, util.ScheduleDataOverview{
Metadata: schedule.Metadata,
ProjectID: schedule.ProjectID,
})
} else {
server.FormatJSON(w, http.StatusOK, schedule)
}
default:
calendar, filename := server.GetScheduleICal(schedule)
server.FormatICAL(w, http.StatusOK, calendar, filename)
}
}
// DeleteSchedule godoc
// @Summary Delete the schedule
// @Description Delete the schedule for a project
// @Tags Schedule
// @Accept application/json
// @Produce application/json
// @Param project_id path int true "Project ID"
// @Success 200 {object} util.Success
// @Failure 400 {object} util.ErrorResponse
// @Failure 404 {object} util.NotFound
// @Router /projects/{project_id}/schedule [delete]
func (server *Server) DeleteSchedule(w http.ResponseWriter, r *http.Request) {
vars := mux.Vars(r)
project_id, err := strconv.ParseInt(vars["project_id"], 10, 64)
if err != nil {
server.FormatJSON(w, http.StatusBadRequest, err)
return
}
ctx := r.Context()
err = server.DBDeleteSchedule(ctx, project_id)
if err != nil {
server.FormatJSON(w, http.StatusBadRequest, err)
return
}
server.FormatJSON(w, http.StatusOK, nil)
}