public static Log LogTransaction(object request, Response response, string origin, Exception exception = null) { Log log = new Log(); log.Date = DateTime.Now; log.Origin = origin; log.Id = response.IdReference = Guid.NewGuid(); log.JsonRequest = JsonConvert.SerializeObject(request, Formatting.Indented); log.JsonResponse = JsonConvert.SerializeObject(response, Formatting.Indented); log.Error = response.ErrorCode != ErrorCode.NoError; if (exception != null) { log.JsonError = JsonConvert.SerializeObject(exception, Formatting.Indented); } db.Logs.Add(log); db.SaveChanges(); return(log); }
public static WalletAvgResponse GetDebitAvg(Guid walletId) { Exception exception = null; WalletAvgResponse response = new WalletAvgResponse(); MyJijoWalletContext db = new MyJijoWalletContext(); try { #region Validate //wallets exists if (!db.Wallets.Where(w => w.Id == walletId).Any()) { throw new WalletValException("Wallet does not exist"); } #endregion Validate response.DebitsAvg = db.Transactions.Where(t => t.Wallet.Id == walletId && t.TransactionType.IsDebit).Average(a => a.Amount); } catch (Exception ex) { if (ex is WalletValException) { response.ErrorCode = ErrorCode.Validation; } else { response.ErrorCode = ErrorCode.Other; } response.ErrorMessage = ex.Message; exception = ex; } finally { db.SaveChanges(); LogAccess.LogTransaction(walletId, response, "GetDebitAvg", exception); } return(response); }
public static TransactionResponse AddTransaction(TransactionRequest request) { Exception exception = null; TransactionResponse response = new TransactionResponse(); MyJijoWalletContext db = new MyJijoWalletContext(); try { #region Validate //client exists if (!db.Clients.Where(c => c.Id == request.Client).Any()) { throw new WalletValException("Client does not exist"); } //wallets exists and it belongs to client Wallet wallet = db.Wallets.Where(w => w.Id == request.Wallet && w.Client.Id == request.Client).FirstOrDefault(); if (wallet == null) { throw new Exception("Wallet does not exist"); } //transaction type code exists TransactionType traType = db.TransactionTypes.Where(tt => tt.ExternalCode == request.TransactionCode).FirstOrDefault(); if (traType == null) { throw new Exception("Transaction type code does not exist"); } #endregion Validate Transaction tra = new Transaction(); tra.Amount = request.Amount; tra.Date = DateTime.Now; tra.Id = Guid.NewGuid(); tra.TransactionType = traType; tra.Wallet = wallet; db.Transactions.Add(tra); response.Amount = tra.Amount; } catch (Exception ex) { if (ex is WalletValException) { response.ErrorCode = ErrorCode.Validation; } else { response.ErrorCode = ErrorCode.Other; } response.ErrorMessage = ex.Message; exception = ex; } finally { db.SaveChanges(); LogAccess.LogTransaction(request, response, "AddTransaction", exception); } return(response); }