public async Task UpdateCategoryAsync(Entities.Category updatedCategory) { var requestMessage = new HttpRequestMessage(HttpMethod.Post, "Inventory/UpdateCategory"); requestMessage.Headers.Add(HttpHeaderName.Authorization, authenticationToken); requestMessage.Content = new FormUrlEncodedContent(new[] { new KeyValuePair <string, string>("category", JsonSerializer.Serialize(updatedCategory)) }); var response = await httpClient.SendAsync(requestMessage); if (response.StatusCode == HttpStatusCode.NoContent) { return; } var responseContent = await response.Content.ReadAsStringAsync(); throw new WebApiKnownException(response.StatusCode, responseContent); }
public Task UpdateCategoryAsync(Entities.Category updatedCategory) { var categoryToUpdate = categories.FirstOrDefault(c => c.Id == updatedCategory.CategoryId); if (categoryToUpdate is null) { throw new WebApiKnownException( HttpStatusCode.BadRequest, "A category with such ID doesn't exist."); } if (categories.Exists(c => string.Equals(c.Name, updatedCategory.CategoryName, StringComparison.OrdinalIgnoreCase))) { throw new WebApiKnownException( HttpStatusCode.BadRequest, "A category with the same name already exists. Use another name."); } categoryToUpdate.Name = updatedCategory.CategoryName; return(Task.CompletedTask); }