public async Task <ActionResult <NestContractView> > AddCurrentContracts( [FromBody] NestContractBinding binding, [FromServices] NestContractCreator contractCreator, [FromServices] INestContractRepository repository, [FromServices] IQueryProcessor queryProcessor, CancellationToken cancellationToken) { try { var contract = await contractCreator.Create(HttpContext.GetUserId(), HttpContext.GetGuildId(), binding.Id, binding.NestId, binding.Reward, binding.CharacterName, cancellationToken); await repository.Save(contract, cancellationToken); } catch (ContractAlreadyExistsException e) { throw new ApiException(HttpStatusCode.Conflict, ErrorCodes.ContractAlreadyExists, e.Message); } catch (NestNotFoundException e) { throw new ApiException(HttpStatusCode.NotFound, ErrorCodes.NestNotFound, e.Message); } catch (LimitExceededException e) { throw new ApiException(HttpStatusCode.Conflict, ErrorCodes.LimitExceeded, e.Message); } return(Ok(await queryProcessor.Process <NestContractQuery, NestContractView>(new NestContractQuery( guildId: HttpContext.GetGuildId(), nestContractId: binding.Id), cancellationToken))); }
private async Task ExpireWorker(INestContractRepository repository, CancellationToken cancellationToken) { try { var contracts = await repository.GetAllUnprocessedExpired(cancellationToken); foreach (var contract in contracts) { if (cancellationToken.IsCancellationRequested) { return; } try { _logger.LogInformation($"ExpireWorker: Start process contract: {contract.Id}"); contract.MarkAsExpire(); await repository.Save(contract, cancellationToken); _logger.LogInformation($"ExpireWorker: End process contract: {contract.Id}"); } catch (Exception e) { _logger.LogError(e, $"ExpireWorker: {e.Message}"); } } } catch (Exception e) { _logger.LogError(e, $"ExpireWorker: {e.Message}"); } }
public async Task <ActionResult <NestContractView> > CloseContract( [FromRoute] Guid contractId, [FromServices] INestContractRepository repository, [FromServices] IQueryProcessor queryProcessor, CancellationToken cancellationToken) { var contract = await repository.Get(contractId, cancellationToken); if (contract == null || contract.UserId != HttpContext.GetUserId()) { throw new ApiException(HttpStatusCode.NotFound, ErrorCodes.ContractNotFound, $"Contract {contractId} fot found"); } contract.Close(); await repository.Save(contract, cancellationToken); return(Ok(await queryProcessor.Process <NestContractQuery, NestContractView>(new NestContractQuery( guildId: HttpContext.GetGuildId(), nestContractId: contractId), cancellationToken))); }