summaryrefslogtreecommitdiffstats
path: root/api/handlers/record.go
diff options
context:
space:
mode:
Diffstat (limited to 'api/handlers/record.go')
-rw-r--r--api/handlers/record.go35
1 files changed, 35 insertions, 0 deletions
diff --git a/api/handlers/record.go b/api/handlers/record.go
new file mode 100644
index 0000000..0f98f11
--- /dev/null
+++ b/api/handlers/record.go
@@ -0,0 +1,35 @@
+package handlers
+
+import (
+ "crypto/subtle"
+ "net/http"
+
+ "git.leonardobishop.net/history/api/dto"
+ "git.leonardobishop.net/history/pkg/entries"
+)
+
+func RecordEntry(service entries.Service, token string) http.HandlerFunc {
+ return dto.WrapResponseFunc(func(w http.ResponseWriter, r *http.Request) error {
+ var request dto.CreateEntryRequest
+ if err := dto.ReadDto(r, &request); err != nil {
+ return err
+ }
+
+ if subtle.ConstantTimeCompare([]byte(token), []byte(request.Token)) != 1 {
+ return &dto.ErrorResponse{
+ Code: http.StatusForbidden,
+ Message: "Forbidden",
+ }
+ }
+
+ entry, err := service.CreateEntry(request.Title, request.Kind, request.Url, request.Description)
+ if err != nil {
+ return err
+ }
+
+ return &dto.OkResponse{
+ Code: http.StatusCreated,
+ Data: entry,
+ }
+ })
+}