-
Notifications
You must be signed in to change notification settings - Fork 0
/
scheduler.go
61 lines (51 loc) · 1.39 KB
/
scheduler.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
package controllers
import (
"net/http"
"github.com/durgaprasad-budhwani/home-automation/backend/models"
"github.com/durgaprasad-budhwani/home-automation/backend/services"
"github.com/gin-gonic/gin"
)
type SchedulerController struct {
service services.SchedulerService
}
func NewSchedulerController(service services.SchedulerService) SchedulerController {
return SchedulerController{
service,
}
}
func (c SchedulerController) GetSchedules(context *gin.Context) {
schedulers, err := c.service.GetAll(10, 0)
if err != nil {
context.JSON(http.StatusInternalServerError, err.Error())
return
}
context.JSON(http.StatusOK, schedulers)
}
func (c SchedulerController) AddSchedule(context *gin.Context) {
var status models.Schedule
err := context.BindJSON(&status)
if err != nil {
context.JSON(http.StatusInternalServerError, err.Error())
return
}
err = c.service.Save(&status)
if err != nil {
context.JSON(http.StatusInternalServerError, err.Error())
return
}
context.JSON(http.StatusOK, status)
}
func (c SchedulerController) UpdateSchedule(context *gin.Context) {
var status models.Schedule
err := context.BindJSON(&status)
if err != nil {
context.JSON(http.StatusInternalServerError, err.Error())
return
}
err = c.service.Update(status.ID, &status)
if err != nil {
context.JSON(http.StatusInternalServerError, err.Error())
return
}
context.JSON(http.StatusOK, status)
}