public async Task <IActionResult> UpdateConsumer([FromQuery] string Id, [FromBody] ConsumerRequestUpdate Request) { try { var guid = Guid.Parse(Id); if (guid.ToString() != Id) { return(BadRequest("The consumer Id specified in the body is different from the one specified in the URL.")); } var result = await _dataProvider.ConsumerUpdate(Request.ToConsumer()); return(Ok(result)); } catch (FormatException) { return(BadRequest("Please specify a valid guid for the consumer.")); } catch (EntityNotFoundException exc) { return(NotFound(exc.Message)); } catch (Exception) { throw; } }
public async Task Should_Add_And_Update_A_Consumer() { var consumer = new ConsumerRequestAdd() { Address = "Some address", Email = "*****@*****.**", FirstName = "First name", LastName = "Last name", Devices = new List <Device>() }; //------------------------------------------- var controller = new ConsumersController(fixture.GetDataProvider()); var added = await controller.AddConsumer(consumer) as OkObjectResult; Assert.NotNull(added.Value); Assert.Equal(added.StatusCode, (int)HttpStatusCode.OK); var foundItem = (await controller.GetConsumer((added.Value as Consumer).ConsumerId.ToString()) as OkObjectResult).Value as Consumer; Assert.Equal(foundItem, added.Value); var consumerUpdate = new ConsumerRequestUpdate() { Id = foundItem.ConsumerId.ToString(), Address = "Some address updated", Email = "[email protected] updated", FirstName = "First name updated", LastName = "Last name updated", Devices = new List <Device>() }; var updated = await controller.UpdateConsumer(consumerUpdate.Id, consumerUpdate) as OkObjectResult; var updatedItem = (await controller.GetConsumer((updated.Value as Consumer).ConsumerId.ToString()) as OkObjectResult).Value as Consumer; Assert.Equal("Some address updated", updatedItem.Address); Assert.Equal("[email protected] updated", updatedItem.Email); Assert.Equal("First name updated", updatedItem.FirstName); Assert.Equal("Last name updated", updatedItem.LastName); Assert.Empty(updatedItem.Devices); }