Skip to content

Commit

Permalink
add request to update incident date
Browse files Browse the repository at this point in the history
  • Loading branch information
konrad2002 committed Oct 24, 2024
1 parent a745aee commit ff15726
Show file tree
Hide file tree
Showing 3 changed files with 71 additions and 0 deletions.
26 changes: 26 additions & 0 deletions controller/incident_controller.go
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@ package controller

import (
"github.com/gin-gonic/gin"
"github.com/swimresults/meeting-service/dto"
"github.com/swimresults/meeting-service/model"
"github.com/swimresults/meeting-service/service"
"go.mongodb.org/mongo-driver/bson/primitive"
Expand All @@ -15,6 +16,8 @@ func incidentController() {
router.DELETE("/incident/:id", removeIncident)
router.POST("/incident", addIncident)
router.PUT("/incident", updateIncident)

router.POST("/incident/meet/:meet_id/change_date", updateIncidentDatesByMeeting)
}

func getIncidentByMeeting(c *gin.Context) {
Expand Down Expand Up @@ -95,3 +98,26 @@ func updateIncident(c *gin.Context) {

c.IndentedJSON(http.StatusOK, r)
}

func updateIncidentDatesByMeeting(c *gin.Context) {
meeting := c.Param("meet_id")

if meeting == "" {
c.String(http.StatusBadRequest, "no meeting id given")
return
}

var request dto.IncidentDateRequest
if err := c.BindJSON(&request); err != nil {
c.IndentedJSON(http.StatusInternalServerError, gin.H{"message": err.Error()})
return
}

info, err := service.UpdateIncidentDateByMeeting(meeting, request.Time, request.UpdateTimeZone)
if err != nil {
c.IndentedJSON(http.StatusNotFound, gin.H{"message": err.Error()})
return
}

c.IndentedJSON(http.StatusOK, info)
}
8 changes: 8 additions & 0 deletions dto/incident_date_request.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
package dto

import "time"

type IncidentDateRequest struct {
Time time.Time `json:"time"`
UpdateTimeZone bool `json:"update_time_zone"`
}
37 changes: 37 additions & 0 deletions service/incident_service.go
Original file line number Diff line number Diff line change
Expand Up @@ -102,3 +102,40 @@ func UpdateIncident(incident model.Incident) (model.Incident, error) {

return GetIncidentById(incident.Identifier)
}

func UpdateIncidentDateByMeeting(meeting string, t time.Time, updateTimeZone bool) ([]model.Incident, error) {
incidents, err := GetIncidentsByMeeting(meeting)

if err != nil {
return []model.Incident{}, err
}

var savedIncidents []model.Incident

for _, incident := range incidents {
t1 := incident.Start
t2 := incident.End

var tz1 *time.Location
var tz2 *time.Location
if updateTimeZone {
tz1 = t.Location()
tz2 = t.Location()
} else {
tz1 = t1.Location()
tz2 = t2.Location()
}

incident.End = time.Date(t.Year(), t.Month(), t.Day(), t1.Hour(), t1.Minute(), t1.Second(), t1.Nanosecond(), tz1)
incident.Start = time.Date(t.Year(), t.Month(), t.Day(), t2.Hour(), t2.Minute(), t2.Second(), t2.Nanosecond(), tz2)

saved, err := UpdateIncident(incident)
if err != nil {
return []model.Incident{}, err
}

savedIncidents = append(savedIncidents, saved)
}

return savedIncidents, nil
}

0 comments on commit ff15726

Please sign in to comment.