Skip to content

Commit 1ead88c

Browse files
authored
Merge pull request #13 from SwimResults/develop
incidents + stream embed url
2 parents ffe4acd + 35deedc commit 1ead88c

File tree

6 files changed

+223
-0
lines changed

6 files changed

+223
-0
lines changed

controller/controller.go

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -26,6 +26,7 @@ func Run() {
2626
eventController()
2727
fileController()
2828
ageGroupController()
29+
incidentController()
2930

3031
router.GET("/actuator", actuator)
3132

controller/incident_controller.go

Lines changed: 97 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,97 @@
1+
package controller
2+
3+
import (
4+
"github.com/gin-gonic/gin"
5+
"github.com/swimresults/meeting-service/model"
6+
"github.com/swimresults/meeting-service/service"
7+
"go.mongodb.org/mongo-driver/bson/primitive"
8+
"net/http"
9+
)
10+
11+
func incidentController() {
12+
router.GET("/incident/:id", getIncident)
13+
router.GET("/incident/meet/:meeting", getIncidentByMeeting)
14+
15+
router.DELETE("/incident/:id", removeIncident)
16+
router.POST("/incident", addIncident)
17+
router.PUT("/incident", updateIncident)
18+
}
19+
20+
func getIncidentByMeeting(c *gin.Context) {
21+
meeting := c.Param("meeting")
22+
if meeting == "" {
23+
c.IndentedJSON(http.StatusBadRequest, gin.H{"message": "given meeting was empty"})
24+
return
25+
}
26+
incidents, err := service.GetIncidentsByMeeting(meeting)
27+
if err != nil {
28+
c.IndentedJSON(http.StatusInternalServerError, gin.H{"message": err.Error()})
29+
return
30+
}
31+
32+
c.IndentedJSON(http.StatusOK, incidents)
33+
}
34+
35+
func getIncident(c *gin.Context) {
36+
id, convErr := primitive.ObjectIDFromHex(c.Param("id"))
37+
if convErr != nil {
38+
c.IndentedJSON(http.StatusBadRequest, gin.H{"message": "given id was not of type ObjectID"})
39+
return
40+
}
41+
42+
incident, err := service.GetIncidentById(id)
43+
if err != nil {
44+
c.IndentedJSON(http.StatusNotFound, gin.H{"message": err.Error()})
45+
return
46+
}
47+
48+
c.IndentedJSON(http.StatusOK, incident)
49+
}
50+
51+
func removeIncident(c *gin.Context) {
52+
id, convErr := primitive.ObjectIDFromHex(c.Param("id"))
53+
if convErr != nil {
54+
c.IndentedJSON(http.StatusBadRequest, gin.H{"message": "given id was not of type ObjectID"})
55+
return
56+
}
57+
58+
err := service.RemoveIncidentById(id)
59+
if err != nil {
60+
c.IndentedJSON(http.StatusNotFound, gin.H{"message": err.Error()})
61+
return
62+
}
63+
64+
c.IndentedJSON(http.StatusNoContent, "")
65+
}
66+
67+
func addIncident(c *gin.Context) {
68+
var incident model.Incident
69+
if err := c.BindJSON(&incident); err != nil {
70+
c.IndentedJSON(http.StatusInternalServerError, gin.H{"message": err.Error()})
71+
return
72+
}
73+
74+
r, err := service.AddIncident(incident)
75+
if err != nil {
76+
c.IndentedJSON(http.StatusInternalServerError, gin.H{"message": err.Error()})
77+
return
78+
}
79+
80+
c.IndentedJSON(http.StatusOK, r)
81+
}
82+
83+
func updateIncident(c *gin.Context) {
84+
var incident model.Incident
85+
if err := c.BindJSON(&incident); err != nil {
86+
c.IndentedJSON(http.StatusInternalServerError, gin.H{"message": err.Error()})
87+
return
88+
}
89+
90+
r, err := service.UpdateIncident(incident)
91+
if err != nil {
92+
c.IndentedJSON(http.StatusNotFound, gin.H{"message": err.Error()})
93+
return
94+
}
95+
96+
c.IndentedJSON(http.StatusOK, r)
97+
}

model/incident.go

Lines changed: 19 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,19 @@
1+
package model
2+
3+
import (
4+
"go.mongodb.org/mongo-driver/bson/primitive"
5+
"time"
6+
)
7+
8+
type Incident struct {
9+
Identifier primitive.ObjectID `json:"_id,omitempty" bson:"_id,omitempty"`
10+
Meeting string `json:"meeting,omitempty" bson:"meeting,omitempty"`
11+
Type string `json:"type,omitempty" bson:"type,omitempty"` // EVENT, DURATION
12+
Name string `json:"name,omitempty" bson:"name,omitempty"` // Einschwimmen, Kampfrichtersitzung, Mannschaftsleistersitzung
13+
Start time.Time `json:"start,omitempty" bson:"start,omitempty"`
14+
End time.Time `json:"end,omitempty" bson:"end,omitempty"`
15+
PrevEvent int `json:"prev_event,omitempty" bson:"prev_event,omitempty"`
16+
NextEvent int `json:"next_event,omitempty" bson:"next_event,omitempty"`
17+
AddedAt time.Time `json:"added_at,omitempty" bson:"added_at,omitempty"`
18+
UpdatedAt time.Time `json:"updated_at,omitempty" bson:"updated_at,omitempty"`
19+
}

model/meeting_data.go

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,7 @@ type MeetingData struct {
44
LivetimingUrl string `json:"livetiming_url,omitempty" bson:"livetiming_url,omitempty"`
55
WebsiteUrl string `json:"website_url,omitempty" bson:"website_url,omitempty"`
66
StreamUrl string `json:"stream_url,omitempty" bson:"stream_url,omitempty"`
7+
StreamEmbedUrl string `json:"stream_embed_url,omitempty" bson:"stream_embed_url,omitempty"`
78
InstagramUrl string `json:"instagram_url,omitempty" bson:"instagram_url,omitempty"`
89
FacebookUrl string `json:"facebook_url,omitempty" bson:"facebook_url,omitempty"`
910
HasLivetiming bool `json:"has_livetiming,omitempty" bson:"has_livetiming,omitempty"`

service/incident_service.go

Lines changed: 104 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,104 @@
1+
package service
2+
3+
import (
4+
"context"
5+
"errors"
6+
"github.com/swimresults/meeting-service/model"
7+
"go.mongodb.org/mongo-driver/bson"
8+
"go.mongodb.org/mongo-driver/bson/primitive"
9+
"go.mongodb.org/mongo-driver/mongo"
10+
"go.mongodb.org/mongo-driver/mongo/options"
11+
"time"
12+
)
13+
14+
var incidentCollection *mongo.Collection
15+
16+
func incidentService(database *mongo.Database) {
17+
incidentCollection = database.Collection("incident")
18+
}
19+
20+
func getIncidentsByBsonDocument(d primitive.D) ([]model.Incident, error) {
21+
var incidents []model.Incident
22+
23+
ctx, cancel := context.WithTimeout(context.Background(), 10*time.Second)
24+
defer cancel()
25+
26+
queryOptions := options.FindOptions{}
27+
queryOptions.SetSort(bson.D{{"start", 1}})
28+
29+
cursor, err := incidentCollection.Find(ctx, d, &queryOptions)
30+
if err != nil {
31+
return []model.Incident{}, err
32+
}
33+
defer cursor.Close(ctx)
34+
35+
for cursor.Next(ctx) {
36+
var incident model.Incident
37+
cursor.Decode(&incident)
38+
incidents = append(incidents, incident)
39+
}
40+
41+
if err := cursor.Err(); err != nil {
42+
return []model.Incident{}, err
43+
}
44+
45+
return incidents, nil
46+
}
47+
48+
func GetIncidentsByMeeting(meeting string) ([]model.Incident, error) {
49+
return getIncidentsByBsonDocument(bson.D{{"meeting", meeting}})
50+
}
51+
52+
func GetIncidentById(id primitive.ObjectID) (model.Incident, error) {
53+
incidents, err := getIncidentsByBsonDocument(bson.D{{"_id", id}})
54+
if err != nil {
55+
return model.Incident{}, err
56+
}
57+
58+
if len(incidents) > 0 {
59+
return incidents[0], nil
60+
}
61+
62+
return model.Incident{}, errors.New("no entry with given id found")
63+
}
64+
65+
func RemoveIncidentById(id primitive.ObjectID) error {
66+
67+
ctx, cancel := context.WithTimeout(context.Background(), 10*time.Second)
68+
defer cancel()
69+
70+
_, err := incidentCollection.DeleteOne(ctx, bson.D{{"_id", id}})
71+
if err != nil {
72+
return err
73+
}
74+
return nil
75+
}
76+
77+
func AddIncident(incident model.Incident) (model.Incident, error) {
78+
ctx, cancel := context.WithTimeout(context.Background(), 10*time.Second)
79+
defer cancel()
80+
81+
incident.AddedAt = time.Now()
82+
incident.UpdatedAt = time.Now()
83+
84+
r, err := incidentCollection.InsertOne(ctx, incident)
85+
if err != nil {
86+
return model.Incident{}, err
87+
}
88+
89+
return GetIncidentById(r.InsertedID.(primitive.ObjectID))
90+
}
91+
92+
func UpdateIncident(incident model.Incident) (model.Incident, error) {
93+
ctx, cancel := context.WithTimeout(context.Background(), 10*time.Second)
94+
defer cancel()
95+
96+
incident.UpdatedAt = time.Now()
97+
98+
_, err := incidentCollection.ReplaceOne(ctx, bson.D{{"_id", incident.Identifier}}, incident)
99+
if err != nil {
100+
return model.Incident{}, err
101+
}
102+
103+
return GetIncidentById(incident.Identifier)
104+
}

service/service.go

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -21,6 +21,7 @@ func Init(c *mongo.Client) {
2121
eventService(database)
2222
fileService(database)
2323
ageGroupService(database)
24+
incidentService(database)
2425
}
2526

2627
func PingDatabase() bool {

0 commit comments

Comments
 (0)