public async Task <IActionResult> AttachDocument( [FromRoute] Guid id, [FromServices] IOperationsRepository operationsRepository, [FromServices] DocumentSetter setter, [FromBody] AddOperationDocumentBinding binding, [FromServices] IQueryProcessor queryProcessor, CancellationToken cancellationToken) { var operation = await operationsRepository.Get(id, cancellationToken); if (operation == null) { throw new ApiException(HttpStatusCode.NotFound, ErrorCodes.OperationNotFound, $"Operation {id} not found"); } try{ switch (binding.Type) { case OperationType.Tax: await setter.SetTax(operation, binding.DocumentId, cancellationToken); break; case OperationType.Penalty: await setter.SetPenalty(operation, binding.DocumentId, cancellationToken); break; case OperationType.Loan: await setter.SetLoan(operation, binding.DocumentId, cancellationToken); break; default: throw new ApiException(HttpStatusCode.BadRequest, "Unsupported operation", "Unsupported operation"); } } catch (DocumentNotFoundException) { throw new ApiException(HttpStatusCode.NotFound, ErrorCodes.DocumentNotFound, $"Document {binding.DocumentId} of {binding.Type} not found"); } await operationsRepository.Save(operation, cancellationToken); return(Ok(await queryProcessor.Process <GetOperationQuery, OperationView>( new GetOperationQuery( guildId: HttpContext.GetGuildId(), operationId: id) , cancellationToken))); }
public async Task <IActionResult> AddNewOperation( [FromServices] IOperationsRepository operationsRepository, [FromServices] OperationCreator operationCreator, [FromBody] AddOperationBinding binding, [FromServices] IQueryProcessor queryProcessor, CancellationToken cancellationToken) { var operation = await operationsRepository.Get(binding.Id, cancellationToken); if (operation != null) { if (operation.Amount != binding.Amount || operation.UserId != binding.UserId || operation.DocumentId != binding.DocumentId || operation.GuildId != HttpContext.GetGuildId() || operation.Type != binding.Type) { throw new ApiException(HttpStatusCode.Conflict, ErrorCodes.OperationAlreadyExists, $"Operation {binding.Id} already exists", new { operation.Id, operation.Amount, operation.Description, operation.Type, operation.UserId }.ToDictionary()); } return(Ok(operation)); } try{ operation = await operationCreator.Create( binding.Id, HttpContext.GetGuildId(), binding.UserId, binding.DocumentId, binding.Amount, binding.Type, binding.Description, binding.ParentOperationId, cancellationToken); } catch (DocumentNotFoundException e) { throw new ApiException(HttpStatusCode.NotFound, ErrorCodes.DocumentNotFound, e.Message); } await operationsRepository.Save(operation, cancellationToken); return(Ok(await queryProcessor.Process <GetOperationQuery, OperationView>( new GetOperationQuery( guildId: HttpContext.GetGuildId(), operationId: binding.Id) , cancellationToken))); }