Skip to content

Commit

Permalink
Removed GuardClasses package
Browse files Browse the repository at this point in the history
Updated docker image to 21.6
  • Loading branch information
oskardudycz committed Jul 20, 2021
1 parent eabdead commit 055e7e2
Show file tree
Hide file tree
Showing 26 changed files with 126 additions and 104 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -21,7 +21,7 @@ public class InitCartFixture: ApiFixture<Startup>

public override async Task InitializeAsync()
{
CommandResponse = await Post(new InitializeCartRequest {ClientId = ClientId });
CommandResponse = await Post(new InitializeCartRequest(ClientId));
}
}

Expand Down
6 changes: 0 additions & 6 deletions CQRS_Flow/.NET/Carts/Carts.Api/Carts.Api.csproj
Original file line number Diff line number Diff line change
Expand Up @@ -9,12 +9,6 @@
<ItemGroup>
<PackageReference Include="Swashbuckle.AspNetCore" Version="6.1.4" />
<PackageReference Include="Microsoft.AspNetCore.Mvc.NewtonsoftJson" Version="5.0.7" />
<PackageReference Include="Microsoft.EntityFrameworkCore.Design" Version="5.0.7">
<PrivateAssets>all</PrivateAssets>
<IncludeAssets>runtime; build; native; contentfiles; analyzers; buildtransitive</IncludeAssets>
</PackageReference>
<PackageReference Include="Microsoft.EntityFrameworkCore" Version="5.0.7" />
<PackageReference Include="Microsoft.EntityFrameworkCore.InMemory" Version="5.0.7" />
</ItemGroup>

<ItemGroup>
Expand Down
38 changes: 18 additions & 20 deletions CQRS_Flow/.NET/Carts/Carts.Api/Controllers/CartsController.cs
Original file line number Diff line number Diff line change
@@ -1,7 +1,6 @@
using System;
using System.Collections.Generic;
using System.Threading.Tasks;
using Ardalis.GuardClauses;
using Carts.Api.Requests.Carts;
using Carts.Carts.GettingCartAtVersion;
using Carts.Carts.GettingCartById;
Expand All @@ -27,19 +26,16 @@ public CartsController(
IQueryBus queryBus,
IIdGenerator idGenerator)
{
Guard.Against.Null(commandBus, nameof(commandBus));
Guard.Against.Null(queryBus, nameof(queryBus));
Guard.Against.Null(idGenerator, nameof(idGenerator));

this.commandBus = commandBus;
this.queryBus = queryBus;
this.idGenerator = idGenerator;
}

