-
Notifications
You must be signed in to change notification settings - Fork 21
Expand file tree
/
Copy pathownerinfo.go
More file actions
108 lines (94 loc) · 3.05 KB
/
Copy pathownerinfo.go
File metadata and controls
108 lines (94 loc) · 3.05 KB
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
108
// SPDX-FileCopyrightText: (C) 2024 Intel Corporation
// SPDX-License-Identifier: Apache 2.0
package handlers
import (
"errors"
"io"
"log/slog"
"net/http"
"github.com/fido-device-onboard/go-fdo-server/internal/db"
"gorm.io/gorm"
)
func OwnerInfoHandler(w http.ResponseWriter, r *http.Request) {
slog.Debug("Received OwnerInfo request", "method", r.Method, "path", r.URL.Path)
switch r.Method {
case http.MethodGet:
getOwnerInfo(w, r)
case http.MethodPost:
createOwnerInfo(w, r)
case http.MethodPut:
updateOwnerInfo(w, r)
default:
slog.Error("Method not allowed", "method", r.Method, "path", r.URL.Path)
http.Error(w, "Method not allowed", http.StatusMethodNotAllowed)
}
}
func getOwnerInfo(w http.ResponseWriter, _ *http.Request) {
slog.Debug("Fetching ownerInfo")
ownerInfoJSON, err := db.FetchOwnerInfoJSON()
if err != nil {
if errors.Is(err, gorm.ErrRecordNotFound) {
slog.Error("No ownerInfo found")
http.Error(w, "No ownerInfo found", http.StatusNotFound)
} else {
slog.Error("Error fetching ownerInfo", "error", err)
http.Error(w, "Error fetching ownerInfo", http.StatusInternalServerError)
}
return
}
w.Header().Set("Content-Type", "application/json")
w.Write(ownerInfoJSON)
}
func createOwnerInfo(w http.ResponseWriter, r *http.Request) {
ownerInfo, err := io.ReadAll(r.Body)
if err != nil {
slog.Error("Error reading body", "error", err)
http.Error(w, "Error reading body", http.StatusInternalServerError)
return
}
if err := db.InsertOwnerInfo(ownerInfo); err != nil {
if errors.Is(err, gorm.ErrDuplicatedKey) {
slog.Error("ownerInfo already exists (constraint)", "error", err)
http.Error(w, "ownerInfo already exists", http.StatusConflict)
return
}
if errors.Is(err, db.ErrInvalidOwnerInfo) {
slog.Error("Invalid ownerInfo payload", "error", err)
http.Error(w, "Invalid ownerInfo", http.StatusBadRequest)
return
}
slog.Error("Error inserting ownerInfo", "error", err)
http.Error(w, "Error inserting ownerInfo", http.StatusInternalServerError)
return
}
slog.Debug("ownerInfo created")
w.Header().Set("Content-Type", "application/json")
w.WriteHeader(http.StatusCreated)
w.Write(ownerInfo)
}
func updateOwnerInfo(w http.ResponseWriter, r *http.Request) {
ownerInfo, err := io.ReadAll(r.Body)
if err != nil {
slog.Error("Error reading body", "error", err)
http.Error(w, "Error reading body", http.StatusInternalServerError)
return
}
if err := db.UpdateOwnerInfo(ownerInfo); err != nil {
if errors.Is(err, gorm.ErrRecordNotFound) {
slog.Error("ownerInfo does not exist, cannot update")
http.Error(w, "ownerInfo does not exist", http.StatusNotFound)
return
}
if errors.Is(err, db.ErrInvalidOwnerInfo) {
slog.Error("Invalid ownerInfo payload", "error", err)
http.Error(w, "Invalid ownerInfo", http.StatusBadRequest)
return
}
slog.Error("Error updating ownerInfo", "error", err)
http.Error(w, "Error updating ownerInfo", http.StatusInternalServerError)
return
}
slog.Debug("ownerInfo updated")
w.Header().Set("Content-Type", "application/json")
w.Write(ownerInfo)
}