-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathserver.go
More file actions
143 lines (130 loc) · 3.76 KB
/
server.go
File metadata and controls
143 lines (130 loc) · 3.76 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
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
package main
import (
"encoding/json"
"errors"
"log"
"net/http"
)
type apiFunc func(w http.ResponseWriter, r *http.Request) error
type Server struct {
listenAddr string
store Storage
}
type ApiError struct {
Error string `json:"error"`
}
func NewApiServer(listenAddr string, store Storage) *Server {
return &Server{
listenAddr: listenAddr,
store: store,
}
}
func (s *Server) Start() {
router := http.NewServeMux()
router.Handle("/application", makeHttpHandleFunc(s.handleGetApplicationById))
router.Handle("/getallapplications", makeHttpHandleFunc(s.handleFetchAllApplication))
router.Handle("/deleteapplication", makeHttpHandleFunc(s.handleDeleteApplicationById))
router.Handle("/updateapplication", makeHttpHandleFunc(s.handleUpdateApplication))
router.Handle("/makeapplication", makeHttpHandleFunc(s.handlePostApplication))
err := http.ListenAndServe(s.listenAddr, router)
if err != nil {
log.Fatal("Unable to start the server due to error : ", err)
}
}
func (s *Server) handlePostApplication(w http.ResponseWriter, r *http.Request) error {
if r.Method != "POST" {
return errors.New("HTTP METHOD POST is only")
}
requestBody:= Application{}
err := json.NewDecoder(r.Body).Decode(&requestBody)
if err != nil {
return err
}
err = s.store.InsertApplication(r.Context(), &requestBody)
if err != nil {
return err
}
WriteJson(w, http.StatusCreated, map[string]string{"message": "application created"})
return nil
}
func (s *Server) handleFetchAllApplication(w http.ResponseWriter, r *http.Request) error {
if r.Method != "POST" {
return errors.New("HTTP METHOD POST is only allowed")
}
var filters Data
err := json.NewDecoder(r.Body).Decode(&filters)
if err != nil {
filters =Data{}
}
applications, err := s.store.FetchAll(r.Context(), filters)
if err != nil {
return err
}
WriteJson(w, http.StatusOK, applications)
return nil
}
func (s *Server) handleDeleteApplicationById(w http.ResponseWriter, r *http.Request) error {
if r.Method != "POST" {
return errors.New("HTTP METHOD should be POST is only allowed")
}
requestId :=RequestId{}
err := json.NewDecoder(r.Body).Decode(&requestId)
if err != nil {
return err
}
err = s.store.Delete(r.Context(), requestId.Id)
if err != nil {
return err
}
WriteJson(w, http.StatusOK, map[string]string{"message": "Application successfully deleted"})
return nil
}
func (s *Server) handleGetApplicationById(w http.ResponseWriter, r *http.Request) error {
if r.Method != "POST" {
return errors.New("HTTP METHOD should be POST is only")
}
requestId := RequestId{}
err := json.NewDecoder(r.Body).Decode(&requestId)
if err != nil {
return err
}
application, err := s.store.GetApplicationById(r.Context(), requestId.Id)
if err != nil {
return err
}
WriteJson(w, http.StatusOK, application)
return nil
}
func (s *Server) handleUpdateApplication(w http.ResponseWriter, r *http.Request) error {
if r.Method != "POST" {
return errors.New("HTTP METHOD should be POST is only")
}
id := r.URL.Query().Get("id")
if id == "" {
return errors.New("id of the document to be updated is not provided")
}
var filters Data
err := json.NewDecoder(r.Body).Decode(&filters)
if err != nil {
return err
}
err = s.store.UpdateApplication(r.Context(), filters, id)
if err != nil {
return err
}
WriteJson(w, http.StatusOK, map[string]string{"message": "application data updated"})
return nil
}
func makeHttpHandleFunc(function apiFunc) http.HandlerFunc {
return func(w http.ResponseWriter, r *http.Request) {
err := function(w, r)
if err != nil {
WriteJson(w, http.StatusBadRequest, ApiError{Error: err.Error()})
}
}
}
func WriteJson(w http.ResponseWriter, status int, v any) error {
w.Header().Add("Content-Type", "application/json")
w.WriteHeader(status)
return json.NewEncoder(w).Encode(v)
}