Skip to content

Commit

Permalink
Adjusts and improvements
Browse files Browse the repository at this point in the history
  • Loading branch information
AntonioFalcaoJr committed Dec 22, 2023
1 parent 27c3886 commit db4d24c
Show file tree
Hide file tree
Showing 24 changed files with 162 additions and 44 deletions.
3 changes: 2 additions & 1 deletion Directory.Packages.props
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
<Project>
<PropertyGroup Label="Common properties">
<Product>Eventual Shop</Product>
<Copyright>Copyright (c) 2021-2023 Antonio Falcão Jr.</Copyright>
<Copyright>Copyright (c) 2021-2024 Antonio Falcão Jr.</Copyright>
<Authors>Antônio Falcão Jr.</Authors>
<TargetFramework>net9.0</TargetFramework>
<LangVersion>preview</LangVersion>
Expand Down Expand Up @@ -77,6 +77,7 @@
<PackageVersion Include="Microsoft.Extensions.Http" Version="9.0.0-ci" />
<PackageVersion Include="Microsoft.Extensions.Http.Polly" Version="9.0.0-alpha.1.23560.2" />
<PackageVersion Include="Microsoft.Extensions.Options.DataAnnotations" Version="9.0.0-ci" />
<PackageVersion Include="Microsoft.Fast.Components.FluentUI" Version="3.3.0" />
<!--Microsoft.NET.Test-->
<PackageVersion Include="Microsoft.NET.Test.Sdk" Version="17.9.0-preview-23531-01" />
<PackageVersion Include="Microsoft.VisualStudio.Web.CodeGeneration.Design" Version="8.0.1-alpha.1.23158.1" />
Expand Down
1 change: 1 addition & 0 deletions src/Contracts/Abstractions/Messages/IEvent.cs
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,7 @@ public interface IVersionedEvent : IEvent
string Version { get; }
}

[ExcludeFromTopology]
public interface IDomainEvent : IVersionedEvent;

[ExcludeFromTopology]
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -4,10 +4,10 @@ namespace Domain.Enumerations;

public class CatalogStatus(string name, int value) : SmartEnum<CatalogStatus>(name, value)
{
public static readonly CatalogStatusEmpty Empty = new();
public static readonly CatalogStatusActive Active = new();
public static readonly CatalogStatusInactive Inactive = new();
public static readonly CatalogStatusDiscarded Discarded = new();
public static readonly CatalogStatus Empty = new CatalogStatusEmpty();
public static readonly CatalogStatus Active = new CatalogStatusActive();
public static readonly CatalogStatus Inactive = new CatalogStatusInactive();
public static readonly CatalogStatus Discarded = new CatalogStatusDiscarded();

public static explicit operator CatalogStatus(int value) => FromValue(value);
public static explicit operator CatalogStatus(string name) => FromName(name);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -13,8 +13,8 @@ private Description(string description)
_value = description;
}

public static Description Undefined => "Undefined";
public static Description Undefined => new("Undefined");
public static implicit operator string(Description description) => description._value;
public static implicit operator Description(string description) => new(description);
public static explicit operator Description(string description) => new(description);
public override string ToString() => _value;
}
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,7 @@ public Title(string productName)
}

