Skip to content

Commit

Permalink
remove email from players on open APIs;
Browse files Browse the repository at this point in the history
  • Loading branch information
ctcac00 committed Jul 31, 2024
1 parent a9c95ee commit 79f18af
Showing 1 changed file with 21 additions and 5 deletions.
26 changes: 21 additions & 5 deletions rest_service/Controllers/PlayersController.cs
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
using System.Xml.Linq;
using System.Xml.Linq;
using Microsoft.AspNetCore.Mvc;
using MongoDB.Bson;
using MongoDB.Bson.Serialization;
Expand Down Expand Up @@ -47,23 +47,33 @@ public async Task<List<PlayerResponse>> GetPlayers([FromQuery] PlayerRequest pla
if (!string.IsNullOrEmpty(playerRequest.Id))
filter &= Builders<Player>.Filter.Eq("Id", playerRequest.Id);

// Projection to only include Name and Age fields
var projection = Builders<Player>.Projection
.Exclude("Email");

// If Name but no Location, then get Location from players_unique
if (!string.IsNullOrEmpty(playerRequest.Name) && string.IsNullOrEmpty(playerRequest.Location))
{

// Projection to only include Name and Age fields
var projectionUnique = Builders<PlayerUnique>.Projection
.Exclude("Email");

var playerUnique = _playersUniqueCollection
.Find(Builders<PlayerUnique>
.Filter.Eq(x => x.Name, playerRequest.Name))
.Project<PlayerUnique>(projectionUnique)
.FirstOrDefault<PlayerUnique>();

if (playerUnique != null)
filter &= Builders<Player>.Filter.Eq(x => x.Location, playerUnique.Location);
}

var players = await _playersCollection.FindAsync(filter, new FindOptions<Player,Player>() { Limit = 10 });
var players = await _playersCollection.FindAsync(filter, new FindOptions<Player, Player>() { Limit = 10, Projection = projection });

var playersResponse =
players.ToList().Select(player => new PlayerResponse(player)).ToList();

return playersResponse;
}

Expand Down Expand Up @@ -156,7 +166,7 @@ public async Task<ActionResult<PlayerResponse>> CreatePlayer(PlayerRequest playe
try
{
await _playersCollection.InsertOneAsync(session, player);

await _playersUniqueCollection.InsertOneAsync(session, playerUnique);

if (session.IsInTransaction)
Expand Down Expand Up @@ -205,7 +215,8 @@ public async Task<List<string>> PlayerAutoComplete([FromQuery] string Name)
return arrMatches.GetElement("matches").Value.AsBsonArray
.Select(x => x.ToString())
.ToList();
} catch (Exception e)
}
catch (Exception e)
{
Logger.LogError("GetPlayerAutoComplete did not find matches");
Logger.LogError(e.Message);
Expand Down Expand Up @@ -307,6 +318,11 @@ public async Task<List<PlayerResponse>> PlayerSearch([FromQuery] PlayerRequest p
}
}
}
}),
new BsonDocument("$project", new BsonDocument
{
{ "Email", 0 },

})
}
},
Expand Down

0 comments on commit 79f18af

Please sign in to comment.