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
7 changes: 0 additions & 7 deletions .idea/.idea.Fin-Backend/.idea/data_source_mapping.xml

This file was deleted.

52 changes: 52 additions & 0 deletions Fin.Api/CardBrands/CardBrandController.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,52 @@
using Fin.Application.CardBrands;
using Fin.Domain.Global.Classes;
using Fin.Domain.CardBrands.Dtos;
using Fin.Infrastructure.Authentications.Constants;
using Microsoft.AspNetCore.Authorization;
using Microsoft.AspNetCore.Mvc;

namespace Fin.Api.CardBrands;

[Route("card-brand")]
[Authorize]
public class CardBrandController(ICardBrandService service): ControllerBase
{
[HttpGet]
public async Task<PagedOutput<CardBrandOutput>> GetList([FromQuery] PagedFilteredAndSortedInput input)
{
return await service.GetList(input);
}


[HttpGet("{id:guid}")]
[Authorize(Roles = AuthenticationRoles.Admin)]
public async Task<ActionResult<CardBrandOutput>> Get([FromRoute] Guid id)
{
var menu = await service.Get(id);
return menu != null ? Ok(menu) : NotFound();
}

[HttpPost]
[Authorize(Roles = AuthenticationRoles.Admin)]
public async Task<ActionResult<CardBrandOutput>> Create([FromBody] CardBrandInput input)
{
var menu = await service.Create(input, autoSave: true);
return menu != null ? Created($"card-brand/{menu.Id}", menu) : UnprocessableEntity();
}

[HttpPut("{id:guid}")]
[Authorize(Roles = AuthenticationRoles.Admin)]
public async Task<ActionResult> Update([FromRoute] Guid id, [FromBody] CardBrandInput input)
{
var updated = await service.Update(id, input, autoSave: true);
return updated ? Ok() : UnprocessableEntity();
}

[HttpDelete("{id:guid}")]
[Authorize(Roles = AuthenticationRoles.Admin)]
public async Task<ActionResult> Delete([FromRoute] Guid id)
{
var deleted = await service.Delete(id, autoSave: true);
return deleted ? Ok() : UnprocessableEntity();
}
}
91 changes: 91 additions & 0 deletions Fin.Application/CardBrands/CardBrandService.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,91 @@
using Fin.Application.CardBrands;
using Fin.Domain.Global.Classes;
using Fin.Domain.CardBrands.Dtos;
using Fin.Domain.CardBrands.Entities;
using Fin.Infrastructure.AmbientDatas;
using Fin.Infrastructure.AutoServices.Interfaces;
using Fin.Infrastructure.Database.Extensions;
using Fin.Infrastructure.Database.Repositories;
using Microsoft.AspNetCore.Http;
using Microsoft.EntityFrameworkCore;

namespace Fin.Application.CardBrands;

public interface ICardBrandService
{
public Task<CardBrandOutput> Get(Guid id);
public Task<PagedOutput<CardBrandOutput>> GetList(PagedFilteredAndSortedInput input);
public Task<CardBrandOutput> Create(CardBrandInput input, bool autoSave = false);
public Task<bool> Update(Guid id, CardBrandInput input, bool autoSave = false);
public Task<bool> Delete(Guid id, bool autoSave = false);
public Task<List<CardBrandOutput>> GetListForSideNav();
}

public class CardBrandService(
IRepository<CardBrand> repository,
IAmbientData ambientData
) : ICardBrandService, IAutoTransient
{
public async Task<CardBrandOutput> Get(Guid id)
{
var entity = await repository.Query()
.FirstOrDefaultAsync(n => n.Id == id);
return entity != null ? new CardBrandOutput(entity) : null;
}

public async Task<PagedOutput<CardBrandOutput>> GetList(PagedFilteredAndSortedInput input)
{
return await repository.Query(false)
.OrderBy(m => m.Name)
.ApplyFilterAndSorter(input)
.Select(n => new CardBrandOutput(n))
.ToPagedResult(input);
}

public async Task<List<CardBrandOutput>> GetListForSideNav()
{
return await repository.Query(false)
.OrderBy(m => m.Name)
.Select(m => new CardBrandOutput(m))
.ToListAsync();
}

public async Task<CardBrandOutput> Create(CardBrandInput input, bool autoSave = false)
{
ValidarInput(input);
var cardBrand = new CardBrand(input);
await repository.AddAsync(cardBrand, autoSave);
return new CardBrandOutput(cardBrand);
}

public async Task<bool> Update(Guid id, CardBrandInput input, bool autoSave = false)
{
ValidarInput(input);
var cardBrand = await repository.Query()
.FirstOrDefaultAsync(u => u.Id == id);
if (cardBrand == null) return false;

cardBrand.Update(input);
await repository.UpdateAsync(cardBrand, autoSave);

return true;
}

public async Task<bool> Delete(Guid id, bool autoSave = false)
{
var cardBrand = await repository.Query()
.FirstOrDefaultAsync(u => u.Id == id);
if (cardBrand == null) return false;

await repository.DeleteAsync(cardBrand, autoSave);
return true;
}

private static void ValidarInput(CardBrandInput input)
{
if (string.IsNullOrWhiteSpace(input.Name))
throw new BadHttpRequestException("Name is required");
if (string.IsNullOrWhiteSpace(input.Icon))
throw new BadHttpRequestException("Icon is required");
}
}
15 changes: 15 additions & 0 deletions Fin.Domain/CardBrands/Dtos/CardBrandInput.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,15 @@
using System.ComponentModel.DataAnnotations;
using Fin.Domain.Menus.Enums;

