|
| 1 | +using MerchantPos.EF.Models; |
| 2 | +using Microsoft.EntityFrameworkCore; |
| 3 | + |
| 4 | +namespace MerchantPos.EF.Persistence |
| 5 | +{ |
| 6 | + public interface IEfRepository |
| 7 | + { |
| 8 | + Task<List<Merchant>> GetAllMerchants(); |
| 9 | + Task<Decimal> GetBalance(Guid merchantId); |
| 10 | + Task<Merchant> CreateMerchantRecord(Guid merchantId, String merchantName); |
| 11 | + Task UpdateBalance(Guid merchantId, String merchantName, Decimal balance); |
| 12 | + Task UpdateLastEndOfDay(Guid merchantId, String merchantName, DateTime lastEndOfDayDateTime); |
| 13 | + Task UpdateLastLogon(Guid merchantId, String merchantName, DateTime lastLogonDateTime); |
| 14 | + Task UpdateTotals(Guid merchantId, Guid operatorId, Guid contractId, Decimal amount); |
| 15 | + Task<List<OperatorTotal>> GetTotals(Guid merchantId); |
| 16 | + Task ClearTotals(Guid merchantId); |
| 17 | + Task<Merchant> GetMerchant(Guid merchantId); |
| 18 | + |
| 19 | + Task IncrementTransactionNumber(Guid merchantId, String merchantName); |
| 20 | + } |
| 21 | + |
| 22 | + public class EfRepository : IEfRepository |
| 23 | + { |
| 24 | + private readonly MerchantDbContext _db; |
| 25 | + |
| 26 | + public EfRepository(MerchantDbContext db) |
| 27 | + { |
| 28 | + _db = db; |
| 29 | + } |
| 30 | + |
| 31 | + public async Task<Merchant> CreateMerchantRecord(Guid merchantId, |
| 32 | + String merchantName) { |
| 33 | + Merchant merchant = new Merchant(); |
| 34 | + merchant.MerchantId = merchantId; |
| 35 | + merchant.MerchantName = merchantName; |
| 36 | + merchant.Balance = 0; |
| 37 | + merchant.TransactionNumber = 0; |
| 38 | + merchant.LastEndOfDayDateTime = DateTime.MinValue; |
| 39 | + merchant.LastLogonDateTime = DateTime.MinValue; |
| 40 | + _db.Merchants.Add(merchant); |
| 41 | + await _db.SaveChangesAsync(); |
| 42 | + return merchant; |
| 43 | + } |
| 44 | + |
| 45 | + public async Task UpdateBalance(Guid merchantId,String merchantName, Decimal balance) |
| 46 | + { |
| 47 | + Merchant? entry = await this.GetMerchant(merchantId); |
| 48 | + |
| 49 | + if (entry == null) { |
| 50 | + entry = await this.CreateMerchantRecord(merchantId, merchantName); |
| 51 | + } |
| 52 | + |
| 53 | + entry.MerchantName = merchantName; |
| 54 | + entry.Balance = balance; |
| 55 | + |
| 56 | + await _db.SaveChangesAsync(); |
| 57 | + } |
| 58 | + |
| 59 | + public async Task<Merchant?> GetMerchant(Guid merchantId) |
| 60 | + { |
| 61 | + return await _db.Merchants.FindAsync(merchantId); |
| 62 | + } |
| 63 | + |
| 64 | + public async Task IncrementTransactionNumber(Guid merchantId, |
| 65 | + String merchantName) { |
| 66 | + Merchant? entry = await this.GetMerchant(merchantId); |
| 67 | + |
| 68 | + if (entry == null) |
| 69 | + { |
| 70 | + entry = await this.CreateMerchantRecord(merchantId, merchantName); |
| 71 | + } |
| 72 | + |
| 73 | + Int32 nextTransactionNumber = entry.TransactionNumber + 1; |
| 74 | + if (nextTransactionNumber == 9999) { |
| 75 | + nextTransactionNumber = 1; |
| 76 | + } |
| 77 | + entry.TransactionNumber = nextTransactionNumber; |
| 78 | + await _db.SaveChangesAsync(); |
| 79 | + } |
| 80 | + |
| 81 | + public async Task UpdateLastEndOfDay(Guid merchantId, String merchantName, DateTime lastEndOfDayDateTime) |
| 82 | + { |
| 83 | + Merchant? entry = await this.GetMerchant(merchantId); |
| 84 | + |
| 85 | + if (entry == null) |
| 86 | + { |
| 87 | + entry = await this.CreateMerchantRecord(merchantId, merchantName); |
| 88 | + } |
| 89 | + entry.LastEndOfDayDateTime = lastEndOfDayDateTime; |
| 90 | + |
| 91 | + await _db.SaveChangesAsync(); |
| 92 | + } |
| 93 | + |
| 94 | + public async Task UpdateLastLogon(Guid merchantId, |
| 95 | + String merchantName, |
| 96 | + DateTime lastLogonDateTime) { |
| 97 | + Merchant? entry = await this.GetMerchant(merchantId); |
| 98 | + |
| 99 | + if (entry == null) |
| 100 | + { |
| 101 | + entry = await this.CreateMerchantRecord(merchantId, merchantName); |
| 102 | + } |
| 103 | + entry.LastLogonDateTime = lastLogonDateTime; |
| 104 | + |
| 105 | + await _db.SaveChangesAsync(); |
| 106 | + } |
| 107 | + |
| 108 | + |
| 109 | + public async Task<List<Merchant>> GetAllMerchants() { |
| 110 | + var entries = await _db.Merchants.ToListAsync(); |
| 111 | + return entries.ToList(); |
| 112 | + } |
| 113 | + |
| 114 | + public async Task<decimal> GetBalance(Guid merchantId) |
| 115 | + { |
| 116 | + Merchant? entry = await this.GetMerchant(merchantId); |
| 117 | + return entry?.Balance ?? 0; |
| 118 | + } |
| 119 | + |
| 120 | + public async Task<DateTime> GetLastEndOfDay(Guid merchantId) |
| 121 | + { |
| 122 | + Merchant? entry = await this.GetMerchant(merchantId); |
| 123 | + return entry?.LastEndOfDayDateTime ?? DateTime.MinValue; |
| 124 | + } |
| 125 | + |
| 126 | + public async Task UpdateTotals(Guid merchantId, Guid operatorId, Guid contractId, decimal amount) |
| 127 | + { |
| 128 | + OperatorTotal? entry = await _db.OperatorTotals |
| 129 | + .SingleOrDefaultAsync(o => o.MerchantId == merchantId && o.OperatorId == operatorId |
| 130 | + && o.ContractId == contractId); |
| 131 | + |
| 132 | + if (entry == null) |
| 133 | + { |
| 134 | + entry = new OperatorTotal |
| 135 | + { |
| 136 | + MerchantId = merchantId, |
| 137 | + OperatorId = operatorId, |
| 138 | + ContractId = contractId, |
| 139 | + Total = amount |
| 140 | + }; |
| 141 | + _db.OperatorTotals.Add(entry); |
| 142 | + } |
| 143 | + else |
| 144 | + { |
| 145 | + entry.Total += amount; |
| 146 | + } |
| 147 | + |
| 148 | + await _db.SaveChangesAsync(); |
| 149 | + } |
| 150 | + |
| 151 | + public async Task<List<OperatorTotal>> GetTotals(Guid merchantId) |
| 152 | + { |
| 153 | + return await _db.OperatorTotals |
| 154 | + .Where(o => o.MerchantId == merchantId) |
| 155 | + .ToListAsync(); |
| 156 | + } |
| 157 | + |
| 158 | + public async Task ClearTotals(Guid merchantId) |
| 159 | + { |
| 160 | + IQueryable<OperatorTotal> rows = _db.OperatorTotals.Where(o => o.MerchantId == merchantId); |
| 161 | + _db.OperatorTotals.RemoveRange(rows); |
| 162 | + await _db.SaveChangesAsync(); |
| 163 | + } |
| 164 | + } |
| 165 | +} |
| 166 | + |
0 commit comments