Skip to content

Commit

Permalink
Merge pull request #125 from mongodb-developer/feature/create-event
Browse files Browse the repository at this point in the history
Feature/create event
  • Loading branch information
snarvaez authored Mar 24, 2024
2 parents aae5e9f + 4e495ee commit 2001aff
Show file tree
Hide file tree
Showing 11 changed files with 805 additions and 241 deletions.
4 changes: 3 additions & 1 deletion deployment/game_database/indexes.js
Original file line number Diff line number Diff line change
@@ -1 +1,3 @@
db.recordings.createIndex({ DateTime: 1 });
db.recordings.createIndex({ "DateTime": 1 });
db.recordings.createIndex({ "location":1, "Event._id":1, "Player.Nickname": 1});
db.recordings.createIndex({ "Player.Nickname": 1,"Event._id": 1 });
56 changes: 56 additions & 0 deletions rest_service/Controllers/PlayersController.cs
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
using System.Xml.Linq;
using Microsoft.AspNetCore.Mvc;
using MongoDB.Bson;
using MongoDB.Bson.Serialization;
using MongoDB.Driver;
using RestService.Dtos.RequestObjects;
using RestService.Dtos.ResponseObjects;
Expand All @@ -15,6 +16,7 @@ public class PlayersController : BaseController
private readonly IMongoCollection<Player> _playersCollection;
private readonly IMongoCollection<PlayerUnique> _playersUniqueCollection;
private readonly IMongoCollection<PlayerUnique> _playersUniqueCollectionOnSecondary;
private readonly IMongoCollection<Recording> _recordingsCollection;

public PlayersController(ILogger<PlayersController> logger) : base(logger)
{
Expand All @@ -26,6 +28,8 @@ public PlayersController(ILogger<PlayersController> logger) : base(logger)
Constants.PlayersUniqueCollectionName,
new MongoCollectionSettings() { ReadPreference = ReadPreference.SecondaryPreferred }
);

_recordingsCollection = Database!.GetCollection<Recording>(Constants.RecordingsCollectionName);
}

[HttpGet(Name = "GetPlayers")]
Expand Down Expand Up @@ -63,6 +67,58 @@ public async Task<List<PlayerResponse>> GetPlayers([FromQuery] PlayerRequest pla
return playersResponse;
}

[HttpGet("events", Name = "GetPlayerEvents")]
public async Task<List<EventResponse>> GetEventsForPlayer([FromQuery] PlayerRequest playerRequest)
{
var pipeline = new List<IPipelineStageDefinition>
{
new JsonPipelineStageDefinition<Recording, BsonDocument>(
"{$match:{'Player.Nickname': '" + playerRequest.Name + "'}}"
),
new JsonPipelineStageDefinition<BsonDocument, BsonDocument>(
"{$project:{_id: 0,'Player.Nickname': 1,'Event._id': 1}}"
),
new JsonPipelineStageDefinition<BsonDocument, BsonDocument>(
"{$group:{_id: 1,EventIds:{$addToSet: '$Event._id'}}}"
),
new JsonPipelineStageDefinition<BsonDocument, BsonDocument>(
"{$project:{_id: 0,EventId:{$sortArray:{input: '$EventIds',sortBy: 1}}}}"
),
new JsonPipelineStageDefinition<BsonDocument, BsonDocument>(
"{$unwind:{path: '$EventId'}}"
),
new JsonPipelineStageDefinition<BsonDocument, BsonDocument>(
@"{$lookup:{
from: 'events',
localField: 'EventId',
foreignField: '_id',
as: 'event'}}"
),
new JsonPipelineStageDefinition<BsonDocument, BsonDocument>(
"{$unwind:{path: '$event'}}"
),
new JsonPipelineStageDefinition<BsonDocument, BsonDocument>(
"{$replaceRoot:{newRoot: '$event'}}"
)
};

