Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -9,4 +9,5 @@ public static class ProductErrors
public const string PriceEmpty = "O produto deve ter um preço.";
public const string PriceGreaterThanZero = "O preço do produto deve ser maior que zero.";
public const string InvalidCategory = "O produto deve pertencer a uma categoria.";
public const string InvalidCompany = "O produto deve pertencer a uma empresa.";
}
2 changes: 2 additions & 0 deletions src/Riber.Domain/Entities/BaseEntity.cs
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,9 @@
/// Representa uma entidade base abstrata que fornece funcionalidades comuns para entidades de domínio.
/// Esta classe inclui um identificador, gerenciamento de eventos de domínio e mecanismos de comparação de igualdade.
/// </summary>
#pragma warning disable CA1067
public abstract class BaseEntity(Guid id) : IEquatable<BaseEntity>

Check warning on line 10 in src/Riber.Domain/Entities/BaseEntity.cs

View workflow job for this annotation

GitHub Actions / sonarcloud

Seal class 'BaseEntity' or implement 'IEqualityComparer<T>' instead. (https://rules.sonarsource.com/csharp/RSPEC-4035)
#pragma warning restore CA1067
{
#region Private Members

Expand Down
10 changes: 3 additions & 7 deletions src/Riber.Domain/Entities/Company.cs
Original file line number Diff line number Diff line change
Expand Up @@ -22,13 +22,9 @@ public sealed class Company

#region Constructors

private Company() : base(Guid.Empty)
{
Name = null!;
TaxId = null!;
Email = null!;
Phone = null!;
}
#pragma warning disable CS8618, CA1823
private Company() : base(Guid.Empty) { }
#pragma warning restore CS8618, CA1823

private Company(
CompanyName name,
Expand Down
14 changes: 4 additions & 10 deletions src/Riber.Domain/Entities/Image.cs
Original file line number Diff line number Diff line change
Expand Up @@ -33,16 +33,10 @@ public sealed class Image : BaseEntity

#region Constructors

private Image() : base(Guid.Empty)
{
Length = 0;
ContentType = string.Empty;
OriginalName = string.Empty;
Key = string.Empty;
Extension = string.Empty;
ShouldDelete = false;
MarkedForDeletionAt = null;
}
#pragma warning disable CS8618, CA1823
private Image() : base(Guid.Empty) { }
#pragma warning restore CS8618, CA1823

private Image(long length, string contentType, string originalName, string key, string extension)
: base(Guid.CreateVersion7())
{
Expand Down
15 changes: 3 additions & 12 deletions src/Riber.Domain/Entities/Invitation.cs
Original file line number Diff line number Diff line change
Expand Up @@ -25,18 +25,9 @@ public sealed class Invitation

#region Constructors

private Invitation() : base(Guid.CreateVersion7())
{
Email = null!;
CompanyId = Guid.Empty;
Role = null!;
IsUsed = false;
Position = default;
Permissions = string.Empty;
CreatedByUserId = Guid.Empty;
ExpiresAt = default;
Token = null!;
}
#pragma warning disable CS8618, CA1823
private Invitation() : base(Guid.CreateVersion7()) { }
#pragma warning restore CS8618, CA1823

private Invitation(
Email email,
Expand Down
27 changes: 12 additions & 15 deletions src/Riber.Domain/Entities/Order.cs
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@

namespace Riber.Domain.Entities;

public sealed class Order
public sealed class Order
: TenantEntity, IAggregateRoot, IHasRandomToken
{
#region Properties
Expand All @@ -16,27 +16,24 @@ public sealed class Order
public decimal SubTotal => _items.Sum(x => x.SubTotal);
public decimal TotalDiscounts => _items.Sum(x => x.DiscountAmount);
public decimal TotalAmount => SubTotal - TotalDiscounts;

#endregion

#region Navigation Properties

public Company Company { get; private set; } = null!;
public User Attendant { get; private set; } = null!;
public IReadOnlyCollection<OrderItem> ItemsReadOnly => _items.AsReadOnly();

#endregion

#region Constructors

private Order() : base(Guid.Empty)
{
Token = null!;
CompanyId = Guid.Empty;
AttendantId = Guid.Empty;
}
#pragma warning disable CS8618, CA1823
private Order() : base(Guid.Empty) { }
#pragma warning restore CS8618, CA1823

private Order(Guid companyId, Guid attendantId)
private Order(Guid companyId, Guid attendantId)
: base(Guid.CreateVersion7())
{
CompanyId = companyId;
Expand All @@ -45,11 +42,11 @@ private Order(Guid companyId, Guid attendantId)
}

#endregion

#region Factories

public static Order Create(Guid companyId, Guid attendantId)
=> new(companyId, attendantId);

#endregion
}
11 changes: 3 additions & 8 deletions src/Riber.Domain/Entities/OrderItem.cs
Original file line number Diff line number Diff line change
Expand Up @@ -33,14 +33,9 @@ public sealed class OrderItem

#region Constructors

private OrderItem() : base(Guid.Empty)
{
OrderId = Guid.Empty;
ProductId = Guid.Empty;
ProductName = string.Empty;
UnitPrice = Money.Zero();
Quantity = Quantity.Zero();
}
#pragma warning disable CS8618, CA1823
private OrderItem() : base(Guid.Empty) { }
#pragma warning restore CS8618, CA1823

private OrderItem(
Guid orderId,
Expand Down
20 changes: 7 additions & 13 deletions src/Riber.Domain/Entities/Product.cs
Original file line number Diff line number Diff line change
Expand Up @@ -32,15 +32,9 @@ public sealed class Product

#region Constructors

private Product() : base(Guid.Empty)
{
Name = string.Empty;
Description = string.Empty;
UnitPrice = null!;
CategoryId = Guid.Empty;
IsActive = false;
CompanyId = Guid.Empty;
}
#pragma warning disable CS8618, CA1823
private Product() : base(Guid.Empty) { }
#pragma warning restore CS8618, CA1823

private Product(
string name,
Expand All @@ -57,6 +51,7 @@ private Product(
CompanyId = companyId;
ImageId = imageId;
IsActive = true;
Image = null;
}

#endregion
Expand All @@ -80,10 +75,9 @@ public static Product Create(
if (categoryId == Guid.Empty)
throw new IdentifierNullException(ProductErrors.InvalidCategory);

if (companyId == Guid.Empty)
throw new IdentifierNullException(ProductErrors.InvalidCategory);

return new Product(name, description, price, categoryId, companyId, imageId);
return companyId == Guid.Empty
? throw new IdentifierNullException(ProductErrors.InvalidCompany)
: new Product(name, description, price, categoryId, companyId, imageId);
}

#endregion
Expand Down
22 changes: 8 additions & 14 deletions src/Riber.Domain/Entities/ProductCategory.cs
Original file line number Diff line number Diff line change
Expand Up @@ -26,14 +26,9 @@ public sealed class ProductCategory

#region Constructors

private ProductCategory() : base(Guid.Empty)
{
Name = string.Empty;
Description = string.Empty;
Code = string.Empty;
IsActive = false;
CompanyId = Guid.Empty;
}
#pragma warning disable CS8618, CA1823
private ProductCategory() : base(Guid.Empty) { }
#pragma warning restore CS8618, CA1823

private ProductCategory(
string name,
Expand Down Expand Up @@ -64,10 +59,9 @@ public static ProductCategory Create(
if (string.IsNullOrWhiteSpace(code))
throw new ProductCategoryCodeNullException(CategoryErrors.CodeEmpty);

if (companyId == Guid.Empty)
throw new IdentifierNullException(CompanyErrors.Invalid);

return new ProductCategory(name, description, code, companyId);
return companyId == Guid.Empty
? throw new IdentifierNullException(CompanyErrors.Invalid)
: new ProductCategory(name, description, code, companyId);
}

#endregion
Expand All @@ -80,12 +74,12 @@ public void UpdateDetails(string name, string? description)
throw new ProductCategoryNameNullException(CategoryErrors.NameEmpty);

Name = name;
if(description is not null)
if (description is not null)
Description = description;
}

public void Activate() => IsActive = true;
public void Deactivate() => IsActive = false;

#endregion
}
}
10 changes: 3 additions & 7 deletions src/Riber.Domain/Entities/User.cs
Original file line number Diff line number Diff line change
Expand Up @@ -29,13 +29,9 @@ public sealed class User

#region Constructors

public User() : base(Guid.Empty)
{
FullName = null!;
TaxId = null!;
CompanyId = null!;
IsActive = false;
}
#pragma warning disable CS8618, CA1823
public User() : base(Guid.Empty) { }
#pragma warning restore CS8618, CA1823

private User(
FullName fullName,
Expand Down
10 changes: 4 additions & 6 deletions src/Riber.Domain/Validators/DocumentValidator/CnpjValidator.cs
Original file line number Diff line number Diff line change
Expand Up @@ -21,12 +21,10 @@ public void IsValid(string document)
if (cnpj.Distinct().Count() == 1)
throw new InvalidCnpjException(CnpjErrors.OnlyRepeatedDigits);

int[] digits = cnpj
.Select(c => c - '0')
.ToArray();
int[] digits = [.. cnpj.Select(c => c - '0')];

// Cálculo do primeiro dígito verificador
int[] multiplier1 = { 5, 4, 3, 2, 9, 8, 7, 6, 5, 4, 3, 2 };
int[] multiplier1 = [5, 4, 3, 2, 9, 8, 7, 6, 5, 4, 3, 2];
int sum = 0;

for (int i = 0; i < 12; i++)
Expand All @@ -39,7 +37,7 @@ public void IsValid(string document)
throw new InvalidCnpjException(CnpjErrors.Invalid);

// Cálculo do segundo dígito verificador
int[] multiplier2 = { 6, 5, 4, 3, 2, 9, 8, 7, 6, 5, 4, 3, 2 };
int[] multiplier2 = [6, 5, 4, 3, 2, 9, 8, 7, 6, 5, 4, 3, 2];
sum = 0;

for (int i = 0; i < 13; i++)
Expand All @@ -66,7 +64,7 @@ public string Sanitize(string document)
}

public static string Format(string document)
=> $"{document.Substring(0, 2)}.{document.Substring(2, 3)}.{document.Substring(5, 3)}/{document.Substring(8, 4)}-{document.Substring(12, 2)}";
=> $"{document[..2]}.{document.Substring(2, 3)}.{document.Substring(5, 3)}/{document.Substring(8, 4)}-{document.Substring(12, 2)}";

#endregion
}
6 changes: 2 additions & 4 deletions src/Riber.Domain/Validators/DocumentValidator/CpfValidator.cs
Original file line number Diff line number Diff line change
Expand Up @@ -21,9 +21,7 @@ public void IsValid(string document)
if (cpf.Distinct().Count() == 1)
throw new InvalidCpfException(CpfErrors.OnlyRepeatedDigits);

int[] digits = cpf
.Select(c => c - '0')
.ToArray();
int[] digits = [.. cpf.Select(c => c - '0')];

int sum = 0;
for (int i = 0; i < 9; i++)
Expand Down Expand Up @@ -60,7 +58,7 @@ public string Sanitize(string document)
}

public static string Format(string document)
=> $"{document.Substring(0, 3)}.{document.Substring(3, 3)}.{document.Substring(6, 3)}-{document.Substring(9, 2)}";
=> $"{document[..3]}.{document.Substring(3, 3)}.{document.Substring(6, 3)}-{document.Substring(9, 2)}";

#endregion
}
15 changes: 6 additions & 9 deletions src/Riber.Infrastructure/DependencyInjection.cs
Original file line number Diff line number Diff line change
Expand Up @@ -42,7 +42,7 @@ public static void AddInfrastructure(this IServiceCollection services, IConfigur
ILoggingBuilder logging)
{
var defaultConnection = configuration.GetConnectionString("DefaultConnection")
?? throw new InternalException("Default connection string is not set.");
?? throw new InternalException("Default connection string is not set.");

logging.AddLogging();
services.AddPersistence(defaultConnection);
Expand Down Expand Up @@ -86,14 +86,11 @@ private static void AddLogging(this ILoggingBuilder logging)

private static void AddPersistence(this IServiceCollection services, string defaultConnection)
{
services.AddDbContext<AppDbContext>(options =>
{
options
.UseNpgsql(defaultConnection, b => b.MigrationsAssembly(typeof(AppDbContext).Assembly.FullName))
.AddInterceptors(
new CaseInsensitiveInterceptor(),
new AuditInterceptor());
});
services.AddDbContext<AppDbContext>(options => options
.UseNpgsql(defaultConnection, b => b.MigrationsAssembly(typeof(AppDbContext).Assembly.FullName))
.AddInterceptors(
new CaseInsensitiveInterceptor(),
new AuditInterceptor()));
}

private static void AddRepositories(this IServiceCollection services)
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -29,9 +29,8 @@ public Guid GetUserId()
public Guid GetCompanyId()
{
var companyIdClaim = CurrentUser.FindFirst("companyId")!;
if (!Guid.TryParse(companyIdClaim.Value, out var companyId))
throw new BadRequestException(CompanyErrors.Invalid);

return companyId;
return !Guid.TryParse(companyIdClaim.Value, out var companyId)
? throw new BadRequestException(CompanyErrors.Invalid)
: companyId;
}
}
3 changes: 2 additions & 1 deletion tests/Riber.Domain.Tests/Entities/ProductTests.cs
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
using FluentAssertions;
using Riber.Domain.Constants.Messages.Entities;
using Riber.Domain.Entities;
using Riber.Domain.Exceptions;

Expand Down Expand Up @@ -167,7 +168,7 @@ public void Create_WhenEmptyCompanyId_ShouldThrowIdentifierNullException()
companyId
);

act.Should().Throw<IdentifierNullException>();
act.Should().Throw<IdentifierNullException>().WithMessage(ProductErrors.InvalidCompany);
}

#endregion
Expand Down
Loading
Loading