-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathWalletRepository.cs
More file actions
54 lines (45 loc) · 1.53 KB
/
WalletRepository.cs
File metadata and controls
54 lines (45 loc) · 1.53 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
using Kata.Wallet.Database;
using Kata.Wallet.Domain;
using Microsoft.EntityFrameworkCore;
using System.Collections.Generic;
using System.Linq;
using System.Threading.Tasks;
namespace Kata.Wallet.Persistence.Repositories
{
public class WalletRepository : IWalletRepository
{
private readonly DataContext _context;
public WalletRepository(DataContext context)
{
_context = context;
}
public async Task<Wallet> CreateAsync(Wallet wallet)
{
_context.Wallets.Add(wallet);
await _context.SaveChangesAsync();
return wallet;
}
public async Task<Wallet?> GetByIdAsync(int id)
{
return await _context.Wallets.FindAsync(id);
}
public async Task<Wallet?> GetByDocumentAndCurrencyAsync(string userDocument, string currency)
{
return await _context.Wallets
.FirstOrDefaultAsync(w => w.UserDocument == userDocument && w.Currency == currency);
}
public async Task<IEnumerable<Wallet>> GetAllAsync(string? currency, string? userDocument)
{
var query = _context.Wallets.AsQueryable();
if (!string.IsNullOrEmpty(currency))
{
query = query.Where(w => w.Currency == currency);
}
if (!string.IsNullOrEmpty(userDocument))
{
query = query.Where(w => w.UserDocument == userDocument);
}
return await query.ToListAsync();
}
}
}