/
module.go
124 lines (94 loc) · 2.79 KB
/
module.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
package main
import (
"net/http"
"strconv"
"github.com/labstack/echo"
)
// Module is a slice of lessons with some meta data
type Module struct {
ID int `json:"id,omitempty"`
Title string `json:"title,omitempty"`
Description string `json:"description,omitempty"`
CourseID int `json:"course_id,omitempty"`
Draft bool `json:"draft,omitempty"`
Lessons []Lesson `json:"lessons,omitempty"`
}
func createModule(c echo.Context) error {
m := &Module{}
if err := c.Bind(m); err != nil {
return err
}
insertCourseSQL := "INSERT INTO modules(title, description, course_id, draft) VALUES(?,?,?,?)"
stmt, err := db.Prepare(insertCourseSQL)
if err != nil {
return err
}
_, err = stmt.Exec(m.Title, m.Description, m.CourseID, m.Draft)
return c.JSON(http.StatusCreated, m)
}
func getModules(c echo.Context) error {
var mod []Module = make([]Module, 0)
mid := c.QueryParam("course_id")
if len(mid) != 0 {
courseID, err := strconv.Atoi(mid)
if err != nil {
return c.JSON(http.StatusBadRequest, map[string]string{"error": "malformatted course_id"})
}
rows, err := db.Query("SELECT id, title, description, course_id, draft FROM modules WHERE course_id=?", courseID)
if err != nil {
return err
}
defer rows.Close()
for rows.Next() {
m := Module{}
rows.Scan(&m.ID, &m.Title, &m.Description, &m.CourseID, &m.Draft)
m.Lessons = getLessonsForModule(m.ID)
mod = append(mod, m)
}
return c.JSON(http.StatusOK, mod)
}
row, err := db.Query("SELECT id, title, description, course_id, draft FROM modules")
if err != nil {
return err
}
defer row.Close()
for row.Next() {
m := Module{}
row.Scan(&m.ID, &m.Title, &m.Description, &m.CourseID, &m.Draft)
m.Lessons = getLessonsForModule(m.ID)
mod = append(mod, m)
}
return c.JSON(http.StatusOK, mod)
}
func getModuleByID(c echo.Context) error {
id, _ := strconv.Atoi(c.Param("id"))
row := db.QueryRow("SELECT id, title, description, course_id, draft FROM modules WHERE id=?", id)
m := Module{}
row.Scan(&m.ID, &m.Title, &m.Description, &m.CourseID, &m.Draft)
m.Lessons = getLessonsForModule(m.ID)
return c.JSON(http.StatusOK, m)
}
func updateModule(c echo.Context) error {
id, _ := strconv.Atoi(c.Param("id"))
m := &Module{}
if err := c.Bind(&m); err != nil {
return err
}
stmt, err := db.Prepare("UPDATE modules SET title=?, description=?, course_id=?, draft=? WHERE id=?")
if err != nil {
return err
}
defer stmt.Close()
_, err = stmt.Exec(m.Title, m.Description, m.CourseID, m.Draft, id)
return c.JSON(http.StatusOK, m)
}
func deleteModule(c echo.Context) error {
id, _ := strconv.Atoi(c.Param("id"))
stmt, err := db.Prepare("DELETE FROM modules where id=?")
if err != nil {
return err
}
defer stmt.Close()
stmt.Exec(id)
return c.NoContent(http.StatusOK)
}