public async Task <ActionResult> counter_callback([FromBody] CounterCallback counter) { if (counter == null) { return(BadRequest("Please inform counter details")); } var villageName = await _externalAPI.GetVillageNameAsync(counter.Counter_id); var consumptionCounter = new ConsumptionCounter(villageName, counter.Counter_id, counter.Amount); if (consumptionCounter.IsValid) { try { await _repository.RegisterAsync(consumptionCounter); return(Ok(consumptionCounter)); } catch (ConsumptionCounterAlreadyIsRegisteredException) { return(BadRequest("The counter already is registered.")); } } else { return(BadRequest("The counter details supplied are invalid")); } }
public async Task RegisterAsync(ConsumptionCounter consumptionCounter) { var exists = await ChecIfEntityExists(consumptionCounter.VillageName, consumptionCounter.CounterId); if (exists) { throw new ConsumptionCounterAlreadyIsRegisteredException(); } var entity = new ConsumptionCounterEntity(consumptionCounter.VillageName, consumptionCounter.CounterId); entity.Amount = consumptionCounter.Amount; var insertEntityOperation = TableOperation.Insert(entity); await _consumptionCounterTable.ExecuteAsync(insertEntityOperation); }
public async Task counter_callback_returns_ok_when_valid_input() { var counter = new CounterCallback("1", 10000.123); _mockExternalAPI.Setup(api => api.GetVillageNameAsync(counter.Counter_id)) .Returns(Task.FromResult(GetVillageName(counter.Counter_id))); var expectedResultObject = new ConsumptionCounter(GetVillageName(counter.Counter_id), counter.Counter_id, counter.Amount); _mockRepo.Setup(repo => repo.RegisterAsync(expectedResultObject)).Returns(RegisterAsync()); var controller = new ConsumptionCounterController(_mockRepo.Object, _mockExternalAPI.Object); var result = await controller.counter_callback(counter); var content = ((OkObjectResult)result).Value; result.Should().BeOfType <OkObjectResult>(); content.ShouldBeEquivalentTo(expectedResultObject); }
public async Task CloudRepositoryIsValidAsync() { var villageName = Guid.NewGuid().ToString(); var consumptionCounter = new ConsumptionCounter(villageName, Guid.NewGuid().ToString(), 1000.123); var cutOffDate = DateTime.Now.AddDays(-24); bool isValid = false; await Task.Run(async() => { await _repository.RegisterAsync(consumptionCounter); await _repository.GetByCutOffDateAsync(cutOffDate); try { await _repository.RegisterAsync(consumptionCounter); } catch (ConsumptionCounterAlreadyIsRegisteredException) { isValid = true; } }); isValid.Should().BeTrue(); }