summaryrefslogtreecommitdiff
path: root/view/json.go
blob: deaece2cfdac42b9dc0f5d3e534cada14a04613d (plain)
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
package view

import (
	"encoding/json"
	"io"
	"net/http"
	"strconv"

	"xengineering.eu/ceres/model"
)

func CreateJSON[O model.Object](db *model.DB, constructor func() model.Object) http.Handler {
	return http.HandlerFunc(
		func(w http.ResponseWriter, r *http.Request) {
			buf, err := io.ReadAll(r.Body)
			if err != nil {
				http.Error(w, "Request body could not be read: "+err.Error(), http.StatusBadRequest)
				return
			}

			object := constructor()
			err = json.Unmarshal(buf, &object)
			if err != nil {
				http.Error(w, "JSON could not be parsed: "+err.Error(), http.StatusBadRequest)
				return
			}

			err = db.Transaction(object.Create)
			if err != nil {
				http.Error(w, "Object could not be saved to database: "+err.Error(), http.StatusBadRequest)
				return
			}
		},
	)
}

func ReadJSON[O model.Object](db *model.DB, constructor func() model.Object) http.Handler {
	return http.HandlerFunc(
		func(w http.ResponseWriter, r *http.Request) {
			object := constructor()

			id, err := strconv.Atoi(r.PathValue("id"))
			if err == nil {
				object.SetID(id)
			}

			err = db.Transaction(object.Read)
			if err != nil {
				http.Error(w, "Object could not be read from database: "+err.Error(), http.StatusBadRequest)
				return
			}

			w.Header().Set("Content-Type", "application/json")
			err = json.NewEncoder(w).Encode(object)
			if err != nil {
				http.Error(w, "Object could not be encoded to JSON: "+err.Error(), http.StatusBadRequest)
				return
			}
		},
	)
}

func UpdateJSON[O model.Object](db *model.DB, constructor func() model.Object) http.Handler {
	return http.HandlerFunc(
		func(w http.ResponseWriter, r *http.Request) {
			buf, err := io.ReadAll(r.Body)
			if err != nil {
				http.Error(w, "Request body could not be read: "+err.Error(), http.StatusBadRequest)
				return
			}

			object := constructor()
			err = json.Unmarshal(buf, &object)
			if err != nil {
				http.Error(w, "JSON could not be parsed: "+err.Error(), http.StatusBadRequest)
				return
			}

			err = db.Transaction(object.Update)
			if err != nil {
				http.Error(w, "Object could not be updated in database: "+err.Error(), http.StatusBadRequest)
				return
			}
		},
	)
}

func DeleteJSON[O model.Object](db *model.DB, constructor func() model.Object) http.Handler {
	return http.HandlerFunc(
		func(w http.ResponseWriter, r *http.Request) {
			object := constructor()

			id, err := strconv.Atoi(r.PathValue("id"))
			if err != nil {
				http.Error(w, "ID of object could not be parsed to an integer: "+err.Error(), http.StatusBadRequest)
				return
			}
			object.SetID(id)

			err = db.Transaction(object.Delete)
			if err != nil {
				http.Error(w, "Object could not be deleted from database: "+err.Error(), http.StatusBadRequest)
				return
			}
		},
	)
}