Skip to content

Commit

Permalink
feat(infrastructure & api): Implement banking account repository and …
Browse files Browse the repository at this point in the history
…fix controllers
  • Loading branch information
wiktormuller committed Oct 22, 2023
1 parent 3a91725 commit 6dd7221
Show file tree
Hide file tree
Showing 9 changed files with 122 additions and 9 deletions.
4 changes: 2 additions & 2 deletions BankingSystem/API/Controllers/BankingAccountController.cs
Original file line number Diff line number Diff line change
Expand Up @@ -21,9 +21,9 @@ public BankingAccountController(IMediator mediator)
[HttpGet("{backingAccountId:guid}")]
[ProducesResponseType(StatusCodes.Status200OK)]
[ProducesResponseType(StatusCodes.Status404NotFound)]
public async Task<ActionResult<BankingAccountResponse>> Get(Guid backingAccountId)
public async Task<ActionResult<BankingAccountResponse>> Get([FromQuery] Guid id)
{
var bankingAccount = await _mediator.Send(new GetBankingAccount(backingAccountId));
var bankingAccount = await _mediator.Send(new GetBankingAccount(id));
if (bankingAccount is not null)
{
return Ok(bankingAccount);
Expand Down
2 changes: 1 addition & 1 deletion BankingSystem/API/Controllers/FundsController.cs
Original file line number Diff line number Diff line change
Expand Up @@ -25,7 +25,7 @@ public async Task<IActionResult> Post(AddFundsRequest request)
return NoContent();
}

[HttpPost]
[HttpPut]
[ProducesResponseType(StatusCodes.Status204NoContent)]
[ProducesResponseType(StatusCodes.Status400BadRequest)]
public async Task<IActionResult> Post(WithdrawFundsRequest request)
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,39 @@
using BankingSystem.Core.Entities;
using Microsoft.EntityFrameworkCore.Metadata.Builders;
using Microsoft.EntityFrameworkCore;

namespace BankingSystem.Infrastructure.Configuration
{
public class BankingAccountsConfiguration : IEntityTypeConfiguration<BankingAccount>
{
public void Configure(EntityTypeBuilder<BankingAccount> builder)
{
builder.ToTable("BankingAccounts");

builder.HasKey(ba => ba.Id);

builder.Property(ba => ba.UserId)
.IsRequired();

builder.Property(ba => ba.Name)
.IsRequired();

builder.Property(ba => ba.CreatedAt)
.IsRequired();

builder.Property(ba => ba.Version)
.IsRequired();

builder.Property(ba => ba.Version)
.IsConcurrencyToken(); // Optimistic concurrency to protect the model from concurrent modifications

builder.HasIndex(ba => ba.Id);

builder.HasIndex(ba => ba.UserId);

builder.HasOne<User>()
.WithMany()
.HasForeignKey(ba => ba.UserId);
}
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,34 @@
using BankingSystem.Core.Entities;
using Microsoft.EntityFrameworkCore.Metadata.Builders;
using Microsoft.EntityFrameworkCore;
using static BankingSystem.Core.Entities.Transfer;

namespace BankingSystem.Infrastructure.Configuration
{
public class TransferConfiguration : IEntityTypeConfiguration<Transfer>
{
public void Configure(EntityTypeBuilder<Transfer> builder)
{
builder.ToTable("Transfers");

builder.HasKey(x => x.Id);

builder.Property(x => x.CorrelationTransferId);

builder.Property(x => x.BankingAccountId)
.IsRequired();

builder.Property(x => x.Amount)
.IsRequired();

builder.Property(x => x.CreatedAt)
.IsRequired();

builder.Property(x => x.Direction)
.IsRequired()
.HasConversion(
v => v.ToString(),
v => (TransferDirection)Enum.Parse(typeof(TransferDirection), v));
}
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -4,17 +4,20 @@

namespace BankingSystem.Infrastructure.Contexts
{
public class UsersDbContext : DbContext
public class BankingSystemDbContext : DbContext
{
public DbSet<User> Users { get; set; }
public DbSet<BankingAccount> BankingAccounts { get; set; }

public UsersDbContext(DbContextOptions<UsersDbContext> options) : base(options)
public BankingSystemDbContext(DbContextOptions<BankingSystemDbContext> options) : base(options)
{
}

protected override void OnModelCreating(ModelBuilder modelBuilder)
{
modelBuilder.ApplyConfiguration(new UsersConfiguration());
modelBuilder.ApplyConfiguration(new BankingAccountsConfiguration());
modelBuilder.ApplyConfiguration(new TransferConfiguration());

base.OnModelCreating(modelBuilder);
}
Expand Down
3 changes: 2 additions & 1 deletion BankingSystem/Infrastructure/Extensions.cs
Original file line number Diff line number Diff line change
Expand Up @@ -17,13 +17,14 @@ public static class Extensions
public static IServiceCollection AddInfrastructure(this IServiceCollection services, IConfiguration configuration)
{
services.AddScoped<IUserRepository, UserRepository>();
services.AddScoped<IBankingAccountRepository, BankingAccountRepository>();

services.AddSingleton<IClock, Clock>();
services.AddSingleton<IPasswordService, PasswordService>();
services.AddSingleton<IJwtService, JwtService>();
services.AddSingleton<IPasswordHasher<PasswordService>, PasswordHasher<PasswordService>>();

services.AddDbContext<UsersDbContext>(opt =>
services.AddDbContext<BankingSystemDbContext>(opt =>
opt.UseSqlServer(configuration.GetConnectionString("DefaultConnection")));


Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,7 @@ public class GetUserMeHandler : IRequestHandler<GetUserMe, UserMeResponse>
{
private readonly DbSet<User> _users;

public GetUserMeHandler(UsersDbContext dbContext)
public GetUserMeHandler(BankingSystemDbContext dbContext)
{
_users = dbContext.Users;
}
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,36 @@
using BankingSystem.Core.Entities;
using BankingSystem.Core.Repositories;
using BankingSystem.Infrastructure.Contexts;
using Microsoft.EntityFrameworkCore;

namespace BankingSystem.Infrastructure.Repositories
{
public class BankingAccountRepository : IBankingAccountRepository
{
private readonly BankingSystemDbContext _context;
private readonly DbSet<BankingAccount> _bankingAccounts;

public BankingAccountRepository(BankingSystemDbContext context)
{
_context = context;
_bankingAccounts = _context.BankingAccounts;
}

public Task<BankingAccount?> GetAsync(Guid id)
=> _bankingAccounts
.Include(x => x.Transfers)
.SingleOrDefaultAsync(x => x.Id.Equals(id));

public async Task AddAsync(BankingAccount bankingAccount)
{
await _bankingAccounts.AddAsync(bankingAccount);
await _context.SaveChangesAsync();
}

public async Task UpdateAsync(BankingAccount bankingAccount)
{
_bankingAccounts.Update(bankingAccount);
await _context.SaveChangesAsync();
}
}
}
4 changes: 2 additions & 2 deletions BankingSystem/Infrastructure/Repositories/UserRepository.cs
Original file line number Diff line number Diff line change
Expand Up @@ -7,10 +7,10 @@ namespace BankingSystem.Infrastructure.Repositories
{
public class UserRepository : IUserRepository
{
private readonly UsersDbContext _dbContext;
private readonly BankingSystemDbContext _dbContext;
private readonly DbSet<User> _users;

public UserRepository(UsersDbContext dbContext)
public UserRepository(BankingSystemDbContext dbContext)
{
_dbContext = dbContext;
_users = dbContext.Users;
Expand Down

0 comments on commit 6dd7221

Please sign in to comment.