Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
93 changes: 84 additions & 9 deletions handlers.go
Original file line number Diff line number Diff line change
Expand Up @@ -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)

Expand Down Expand Up @@ -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)

Expand Down Expand Up @@ -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(&quotationRequest); 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
Expand Down
12 changes: 12 additions & 0 deletions models.go
Original file line number Diff line number Diff line change
@@ -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"`
Expand All @@ -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"`
Expand Down
2 changes: 2 additions & 0 deletions routes.go
Original file line number Diff line number Diff line change
Expand Up @@ -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")
}