Skip to content

Commit

Permalink
Add catalogitem update endpoint to public api (dotnet-architecture#418)
Browse files Browse the repository at this point in the history
* Initial update endpoint working

* Updated CatalogItem to support more updates; added tests

* Got MediatR domain events working to check for duplicate item names

* Adding reference link

* Remove domain events spike code

* clean up usings
  • Loading branch information
ardalis authored Jul 10, 2020
1 parent f89c20e commit 1c75f08
Show file tree
Hide file tree
Showing 13 changed files with 205 additions and 7 deletions.
1 change: 1 addition & 0 deletions src/ApplicationCore/ApplicationCore.csproj
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@
<ItemGroup>
<PackageReference Include="Ardalis.GuardClauses" Version="1.5.0" />
<PackageReference Include="Ardalis.Specification" Version="3.0.0" />
<PackageReference Include="MediatR" Version="8.0.2" />
<PackageReference Include="System.Security.Claims" Version="4.3.0" />
<PackageReference Include="System.Text.Json" Version="4.7.2" />
</ItemGroup>
Expand Down
28 changes: 26 additions & 2 deletions src/ApplicationCore/Entities/CatalogItem.cs
Original file line number Diff line number Diff line change
@@ -1,8 +1,11 @@
using Ardalis.GuardClauses;
using Microsoft.eShopWeb.ApplicationCore.Interfaces;
using System.Collections.Generic;

namespace Microsoft.eShopWeb.ApplicationCore.Entities
{


public class CatalogItem : BaseEntity, IAggregateRoot
{
public string Name { get; private set; }
Expand All @@ -14,7 +17,12 @@ public class CatalogItem : BaseEntity, IAggregateRoot
public int CatalogBrandId { get; private set; }
public CatalogBrand CatalogBrand { get; private set; }

public CatalogItem(int catalogTypeId, int catalogBrandId, string description, string name, decimal price, string pictureUri)
public CatalogItem(int catalogTypeId,
int catalogBrandId,
string description,
string name,
decimal price,
string pictureUri)
{
CatalogTypeId = catalogTypeId;
CatalogBrandId = catalogBrandId;
Expand All @@ -24,11 +32,27 @@ public CatalogItem(int catalogTypeId, int catalogBrandId, string description, st
PictureUri = pictureUri;
}

public void Update(string name, decimal 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;
}

public void UpdateBrand(int catalogBrandId)
{
Guard.Against.Zero(catalogBrandId, nameof(catalogBrandId));
CatalogBrandId = catalogBrandId;
}

public void UpdateType(int catalogTypeId)
{
Guard.Against.Zero(catalogTypeId, nameof(catalogTypeId));
CatalogTypeId = catalogTypeId;
}
}
}
Original file line number Diff line number Diff line change
@@ -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; }
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -9,4 +9,5 @@ public class CreateCatalogItemRequest : BaseRequest
public string PictureUri { get; set; }
public decimal Price { get; set; }
}

}
1 change: 1 addition & 0 deletions src/PublicApi/CatalogItemEndpoints/Create.cs
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,7 @@

namespace Microsoft.eShopWeb.PublicApi.CatalogItemEndpoints
{

[Authorize(Roles = AuthorizationConstants.Roles.ADMINISTRATORS, AuthenticationSchemes = JwtBearerDefaults.AuthenticationScheme)]
public class Create : BaseAsyncEndpoint<CreateCatalogItemRequest, CreateCatalogItemResponse>
{
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,21 @@
using System.ComponentModel.DataAnnotations;

namespace Microsoft.eShopWeb.PublicApi.CatalogItemEndpoints
{
public class UpdateCatalogItemRequest : BaseRequest
{
[Range(1, 10000)]
public int Id { get; set; }
[Range(1, 10000)]
public int CatalogBrandId { get; set; }
[Range(1, 10000)]
public int CatalogTypeId { get; set; }
[Required]
public string Description { get; set; }
[Required]
public string Name { get; set; }
public string PictureUri { get; set; }
[Range(0.01, 10000)]
public decimal Price { get; set; }
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,17 @@
using System;

namespace Microsoft.eShopWeb.PublicApi.CatalogItemEndpoints
{
public class UpdateCatalogItemResponse : BaseResponse
{
public UpdateCatalogItemResponse(Guid correlationId) : base(correlationId)
{
}

public UpdateCatalogItemResponse()
{
}

public CatalogItemDto CatalogItem { get; set; }
}
}
58 changes: 58 additions & 0 deletions src/PublicApi/CatalogItemEndpoints/Update.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,58 @@
using Ardalis.ApiEndpoints;
using Microsoft.AspNetCore.Authentication.JwtBearer;
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
{
[Authorize(Roles = AuthorizationConstants.Roles.ADMINISTRATORS, AuthenticationSchemes = JwtBearerDefaults.AuthenticationScheme)]
public class Update : BaseAsyncEndpoint<UpdateCatalogItemRequest, UpdateCatalogItemResponse>
{
private readonly IAsyncRepository<CatalogItem> _itemRepository;

public Update(IAsyncRepository<CatalogItem> itemRepository)
{
_itemRepository = itemRepository;
}

[HttpPut("api/catalog-items")]
[SwaggerOperation(
Summary = "Updates a Catalog Item",
Description = "Updates a Catalog Item",
OperationId = "catalog-items.update",
Tags = new[] { "CatalogItemEndpoints" })
]
public override async Task<ActionResult<UpdateCatalogItemResponse>> HandleAsync(UpdateCatalogItemRequest request)
{
var response = new UpdateCatalogItemResponse(request.CorrelationId());

var existingItem = await _itemRepository.GetByIdAsync(request.Id);

existingItem.UpdateDetails(request.Name, request.Description, request.Price);
existingItem.UpdateBrand(request.CatalogBrandId);
existingItem.UpdateType(request.CatalogTypeId);

await _itemRepository.UpdateAsync(existingItem);

var dto = new CatalogItemDto
{
Id = existingItem.Id,
CatalogBrandId = existingItem.CatalogBrandId,
CatalogTypeId = existingItem.CatalogTypeId,
Description = existingItem.Description,
Name = existingItem.Name,
PictureUri = existingItem.PictureUri,
Price = existingItem.Price
};
response.CatalogItem = dto;
return response;
}
}
}
2 changes: 2 additions & 0 deletions src/PublicApi/PublicApi.csproj
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,8 @@
<ItemGroup>
<PackageReference Include="Ardalis.ApiEndpoints" Version="1.0.0" />
<PackageReference Include="AutoMapper.Extensions.Microsoft.DependencyInjection" Version="7.0.0" />
<PackageReference Include="MediatR" Version="8.0.2" />
<PackageReference Include="MediatR.Extensions.Microsoft.DependencyInjection" Version="8.0.1" />
<PackageReference Include="Swashbuckle.AspNetCore" Version="5.5.0" />
<PackageReference Include="Swashbuckle.AspNetCore.SwaggerUI" Version="5.5.0" />
<PackageReference Include="Swashbuckle.AspNetCore.Annotations" Version="5.5.0" />
Expand Down
7 changes: 5 additions & 2 deletions src/PublicApi/Startup.cs
Original file line number Diff line number Diff line change
@@ -1,13 +1,17 @@
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;
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;
Expand Down Expand Up @@ -119,9 +123,9 @@ public void ConfigureServices(IServiceCollection services)


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" });
Expand Down Expand Up @@ -157,7 +161,6 @@ public void ConfigureServices(IServiceCollection services)
}
});
});

}

