-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathcourses.go
More file actions
106 lines (91 loc) · 2.87 KB
/
Copy pathcourses.go
File metadata and controls
106 lines (91 loc) · 2.87 KB
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
package main
import (
"encoding/json"
"net/http"
"github.com/gorilla/mux"
)
type Course struct {
ID string `json:"id"`
Title string `json:"title"`
Lessons int `json:"lessons"`
}
func courseGet(w http.ResponseWriter, r *http.Request) {
rows, err := db.Query("select * from courses;")
if err != nil {
http.Error(w, "Failed to query courses from database", http.StatusInternalServerError)
return
}
defer rows.Close()
var course Course
var courseSlice = []Course{}
for rows.Next() {
err := rows.Scan(&course.ID, &course.Title, &course.Lessons)
if err != nil {
http.Error(w, "Failed to scan course row", http.StatusInternalServerError)
return
}
courseSlice = append(courseSlice, course)
}
w.Header().Set("Content-Type", "application/json")
json.NewEncoder(w).Encode(courseSlice)
}
func coursePost(w http.ResponseWriter, r *http.Request) {
var course = Course{}
err := json.NewDecoder(r.Body).Decode(&course)
if err != nil {
http.Error(w, "Failed to decode request body", http.StatusBadRequest)
return
}
if course.ID == "" {
http.Error(w, "ID cannot be empty", http.StatusBadRequest)
return
}
_, err = db.Exec("insert into courses (id, title, lessons) values ($1, $2, $3)", course.ID, course.Title, course.Lessons)
if err != nil {
http.Error(w, "POST method failed to insert course", http.StatusInternalServerError)
return
}
w.Header().Set("Content-Type", "application/json")
json.NewEncoder(w).Encode(course)
}
func specificCourseGet(w http.ResponseWriter, r *http.Request) {
vars := mux.Vars(r)
id := vars["ID"]
var course = Course{}
err := db.QueryRow("select * from courses where id = $1", id).Scan(&course.ID, &course.Title, &course.Lessons)
if err != nil {
http.Error(w, "Failed to query course from database", http.StatusInternalServerError)
return
}
w.Header().Set("Content-Type", "application/json")
json.NewEncoder(w).Encode(course)
}
func specificCoursePut(w http.ResponseWriter, r *http.Request) {
vars := mux.Vars(r)
id := vars["ID"]
var course = Course{}
err := json.NewDecoder(r.Body).Decode(&course)
if err != nil {
http.Error(w, "Failed to decode request body", http.StatusBadRequest)
return
}
_, err = db.Exec("update courses set lessons = $1, title = $2 where id = $3", course.Lessons, course.Title, id)
if err != nil {
http.Error(w, "PUT method failed to update course", http.StatusInternalServerError)
return
}
course.ID = id
w.Header().Set("Content-Type", "application/json")
json.NewEncoder(w).Encode(course)
}
func specificCourseDelete(w http.ResponseWriter, r *http.Request) {
vars := mux.Vars(r)
id := vars["ID"]
_, err := db.Exec("delete from courses where id = $1", id)
if err != nil {
http.Error(w, "DELETE method failed to delete course", http.StatusInternalServerError)
return
}
w.Header().Set("Content-Type", "application/json")
json.NewEncoder(w).Encode(map[string]string{"deleted": id})
}