From e9065db23c13ffb67c13d2ea550ef0d9d87505fa Mon Sep 17 00:00:00 2001 From: Brandon Watson Date: Fri, 5 Mar 2021 19:09:42 -0500 Subject: [PATCH] First pass at party service --- AuroraCradle.test/CursorList.test.cs | 14 ++--- AuroraCradle/Src/Cursor/Cursor.cs | 7 +++ AuroraCradle/Src/Cursor/CursorList.cs | 9 ++- AuroraCradle/Src/Protos/signal.proto | 7 +++ AuroraCradle/Src/Services/Signal/Party.cs | 55 +++++++++++++++++-- .../Src/Services/Signal/SignalService.cs | 1 - 6 files changed, 76 insertions(+), 17 deletions(-) diff --git a/AuroraCradle.test/CursorList.test.cs b/AuroraCradle.test/CursorList.test.cs index e62dac8..4a99d96 100644 --- a/AuroraCradle.test/CursorList.test.cs +++ b/AuroraCradle.test/CursorList.test.cs @@ -1,21 +1,17 @@ using Xunit; using Aurora.Services.Signal; using Aurora.Cursor; -using Faker; -using System; using System.Collections.Generic; using System.Linq; - - namespace AuroraCradle.test { public class CursorListTest { [Theory()] - [InlineData(SortDirection.Asc)] - [InlineData(SortDirection.Desc)] - public void CursorListSortOnStringValue(SortDirection direction) + [InlineData(Aurora.Cursor.SortDirection.Asc)] + [InlineData(Aurora.Cursor.SortDirection.Desc)] + public void CursorListSortOnStringValue(Aurora.Cursor.SortDirection direction) { CursorList list = new CursorList(); list.Add(new Party() { Name = "asdf", Id = Faker.RandomNumber.Next().ToString() }); @@ -26,7 +22,7 @@ namespace AuroraCradle.test .WithSort(item => item.Value.Name, direction) .GetNextPage(); - if (direction == SortDirection.Desc) + if (direction == Aurora.Cursor.SortDirection.Desc) { Assert.Collection(result.Result, item => item.Name.Equals("asdf"), @@ -149,7 +145,7 @@ namespace AuroraCradle.test { CursorResult res = cursor .WithSize(10) - .WithSort(item => item.Value.Name, SortDirection.Asc) + .WithSort(item => item.Value.Name, Aurora.Cursor.SortDirection.Asc) .WithNextPageToken(pageToken) .GetNextPage(); diff --git a/AuroraCradle/Src/Cursor/Cursor.cs b/AuroraCradle/Src/Cursor/Cursor.cs index 91db9ea..9a60991 100644 --- a/AuroraCradle/Src/Cursor/Cursor.cs +++ b/AuroraCradle/Src/Cursor/Cursor.cs @@ -109,6 +109,13 @@ namespace Aurora.Cursor return this; } + public Cursor WithSort(string key, SortDirection direction) + { + this._sortDelgate = (item) => key; + this._direction = direction; + return this; + } + public Cursor WithSize(int size) { this._pageSize = size; diff --git a/AuroraCradle/Src/Cursor/CursorList.cs b/AuroraCradle/Src/Cursor/CursorList.cs index fbff1f3..0dcbf04 100644 --- a/AuroraCradle/Src/Cursor/CursorList.cs +++ b/AuroraCradle/Src/Cursor/CursorList.cs @@ -17,8 +17,13 @@ namespace Aurora.Cursor public CursorList Add(T item) { - string itemHashId = HashUtil.GetHash(new string[] { item.Id, item.GetHashCode().ToString() }).ToString(); - bool res = this.TryAdd(itemHashId, item); + string id = item.Id; + if (item.Id == null) + { + id = HashUtil.GetHash(new string[] { item.GetHashCode().ToString() }).ToString(); + item.Id = id; + } + bool res = this.TryAdd(id, item); if (res == false) { diff --git a/AuroraCradle/Src/Protos/signal.proto b/AuroraCradle/Src/Protos/signal.proto index 5d62e9c..078630d 100644 --- a/AuroraCradle/Src/Protos/signal.proto +++ b/AuroraCradle/Src/Protos/signal.proto @@ -69,6 +69,13 @@ message PartyListItem { message ListPartiesRequest { int32 page_size = 1; string page_token = 2; + string order_by = 3; + SortDirection order_direction = 4; +} + +enum SortDirection { + Asc = 0; + Desc = 1; } message ListPartiesResponse { diff --git a/AuroraCradle/Src/Services/Signal/Party.cs b/AuroraCradle/Src/Services/Signal/Party.cs index cccf8f8..c044cde 100644 --- a/AuroraCradle/Src/Services/Signal/Party.cs +++ b/AuroraCradle/Src/Services/Signal/Party.cs @@ -1,34 +1,79 @@ using System.Threading.Tasks; using Google.Protobuf.WellKnownTypes; using Grpc.Core; +using Aurora.Cursor; +using Microsoft.Extensions.Logging; namespace Aurora.Services.Signal { public partial class SignalService : Signal.SignalBase { + private CursorList _partyList; + public override Task CreateParty(CreatePartyRequest request, ServerCallContext context) { - return base.CreateParty(request, context); + Party party = new Party(request.Party); + _partyList.Add(party); + + this._logger.LogInformation(string.Format("Added party with name: ${0} to parties", party.Name)); + + return Task.FromResult(party); } public override Task DeleteParty(DeletePartyRequest request, ServerCallContext context) { - return base.DeleteParty(request, context); + if (this._partyList.ContainsKey(request.PartyId)) + { + this._partyList.Remove(request.PartyId); + } + + this._logger.LogInformation(string.Format("Deleted party with id: ${0} to parties", request.PartyId)); + return Task.FromResult(new Empty()); } public override Task ListParties(ListPartiesRequest request, ServerCallContext context) { - return base.ListParties(request, context); + Cursor cursor = new Cursor(ref this._partyList); + + Aurora.Cursor.SortDirection direction = Aurora.Cursor.SortDirection.Asc; + if (request.OrderDirection == SortDirection.Desc) + { + direction = Aurora.Cursor.SortDirection.Desc; + } + + CursorResult res = cursor + .WithSort(request.OrderBy, direction) + .WithNextPageToken(request.PageToken) + .WithSize(request.PageSize) + .GetNextPage(); + + ListPartiesResponse response = new ListPartiesResponse() + { + NextPageToken = res.NextPageToken + }; + response.Parties.AddRange(res.Result.ConvertAll(party => new PartyListItem() + { + Name = party.Name, + Id = party.Id + })); + return Task.FromResult(response); } public override Task GetParty(GetPartyRequest request, ServerCallContext context) { - return base.GetParty(request, context); + Party party = new Party(); + + if (this._partyList.ContainsKey(request.PartyId)) + { + this._partyList.TryGetValue(request.PartyId, out party); + } + + return Task.FromResult(party); } public override Task UpdateParty(UpdatePartyRequest request, ServerCallContext context) { - return base.UpdateParty(request, context); + throw new System.NotImplementedException(); } } } \ No newline at end of file diff --git a/AuroraCradle/Src/Services/Signal/SignalService.cs b/AuroraCradle/Src/Services/Signal/SignalService.cs index 8a59f50..af4100a 100644 --- a/AuroraCradle/Src/Services/Signal/SignalService.cs +++ b/AuroraCradle/Src/Services/Signal/SignalService.cs @@ -12,7 +12,6 @@ namespace Aurora.Services.Signal { private readonly ILogger _logger; - private CursorList _partyList; public SignalService(ILogger logger) { _logger = logger;