-
Notifications
You must be signed in to change notification settings - Fork 0
/
contact.go
97 lines (90 loc) · 2.61 KB
/
contact.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
package main
import (
"encoding/json"
"net/http"
"github.com/go-chi/chi"
"github.com/go-chi/render"
"github.com/serbe/edc"
)
func listContacts(w http.ResponseWriter, r *http.Request) {
type context struct {
Title string `json:"title"`
Contacts []edc.ContactList `json:"contacts"`
}
contacts, err := db.GetContactList()
if err != nil {
errmsg("listContacts GetContactList", err)
return
}
ctx := context{Title: "List", Contacts: contacts}
render.DefaultResponder(w, r, ctx)
}
func getContact(w http.ResponseWriter, r *http.Request) {
type context struct {
Title string `json:"title"`
Contact edc.Contact `json:"contact"`
Companies []edc.SelectItem `json:"companies"`
Departments []edc.SelectItem `json:"departments"`
Posts []edc.SelectItem `json:"posts"`
PostsGO []edc.SelectItem `json:"posts_go"`
Ranks []edc.SelectItem `json:"ranks"`
}
id := toInt(chi.URLParam(r, "id"))
contact, err := db.GetContact(id)
if err != nil {
errmsg("getContact GetContact", err)
return
}
companies, err := db.GetCompanySelectAll()
if err != nil {
errmsg("getContact GetCompanySelectAll", err)
return
}
departments, err := db.GetDepartmentSelectAll()
if err != nil {
errmsg("getContact GetDepartmentSelectAll", err)
return
}
posts, err := db.GetPostSelectAll(false)
if err != nil {
errmsg("getContact GetPostSelectAll(false)", err)
return
}
postsgo, err := db.GetPostSelectAll(true)
if err != nil {
errmsg("getContact GetPostSelectAll(true)", err)
return
}
ranks, err := db.GetRankSelectAll()
if err != nil {
errmsg("getContact GetRankSelectAll", err)
return
}
ctx := context{
Title: "Create contact",
Contact: contact,
Companies: companies,
Departments: departments,
Posts: posts,
PostsGO: postsgo,
Ranks: ranks,
}
render.DefaultResponder(w, r, ctx)
}
func createContact(_ http.ResponseWriter, r *http.Request) {
var contact edc.Contact
errchkmsg("createContact Decode", json.NewDecoder(r.Body).Decode(&contact))
_, err := db.CreateContact(contact)
errchkmsg("createContact CreateContact", err)
errchkmsg("createContact CloseBody", r.Body.Close())
}
func updateContact(_ http.ResponseWriter, r *http.Request) {
var contact edc.Contact
errchkmsg("updateContact Decode", json.NewDecoder(r.Body).Decode(&contact))
errchkmsg("updateContact UpdateContact", db.UpdateContact(contact))
errchkmsg("updateContact CloseBody", r.Body.Close())
}
func deleteContact(_ http.ResponseWriter, r *http.Request) {
id := toInt(chi.URLParam(r, "id"))
errchkmsg("deleteContact DeleteContact", db.DeleteContact(id))
}