First pass at party service
This commit is contained in:
parent
6f73df0fb7
commit
e9065db23c
@ -1,21 +1,17 @@
|
|||||||
using Xunit;
|
using Xunit;
|
||||||
using Aurora.Services.Signal;
|
using Aurora.Services.Signal;
|
||||||
using Aurora.Cursor;
|
using Aurora.Cursor;
|
||||||
using Faker;
|
|
||||||
using System;
|
|
||||||
using System.Collections.Generic;
|
using System.Collections.Generic;
|
||||||
using System.Linq;
|
using System.Linq;
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
namespace AuroraCradle.test
|
namespace AuroraCradle.test
|
||||||
{
|
{
|
||||||
public class CursorListTest
|
public class CursorListTest
|
||||||
{
|
{
|
||||||
[Theory()]
|
[Theory()]
|
||||||
[InlineData(SortDirection.Asc)]
|
[InlineData(Aurora.Cursor.SortDirection.Asc)]
|
||||||
[InlineData(SortDirection.Desc)]
|
[InlineData(Aurora.Cursor.SortDirection.Desc)]
|
||||||
public void CursorListSortOnStringValue(SortDirection direction)
|
public void CursorListSortOnStringValue(Aurora.Cursor.SortDirection direction)
|
||||||
{
|
{
|
||||||
CursorList<Party> list = new CursorList<Party>();
|
CursorList<Party> list = new CursorList<Party>();
|
||||||
list.Add(new Party() { Name = "asdf", Id = Faker.RandomNumber.Next().ToString() });
|
list.Add(new Party() { Name = "asdf", Id = Faker.RandomNumber.Next().ToString() });
|
||||||
@ -26,7 +22,7 @@ namespace AuroraCradle.test
|
|||||||
.WithSort(item => item.Value.Name, direction)
|
.WithSort(item => item.Value.Name, direction)
|
||||||
.GetNextPage();
|
.GetNextPage();
|
||||||
|
|
||||||
if (direction == SortDirection.Desc)
|
if (direction == Aurora.Cursor.SortDirection.Desc)
|
||||||
{
|
{
|
||||||
Assert.Collection<Party>(result.Result,
|
Assert.Collection<Party>(result.Result,
|
||||||
item => item.Name.Equals("asdf"),
|
item => item.Name.Equals("asdf"),
|
||||||
@ -149,7 +145,7 @@ namespace AuroraCradle.test
|
|||||||
{
|
{
|
||||||
CursorResult<Party> res = cursor
|
CursorResult<Party> res = cursor
|
||||||
.WithSize(10)
|
.WithSize(10)
|
||||||
.WithSort(item => item.Value.Name, SortDirection.Asc)
|
.WithSort(item => item.Value.Name, Aurora.Cursor.SortDirection.Asc)
|
||||||
.WithNextPageToken(pageToken)
|
.WithNextPageToken(pageToken)
|
||||||
.GetNextPage();
|
.GetNextPage();
|
||||||
|
|
||||||
|
@ -109,6 +109,13 @@ namespace Aurora.Cursor
|
|||||||
return this;
|
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)
|
public Cursor<T> WithSize(int size)
|
||||||
{
|
{
|
||||||
this._pageSize = size;
|
this._pageSize = size;
|
||||||
|
@ -17,8 +17,13 @@ namespace Aurora.Cursor
|
|||||||
|
|
||||||
public CursorList<T> Add(T item)
|
public CursorList<T> Add(T item)
|
||||||
{
|
{
|
||||||
string itemHashId = HashUtil.GetHash(new string[] { item.Id, item.GetHashCode().ToString() }).ToString();
|
string id = item.Id;
|
||||||
bool res = this.TryAdd(itemHashId, item);
|
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)
|
if (res == false)
|
||||||
{
|
{
|
||||||
|
@ -69,6 +69,13 @@ message PartyListItem {
|
|||||||
message ListPartiesRequest {
|
message ListPartiesRequest {
|
||||||
int32 page_size = 1;
|
int32 page_size = 1;
|
||||||
string page_token = 2;
|
string page_token = 2;
|
||||||
|
string order_by = 3;
|
||||||
|
SortDirection order_direction = 4;
|
||||||
|
}
|
||||||
|
|
||||||
|
enum SortDirection {
|
||||||
|
Asc = 0;
|
||||||
|
Desc = 1;
|
||||||
}
|
}
|
||||||
|
|
||||||
message ListPartiesResponse {
|
message ListPartiesResponse {
|
||||||
|
@ -1,34 +1,79 @@
|
|||||||
using System.Threading.Tasks;
|
using System.Threading.Tasks;
|
||||||
using Google.Protobuf.WellKnownTypes;
|
using Google.Protobuf.WellKnownTypes;
|
||||||
using Grpc.Core;
|
using Grpc.Core;
|
||||||
|
using Aurora.Cursor;
|
||||||
|
using Microsoft.Extensions.Logging;
|
||||||
|
|
||||||
namespace Aurora.Services.Signal
|
namespace Aurora.Services.Signal
|
||||||
{
|
{
|
||||||
public partial class SignalService : Signal.SignalBase
|
public partial class SignalService : Signal.SignalBase
|
||||||
{
|
{
|
||||||
|
private CursorList<Party> _partyList;
|
||||||
|
|
||||||
public override Task<Party> CreateParty(CreatePartyRequest request, ServerCallContext context)
|
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)
|
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)
|
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)
|
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)
|
public override Task<Party> UpdateParty(UpdatePartyRequest request, ServerCallContext context)
|
||||||
{
|
{
|
||||||
return base.UpdateParty(request, context);
|
throw new System.NotImplementedException();
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
@ -12,7 +12,6 @@ namespace Aurora.Services.Signal
|
|||||||
{
|
{
|
||||||
private readonly ILogger<SignalService> _logger;
|
private readonly ILogger<SignalService> _logger;
|
||||||
|
|
||||||
private CursorList<Party> _partyList;
|
|
||||||
public SignalService(ILogger<SignalService> logger)
|
public SignalService(ILogger<SignalService> logger)
|
||||||
{
|
{
|
||||||
_logger = logger;
|
_logger = logger;
|
||||||
|
Reference in New Issue
Block a user