namespace Fin.Domain.CardBrands.Dtos;

public class CardBrandInput
{
[Required]
public string Name { get; set; }

[Required]
public string Icon { get; set; }
public string Color { get; set; }

}
23 changes: 23 additions & 0 deletions Fin.Domain/CardBrands/Dtos/CardBrandOutput.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,23 @@

using Fin.Domain.CardBrands.Entities;

public class CardBrandOutput
{
public Guid Id { get; set; }
public string Name { get; set; }
public string Icon { get; set; }
public string Color { get; set; }


public CardBrandOutput()
{
}

public CardBrandOutput(CardBrand input)
{
Id = input.Id;
Name = input.Name;
Icon = input.Icon;
Color = input.Color;
}
}
36 changes: 36 additions & 0 deletions Fin.Domain/CardBrands/Entities/CardBrand.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,36 @@
using Fin.Domain.CardBrands.Dtos;
using Fin.Domain.Global.Interfaces;

namespace Fin.Domain.CardBrands.Entities;

public class CardBrand: IAuditedEntity
{
public string Name { get; set; }
public string Icon { get; set; }
public string Color { get; set; }

public Guid Id { get; set; }
public Guid CreatedBy { get; set; }
public Guid UpdatedBy { get; set; }
public DateTime CreatedAt { get; set; }
public DateTime UpdatedAt { get; set; }

public CardBrand()
{
}

public CardBrand(CardBrandInput input)
{
Name = input.Name;
Icon = input.Icon;
Color = input.Color;
}

public void Update(CardBrandInput input)
{
Name = input.Name;
Icon = input.Icon;
Color = input.Color;

}
}
4 changes: 4 additions & 0 deletions Fin.Domain/Fin.Domain.csproj
Original file line number Diff line number Diff line change
Expand Up @@ -5,4 +5,8 @@
<ImplicitUsings>enable</ImplicitUsings>
</PropertyGroup>

<ItemGroup>
<Folder Include="CardBrands\Enums\" />
</ItemGroup>

</Project>
Original file line number Diff line number Diff line change
@@ -0,0 +1,16 @@
using Fin.Domain.CardBrands.Entities;
using Microsoft.EntityFrameworkCore;
using Microsoft.EntityFrameworkCore.Metadata.Builders;

namespace Fin.Infrastructure.Database.Configurations.CardBrands;

public class CardBrandConfiguration: IEntityTypeConfiguration<CardBrand>
{
public void Configure(EntityTypeBuilder<CardBrand> builder)
{
builder.HasKey(x => x.Id);
builder.Property(x => x.Name).HasMaxLength(100).IsRequired();
builder.Property(x => x.Icon).HasMaxLength(20).IsRequired();
builder.Property(x => x.Color).HasMaxLength(20);
}
}
3 changes: 2 additions & 1 deletion Fin.Infrastructure/Database/FinDbContext.cs
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
using System.Reflection;
using Fin.Domain.CardBrands.Entities;
using Fin.Domain.Global.Interfaces;
using Fin.Domain.Menus.Entities;
using Fin.Domain.Notifications;
Expand Down Expand Up @@ -30,8 +31,8 @@ public class FinDbContext : DbContext

public DbSet<Menu> Menus { get; set; }

public DbSet<CardBrand> CardBrands { get; set; }
public DbSet<TitleCategory> TitleCategories { get; set; }

public DbSet<Wallet> Wallets { get; set; }

private readonly IAmbientData _ambientData;
Expand Down
Loading
Loading