-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathTransactionRepository.cs
More file actions
59 lines (55 loc) · 2.15 KB
/
Copy pathTransactionRepository.cs
File metadata and controls
59 lines (55 loc) · 2.15 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
55
56
57
58
59
using api.Data;
using api.Dtos;
using api.Interfaces;
using api.Models;
using Microsoft.EntityFrameworkCore;
namespace api.Repository
{
public class UserTransactionRepository : IUserTransactionRepository
{
private readonly AppDbContext _context;
public UserTransactionRepository(AppDbContext context)
{
_context = context;
}
public async Task<List<UserTransaction>> GetAllAsync(int pageNumber, int PageSize)
{
var transactions = _context.Transactions.AsQueryable();
return await transactions.Skip(pageNumber - 1).Take(PageSize).ToListAsync();
}
public async Task<UserTransaction?> GetByIdAsync(int id)
{
var userTransaction = await _context.Transactions.FirstOrDefaultAsync(userTransaction => userTransaction.Id == id);
return userTransaction;
}
public async Task<UserTransaction> AddAsync(UserTransaction userTransaction)
{
using var transaction = _context.Database.BeginTransaction();
userTransaction.Sender.Salary -= userTransaction.Amount;
userTransaction.Receiver.Salary += userTransaction.Amount;
await _context.Transactions.AddAsync(userTransaction);
await _context.SaveChangesAsync();
await transaction.CommitAsync();
// Delete
Console.WriteLine("Iddd" + userTransaction.Id);
var doneTrans = await _context.Transactions.FirstOrDefaultAsync(doneTrans => doneTrans.Id == userTransaction.Id);
if (doneTrans != null)
{
Console.WriteLine("In add" + doneTrans.Receiver.Id);
}
else
{
Console.WriteLine("null");
}
return userTransaction;
}
public async Task<User?> GetSenderByIdAsync(int id)
{
return await _context.Users.FirstOrDefaultAsync(user => user.Id == id);
}
public async Task<User?> GetReceiverByIdAsync(int id)
{
return await _context.Users.FirstOrDefaultAsync(user => user.Id == id);
}
}
}