public async Task <PrizeBulkCreationResult> CreatePrizes(PrizeBulkCreationRequest prizeBulkCreationRequest) { var stringContent = await prizesApiClient.CreatePrizesBulkAsync(prizeBulkCreationRequest); var result = jsonConverter.DeserializeObject <PrizeBulkCreationResult>(stringContent); return(result); }
public async Task <ActionResult> InitPromotionAsync(PromotionRequest request) { //var result = Created(??); //The correct status code will be 201 ( created ) however we need to build a url // which stores the result of the method. This is out of scope but we can modify this later. var result = StatusCode(500); var customerCreateRequest = new CustomerCreateRequest { CustomerName = request.CustomerName }; //create customer var taskResult = await _customerRepository.Create(customerCreateRequest).ContinueWith(async taskResult => { var newCustomer = taskResult.Result; var distributionPerPrize = request.TotalAmount / request.NumberOfPrizes; //create prizes var prizeBulkCreationRequest = new PrizeBulkCreationRequest { CustomerId = newCustomer.CustomerId, TotalPrizes = request.NumberOfPrizes, DistributionPerPrize = distributionPerPrize }; //now the desicion to make here. // Do we want to create a list with {NumberOfPrizes} length or // delegate this to the prizes API. //to avoid blocking the current API we will delegate this to the prizes API. var prizesBulkCreationResponse = await _prizesRepository.CreatePrizes(prizeBulkCreationRequest); }); if (!taskResult.IsFaulted) { result = Ok(); } return(result); }
public async Task <ActionResult> BulkInsertPrizesAsync(PrizeBulkCreationRequest request) { // there are so many ways we can handle this: // * Queue // * Kafka // * etc //for the moment it will go with a simple range add var listOfPrizes = new List <DTO.Prize>(); for (int i = 0; i < request.TotalPrizes; i++) { var p = new DTO.Prize { //id is autogenerated Amount = request.DistributionPerPrize, Name = $"Name = {i}", CustomerId = request.CustomerId, Status = StatusEnum.NOT_INITIALIZED }; listOfPrizes.Add(p); } await _prizeRepository.BulkInsertPrizes(listOfPrizes); return(Ok()); //it really shoud return Accepted() if we ever move this to a queue or kafka. /* * https://tools.ietf.org/html/rfc7231#section-6.3.3 * * The 202 (Accepted) status code indicates that the request has been * accepted for processing, but the processing has not been completed. * The request might or might not eventually be acted upon, as it might * be disallowed when processing actually takes place. There is no * facility in HTTP for re-sending a status code from an asynchronous * operation. */ }
public async Task <string> CreatePrizesBulkAsync(PrizeBulkCreationRequest prizeBulkCreationRequest) { return(await DoPostAsync(prizeBulkCreationRequest)); }