public async void DeleteIngredients_WhenExistsInDB(string url) { //Act IngredientRespondModel testIngredient = new IngredientRespondModel() { Title = "Test_Ingredient_1", Description = "This is a Test value", Owner = "testUser" }; var stringIngredient = await Task.Run(() => JsonConvert.SerializeObject(testIngredient)); var httpContent = new StringContent(stringIngredient, Encoding.UTF8, "application/json"); var postResponse = await Client.PostAsync(url, httpContent); //Assert postResponse.StatusCode.Should().Be(HttpStatusCode.Created); var getResponse = await Client.GetAsync(url); getResponse.StatusCode.Should().Be(HttpStatusCode.OK); (await getResponse.Content.ReadAsAsync <List <IngredientRespondModel> >()).Should().HaveCount(1); var deleteResponse = await Client.DeleteAsync($"{url}/1"); deleteResponse.StatusCode.Should().Be(HttpStatusCode.OK); getResponse = await Client.GetAsync(url); getResponse.StatusCode.Should().Be(HttpStatusCode.OK); (await getResponse.Content.ReadAsAsync <List <IngredientRespondModel> >()).Should().HaveCount(0); }
public async Task <IActionResult> UpdateIngredient([FromRoute] int id, [FromBody] IngredientRespondModel ingredient) { if (!ModelState.IsValid) { return(BadRequest(ModelState)); } var existingIngredient = await _context.Ingredients.FindAsync(id); existingIngredient.Title = ingredient.Title; existingIngredient.Description = ingredient.Description; _context.Entry(existingIngredient).State = EntityState.Modified; try { await _context.SaveChangesAsync(); } catch (DbUpdateConcurrencyException) { if (!IngredientExists(id)) { return(NotFound()); } else { throw; } } return(NoContent()); }
private IngredientRespondModel InitializeIngredientRespondModel(Ingredient ingredient) { var ingredientRespondModel = new IngredientRespondModel { Title = ingredient.Title, Description = ingredient.Description, Owner = _context.Users.FirstOrDefault(a => a.UserId.Equals(ingredient.UserId))?.UserName }; return(ingredientRespondModel); }
public async Task <IActionResult> CreateIngredient([FromBody] IngredientRespondModel ingredientRespondModel) { if (!ModelState.IsValid) { return(BadRequest(ModelState)); } var ingredient = new Ingredient() { Title = ingredientRespondModel.Title, Description = ingredientRespondModel.Description }; _context.Ingredients.Add(ingredient); await _context.SaveChangesAsync(); return(CreatedAtAction("GetIngredientByID", new { id = ingredient.IngredientId }, ingredient)); }
public async void UpdateIngredients_WhenExistsInDB(string url) { //Act IngredientRespondModel testIngredient = new IngredientRespondModel() { Title = "Test_Ingredient_1", Description = "This is a Test value", Owner = "testUser" }; var stringIngredient = await Task.Run(() => JsonConvert.SerializeObject(testIngredient)); var httpContent = new StringContent(stringIngredient, Encoding.UTF8, "application/json"); var postResponse = await Client.PostAsync(url, httpContent); //Assert postResponse.StatusCode.Should().Be(HttpStatusCode.Created); var response = await Client.GetAsync(url); response.EnsureSuccessStatusCode(); string responseBody = await response.Content.ReadAsStringAsync(); var existingItems = JsonConvert.DeserializeObject <List <IngredientRespondModel> >(responseBody); existingItems.First().Title = "Test_Ingredient_2"; existingItems.First().Description = "This is a Test value2"; stringIngredient = await Task.Run(() => JsonConvert.SerializeObject(existingItems.First())); httpContent = new StringContent(stringIngredient, Encoding.UTF8, "application/json"); var putResponse = await Client.PutAsync($"{url}/1", httpContent); putResponse.StatusCode.Should().Be(HttpStatusCode.NoContent); response = await Client.GetAsync($"{url}/1"); response.EnsureSuccessStatusCode(); responseBody = await response.Content.ReadAsStringAsync(); var existingItem = JsonConvert.DeserializeObject <IngredientRespondModel>(responseBody); Assert.Equal("Test_Ingredient_2", existingItem.Title); Assert.Equal("This is a Test value2", existingItem.Description); }