From 4cbcffe571de8efecb8cdc87ce25ab1b16343ac3 Mon Sep 17 00:00:00 2001 From: Caelan Hill Date: Sat, 22 Jun 2024 20:54:49 +0200 Subject: [PATCH 1/2] integrated db side archive get --- api/handlers/dispute.go | 98 +++++++++++++++++++++++++++-------------- 1 file changed, 65 insertions(+), 33 deletions(-) diff --git a/api/handlers/dispute.go b/api/handlers/dispute.go index 4c60face..42e2f5da 100644 --- a/api/handlers/dispute.go +++ b/api/handlers/dispute.go @@ -18,7 +18,7 @@ import ( func SetupDisputeRoutes(router *mux.Router, h Handler) { //dispute routes disputeRouter := router.PathPrefix("").Subrouter() - disputeRouter.Use(middleware.JWTMiddleware) + disputeRouter.Use(middleware.JWTMiddleware) disputeRouter.HandleFunc("", h.getSummaryListOfDisputes).Methods(http.MethodGet) disputeRouter.HandleFunc("/{id}", h.getDispute).Methods(http.MethodGet) disputeRouter.HandleFunc("/{id}", h.patchDispute).Methods(http.MethodPatch) @@ -58,8 +58,8 @@ func (h Handler) getDispute(w http.ResponseWriter, r *http.Request) { vars := mux.Vars(r) id := vars["id"] - // var - + // var + utilities.WriteJSON(w, http.StatusOK, models.Response{Data: "Dispute Detail Endpoint for ID: " + id}) } @@ -120,7 +120,7 @@ func (h Handler) getSummaryListOfArchives(w http.ResponseWriter, r *http.Request } if body.Sort != nil { sort = string(*body.Sort) - + } // Query the database @@ -155,13 +155,13 @@ func (h Handler) getSummaryListOfArchives(w http.ResponseWriter, r *http.Request var archiveDisputeSummaries []models.ArchivedDisputeSummary for _, dispute := range disputes { archiveDisputeSummaries = append(archiveDisputeSummaries, models.ArchivedDisputeSummary{ - ID: dispute.ID, - Title: dispute.Title, - Summary: dispute.Description, - Category: []string{"Dispute"}, // Assuming a default category for now - DateFiled: dispute.CaseDate, - DateResolved: dispute.CaseDate.Add(48 * time.Hour), // Placeholder for resolved date - Resolution: string(dispute.Decision), + ID: dispute.ID, + Title: dispute.Title, + Summary: dispute.Description, + Category: []string{"Dispute"}, // Assuming a default category for now + DateFiled: dispute.CaseDate, + DateResolved: dispute.CaseDate.Add(48 * time.Hour), // Placeholder for resolved date + Resolution: string(dispute.Decision), }) } @@ -282,29 +282,61 @@ func (h Handler) getArchive(w http.ResponseWriter, r *http.Request) { } //mock body - body := models.ArchivedDispute{ - ArchivedDisputeSummary: models.ArchivedDisputeSummary{ - ID: int64(intID), - Title: "Dispute " + id, - Summary: "Summary " + id, - Category: []string{"Category " + id}, - DateFiled: time.Date(2021, time.January, 1, 0, 0, 0, 0, time.UTC), - DateResolved: time.Date(2021, time.January, 2, 0, 0, 0, 0, time.UTC), - Resolution: "Resolution " + id, - }, - Events: []models.Event{ - { - Timestamp: "2021-01-01T00:00:00Z", - Type: "Type 1", - Description: "Details 1", - }, - { - Timestamp: "2021-01-02T00:00:00Z", - Type: "Type 2", - Description: "Details 2", + // body := models.ArchivedDispute{ + // ArchivedDisputeSummary: models.ArchivedDisputeSummary{ + // ID: int64(intID), + // Title: "Dispute " + id, + // Summary: "Summary " + id, + // Category: []string{"Category " + id}, + // DateFiled: time.Date(2021, time.January, 1, 0, 0, 0, 0, time.UTC), + // DateResolved: time.Date(2021, time.January, 2, 0, 0, 0, 0, time.UTC), + // Resolution: "Resolution " + id, + // }, + // Events: []models.Event{ + // { + // Timestamp: "2021-01-01T00:00:00Z", + // Type: "Type 1", + // Description: "Details 1", + // }, + // { + // Timestamp: "2021-01-02T00:00:00Z", + // Type: "Type 2", + // Description: "Details 2", + // }, + // }, + // } + + //request to db + var dispute models.Dispute + + err = h.DB.Where("id = ?", intID).First(&dispute).Error + if err != nil && err.Error() == "record not found" { + utilities.WriteJSON(w, http.StatusNotFound, models.Response{Data: ""}) + return + } else if err != nil { + utilities.WriteJSON(w, http.StatusInternalServerError, models.Response{Error: "Error retrieving dispute"}) + return + } + + //transform to archive dispute + var archiveDispute models.ArchivedDispute + if dispute.ID != 0 { + archiveDispute = models.ArchivedDispute{ + ArchivedDisputeSummary: models.ArchivedDisputeSummary{ + ID: dispute.ID, + Title: dispute.Title, + Summary: dispute.Description, + Category: []string{"Dispute"}, // Assuming a default category for now + DateFiled: dispute.CaseDate, + DateResolved: dispute.CaseDate.Add(48 * time.Hour), // Placeholder for resolved date + Resolution: string(dispute.Decision), }, - }, + Events: []models.Event{}, + } + utilities.WriteJSON(w, http.StatusOK, models.Response{Data: archiveDispute}) + return + } else { + utilities.WriteJSON(w, http.StatusNotFound, models.Response{Data: ""}) } - utilities.WriteJSON(w, http.StatusOK, models.Response{Data: body}) } From 62907ef8d4c99d8a2a09aac2f9b8431a469f2bfd Mon Sep 17 00:00:00 2001 From: ZaguePrime Date: Sat, 22 Jun 2024 21:11:02 +0200 Subject: [PATCH 2/2] feat/api-disputes: updates to jwt with caelan and redis buddy --- api/middleware/jwt.go | 13 +++++++++++++ 1 file changed, 13 insertions(+) diff --git a/api/middleware/jwt.go b/api/middleware/jwt.go index 2f3a12a4..46776d9b 100644 --- a/api/middleware/jwt.go +++ b/api/middleware/jwt.go @@ -94,6 +94,9 @@ func JWTMiddleware(next http.Handler) http.Handler { token, err := jwt.ParseWithClaims(tokenString, &Claims{}, func(token *jwt.Token) (interface{}, error) { return jwtSecretKey, nil }) + + + if err != nil { utilities.WriteJSON(w, http.StatusUnauthorized, models.Response{Error: "Unauthorized"}) return @@ -101,6 +104,16 @@ func JWTMiddleware(next http.Handler) http.Handler { if claims, ok := token.Claims.(*Claims); ok && token.Valid { ctx := context.WithValue(r.Context(), "user", claims) + userEmail := claims.Email + jwt, err := GetJWT(userEmail) + if err != nil { + utilities.WriteJSON(w, http.StatusUnauthorized, models.Response{Error: "Unauthorized"}) + return + } + if jwt != tokenString { + utilities.WriteJSON(w, http.StatusUnauthorized, models.Response{Error: "Unauthorized"}) + return + } next.ServeHTTP(w, r.WithContext(ctx)) } else { utilities.WriteJSON(w, http.StatusUnauthorized, models.Response{Error: "Unauthorized"})