-
Notifications
You must be signed in to change notification settings - Fork 0
/
delete_contact.go
57 lines (46 loc) · 1.35 KB
/
delete_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
package server
import (
"fmt"
"github.com/gorilla/mux"
"github.com/ministryofjustice/opg-sirius-supervision-deputy-hub/internal/sirius"
"net/http"
"strconv"
)
type DeleteContact interface {
GetContactById(ctx sirius.Context, deputyId int, contactId int) (sirius.Contact, error)
DeleteContact(sirius.Context, int, int) error
}
// Could just use ErrorVars?
type DeleteContactVars struct {
ContactName string
AppVars
}
func renderTemplateForDeleteContact(client DeleteContact, tmpl Template) Handler {
return func(appVars AppVars, w http.ResponseWriter, r *http.Request) error {
ctx := getContext(r)
routeVars := mux.Vars(r)
deputyId, _ := strconv.Atoi(routeVars["id"])
contactId, _ := strconv.Atoi(routeVars["contactId"])
appVars.PageName = "Delete contact"
vars := DeleteContactVars{
AppVars: appVars,
}
contact, err := client.GetContactById(ctx, deputyId, contactId)
if err != nil {
return err
}
switch r.Method {
case http.MethodGet:
vars.ContactName = contact.ContactName
return tmpl.ExecuteTemplate(w, "page", vars)
case http.MethodPost:
err := client.DeleteContact(ctx, deputyId, contactId)
if err != nil {
return err
}
return Redirect(fmt.Sprintf("/%d/contacts?success=deletedContact&contactName=%s", deputyId, contact.ContactName))
default:
return StatusError(http.StatusMethodNotAllowed)
}
}
}