-
Notifications
You must be signed in to change notification settings - Fork 0
/
create.go
43 lines (35 loc) · 925 Bytes
/
create.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
package folders
import (
"database/sql"
"encoding/json"
"net/http"
)
func (h *handler) Create(rw http.ResponseWriter, r *http.Request) {
f := new(Folder)
err := json.NewDecoder(r.Body).Decode(f)
if err != nil {
http.Error(rw, err.Error(), http.StatusInternalServerError)
return
}
err = f.Validate()
if err != nil {
http.Error(rw, err.Error(), http.StatusBadRequest)
return
}
id, err := Insert(h.db, f)
if err != nil {
http.Error(rw, err.Error(), http.StatusInternalServerError)
return
}
f.ID = id
rw.Header().Add("Content-Type", "application/json")
json.NewEncoder(rw).Encode(f)
}
func Insert(db *sql.DB, f *Folder) (int64, error) {
stmt := `INSERT INTO folders (parent_id, name, created_at, modified_at) VALUES ($1, $2, $3, $4) RETURNING id`
result, err := db.Exec(stmt, f.ParentID, f.Name, f.CreatedAt, f.ModifiedAt)
if err != nil {
return 0, err
}
return result.LastInsertId()
}