namespace AspNetCoreApi.Mapper
{
public class CosmosDbRepository:ICosmosDbRepository
{
private static CosmosClient? client;
private static Database? db;
private static Container container;
public CosmosDbRepository(string dbId, string contaierId)
{
var config = CoreConfig.GetCosmosdbConfig();
client = new CosmosClient(config["uri"], config["connectionKey"]);
db = client.GetDatabase(dbId);
container = db.GetContainer(contaierId);
}
public FeedIterator<Gage> Query(string query)
{
QueryDefinition definition = new QueryDefinition(query);
return container.GetItemQueryIterator<Gage>(definition);
}
public void Save(Gage entity)
{
container.UpsertItemAsync(entity);
}
public FeedIterator<Gage> SelectByCharacterIdAndActivityType(string characterId, string activityType)
{
string query = $"SELECT * FROM c ;
FeedIterator<Gage> items = Query(query);
return items;
}
}
}
namespace AspNetCoreApi.Services
{
public class GageService : IGageService
{
ICosmosDbRepository cosmosdb;
public GageService(ICosmosDbRepository cosmosdb)
{
this.cosmosdb = cosmosdb;
}
public async Task<GageEvent.GageEventResult> AccumulateGage(string characterId, string activityType,string gageDelta)
{
Gage gage = new Gage();
FeedIterator<Gage> item = cosmosdb.SelectByCharacterIdAndActivityType(characterId, activityType);
if (item.HasMoreResults)
{
while (item.HasMoreResults)
{
FeedResponse<Gage> i = await item.ReadNextAsync();
foreach(Gage g in i.Resource){
gage = g;
}
}
//item['lastValue'] = add(item['lastValue'], entity['lastValue'])
//item['lastTriggeredAt'] = entity['lastTriggeredAt']
}
else
{
gage.activityType = activityType;
}
return new GageEvent.GageEventResult.Success("", "", "");
}
public async Task<GageEvent.GageEventResult> Refresh(string characterId, string activityType)
{
Gage gage = new Gage();
FeedIterator<Gage> item = this.cosmosdb.SelectByCharacterIdAndActivityType(characterId, activityType);
if (item.HasMoreResults)
{
while (item.HasMoreResults)
{
FeedResponse<Gage> i = await item.ReadNextAsync();
foreach (Gage g in i.Resource)
{
gage = g;
}
}
}
else
{
//TODO item = dict(id=uuidgen(), characterId=character_id,
//activityType = activity_type, lastValue = 0, lastTriggeredAt = current_time())
gage.activityType = activityType;
}
cosmosdb.Save(gage);
return new GageEvent.GageEventResult.Success("","",item);
}
}
}