Skip to content

Commit

Permalink
Merge pull request #158 from COS301-SE-2024/feat/api-disputes
Browse files Browse the repository at this point in the history
Feat/api disputes
  • Loading branch information
CaelanHill authored Jun 23, 2024
2 parents 6c142d0 + 62907ef commit 9ccd709
Show file tree
Hide file tree
Showing 2 changed files with 67 additions and 22 deletions.
76 changes: 54 additions & 22 deletions api/handlers/dispute.go
Original file line number Diff line number Diff line change
Expand Up @@ -311,29 +311,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})
}
13 changes: 13 additions & 0 deletions api/middleware/jwt.go
Original file line number Diff line number Diff line change
Expand Up @@ -94,13 +94,26 @@ 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
}

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"})
Expand Down

0 comments on commit 9ccd709

Please sign in to comment.