forked from shijuvar/go-web
-
Notifications
You must be signed in to change notification settings - Fork 0
/
noteController.go
162 lines (152 loc) · 4.44 KB
/
noteController.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
package controllers
import (
"encoding/json"
"net/http"
"github.com/gorilla/mux"
"gopkg.in/mgo.v2"
"gopkg.in/mgo.v2/bson"
"github.com/shijuvar/go-web/taskmanager/common"
"github.com/shijuvar/go-web/taskmanager/data"
"github.com/shijuvar/go-web/taskmanager/models"
)
// CreateNote inserts a new Note document for a TaskId
// Handler for HTTP Post - "/notes"
func CreateNote(w http.ResponseWriter, r *http.Request) {
var dataResource NoteResource
// Decode the incoming Note json
err := json.NewDecoder(r.Body).Decode(&dataResource)
if err != nil {
common.DisplayAppError(w, err, "Invalid Note data", 500)
return
}
noteModel := dataResource.Data
note := &models.TaskNote{
TaskId: bson.ObjectIdHex(noteModel.TaskId),
Description: noteModel.Description,
}
context := NewContext()
defer context.Close()
col := context.DbCollection("notes")
//Insert a note document
repo := &data.NoteRepository{C: col}
repo.Create(note)
j, err := json.Marshal(note)
if err != nil {
common.DisplayAppError(w, err, "An unexpected error has occurred", 500)
return
}
w.Header().Set("Content-Type", "application/json")
w.WriteHeader(http.StatusCreated)
w.Write(j)
}
// GetNotesByTask returns all Notes documents under a TaskId
// Handler for HTTP Get - "/notes/tasks/{id}
func GetNotesByTask(w http.ResponseWriter, r *http.Request) {
vars := mux.Vars(r)
id := vars["id"]
context := NewContext()
defer context.Close()
col := context.DbCollection("notes")
repo := &data.NoteRepository{C: col}
notes := repo.GetByTask(id)
j, err := json.Marshal(NotesResource{Data: notes})
if err != nil {
common.DisplayAppError(w, err, "An unexpected error has occurred", 500)
return
}
w.Header().Set("Content-Type", "application/json")
w.WriteHeader(http.StatusOK)
w.Write(j)
}
// GetNotes returns all Note documents
// Handler for HTTP Get - "/notes"
func GetNotes(w http.ResponseWriter, r *http.Request) {
context := NewContext()
defer context.Close()
col := context.DbCollection("notes")
repo := &data.NoteRepository{C: col}
notes := repo.GetAll()
j, err := json.Marshal(NotesResource{Data: notes})
if err != nil {
common.DisplayAppError(w, err, "An unexpected error has occurred", 500)
return
}
w.Header().Set("Content-Type", "application/json")
w.WriteHeader(http.StatusOK)
w.Write(j)
}
// GetNoteByID returns a single Note document by id
// Handler for HTTP Get - "/notes/{id}"
func GetNoteByID(w http.ResponseWriter, r *http.Request) {
// Get id from the incoming url
vars := mux.Vars(r)
id := vars["id"]
context := NewContext()
defer context.Close()
col := context.DbCollection("notes")
repo := &data.NoteRepository{C: col}
note, err := repo.GetById(id)
if err != nil {
if err == mgo.ErrNotFound {
w.WriteHeader(http.StatusNoContent)
return
}
common.DisplayAppError(w, err, "An unexpected error has occurred", 500)
return
}
j, err := json.Marshal(note)
if err != nil {
common.DisplayAppError(w, err, "An unexpected error has occurred", 500)
return
}
w.Header().Set("Content-Type", "application/json")
w.WriteHeader(http.StatusOK)
w.Write(j)
}
// UpdateNote updates an existing Note document
// Handler for HTTP Put - "/notes/{id}"
func UpdateNote(w http.ResponseWriter, r *http.Request) {
// Get id from the incoming url
vars := mux.Vars(r)
id := bson.ObjectIdHex(vars["id"])
var dataResource NoteResource
// Decode the incoming Note json
err := json.NewDecoder(r.Body).Decode(&dataResource)
if err != nil {
common.DisplayAppError(w, err, "Invalid Note data", 500)
return
}
noteModel := dataResource.Data
note := &models.TaskNote{
Id: id,
Description: noteModel.Description,
}
context := NewContext()
defer context.Close()
col := context.DbCollection("notes")
repo := &data.NoteRepository{C: col}
//Update note document
if err := repo.Update(note); err != nil {
common.DisplayAppError(w, err, "An unexpected error has occurred", 500)
return
}
w.WriteHeader(http.StatusNoContent)
}
// DeleteNote deletes an existing Note document
// Handler for HTTP Delete - "/notes/{id}"
func DeleteNote(w http.ResponseWriter, r *http.Request) {
// Get id from the incoming url
vars := mux.Vars(r)
id := vars["id"]
context := NewContext()
defer context.Close()
col := context.DbCollection("notes")
repo := &data.NoteRepository{C: col}
//Delete a note document
err := repo.Delete(id)
if err != nil {
common.DisplayAppError(w, err, "An unexpected error has occurred", 500)
return
}
w.WriteHeader(http.StatusNoContent)
}