[HttpPost]
public async Task<IActionResult> InitializeCart([FromBody] InitializeCartRequest request)
public async Task<IActionResult> InitializeCart([FromBody] InitializeCartRequest? request)
{
Guard.Against.Null(request, nameof(request));
if (request == null)
throw new ArgumentNullException(nameof(request));

var cartId = idGenerator.New();

Expand All @@ -54,16 +50,16 @@ public async Task<IActionResult> InitializeCart([FromBody] InitializeCartRequest
}

[HttpPost("{id}/products")]
public async Task<IActionResult> AddProduct(Guid id, [FromBody] AddProductRequest request)
public async Task<IActionResult> AddProduct(Guid id, [FromBody] AddProductRequest? request)
{
Guard.Against.Null(request, nameof(request));
Guard.Against.Null(request.ProductItem, nameof(request));
if (request == null)
throw new ArgumentNullException(nameof(request));

var command = Carts.AddingProduct.AddProduct.Create(
id,
ProductItem.Create(
request.ProductItem.ProductId,
request.ProductItem.Quantity
request.ProductItem?.ProductId,
request.ProductItem?.Quantity
)
);

Expand All @@ -73,17 +69,17 @@ public async Task<IActionResult> AddProduct(Guid id, [FromBody] AddProductReques
}

[HttpDelete("{id}/products")]
public async Task<IActionResult> RemoveProduct(Guid id, [FromBody] RemoveProductRequest request)
public async Task<IActionResult> RemoveProduct(Guid id, [FromBody] RemoveProductRequest? request)
{
Guard.Against.Null(request, nameof(request));
Guard.Against.Null(request.ProductItem, nameof(request));
if (request == null)
throw new ArgumentNullException(nameof(request));

var command = Carts.RemovingProduct.RemoveProduct.Create(
id,
PricedProductItem.Create(
request.ProductItem.ProductId,
request.ProductItem.Quantity,
request.ProductItem.UnitPrice
request.ProductItem?.ProductId,
request.ProductItem?.Quantity,
request.ProductItem?.UnitPrice
)
);

Expand Down Expand Up @@ -124,9 +120,11 @@ public Task<IReadOnlyList<CartHistory>> GetHistory(Guid id)
}

[HttpGet("{id}/versions")]
public Task<CartDetails> GetVersion(Guid id, [FromQuery] GetCartAtVersion query)
public Task<CartDetails> GetVersion(Guid id, [FromQuery] GetCartAtVersion? query)
{
Guard.Against.Null(query, nameof(query));
if (query == null)
throw new ArgumentNullException(nameof(query));

return queryBus.Send<GetCartAtVersion, CartDetails>(GetCartAtVersion.Create(id, query.Version));
}
}
Expand Down

This file was deleted.

Original file line number Diff line number Diff line change
Expand Up @@ -2,8 +2,7 @@

namespace Carts.Api.Requests.Carts
{
public class InitializeCartRequest
{
public Guid ClientId { get; set; }
}
public record InitializeCartRequest(
Guid? ClientId
);
}
Original file line number Diff line number Diff line change
Expand Up @@ -2,10 +2,8 @@

namespace Carts.Api.Requests.Carts
{
public class ProductItemRequest
{
public Guid ProductId { get; set; }

public int Quantity { get; set; }
}
public record ProductItemRequest(
Guid? ProductId,
int? Quantity
);
}
1 change: 0 additions & 1 deletion CQRS_Flow/.NET/Carts/Carts/Carts.csproj
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,6 @@
</ItemGroup>

<ItemGroup>
<PackageReference Include="Ardalis.GuardClauses" Version="3.2.0" />
<PackageReference Include="Microsoft.Extensions.Configuration.Abstractions" Version="5.0.0" />
</ItemGroup>

Expand Down
6 changes: 2 additions & 4 deletions CQRS_Flow/.NET/Carts/Carts/Carts/AddingProduct/AddProduct.cs
Original file line number Diff line number Diff line change
@@ -1,7 +1,6 @@
using System;
using System.Threading;
using System.Threading.Tasks;
using Ardalis.GuardClauses;
using Carts.Carts.Products;
using Carts.Pricing;
using Core.Commands;
Expand All @@ -12,7 +11,6 @@ namespace Carts.Carts.AddingProduct
{
public class AddProduct: ICommand
{

public Guid CartId { get; }

public ProductItem ProductItem { get; }
Expand All @@ -24,8 +22,8 @@ private AddProduct(Guid cartId, ProductItem productItem)
}
public static AddProduct Create(Guid cartId, ProductItem productItem)
{
Guard.Against.Default(cartId, nameof(cartId));
Guard.Against.Null(productItem, nameof(productItem));
if (cartId == Guid.Empty)
throw new ArgumentOutOfRangeException(nameof(cartId));

return new AddProduct(cartId, productItem);
}
Expand Down
Original file line number Diff line number Diff line change
@@ -1,5 +1,4 @@
using System;
using Ardalis.GuardClauses;
using Carts.Carts.Products;
using Core.Events;

Expand All @@ -19,8 +18,8 @@ private ProductAdded(Guid cartId, PricedProductItem productItem)

public static ProductAdded Create(Guid cartId, PricedProductItem productItem)
{
Guard.Against.Default(cartId, nameof(cartId));
Guard.Against.Null(productItem, nameof(productItem));
if (cartId == Guid.Empty)
throw new ArgumentOutOfRangeException(nameof(cartId));

return new ProductAdded(cartId, productItem);
}
Expand Down
8 changes: 1 addition & 7 deletions CQRS_Flow/.NET/Carts/Carts/Carts/Cart.cs
Original file line number Diff line number Diff line change
@@ -1,7 +1,6 @@
using System;
using System.Collections.Generic;
using System.Linq;
using Ardalis.GuardClauses;
using Carts.Carts.AddingProduct;
using Carts.Carts.ConfirmingCart;
using Carts.Carts.InitializingCart;
Expand Down Expand Up @@ -31,7 +30,7 @@ public static Cart Initialize(
}

private Cart(){}

public override void When(object @event)
{
switch (@event)
Expand Down Expand Up @@ -79,9 +78,6 @@ public void AddProduct(
IProductPriceCalculator productPriceCalculator,
ProductItem productItem)
{
Guard.Against.Null(productPriceCalculator, nameof(productPriceCalculator));
Guard.Against.Null(productItem, nameof(productItem));

if(Status != CartStatus.Pending)
throw new InvalidOperationException($"Adding product for the cart in '{Status}' status is not allowed.");

Expand Down Expand Up @@ -116,8 +112,6 @@ private void Apply(ProductAdded @event)
public void RemoveProduct(
PricedProductItem productItemToBeRemoved)
{
Guard.Against.Null(productItemToBeRemoved, nameof(productItemToBeRemoved));

if(Status != CartStatus.Pending)
throw new InvalidOperationException($"Removing product from the cart in '{Status}' status is not allowed.");

Expand Down
5 changes: 2 additions & 3 deletions CQRS_Flow/.NET/Carts/Carts/Carts/Config.cs
Original file line number Diff line number Diff line change
Expand Up @@ -22,9 +22,8 @@ internal static class CartsConfig
{
internal static void AddCarts(this IServiceCollection services)
{
services.AddScoped<IProductPriceCalculator, RandomProductPriceCalculator>();

services.AddScoped<IRepository<Cart>, EventStoreDBRepository<Cart>>();
services.AddScoped<IProductPriceCalculator, RandomProductPriceCalculator>()
.AddScoped<IRepository<Cart>, EventStoreDBRepository<Cart>>();

AddCommandHandlers(services);
AddProjections(services);
Expand Down
23 changes: 16 additions & 7 deletions CQRS_Flow/.NET/Carts/Carts/Carts/ConfirmingCart/CartConfirmed.cs
Original file line number Diff line number Diff line change
@@ -1,18 +1,27 @@
using System;
using Ardalis.GuardClauses;
using Core.Events;

namespace Carts.Carts.ConfirmingCart
{
public record CartConfirmed(
Guid CartId,
DateTime ConfirmedAt
): IEvent
public class CartConfirmed: IEvent
{
public Guid CartId { get; }

public DateTime ConfirmedAt { get; }

public CartConfirmed(Guid cartId, DateTime confirmedAt)
{
CartId = cartId;
ConfirmedAt = confirmedAt;
}

public static CartConfirmed Create(Guid cartId, DateTime confirmedAt)
{
Guard.Against.Default(cartId, nameof(cartId));
Guard.Against.Default(confirmedAt, nameof(confirmedAt));
if (cartId == Guid.Empty)
throw new ArgumentOutOfRangeException(nameof(cartId));

if(confirmedAt == DateTime.MinValue || confirmedAt == DateTime.MaxValue)
throw new ArgumentOutOfRangeException(nameof(confirmedAt));

return new CartConfirmed(cartId, confirmedAt);
}
Expand Down
Original file line number Diff line number Diff line change
@@ -1,7 +1,6 @@
using System;
using System.Threading;
using System.Threading.Tasks;
using Ardalis.GuardClauses;
using Core.Commands;
using Core.Repositories;
using MediatR;
Expand All @@ -19,7 +18,8 @@ private ConfirmCart(Guid cartId)

public static ConfirmCart Create(Guid cartId)
{
Guard.Against.Default(cartId, nameof(cartId));
if (cartId == Guid.Empty)
throw new ArgumentOutOfRangeException(nameof(cartId));

return new ConfirmCart(cartId);
}
Expand Down
Original file line number Diff line number Diff line change
@@ -1,7 +1,6 @@
using System;
using System.Threading;
using System.Threading.Tasks;
using Ardalis.GuardClauses;
using Carts.Carts.GettingCartById;
using Core.EventStoreDB.Events;
using Core.Exceptions;
Expand All @@ -23,7 +22,8 @@ private GetCartAtVersion(Guid cartId, ulong version)

public static GetCartAtVersion Create(Guid cartId, ulong version)
{
Guard.Against.Default(cartId, nameof(cartId));
if (cartId == Guid.Empty)
throw new ArgumentOutOfRangeException(nameof(cartId));

return new GetCartAtVersion(cartId, version);
}
Expand Down
Original file line number Diff line number Diff line change
@@ -1,7 +1,6 @@
using System;
using System.Threading;
using System.Threading.Tasks;
using Ardalis.GuardClauses;
using Core.ElasticSearch.Indices;
using Core.Exceptions;
using Core.Queries;
Expand All @@ -20,7 +19,8 @@ private GetCartById(Guid cartId)

public static GetCartById Create(Guid cartId)
{
Guard.Against.Default(cartId, nameof(cartId));
if (cartId == Guid.Empty)
throw new ArgumentOutOfRangeException(nameof(cartId));

return new GetCartById(cartId);
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -3,13 +3,12 @@
using System.Linq;
using System.Threading;
using System.Threading.Tasks;
using Ardalis.GuardClauses;
using Core.Queries;
using Nest;

namespace Carts.Carts.GettingCartHistory
{
public class GetCartHistory : IQuery<IReadOnlyList<CartHistory>>
public class GetCartHistory: IQuery<IReadOnlyList<CartHistory>>
{
public Guid CartId { get; }
public int PageNumber { get; }
Expand All @@ -24,14 +23,16 @@ private GetCartHistory(Guid cartId, int pageNumber, int pageSize)

public static GetCartHistory Create(Guid cartId, int pageNumber = 1, int pageSize = 20)
{
Guard.Against.NegativeOrZero(pageNumber, nameof(pageNumber));
Guard.Against.NegativeOrZero(pageSize, nameof(pageSize));
if (pageNumber <= 0)
throw new ArgumentOutOfRangeException(nameof(pageNumber));
if (pageSize is <= 0 or > 100)
throw new ArgumentOutOfRangeException(nameof(pageSize));

return new GetCartHistory(cartId, pageNumber, pageSize);
}
}

internal class HandleGetCartHistory :
internal class HandleGetCartHistory:
IQueryHandler<GetCartHistory, IReadOnlyList<CartHistory>>
{
private readonly IElasticClient elasticClient;
Expand All @@ -56,4 +57,4 @@ public async Task<IReadOnlyList<CartHistory>> Handle(GetCartHistory request,
return result.Documents.ToList();
}
}
}
}
Loading

0 comments on commit 055e7e2

Please sign in to comment.