-
Notifications
You must be signed in to change notification settings - Fork 0
/
certificate.go
85 lines (78 loc) · 2.24 KB
/
certificate.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
package main
import (
"encoding/json"
"net/http"
"github.com/go-chi/chi"
"github.com/go-chi/render"
"github.com/serbe/edc"
)
func listCertificates(w http.ResponseWriter, r *http.Request) {
type context struct {
Title string `json:"title"`
Certificates []edc.CertificateList `json:"certificates"`
}
certificates, err := db.GetCertificateListAll()
if err != nil {
errmsg("listCertificates GetCertificateListAll", err)
return
}
ctx := context{Title: "List", Certificates: certificates}
render.DefaultResponder(w, r, ctx)
}
func getCertificate(w http.ResponseWriter, r *http.Request) {
type context struct {
Title string `json:"title"`
Certificate edc.Certificate `json:"certificate"`
Contacts []edc.SelectItem `json:"contacts"`
Companies []edc.SelectItem `json:"companies"`
}
id := toInt(chi.URLParam(r, "id"))
certificate, err := db.GetCertificate(id)
if err != nil {
errmsg("getCertificate GetCertificate", err)
}
contacts, err := db.GetContactSelectAll()
if err != nil {
errmsg("getCertificate GetContactSelectAll", err)
}
companies, err := db.GetCompanySelectAll()
if err != nil {
errmsg("getCertificate GetCompaniesSelectAll", err)
}
ctx := context{
Title: "Create certificate",
Certificate: certificate,
Contacts: contacts,
Companies: companies,
}
render.DefaultResponder(w, r, ctx)
}
func createCertificate(_ http.ResponseWriter, r *http.Request) {
var certificate edc.Certificate
errchkmsg(
"createCertificate Decode",
json.NewDecoder(r.Body).Decode(&certificate),
)
_, err := db.CreateCertificate(certificate)
errchkmsg("createCertificate CreateCertificate", err)
errchkmsg("createCertificate CloseBody", r.Body.Close())
}
func updateCertificate(_ http.ResponseWriter, r *http.Request) {
var certificate edc.Certificate
errchkmsg(
"updateCertificate Decode",
json.NewDecoder(r.Body).Decode(&certificate),
)
errchkmsg(
"updateCertificate UpdateCertificate",
db.UpdateCertificate(certificate),
)
errchkmsg("updateCertificate CloseBody", r.Body.Close())
}
func deleteCertificate(_ http.ResponseWriter, r *http.Request) {
id := toInt(chi.URLParam(r, "id"))
errchkmsg(
"deleteCertificate DeleteCertificate",
db.DeleteCertificate(id),
)
}