try
{
var result = await _recordingsCollection.AggregateAsync<BsonDocument>(pipeline);
List<BsonDocument> bsonDocuments = await result.ToListAsync();
List<EventResponse> events = bsonDocuments.Select(document => BsonSerializer.Deserialize<EventResponse>(document)).ToList();

return events;
}
catch (Exception e)
{
Logger.LogError("GetEventsForPlayer did not find matches");
Logger.LogError(e.Message);

return new List<EventResponse>();
}
}

[HttpPost(Name = "CreatePlayer")]
public async Task<ActionResult<PlayerResponse>> CreatePlayer(PlayerRequest playerRequest)
{
Expand Down
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
@page "/eventHome"
@page "/eventDashboard"
@using RestSharp
@using website.Data
@using website.Utils
Expand All @@ -21,7 +21,7 @@
margin: auto;
}
</style>
<a href="\?EventId=@_eventId">
<a href="/EventRegister?EventId=@_eventId">
<img src="\mongodb-logo-green-bg.jpg" class="rounded mx-auto d-block" alt="logo">
</a>
<div class="card" style="max-width:400px;background:yellow;text-align:center">
Expand All @@ -33,29 +33,32 @@
<a href="https://github.com/mongodb-developer/leafsteroids" target="_blank">GitHub</a>
</div>
</div>
<div class="card bg-white">
<article class="card-body mx-auto" style="max-width: 400px;">
<table>
<tr>
<td>
<div>
<img src="@GetQRCodeForCurrentUrl()" class="rounded mx-auto d-block" alt="logo">
</div>
</td>
<td>
@if (Event?.Name != null)
{
<h2 class="card-title mt-3 text-center">@Event.Name</h2>
}
</td>
</tr>
</table>
<h4 class="card-title mt-3 text-center">
<h2 class="text-center">Event Dashboard</h2>
</h4>
</article>
</div>

@if (_event != null)
{
<div class="card bg-white">
<article class="card-body mx-auto" style="max-width: 400px;">
<h4 class="card-title mt-3 text-center">
<h2 class="text-center">Event Dashboard</h2>
</h4>
<table>
<tr>
<td>
<div>
<img src="@GetQRCodeForCurrentUrl()" class="rounded mx-auto d-block" alt="logo">
</div>
</td>
<td>
@if (_event?.Name != null)
{
<h2 class="card-title mt-3 text-center">@_event.Name</h2>
}
</td>
</tr>
</table>
</article>
</div>
}

@if (_chartUrl.Length == 0)
{
Expand All @@ -72,7 +75,7 @@ else

@code {

private Event? Event { get; set; }
private Event? _event { get; set; }
private string _eventId = string.Empty;
private string _chartUrl = string.Empty;
private readonly RestClient _restClient = RestServiceClient.Create();
Expand All @@ -90,12 +93,12 @@ else
};
string eventsUrlWithQuery = UrlHelper.BuildUrlWithQuery(Constants.RestServiceEndpointEvents, eventsFilter);
var events = await _restClient.GetJsonAsync<List<Event>>(eventsUrlWithQuery);
Event = events.FirstOrDefault();
_event = events.FirstOrDefault();

DotEnv.Load();
var envVars = DotEnv.Read();
var atlasChartIdEvent = envVars["ATLAS_CHART_ID_EVENT"];
_chartUrl = ChartsUrl.CreateEventUrl(atlasChartIdEvent, Event!.Id!, Event.Location!);
_chartUrl = ChartsUrl.CreateEventUrl(atlasChartIdEvent, _event!.Id!, _event.Location!);
}
catch (Exception e)
{
Expand All @@ -110,7 +113,7 @@ else
private string GetQRCodeForCurrentUrl()
{
string url = "https://api.qrserver.com/v1/create-qr-code/?size=150x150&data=" +
NavigationManager.BaseUri + "?EventId=" + _eventId;
NavigationManager.BaseUri + "/EventRegister?EventId=" + _eventId;
Console.WriteLine(url);
return url;
}
Expand Down
Loading

0 comments on commit 2001aff

Please sign in to comment.