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
4 changes: 4 additions & 0 deletions Api/Api.csproj
Original file line number Diff line number Diff line change
Expand Up @@ -24,4 +24,8 @@
<PackageReference Include="Swashbuckle.AspNetCore" Version="6.4.0" />
</ItemGroup>

<ItemGroup>
<Folder Include="Migrations\" />
</ItemGroup>

</Project>
1 change: 1 addition & 0 deletions Api/Controllers/CurrencieController.cs
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@

namespace Api.Controllers
{
[Route("api/Currencie")]
public class CurrencieController : ControllerBase
{
public readonly ApplicationDbContext _context;
Expand Down
65 changes: 63 additions & 2 deletions Api/Controllers/WalletController.cs
Original file line number Diff line number Diff line change
@@ -1,6 +1,67 @@
namespace Api.Controllers
using Api.Dto.Wallets;
using Api.Interfaces;
using Api.Mappers;
using Microsoft.AspNetCore.Mvc;

namespace Api.Controllers
{
public class WalletController
[Route("api/wallets")]
public class WalletController : ControllerBase
{
private readonly IWalletRepository _WalletRepo;
public WalletController(IWalletRepository walletRepository)
{
_WalletRepo = walletRepository;
}

[HttpGet("{id}")]
public async Task<IActionResult> GetById([FromRoute] string id)
{
var wallets = await _WalletRepo.GetByIdAsync(id);

if (wallets == null)
{
return NotFound();
}
return Ok(wallets.ToWalletDto());
}

[HttpGet("AllByUserId{UserId}")]
public async Task<IActionResult> GetAllByUserId([FromRoute] string UserId)
{
var Wallets = await _WalletRepo.GetAllByUserIdAsync(UserId);
if (Wallets == null)
{
return NotFound();
}
var WalletsDto = Wallets.Select(w => w.ToWalletDto()).ToList();
return Ok(WalletsDto);
}

[HttpDelete("DeleteWallet{id}")]
public async Task<IActionResult> Delete([FromRoute] string id)
{
var WalletModel = await _WalletRepo.DeleteAsync(id);
if (WalletModel == null)
{
return NotFound();
}
return NoContent();
}

[HttpPut("{IdForUpdate}")]
public async Task<IActionResult> Update([FromRoute] string IdForUpdate, [FromBody] WalletDto walletDto)
{
var WalletModel = await _WalletRepo.UpdateAsync(IdForUpdate, walletDto);
return Ok(WalletModel);
}

[HttpPost("{UserId}")]
public async Task<IActionResult> Create([FromRoute] string UserId, [FromBody]NewWalletDto walletDto)
{
var walletModel = walletDto.WalletModelFromDto(UserId);
await _WalletRepo.CreateAsync(walletModel);
return CreatedAtAction(nameof(GetById), new { id = walletModel }, walletModel.ToWalletDto());
}
}
}
13 changes: 1 addition & 12 deletions Api/Data/ApplicationDBContext.cs
Original file line number Diff line number Diff line change
Expand Up @@ -54,20 +54,14 @@ protected override void OnModelCreating(ModelBuilder modelBuilder)
.HasOne(b => b.Currencie)
.WithMany(u => u.Budgets)
.HasForeignKey(b => b.CurrencyId);

modelBuilder.Entity<UserWallets>()
.HasOne(ua => ua.AppUser)
.WithMany(u => u.UserAccounts)
.HasForeignKey(ua => ua.WalletId);
.HasForeignKey(ua => ua.UserId);
modelBuilder.Entity<UserWallets>()
.HasOne(ua => ua.Currencie)
.WithMany(u => u.UserAccounts)
.HasForeignKey(ua => ua.CurrencieId);
modelBuilder.Entity<UserWallets>()
.Property(w => w.WalletId)
.ValueGeneratedOnAdd()
.HasValueGenerator<SequentialGuidValueGenerator>();

modelBuilder.Entity<Transaction>()
.HasOne(a => a.AppUser)
.WithMany(u => u.Transactions)
Expand All @@ -76,16 +70,11 @@ protected override void OnModelCreating(ModelBuilder modelBuilder)
.HasOne(a => a.Currencie)
.WithMany(u => u.Transactions)
.HasForeignKey(a => a.CurrencieId);
modelBuilder.Entity<Transaction>()
.Property(w => w.TransactionId)
.ValueGeneratedOnAdd()
.HasValueGenerator<SequentialGuidValueGenerator>();

modelBuilder.Entity<Attachment>()
.HasOne(a => a.Transaction)
.WithMany(u => u.Attachments)
.HasForeignKey(a => a.TransactionId);

/*modelBuilder.Entity<IdentityRole>().HasData(
new IdentityRole
{
Expand Down
12 changes: 12 additions & 0 deletions Api/Dto/Category/CategoryDto.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,12 @@
using System.ComponentModel.DataAnnotations;

namespace Api.Dto.Category
{
public class CategoryDto
{
[Required]
public string Name { get; set; }
[Required]
public string Icon { get; set; }
}
}
2 changes: 1 addition & 1 deletion Api/Dto/Currences/CurrenceDto.cs
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@ namespace Api.Dto.Currences
public class CurrenceDto
{
[Required]
public string Code { get; set; }
public string? Code { get; set; }
[Required]
public decimal Rate { get; set; }
}
Expand Down
5 changes: 1 addition & 4 deletions Api/Dto/Wallets/NewWalletDto.cs
Original file line number Diff line number Diff line change
Expand Up @@ -4,13 +4,10 @@ namespace Api.Dto.Wallets
{
public class NewWalletDto
{
[Required]
public String UserId { get; set; }

[Required]
public string Name { get; set; }
[Required]
public double Balance { get; set; }
[Required]
public string CurrencieId { get; set; }
}
}
18 changes: 18 additions & 0 deletions Api/Dto/Wallets/WalletDto.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,18 @@
using System.ComponentModel.DataAnnotations;

namespace Api.Dto.Wallets
{
public class WalletDto
{
[Required]
public String UserId { get; set; }
[Required]
public string Name { get; set; }
[Required]
public double Balance { get; set; }
[Required]
public string CurrencieId { get; set; }
[Required]
public bool IsActive { get; set; }
}
}
6 changes: 5 additions & 1 deletion Api/Interfaces/IWalletRepository.cs
Original file line number Diff line number Diff line change
@@ -1,10 +1,14 @@
using Api.Models;
using Api.Dto.Wallets;
using Api.Models;

namespace Api.Interfaces
{
public interface IWalletRepository
{
Task<UserWallets?> GetByIdAsync (string id);
Task<UserWallets> CreateAsync(UserWallets walletsModel);
Task<List<UserWallets>> GetAllByUserIdAsync (string userId);
Task<UserWallets?> DeleteAsync (string id);
Task<UserWallets> UpdateAsync(string id, WalletDto walletDto);
}
}
31 changes: 31 additions & 0 deletions Api/Mappers/WalletMappers.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,31 @@
using Api.Dto.Wallets;
using Api.Models;

namespace Api.Mappers
{
public static class WalletMappers
{
public static UserWallets WalletModelFromDto(this NewWalletDto newWalletDto, string UserId)
{
return new UserWallets
{
UserId = UserId,
Name = newWalletDto.Name,
Balance = 0,
CurrencieId = newWalletDto.CurrencieId,
};

}
public static WalletDto ToWalletDto(this UserWallets userWallet)
{
return new WalletDto
{
UserId = userWallet.UserId,
Name = userWallet.Name,
Balance = userWallet.Balance,
CurrencieId = userWallet.CurrencieId,
IsActive = userWallet.IsActive,
};
}
}
}
Loading