diff --git a/handlers.go b/handlers.go index ba71c77..dcb6ddd 100644 --- a/handlers.go +++ b/handlers.go @@ -43,11 +43,6 @@ func (s *server) handleHelloWord() http.HandlerFunc { } func (s *server) handleUserSignUp() func(w http.ResponseWriter, r *http.Request) { - - type response struct { - Message string `json:"message"` - } - return func(w http.ResponseWriter, r *http.Request) { decoder := json.NewDecoder(r.Body) @@ -84,10 +79,6 @@ func (s *server) handleUserSignIn() func(w http.ResponseWriter, r *http.Request) Message string `json:"token"` } - type response struct { - Message string `json:"message"` - } - return func(w http.ResponseWriter, r *http.Request) { decoder := json.NewDecoder(r.Body) @@ -124,6 +115,90 @@ func (s *server) handleUserSignIn() func(w http.ResponseWriter, r *http.Request) } } +func (s *server) handleQuotationGetAll() func(w http.ResponseWriter, r *http.Request) { + + type responseQuotation struct { + Quotations []quotation `json:"quotattions"` + } + + return func(w http.ResponseWriter, r *http.Request) { + var quotations []quotation + + sqlQuery := `SELECT quotation_id, + event_name, + item_description, + item_quantity, + student_name, + status + FROM Quotations` + + rows, err := s.db.Query(sqlQuery) + + if err != nil { + respondErr(w, r, err, http.StatusInternalServerError) + return + } + + for rows.Next() { + var qtn quotation + + err = rows.Scan( + &qtn.Quotation_id, + &qtn.Event_name, + &qtn.Item_description, + &qtn.Item_quantity, + &qtn.Student_name, + &qtn.Status, + ) + + if err != nil { + respondErr(w, r, err, http.StatusInternalServerError) + return + } + + quotations = append(quotations, qtn) + } + + responseQuotation := responseQuotation{Quotations: quotations} + + respond(w, r, responseQuotation, http.StatusOK) + + } +} + +// Handler function to change status to approved/rejected +// Resubmit quotation could also be done by changing the status (?) +func (s *server) handleQuotationUpdateStatus() func(w http.ResponseWriter, r *http.Request) { + + type request struct { + Quotation_id int `json:"quotation_id"` + New_status int `json:"new_status"` + } + + return func(w http.ResponseWriter, r *http.Request) { + decoder := json.NewDecoder(r.Body) + + var quotationRequest request + + if err := decoder.Decode("ationRequest); err != nil { + respondErr(w, r, err, http.StatusInternalServerError) + } + + sqlQuery := `UPDATE Quotations + SET status = $1 + WHERE quotation_id = $2` + + if _, err := s.db.Exec(sqlQuery, quotationRequest.New_status, quotationRequest.Quotation_id); err != nil { + respondErr(w, r, err, http.StatusInternalServerError) + return + } + + response := response{"Status has been changed"} + respond(w, r, response, http.StatusOK) + } + +} + func generateToken(u user) (string, error) { atClaim := jwt.MapClaims{} atClaim["authorized"] = true diff --git a/models.go b/models.go index 5577cfc..eff9f0f 100644 --- a/models.go +++ b/models.go @@ -1,6 +1,9 @@ package main // Define your models here +type response struct { + Message string `json:"message"` +} type user struct { User_id int `json:"user_id"` Name string `json:"name"` @@ -10,6 +13,15 @@ type user struct { Role int `json:"role"` } +type quotation struct { + Quotation_id int `json:"quotation_id"` + Event_name string `json:"event_name"` + Item_description string `json:"item_description"` + Item_quantity int `json:"item_quantity"` + Student_name string `json:"student_name"` + Status int `json:"status"` +} + // type role struct { // Role_id int `json:"role_id"` // Role_name string `json:"role_name"` diff --git a/routes.go b/routes.go index 0f80369..a494f55 100644 --- a/routes.go +++ b/routes.go @@ -8,4 +8,6 @@ func (s *server) routes() { s.router.HandleFunc("/", s.handleHelloWord()).Methods("GET") s.router.HandleFunc("/user/signup", s.handleUserSignUp()).Methods("POST") s.router.HandleFunc("/user/signin", s.handleUserSignIn()).Methods("POST") + s.router.HandleFunc("/quotation", s.handleQuotationGetAll()).Methods("GET") + s.router.HandleFunc("/quotation/update-status", s.handleQuotationUpdateStatus()).Methods("POST") }