First pass at party service

This commit is contained in:
Brandon Watson 2021-03-05 19:09:42 -05:00
parent 6f73df0fb7
commit e9065db23c
6 changed files with 76 additions and 17 deletions

View File

@ -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<Party> list = new CursorList<Party>();
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<Party>(result.Result,
item => item.Name.Equals("asdf"),
@ -149,7 +145,7 @@ namespace AuroraCradle.test
{
CursorResult<Party> res = cursor
.WithSize(10)
.WithSort(item => item.Value.Name, SortDirection.Asc)
.WithSort(item => item.Value.Name, Aurora.Cursor.SortDirection.Asc)
.WithNextPageToken(pageToken)
.GetNextPage();

View File

@ -109,6 +109,13 @@ namespace Aurora.Cursor
return this;
}
public Cursor<T> WithSort(string key, SortDirection direction)
{
this._sortDelgate = (item) => key;
this._direction = direction;
return this;
}
public Cursor<T> WithSize(int size)
{
this._pageSize = size;

View File

@ -17,8 +17,13 @@ namespace Aurora.Cursor
public CursorList<T> 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)
{

View File

@ -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 {

View File

@ -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<Party> _partyList;
public override Task<Party> 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<Empty> 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<ListPartiesResponse> ListParties(ListPartiesRequest request, ServerCallContext context)
{
return base.ListParties(request, context);
Cursor<Party> cursor = new Cursor<Party>(ref this._partyList);
Aurora.Cursor.SortDirection direction = Aurora.Cursor.SortDirection.Asc;
if (request.OrderDirection == SortDirection.Desc)
{
direction = Aurora.Cursor.SortDirection.Desc;
}
CursorResult<Party> 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<Party> 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<Party> UpdateParty(UpdatePartyRequest request, ServerCallContext context)
{
return base.UpdateParty(request, context);
throw new System.NotImplementedException();
}
}
}

View File

@ -12,7 +12,6 @@ namespace Aurora.Services.Signal
{
private readonly ILogger<SignalService> _logger;
private CursorList<Party> _partyList;
public SignalService(ILogger<SignalService> logger)
{
_logger = logger;