Skip to content

Commit 2911d58

Browse files
committed
Updated CatalogItem to support more updates; added tests
1 parent 14fbb42 commit 2911d58

File tree

4 files changed

+101
-4
lines changed

4 files changed

+101
-4
lines changed

src/ApplicationCore/Entities/CatalogItem.cs

+29-1
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,6 @@
11
using Ardalis.GuardClauses;
22
using Microsoft.eShopWeb.ApplicationCore.Interfaces;
3+
using System.Security.Cryptography;
34

45
namespace Microsoft.eShopWeb.ApplicationCore.Entities
56
{
@@ -14,7 +15,12 @@ public class CatalogItem : BaseEntity, IAggregateRoot
1415
public int CatalogBrandId { get; private set; }
1516
public CatalogBrand CatalogBrand { get; private set; }
1617

17-
public CatalogItem(int catalogTypeId, int catalogBrandId, string description, string name, decimal price, string pictureUri)
18+
public CatalogItem(int catalogTypeId,
19+
int catalogBrandId,
20+
string description,
21+
string name,
22+
decimal price,
23+
string pictureUri)
1824
{
1925
CatalogTypeId = catalogTypeId;
2026
CatalogBrandId = catalogBrandId;
@@ -30,5 +36,27 @@ public void Update(string name, decimal price)
3036
Name = name;
3137
Price = price;
3238
}
39+
40+
public void UpdateDetails(string name, string description, decimal price)
41+
{
42+
Guard.Against.NullOrEmpty(name, nameof(name));
43+
Guard.Against.NullOrEmpty(description, nameof(description));
44+
Guard.Against.NegativeOrZero(price, nameof(price));
45+
Name = name;
46+
Description = description;
47+
Price = price;
48+
}
49+
50+
public void UpdateBrand(int catalogBrandId)
51+
{
52+
Guard.Against.Zero(catalogBrandId, nameof(catalogBrandId));
53+
CatalogBrandId = catalogBrandId;
54+
}
55+
56+
public void UpdateType(int catalogTypeId)
57+
{
58+
Guard.Against.Zero(catalogTypeId, nameof(catalogTypeId));
59+
CatalogTypeId = catalogTypeId;
60+
}
3361
}
3462
}
Original file line numberDiff line numberDiff line change
@@ -1,14 +1,21 @@
1-
namespace Microsoft.eShopWeb.PublicApi.CatalogItemEndpoints
1+
using System.ComponentModel.DataAnnotations;
2+
3+
namespace Microsoft.eShopWeb.PublicApi.CatalogItemEndpoints
24
{
35
public class UpdateCatalogItemRequest : BaseRequest
46
{
7+
[Range(1, 10000)]
58
public int Id { get; set; }
9+
[Range(1, 10000)]
610
public int CatalogBrandId { get; set; }
11+
[Range(1, 10000)]
712
public int CatalogTypeId { get; set; }
13+
[Required]
814
public string Description { get; set; }
15+
[Required]
916
public string Name { get; set; }
1017
public string PictureUri { get; set; }
18+
[Range(0.01, 10000)]
1119
public decimal Price { get; set; }
1220
}
13-
1421
}

src/PublicApi/CatalogItemEndpoints/Update.cs

+3-1
Original file line numberDiff line numberDiff line change
@@ -33,7 +33,9 @@ public override async Task<ActionResult<UpdateCatalogItemResponse>> HandleAsync(
3333

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

36-
existingItem.Update(request.Name, request.Price);
36+
existingItem.UpdateDetails(request.Name, request.Description, request.Price);
37+
existingItem.UpdateBrand(request.CatalogBrandId);
38+
existingItem.UpdateType(request.CatalogTypeId);
3739

3840
await _itemRepository.UpdateAsync(existingItem);
3941

Original file line numberDiff line numberDiff line change
@@ -0,0 +1,60 @@
1+
using Microsoft.eShopWeb.ApplicationCore.Entities.OrderAggregate;
2+
using System.Collections.Generic;
3+
using Microsoft.eShopWeb.UnitTests.Builders;
4+
using Xunit;
5+
using Microsoft.eShopWeb.ApplicationCore.Entities;
6+
using System;
7+
8+
namespace Microsoft.eShopWeb.UnitTests.ApplicationCore.Entities.OrderTests
9+
{
10+
public class UpdateDetails
11+
{
12+
private CatalogItem _testItem;
13+
private decimal _testUnitPrice = 42m;
14+
private int _validTypeId = 1;
15+
private int _validBrandId = 2;
16+
private string _validDescription = "test description";
17+
private string _validName = "test name";
18+
private decimal _validPrice = 1.23m;
19+
private string _validUri = "/123";
20+
21+
public UpdateDetails()
22+
{
23+
_testItem = new CatalogItem(_validTypeId, _validBrandId, _validDescription, _validName, _validPrice, _validUri);
24+
}
25+
26+
[Fact]
27+
public void ThrowsArgumentExceptionGivenEmptyName()
28+
{
29+
string newValue = "";
30+
Assert.Throws<ArgumentException>(() => _testItem.UpdateDetails(newValue, _validDescription, _validPrice));
31+
}
32+
33+
[Fact]
34+
public void ThrowsArgumentExceptionGivenEmptyDescription()
35+
{
36+
string newValue = "";
37+
Assert.Throws<ArgumentException>(() => _testItem.UpdateDetails(_validName, newValue, _validPrice));
38+
}
39+
40+
[Fact]
41+
public void ThrowsArgumentNullExceptionGivenNullName()
42+
{
43+
Assert.Throws<ArgumentNullException>(() => _testItem.UpdateDetails(null, _validDescription, _validPrice));
44+
}
45+
46+
[Fact]
47+
public void ThrowsArgumentNullExceptionGivenNullDescription()
48+
{
49+
Assert.Throws<ArgumentNullException>(() => _testItem.UpdateDetails(_validName, null, _validPrice));
50+
}
51+
52+
[Theory]
53+
[InlineData(0)]
54+
[InlineData(-1.23)]
55+
public void ThrowsArgumentExceptionGivenNonPositivePrice(decimal newPrice)
56+
{
57+
Assert.Throws<ArgumentException>(() => _testItem.UpdateDetails(_validName, _validDescription, newPrice));
58+
}
59+
}
60+
}

0 commit comments

Comments
 (0)