-
Notifications
You must be signed in to change notification settings - Fork 0
/
lesson.go
141 lines (109 loc) · 3.22 KB
/
lesson.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
package main
import (
"net/http"
"strconv"
"github.com/labstack/echo"
)
// Lesson contains info about the lesson of particular course
type Lesson struct {
ID int `json:"id,omitempty"`
Title string `json:"title,omitempty"`
Description string `json:"description,omitempty"`
ModuleID int `json:"module_id,omitempty"`
Draft bool `json:"draft,omitempty"`
Contents []Content `json:"contents,omitempty"`
}
func createLesson(c echo.Context) error {
l := &Lesson{}
if err := c.Bind(l); err != nil {
return err
}
insertLessonSQL := "INSERT INTO lessons(title, description, module_id, draft) VALUES(?,?,?,?)"
stmt, err := db.Prepare(insertLessonSQL)
if err != nil {
return err
}
_, err = stmt.Exec(l.Title, l.Description, l.ModuleID, l.Draft)
return c.JSON(http.StatusCreated, l)
}
func getLessons(c echo.Context) error {
var ls []Lesson = make([]Lesson, 0)
lid := c.QueryParam("module_id")
if len(lid) != 0 {
moduleID, err := strconv.Atoi(lid)
if err != nil {
return c.JSON(http.StatusBadRequest, map[string]string{"error": "malformatted module_id"})
}
rows, err := db.Query("SELECT id, title, description, module_id, draft FROM lessons WHERE module_id=?", moduleID)
if err != nil {
return err
}
defer rows.Close()
for rows.Next() {
l := Lesson{}
rows.Scan(&l.ID, &l.Title, &l.Description, &l.ModuleID, &l.Draft)
l.Contents = getContentsForLesson(l.ID)
ls = append(ls, l)
}
return c.JSON(http.StatusOK, ls)
}
rows, err := db.Query("SELECT id, title, description, module_id, draft FROM lessons")
if err != nil {
return err
}
defer rows.Close()
for rows.Next() {
l := Lesson{}
rows.Scan(&l.ID, &l.Title, &l.Description, &l.ModuleID, &l.Draft)
l.Contents = getContentsForLesson(l.ID)
ls = append(ls, l)
}
return c.JSON(http.StatusOK, ls)
}
func getLessonsForModule(moduleID int) []Lesson {
var ls []Lesson = make([]Lesson, 0)
rows, err := db.Query("SELECT id, title, description, draft FROM lessons WHERE module_id=?", moduleID)
if err != nil {
return []Lesson{}
}
defer rows.Close()
for rows.Next() {
l := Lesson{}
rows.Scan(&l.ID, &l.Title, &l.Description, &l.Draft)
l.Contents = getContentsForLesson(l.ID)
ls = append(ls, l)
}
return ls
}
func getLessonByID(c echo.Context) error {
id, _ := strconv.Atoi(c.Param("id"))
row := db.QueryRow("SELECT id, title, description, module_id, draft FROM lessons WHERE id=?", id)
l := Lesson{}
row.Scan(&l.ID, &l.Title, &l.Description, &l.ModuleID, &l.Draft)
l.Contents = getContentsForLesson(l.ID)
return c.JSON(http.StatusOK, l)
}
func updateLesson(c echo.Context) error {
id, _ := strconv.Atoi(c.Param("id"))
l := &Lesson{}
if err := c.Bind(l); err != nil {
return err
}
stmt, err := db.Prepare("UPDATE lessons SET title=?, description=?, module_id=?, draft=? WHERE id=?")
if err != nil {
return err
}
defer stmt.Close()
_, err = stmt.Exec(l.Title, l.Description, l.ModuleID, l.Draft, id)
return c.JSON(http.StatusOK, l)
}
func deleteLesson(c echo.Context) error {
id, _ := strconv.Atoi(c.Param("id"))
stmt, err := db.Prepare("DELETE FROM lessons where id=?")
if err != nil {
return err
}
defer stmt.Close()
stmt.Exec(id)
return c.NoContent(http.StatusOK)
}