diff --git a/controller/incident_controller.go b/controller/incident_controller.go index b7cf5e6..c7ca714 100644 --- a/controller/incident_controller.go +++ b/controller/incident_controller.go @@ -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" @@ -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) { @@ -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) +} diff --git a/dto/incident_date_request.go b/dto/incident_date_request.go new file mode 100644 index 0000000..67f0609 --- /dev/null +++ b/dto/incident_date_request.go @@ -0,0 +1,8 @@ +package dto + +import "time" + +type IncidentDateRequest struct { + Time time.Time `json:"time"` + UpdateTimeZone bool `json:"update_time_zone"` +} diff --git a/service/incident_service.go b/service/incident_service.go index 3695e24..e873a2f 100644 --- a/service/incident_service.go +++ b/service/incident_service.go @@ -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(t1.Year(), t1.Month(), t1.Day(), t1.Hour(), t1.Minute(), t1.Second(), t1.Nanosecond(), tz1) + incident.Start = time.Date(t2.Year(), t2.Month(), t2.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 +}