|
| 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 | +} |
0 commit comments