public static Title Undefined => new("Undefined");
public static implicit operator Title(string title) => new(title);
public static explicit operator Title(string title) => new(title);
public static implicit operator string(Title title) => title._value;
public override string ToString() => _value;
}
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@
using Contracts.Services.Cataloging.Command.Protobuf;
using Domain.Aggregates;
using Domain.Aggregates.Catalogs;
using Domain.ValueObjects;
using Google.Protobuf;
using Google.Protobuf.WellKnownTypes;
using Grpc.Core;
Expand All @@ -17,7 +18,7 @@ public override async Task<CommandResponse> CreateCatalog(CreateCatalogCommand c
// TODO: Get AppId from context
var appId = AppId.Undefined;

CreateCatalog create = new(appId, cmd.Title, cmd.Description /*, cmd.ImageUrl*/);
CreateCatalog create = new(appId, (Title)cmd.Title, (Description)cmd.Description /*, cmd.ImageUrl*/);
var catalogId = await sender.Send(create, context.CancellationToken);
return Response.Ok<Identifier>(new() { Id = catalogId });
}
Expand Down Expand Up @@ -45,14 +46,14 @@ public override async Task<CommandResponse> DeactivateCatalog(DeactivateCatalogC

public override async Task<CommandResponse> ChangeCatalogTitle(ChangeCatalogTitleCommand cmd, ServerCallContext context)
{
ChangeCatalogTitle changeTitle = new((CatalogId)cmd.CatalogId, cmd.Title);
ChangeCatalogTitle changeTitle = new((CatalogId)cmd.CatalogId, (Title)cmd.Title);
await sender.Send(changeTitle, context.CancellationToken);
return Response.Accepted();
}

public override async Task<CommandResponse> ChangeCatalogDescription(ChangeCatalogDescriptionCommand request, ServerCallContext context)
public override async Task<CommandResponse> ChangeCatalogDescription(ChangeCatalogDescriptionCommand cmd, ServerCallContext context)
{
ChangeCatalogDescription changeDescription = new((CatalogId)request.CatalogId, request.Description);
ChangeCatalogDescription changeDescription = new((CatalogId)cmd.CatalogId, (Description)cmd.Description);
await sender.Send(changeDescription, context.CancellationToken);
return Response.Accepted();
}
Expand Down
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
{
"profiles": {
"ShoppingCart.Command.GrpcService": {
"Cataloging.Command.GrpcService": {
"commandName": "Project",
"dotnetRunMessages": true,
"launchBrowser": false,
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -36,7 +36,7 @@ public static ShoppingCart StartShopping(CustomerId customerId)

public void AddItem(CartItem newItem)
{
CartNotOpen.ThrowIf(Status != CartStatus.Open);
CartNotOpen.ThrowIf(Status is not CartStatusOpen);

_items.TryGetValue(newItem.ProductId, out var item);

Expand Down Expand Up @@ -100,7 +100,7 @@ public void CheckOut()

public void Discard()
{
if (Status == CartStatus.Abandoned) return;
if (Status is CartStatusAbandoned) return;
RaiseEvent(new DomainEvent.CartDiscarded(Id, CartStatus.Abandoned, Version.Next));
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -9,5 +9,4 @@ public record EventBusOptions
[Required, Range(1, 10)] public int RetryLimit { get; init; }
[Required, Timestamp] public TimeSpan InitialInterval { get; init; }
[Required, Timestamp] public TimeSpan IntervalIncrement { get; init; }
[Required, MinLength(5)] public required string SchedulerQueueName { get; init; }
}
12 changes: 6 additions & 6 deletions src/Web/WebAPI/APIs/Catalogs/Queries.cs
Original file line number Diff line number Diff line change
Expand Up @@ -2,25 +2,25 @@ namespace WebAPI.APIs.Catalogs;

public static class Queries
{
// public record ListCatalogsGridItems(CatalogService.CatalogServiceClient Client, int? Limit, int? Offset, CancellationToken CancellationToken)
// public record ListCatalogsGridItems(CatalogService.CatalogServiceClient Client, int? Size, int? Number, CancellationToken CancellationToken)
// : Validatable<ListCatalogsGridItemsValidator>, IQuery<CatalogService.CatalogServiceClient>
// {
// public static implicit operator ListCatalogsGridItemsRequest(ListCatalogsGridItems request)
// => new() { Paging = new() { Limit = request.Limit, Offset = request.Offset } };
// => new() { Paging = new() { Size = request.Size, Number = request.Number } };
// }
//
// public record ListCatalogItemsListItems(CatalogService.CatalogServiceClient Client, Guid CatalogId, int? Limit, int? Offset, CancellationToken CancellationToken)
// public record ListCatalogItemsListItems(CatalogService.CatalogServiceClient Client, Guid CatalogId, int? Size, int? Number, CancellationToken CancellationToken)
// : Validatable<ListCatalogItemsListItemsRequestValidator>, IQuery<CatalogService.CatalogServiceClient>
// {
// public static implicit operator ListCatalogItemsListItemsRequest(ListCatalogItemsListItems request)
// => new() { CatalogId = request.CatalogId.ToString(), Paging = new() { Limit = request.Limit, Offset = request.Offset } };
// => new() { CatalogId = request.CatalogId.ToString(), Paging = new() { Size = request.Size, Number = request.Number } };
// }
//
// public record ListCatalogItemsCards(CatalogService.CatalogServiceClient Client, Guid CatalogId, int? Limit, int? Offset, CancellationToken CancellationToken)
// public record ListCatalogItemsCards(CatalogService.CatalogServiceClient Client, Guid CatalogId, int? Size, int? Number, CancellationToken CancellationToken)
// : Validatable<ListCatalogItemsCardsValidator>, IQuery<CatalogService.CatalogServiceClient>
// {
// public static implicit operator ListCatalogItemsCardsRequest(ListCatalogItemsCards request)
// => new() { CatalogId = request.CatalogId.ToString(), Paging = new() { Limit = request.Limit, Offset = request.Offset } };
// => new() { CatalogId = request.CatalogId.ToString(), Paging = new() { Size = request.Size, Number = request.Number } };
// }
//
// public record GetCatalogItemDetails(CatalogService.CatalogServiceClient Client, Guid CatalogId, Guid ItemId, CancellationToken CancellationToken)
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -8,11 +8,11 @@ public ListCatalogItemsCardsValidator()
// RuleFor(request => request.CatalogId)
// .NotEmpty();
//
// RuleFor(request => request.Limit)
// RuleFor(request => request.Size)
// .GreaterThan(0)
// .LessThanOrEqualTo(100);
//
// RuleFor(request => request.Offset)
// RuleFor(request => request.Number)
// .GreaterThanOrEqualTo(0);
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -8,11 +8,11 @@ public ListCatalogItemsListItemsRequestValidator()
// RuleFor(request => request.CatalogId)
// .NotEmpty();
//
// RuleFor(request => request.Limit)
// RuleFor(request => request.Size)
// .GreaterThan(0)
// .LessThanOrEqualTo(100);
//
// RuleFor(request => request.Offset)
// RuleFor(request => request.Number)
// .GreaterThanOrEqualTo(0);
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -5,11 +5,11 @@ public class ListCatalogsGridItemsValidator
{
public ListCatalogsGridItemsValidator()
{
// RuleFor(request => request.Limit)
// RuleFor(request => request.Size)
// .GreaterThan(0)
// .LessThanOrEqualTo(100);
//
// RuleFor(request => request.Offset)
// RuleFor(request => request.Number)
// .GreaterThanOrEqualTo(0);
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,57 @@
using System.Net;
using Polly.Timeout;

namespace WebAPP.DependencyInjection.Extensions;

// TODO: It is necessary given the incompetence of the Refit team. Remove when the issue is fixed.
public class HttpRequestExceptionHandler : DelegatingHandler
{
protected override async Task<HttpResponseMessage> SendAsync(HttpRequestMessage request,
CancellationToken cancellationToken)
{
try
{
return await base.SendAsync(request, cancellationToken);
}
catch (HttpRequestException exception)
{
return new(HttpStatusCode.ServiceUnavailable)
{
ReasonPhrase = exception.Message,
RequestMessage = request
};
}
catch (TimeoutRejectedException exception)
{
return new(HttpStatusCode.RequestTimeout)
{
ReasonPhrase = exception.Message,
RequestMessage = request
};
}
}

protected override HttpResponseMessage Send(HttpRequestMessage request, CancellationToken cancellationToken)
{
try
{
return base.Send(request, cancellationToken);
}
catch (HttpRequestException exception)
{
return new(HttpStatusCode.ServiceUnavailable)
{
ReasonPhrase = exception.Message,
RequestMessage = request
};
}
catch (TimeoutRejectedException exception)
{
return new(HttpStatusCode.RequestTimeout)
{
ReasonPhrase = exception.Message,
RequestMessage = request
};
}
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,11 @@ public static void AddApis(this IServiceCollection services)
services.AddApiClient<IDeleteCatalogApi>();
services.AddApiClient<IListCatalogsApi>();
services.AddApiClient<IListCatalogItemsApi>();
services.AddApiClient<IAddCatalogItemApi>();
services.AddApiClient<ISearchProductsApi>();

// TODO: It is necessary given the incompetence of the Refit team. Remove when the issue is fixed.
services.AddScoped<HttpRequestExceptionHandler>();
}

private static void AddApiClient<TApi>(this IServiceCollection services) where TApi : class
Expand All @@ -38,9 +43,12 @@ private static void AddApiClient<TApi>(this IServiceCollection services) where T
var options = provider.GetRequiredService<IOptions<ECommerceHttpClientOptions>>().Value;
return Policy.WrapAsync(
HttpPolicy.GetRetryPolicyAsync(options.RetryCount, options.SleepDurationPower, options.EachRetryTimeout),
HttpPolicy.GetRetryPolicyAsync(options.RetryCount, options.SleepDurationPower,
options.EachRetryTimeout),
HttpPolicy.GetCircuitBreakerPolicyAsync(options.CircuitBreaking, options.DurationOfBreak));
});
})
// TODO: It is necessary given the incompetence of the Refit team. Remove when the issue is fixed.
.AddHttpMessageHandler<HttpRequestExceptionHandler>();
}

private static IServiceCollection AddLazyTransient<T>(this IServiceCollection services) where T : class
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,7 @@ public static IAsyncPolicy<HttpResponseMessage> GetRetryPolicyAsync(int retryCou
retryCount: retryCount,
sleepDurationProvider: retryAttempt => TimeSpan.FromSeconds(Math.Pow(sleepDurationPower, retryAttempt)),
onRetry: (response, waitingTime, retryAttempt, _)
=> Log.Warning(@"Retrying in {WaitingTime}s. Attempt: {RetryAttempt}/{RetryCount}. Status Code: {StatusCode}. Message: {Message}",
=> Log.Warning("Retrying in {WaitingTime}s. Attempt: {RetryAttempt}/{RetryCount}. Status Code: {StatusCode}. Message: {Message}",
waitingTime.TotalSeconds, retryAttempt, retryCount, response?.Result?.StatusCode ?? default, response?.Exception?.Message ?? "No message"))
.WrapAsync(
Policy.TimeoutAsync<HttpResponseMessage>(
Expand Down
2 changes: 1 addition & 1 deletion src/Web/WebAPP/Pages/Catalogs.razor
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,7 @@

<NavBar/>
<SearchBar/>
<CatalogCanvas/>
<CreateCatalogCanvas/>

<BSContainer>
<BSRow Align="Align.Center" Justify="Justify.Around">
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -53,7 +53,7 @@

@code {

private BSOffCanvasBase Canvas { get; set; } = new BSOffCanvas();
private BSOffCanvas Canvas { get; set; } = new();
private readonly CancellationTokenSource _cancellationTokenSource = new();

protected override void OnInitialized()
Expand Down
15 changes: 13 additions & 2 deletions src/Web/WebAPP/Store/Cataloging/CatalogingState.cs
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
using System.Collections.Immutable;
using Fluxor;
using WebAPP.Abstractions;
using WebAPP.Store.Catalogs;

namespace WebAPP.Store.Cataloging;

Expand All @@ -9,20 +10,30 @@ public record CatalogingState
{
public IImmutableList<Catalog> Catalogs { get; init; } = ImmutableList<Catalog>.Empty;
public Catalog NewCatalog { get; init; } = new();
public CatalogItem NewItem { get; init; } = new();
public bool IsCreating { get; init; }
public bool IsFetching { get; init; }
public bool IsDeleting { get; init; }
public bool IsEditingTitle { get; init; }
public bool IsAddingItem { get; init; }
public bool IsEditingDescription { get; init; }
public bool HasError => Error != string.Empty;
public string Error { get; init; } = string.Empty;
public Page Page { get; init; } = new();
public bool IsCreatingItem { get; set; }
public string CatalogId { get; set; } = "Undefined";
public bool HasError => Error != string.Empty;

// It is necessary to have a separate property for the searching state
public bool IsSearching { get; set; }
public string Fragment { get; set; } = string.Empty;
public IImmutableList<Product> Products { get; set; } = ImmutableList<Product>.Empty;
////
}

public record Catalog
{
public bool IsActive { get; set; }
public string Id { get; set; } = string.Empty;
public string CatalogId { get; set; } = "Undefined";
public string Title { get; set; } = string.Empty;
public string Description { get; set; } = string.Empty;
}
2 changes: 1 addition & 1 deletion src/Web/WebAPP/Store/Cataloging/Commands/CreateCatalog.cs
Original file line number Diff line number Diff line change
Expand Up @@ -29,7 +29,7 @@ public override async Task HandleAsync(CreateCatalog cmd, IDispatcher dispatcher
var response = await api.CreateAsync(cmd.NewCatalog, cmd.CancellationToken);

dispatcher.Dispatch(response is { IsSuccessStatusCode: true, Content: not null }
? new CatalogCreated { NewCatalog = cmd.NewCatalog with { Id = response.Content.Id } }
? new CatalogCreated { NewCatalog = cmd.NewCatalog with { CatalogId = response.Content.Id } }
: new CatalogCreationFailed { Error = response.ReasonPhrase ?? response.Error?.Message ?? "Unknown error" });
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,6 @@

public record CatalogTitleChangeFailed
{
public string CatalogId;
public string Error;
public required string CatalogId;
public required string Error;
}
Loading

0 comments on commit db4d24c

Please sign in to comment.