// This method gets called by the runtime. Use this method to configure the HTTP request pipeline.
Expand Down
2 changes: 1 addition & 1 deletion src/Web/Services/CatalogItemViewModelService.cs
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,7 @@ public CatalogItemViewModelService(IAsyncRepository<CatalogItem> catalogItemRepo
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);
}
}
Expand Down
4 changes: 2 additions & 2 deletions src/Web/Web.csproj
Original file line number Diff line number Diff line change
Expand Up @@ -25,8 +25,8 @@
<PackageReference Include="Ardalis.Specification" Version="3.0.0" />
<PackageReference Include="AutoMapper.Extensions.Microsoft.DependencyInjection" Version="7.0.0" />

<PackageReference Include="MediatR" Version="8.0.1" />
<PackageReference Include="MediatR.Extensions.Microsoft.DependencyInjection" Version="8.0.0" />
<PackageReference Include="MediatR" Version="8.0.2" />
<PackageReference Include="MediatR.Extensions.Microsoft.DependencyInjection" Version="8.0.1" />
<PackageReference Include="BuildBundlerMinifier" Version="2.9.406" Condition="'$(Configuration)'=='Release'" PrivateAssets="All" />
<PackageReference Include="Microsoft.CodeAnalysis" Version="3.6.0" />
<PackageReference Include="Microsoft.EntityFrameworkCore.InMemory" Version="3.1.5" />
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,56 @@
using Xunit;
using Microsoft.eShopWeb.ApplicationCore.Entities;
using System;

namespace Microsoft.eShopWeb.UnitTests.ApplicationCore.Entities.CatalogItemTests
{
public class UpdateDetails
{
private CatalogItem _testItem;
private int _validTypeId = 1;
private int _validBrandId = 2;
private string _validDescription = "test description";
private string _validName = "test name";
private decimal _validPrice = 1.23m;
private string _validUri = "/123";

public UpdateDetails()
{
_testItem = new CatalogItem(_validTypeId, _validBrandId, _validDescription, _validName, _validPrice, _validUri);
}

[Fact]
public void ThrowsArgumentExceptionGivenEmptyName()
{
string newValue = "";
Assert.Throws<ArgumentException>(() => _testItem.UpdateDetails(newValue, _validDescription, _validPrice));
}

[Fact]
public void ThrowsArgumentExceptionGivenEmptyDescription()
{
string newValue = "";
Assert.Throws<ArgumentException>(() => _testItem.UpdateDetails(_validName, newValue, _validPrice));
}

[Fact]
public void ThrowsArgumentNullExceptionGivenNullName()
{
Assert.Throws<ArgumentNullException>(() => _testItem.UpdateDetails(null, _validDescription, _validPrice));
}

[Fact]
public void ThrowsArgumentNullExceptionGivenNullDescription()
{
Assert.Throws<ArgumentNullException>(() => _testItem.UpdateDetails(_validName, null, _validPrice));
}

[Theory]
[InlineData(0)]
[InlineData(-1.23)]
public void ThrowsArgumentExceptionGivenNonPositivePrice(decimal newPrice)
{
Assert.Throws<ArgumentException>(() => _testItem.UpdateDetails(_validName, _validDescription, newPrice));
}
}
}

0 comments on commit 1c75f08

Please sign in to comment.