diff --git a/src/ApplicationCore/ApplicationCore.csproj b/src/ApplicationCore/ApplicationCore.csproj index 4eb43c5..8e8906c 100644 --- a/src/ApplicationCore/ApplicationCore.csproj +++ b/src/ApplicationCore/ApplicationCore.csproj @@ -8,6 +8,7 @@ + diff --git a/src/ApplicationCore/Entities/CatalogItem.cs b/src/ApplicationCore/Entities/CatalogItem.cs index 1dbdcf6..7fe0149 100644 --- a/src/ApplicationCore/Entities/CatalogItem.cs +++ b/src/ApplicationCore/Entities/CatalogItem.cs @@ -1,9 +1,11 @@ using Ardalis.GuardClauses; using Microsoft.eShopWeb.ApplicationCore.Interfaces; -using System.Security.Cryptography; +using System.Collections.Generic; namespace Microsoft.eShopWeb.ApplicationCore.Entities { + + public class CatalogItem : BaseEntity, IAggregateRoot { public string Name { get; private set; } @@ -30,18 +32,12 @@ namespace Microsoft.eShopWeb.ApplicationCore.Entities PictureUri = pictureUri; } - public void Update(string name, decimal price) - { - Guard.Against.NullOrEmpty(name, nameof(name)); - Name = name; - Price = price; - } - public void UpdateDetails(string name, string description, decimal price) { Guard.Against.NullOrEmpty(name, nameof(name)); Guard.Against.NullOrEmpty(description, nameof(description)); Guard.Against.NegativeOrZero(price, nameof(price)); + Name = name; Description = description; Price = price; diff --git a/src/ApplicationCore/Exceptions/DuplicateCatalogItemNameException.cs b/src/ApplicationCore/Exceptions/DuplicateCatalogItemNameException.cs new file mode 100644 index 0000000..09fe302 --- /dev/null +++ b/src/ApplicationCore/Exceptions/DuplicateCatalogItemNameException.cs @@ -0,0 +1,14 @@ +using System; + +namespace Microsoft.eShopWeb.ApplicationCore.Exceptions +{ + public class DuplicateCatalogItemNameException : Exception + { + public DuplicateCatalogItemNameException(string message, int duplicateItemId) : base(message) + { + DuplicateItemId = duplicateItemId; + } + + public int DuplicateItemId { get; } + } +} diff --git a/src/PublicApi/CatalogItemEndpoints/Update.cs b/src/PublicApi/CatalogItemEndpoints/Update.cs index 4a17074..0672006 100644 --- a/src/PublicApi/CatalogItemEndpoints/Update.cs +++ b/src/PublicApi/CatalogItemEndpoints/Update.cs @@ -4,8 +4,10 @@ using Microsoft.AspNetCore.Authorization; using Microsoft.AspNetCore.Mvc; using Microsoft.eShopWeb.ApplicationCore.Constants; using Microsoft.eShopWeb.ApplicationCore.Entities; +using Microsoft.eShopWeb.ApplicationCore.Exceptions; using Microsoft.eShopWeb.ApplicationCore.Interfaces; using Swashbuckle.AspNetCore.Annotations; +using System; using System.Threading.Tasks; namespace Microsoft.eShopWeb.PublicApi.CatalogItemEndpoints diff --git a/src/PublicApi/PublicApi.csproj b/src/PublicApi/PublicApi.csproj index fb7c28d..7b305d1 100644 --- a/src/PublicApi/PublicApi.csproj +++ b/src/PublicApi/PublicApi.csproj @@ -11,6 +11,8 @@ + + diff --git a/src/PublicApi/Startup.cs b/src/PublicApi/Startup.cs index 854565e..fc1d6db 100644 --- a/src/PublicApi/Startup.cs +++ b/src/PublicApi/Startup.cs @@ -1,6 +1,9 @@ +using System; using System.Collections.Generic; using System.Text; using AutoMapper; +using MediatR; + using Microsoft.AspNetCore.Authentication.Cookies; using Microsoft.AspNetCore.Authentication.JwtBearer; using Microsoft.AspNetCore.Builder; @@ -8,6 +11,7 @@ using Microsoft.AspNetCore.Hosting; using Microsoft.AspNetCore.Identity; using Microsoft.EntityFrameworkCore; using Microsoft.eShopWeb.ApplicationCore.Constants; +using Microsoft.eShopWeb.ApplicationCore.Entities; using Microsoft.eShopWeb.ApplicationCore.Interfaces; using Microsoft.eShopWeb.ApplicationCore.Services; using Microsoft.eShopWeb.Infrastructure.Data; @@ -119,9 +123,9 @@ namespace Microsoft.eShopWeb.PublicApi services.AddControllers(); + services.AddMediatR(typeof(CatalogItem).Assembly); services.AddAutoMapper(typeof(Startup).Assembly); - services.AddSwaggerGen(c => { c.SwaggerDoc("v1", new OpenApiInfo { Title = "My API", Version = "v1" }); @@ -157,7 +161,6 @@ namespace Microsoft.eShopWeb.PublicApi } }); }); - } // This method gets called by the runtime. Use this method to configure the HTTP request pipeline. diff --git a/src/Web/Services/CatalogItemViewModelService.cs b/src/Web/Services/CatalogItemViewModelService.cs index 8d93585..fad2852 100644 --- a/src/Web/Services/CatalogItemViewModelService.cs +++ b/src/Web/Services/CatalogItemViewModelService.cs @@ -18,7 +18,7 @@ namespace Microsoft.eShopWeb.Web.Services public async Task UpdateCatalogItem(CatalogItemViewModel viewModel) { var existingCatalogItem = await _catalogItemRepository.GetByIdAsync(viewModel.Id); - existingCatalogItem.Update(viewModel.Name, viewModel.Price); + existingCatalogItem.UpdateDetails(viewModel.Name, existingCatalogItem.Description, viewModel.Price); await _catalogItemRepository.UpdateAsync(existingCatalogItem); } } diff --git a/src/Web/Web.csproj b/src/Web/Web.csproj index 8dc02ce..c4185cd 100644 --- a/src/Web/Web.csproj +++ b/src/Web/Web.csproj @@ -25,8 +25,8 @@ - - + + diff --git a/tests/UnitTests/ApplicationCore/Entities/CatalogItemTests/UpdateDetails.cs b/tests/UnitTests/ApplicationCore/Entities/CatalogItemTests/UpdateDetails.cs index bef89b5..d1d4600 100644 --- a/tests/UnitTests/ApplicationCore/Entities/CatalogItemTests/UpdateDetails.cs +++ b/tests/UnitTests/ApplicationCore/Entities/CatalogItemTests/UpdateDetails.cs @@ -1,16 +1,12 @@ -using Microsoft.eShopWeb.ApplicationCore.Entities.OrderAggregate; -using System.Collections.Generic; -using Microsoft.eShopWeb.UnitTests.Builders; -using Xunit; +using Xunit; using Microsoft.eShopWeb.ApplicationCore.Entities; using System; -namespace Microsoft.eShopWeb.UnitTests.ApplicationCore.Entities.OrderTests +namespace Microsoft.eShopWeb.UnitTests.ApplicationCore.Entities.CatalogItemTests { public class UpdateDetails { private CatalogItem _testItem; - private decimal _testUnitPrice = 42m; private int _validTypeId = 1; private int _validBrandId = 2; private string _validDescription = "test description";