From 04aeb73220f97a3b2ae313bbb16a6002608b01fb Mon Sep 17 00:00:00 2001 From: minh Date: Fri, 27 Aug 2021 10:44:01 +0700 Subject: [PATCH 1/3] Update Currency --- .../Admin/Controllers/CurrenciesController.cs | 40 +++++++++++++++++-- .../Areas/Admin/Views/Currencies/Index.cshtml | 17 +++++--- Backend/Web.config | 4 +- OnlineBanking.DAL/DataMapping/Currencies.cs | 2 +- .../ViewModel/ProfileViewModel.cs | 2 +- 5 files changed, 53 insertions(+), 12 deletions(-) diff --git a/Backend/Areas/Admin/Controllers/CurrenciesController.cs b/Backend/Areas/Admin/Controllers/CurrenciesController.cs index 2eef53d..52fab87 100644 --- a/Backend/Areas/Admin/Controllers/CurrenciesController.cs +++ b/Backend/Areas/Admin/Controllers/CurrenciesController.cs @@ -155,18 +155,52 @@ public ActionResult PutData(Currencies c) // GET: Admin/Currencies/Delete/5 public ActionResult Delete(int id) { - if (currencies.Delete(id)) + try + { + var currency = currencies.Get(id); + if (currency == null) + { + return Json(new + { + statusCode = 400, + data = "This currency does not exist", + message = "Error" + }, JsonRequestBehavior.AllowGet); + } + + if (currency.BankAccounts.Count > 0) + { + return Json(new + { + statusCode = 400, + data = "This currency have bank account link to it, cannot delete!", + message = "Error" + }, JsonRequestBehavior.AllowGet); + } + + if (currencies.Delete(currency)) + { + return Json(new + { + statusCode = 200, + message = "Success" + }, JsonRequestBehavior.AllowGet); + } + } + catch (System.Exception) { return Json(new { - statusCode = 200, - message = "Success" + statusCode = 400, + data = "Something error happen", + message = "Error" }, JsonRequestBehavior.AllowGet); } return Json(new { statusCode = 400, + data = "Something error happen", message = "Error" }, JsonRequestBehavior.AllowGet); } diff --git a/Backend/Areas/Admin/Views/Currencies/Index.cshtml b/Backend/Areas/Admin/Views/Currencies/Index.cshtml index ec97907..bd6eef5 100644 --- a/Backend/Areas/Admin/Views/Currencies/Index.cshtml +++ b/Backend/Areas/Admin/Views/Currencies/Index.cshtml @@ -117,9 +117,10 @@ if (res.statusCode === 200) { $("#modifyModal").modal("hide"); $('#datatables').DataTable().ajax.reload(); - toastr.success('Created Successfully'); + notifySuccess('Success', 'Created Successfully'); } else { validator.showErrors(res.data); + notifyError('Error', 'Created Fail'); } } @@ -134,9 +135,10 @@ if (res.statusCode === 200) { $("#modifyModal").modal("hide"); $('#datatables').DataTable().ajax.reload(); - toastr.success('Updated Successfully'); + notifySuccess('Success', 'Updated Successfully'); } else { validator.showErrors(res.data); + notifyError('Error', 'Updated Fail'); } } }) @@ -156,9 +158,14 @@ $.ajax({ type: "GET", url: `/Admin/Currencies/Delete/${id}`, - success: function () { - $('#datatables').DataTable().ajax.reload(); - toastr.success('Deleted Successfully'); + success: function (res) { + if (res.statusCode == 200) { + $('#datatables').DataTable().ajax.reload(); + notifySuccess('Success', 'Deleted Successfully'); + } else { + notifyError('Error', res.data); + } + } }) } diff --git a/Backend/Web.config b/Backend/Web.config index 6badf91..352d4ea 100644 --- a/Backend/Web.config +++ b/Backend/Web.config @@ -63,8 +63,8 @@ - - + + diff --git a/OnlineBanking.DAL/DataMapping/Currencies.cs b/OnlineBanking.DAL/DataMapping/Currencies.cs index 30935b7..ca5c7b0 100644 --- a/OnlineBanking.DAL/DataMapping/Currencies.cs +++ b/OnlineBanking.DAL/DataMapping/Currencies.cs @@ -12,6 +12,6 @@ public class Currencies [Required(AllowEmptyStrings = false)] public string Name { get; set; } public int Status { get; set; } - public ICollection BankAccounts { get; set; } + public virtual ICollection BankAccounts { get; set; } } } \ No newline at end of file diff --git a/OnlineBanking.DAL/ViewModel/ProfileViewModel.cs b/OnlineBanking.DAL/ViewModel/ProfileViewModel.cs index 0b2300b..f4a7895 100644 --- a/OnlineBanking.DAL/ViewModel/ProfileViewModel.cs +++ b/OnlineBanking.DAL/ViewModel/ProfileViewModel.cs @@ -14,7 +14,7 @@ public class ProfileViewModel [Required] [MinLength(10)] public string Phone { get; set; } [Required] public string Birthday { get; set; } [Required] public string Address { get; set; } - [Required] [MinLength(10)] public string NumberId { get; set; } + [Required] [MinLength(9)] public string NumberId { get; set; } public string StatusName { get; set; } // active, delete, lock public string RoleName { get; set; } // Quyền } From 1d657ac43a4244198707cba4b2c76c8ec7beb043 Mon Sep 17 00:00:00 2001 From: Tuan Anh Nguyen Manh Date: Fri, 27 Aug 2021 11:17:51 +0700 Subject: [PATCH 2/3] Update create notify cheque --- Backend/Controllers/BankAccountsController.cs | 57 ++-- Backend/Controllers/ChequesController.cs | 249 +++++++++++------- .../Controllers/NotificationsController.cs | 8 +- Backend/Hubs/ChatHub.cs | 21 +- Backend/Views/Home/Index.cshtml | 4 +- .../Transactions/TransactionsDetails.cshtml | 18 +- OnlineBanking.DAL/Common/Status.cs | 2 +- .../ViewModel/NotificationViewModel.cs | 6 +- 8 files changed, 212 insertions(+), 153 deletions(-) diff --git a/Backend/Controllers/BankAccountsController.cs b/Backend/Controllers/BankAccountsController.cs index 5173c2e..46f9de2 100644 --- a/Backend/Controllers/BankAccountsController.cs +++ b/Backend/Controllers/BankAccountsController.cs @@ -28,12 +28,14 @@ public ActionResult Index() [HttpPost] public ActionResult GetBalance(int bankId) { - var data = bankAccounts.Get().Where(x => x.BankAccountId == bankId).Select(x => new BalanceViewModels - { - Balance = x.Balance, - BankId = x.BankAccountId, - Currency = x.Currency.Name - }); + var data = bankAccounts.Get() + .Where(x => x.BankAccountId == bankId) + .Select(x => new BalanceViewModels + { + Balance = x.Balance, + BankId = x.BankAccountId, + Currency = x.Currency.Name + }); return Json(new { @@ -45,11 +47,14 @@ public ActionResult GetBalance(int bankId) public ActionResult GetInfoBankAccount(string name) { - var data = bankAccounts.Get().Where(x => x.Name == name).Select(x => new GetInfoBankAccountViewModels - { - Name = x.Account.Name, - Id = x.BankAccountId - }); + var data = bankAccounts + .Get() + .Where(x => x.Name == name) + .Select(x => new GetInfoBankAccountViewModels + { + Name = x.Account.Name, + Id = x.BankAccountId + }); if (data.FirstOrDefault() == null) { @@ -72,17 +77,20 @@ public ActionResult GetInfoBankAccount(string name) public ActionResult GetData(int Account) { ViewBag.Accounts = "active"; - var data = bankAccounts.Get().Where(a => a.AccountId == Account).Select(x => new BankAccountsViewModels - { - AccountId = x.AccountId, - BankAccountId = x.BankAccountId, - CurrencyId = x.CurrencyId, - CurrencyName = x.Currency.Name, - Name = x.Name, - Balance = x.Balance, - Status = x.Status, - StatusName = ((BankAccountStatus) x.Status).ToString() - }); + var data = bankAccounts + .Get() + .Where(a => a.AccountId == Account) + .Select(x => new BankAccountsViewModels + { + AccountId = x.AccountId, + BankAccountId = x.BankAccountId, + CurrencyId = x.CurrencyId, + CurrencyName = x.Currency.Name, + Name = x.Name, + Balance = x.Balance, + Status = x.Status, + StatusName = ((BankAccountStatus) x.Status).ToString() + }); return Json(new { @@ -95,7 +103,10 @@ public ActionResult GetData(int Account) // GET: Admin/BankAccounts/Details/5 public ActionResult GetStatus() { - var data = Enum.GetValues(typeof(BankAccountStatus)).Cast().Select(v => v.ToString()) + var data = Enum + .GetValues(typeof(BankAccountStatus)) + .Cast() + .Select(v => v.ToString()) .ToArray(); return Json(data, JsonRequestBehavior.AllowGet); diff --git a/Backend/Controllers/ChequesController.cs b/Backend/Controllers/ChequesController.cs index 1bdfec9..72656d9 100644 --- a/Backend/Controllers/ChequesController.cs +++ b/Backend/Controllers/ChequesController.cs @@ -5,6 +5,7 @@ using System.Web; using System.Web.Mvc; using Backend.Areas.Admin.Data; +using Backend.Hubs; using OnlineBanking.BLL.Repositories; using OnlineBanking.DAL; @@ -103,104 +104,133 @@ public ActionResult FindId(int chequeId) [HttpPost] public ActionResult PostData(Cheques chequeInformation) { - var errors = new Dictionary(); - string code; - - - if (!ModelState.IsValid) + using (_context = new ApplicationDbContext()) { - foreach (var k in ModelState.Keys) - foreach (var err in ModelState[k].Errors) + using (var transaction = _context.Database.BeginTransaction()) { - var key = Regex.Replace(k, @"(\w+)\.(\w+)", @"$2"); - if (!errors.ContainsKey(key)) - errors.Add(key, err.ErrorMessage); - } + try + { + var errors = new Dictionary(); + string code; - return Json(new - { - message = "Error", - data = errors, - statusCode = 400, - }, JsonRequestBehavior.AllowGet); - } - var user = (Accounts) Session["user"]; - var account = accounts.Get(user.AccountId); - if (!chequebooks.CheckDuplicate(x => - x.ChequeBookId == chequeInformation.ChequeBookId && x.AccountId == account.AccountId)) - { - return Json(new - { - message = "Error", - statusCode = 404, - }, JsonRequestBehavior.AllowGet); - } + if (!ModelState.IsValid) + { + foreach (var k in ModelState.Keys) + foreach (var err in ModelState[k].Errors) + { + var key = Regex.Replace(k, @"(\w+)\.(\w+)", @"$2"); + if (!errors.ContainsKey(key)) + errors.Add(key, err.ErrorMessage); + } - var fromBankAccount = bankAccounts.Get(chequeInformation.FromBankAccountId); + return Json(new + { + message = "Error", + data = errors, + statusCode = 400, + }, JsonRequestBehavior.AllowGet); + } - if (fromBankAccount.Status != (int) BankAccountStatus.Actived) - { - errors.Add("FromBankAccountId", "This bank account is not actived"); - return Json(new - { - message = "Error", - data = errors, - statusCode = 400 - }, JsonRequestBehavior.AllowGet); - } + var user = (Accounts) Session["user"]; + var account = _context.Accounts.FirstOrDefault(x => x.AccountId == user.AccountId); + if (!_context.ChequeBooks.AsNoTracking().Any(x => + x.ChequeBookId == chequeInformation.ChequeBookId && x.AccountId == account.AccountId)) + { + return Json(new + { + message = "Error", + statusCode = 404, + }, JsonRequestBehavior.AllowGet); + } - var chequeBook = chequebooks.Get(chequeInformation.ChequeBookId); - if (chequeBook.Status != (int) ChequeBookStatus.Opened) - { - return Json(new - { - message = "Error", - data = "This cheque book is not opened", - statusCode = 400 - }, JsonRequestBehavior.AllowGet); - } + var fromBankAccount = _context.BankAccounts.FirstOrDefault(x => + x.BankAccountId == chequeInformation.FromBankAccountId); - do - { - code = Utils.RandomString(16); - } while (cheques.CheckDuplicate(x => x.Code == code)); + if (fromBankAccount != null && fromBankAccount.Status != (int) BankAccountStatus.Actived) + { + errors.Add("FromBankAccountId", "This bank account is not actived"); + return Json(new + { + message = "Error", + data = errors, + statusCode = 400 + }, JsonRequestBehavior.AllowGet); + } - chequeInformation.Code = code; - chequeInformation.Status = (int) ChequeStatus.Actived; + var chequeBook = + _context.ChequeBooks.FirstOrDefault(x => x.ChequeBookId == chequeInformation.ChequeBookId); - if (fromBankAccount.Balance < chequeInformation.Amount) - { - errors.Add("Amount", "Your balance is not enough"); - return Json(new - { - message = "Error", - data = errors, - statusCode = 400 - }, JsonRequestBehavior.AllowGet); - } + if (chequeBook != null && chequeBook.Status != (int) ChequeBookStatus.Opened) + { + return Json(new + { + message = "Error", + data = "This cheque book is not opened", + statusCode = 400 + }, JsonRequestBehavior.AllowGet); + } - if (0 > chequeInformation.Amount) - { - errors.Add("Amount", "Please enter a positive number"); - return Json(new - { - message = "Error", - data = errors, - statusCode = 400 - }, JsonRequestBehavior.AllowGet); - } + do + { + code = Utils.RandomString(16); + } while (_context.Cheques.AsNoTracking().Any(x => x.Code == code)); - if (!cheques.Add(chequeInformation)) - return Json(new - { - message = "Error", - statusCode = 400, - data = ModelState - }, JsonRequestBehavior.AllowGet); + chequeInformation.Code = code; + chequeInformation.Status = (int) ChequeStatus.Actived; + + if (fromBankAccount.Balance < chequeInformation.Amount) + { + errors.Add("Amount", "Your balance is not enough"); + return Json(new + { + message = "Error", + data = errors, + statusCode = 400 + }, JsonRequestBehavior.AllowGet); + } + + if (0 > chequeInformation.Amount) + { + errors.Add("Amount", "Please enter a positive number"); + return Json(new + { + message = "Error", + data = errors, + statusCode = 400 + }, JsonRequestBehavior.AllowGet); + } + + // cheques.Add(chequeInformation); + _context.Cheques.Add(chequeInformation); + _context.SaveChanges(); + + fromBankAccount.Balance -= chequeInformation.Amount; + // bankAccounts.Update(fromBankAccount); + _context.SaveChanges(); + + var message = "Your account balance -" + chequeInformation.Amount + + ", available balance: " + fromBankAccount.Balance; + + var newNotifications = CreateNotification(chequeInformation, message); + + transaction.Commit(); - fromBankAccount.Balance -= chequeInformation.Amount; - bankAccounts.Update(fromBankAccount); + ChatHub.Instance().SendNotifications(newNotifications); + } + catch (Exception ex) + { + transaction.Rollback(); + return Json(new + { + data = ex, + message = "error", + statuscode = 404 + }, JsonRequestBehavior.AllowGet); + throw; + } + } + } return Json(new { @@ -343,40 +373,67 @@ public ActionResult DeleteData(int chequeId) var fromBankAccount = _context.BankAccounts.FirstOrDefault(x => x.BankAccountId == cheque.FromBankAccountId); - + List newNotifications = null; if (fromBankAccount != null) { fromBankAccount.Balance += cheque.Amount; _context.SaveChanges(); + var message = "Your account balance +" + cheque.Amount + + ", available balance: " + cheque.FromBankAccount.Balance; + + newNotifications = CreateNotification(cheque, message); + // cheques.Delete(cheque); // bankAccounts.Edit(fromBankAccount); - _context.Cheques.Remove(cheque); - _context.SaveChanges(); } - + + _context.Cheques.Remove(cheque); + _context.SaveChanges(); + transaction.Commit(); - return Json(new - { - message = "Success", - data = "Delete Successfully", - statusCode = 200, - }, JsonRequestBehavior.AllowGet); + ChatHub.Instance().SendNotifications(newNotifications); } catch (Exception ex) { transaction.Rollback(); + return Json(new { data = ex, message = "error", statuscode = 404 }, JsonRequestBehavior.AllowGet); - throw; } + + return Json(new + { + message = "Success", + data = "Delete Successfully", + statusCode = 200, + }, JsonRequestBehavior.AllowGet); } } } + + private List CreateNotification(Cheques cheque, string message) + { + var lstNotification = new List() + { + new Notifications + { + AccountId = cheque.FromBankAccount.AccountId, + Content = message, + Status = (int) NotificationStatus.Unread, + PkType = (int) NotificationType.Cheque, + PkId = cheque.ChequeBookId, + } + }; + + _context.Set().AddRange(lstNotification); + _context.SaveChanges(); + return lstNotification; + } } } \ No newline at end of file diff --git a/Backend/Controllers/NotificationsController.cs b/Backend/Controllers/NotificationsController.cs index 764df5e..bccefc1 100644 --- a/Backend/Controllers/NotificationsController.cs +++ b/Backend/Controllers/NotificationsController.cs @@ -37,13 +37,7 @@ public ActionResult GetData() var data = notifications.Get() .Where(x => x.AccountId == currentUser.AccountId) .OrderByDescending(x => x.CreatedAt) - .Select(x => - { - var pkObject = transactionDetails - .Get().FirstOrDefault(y => y.TransactionDetailId == x.PkId); - - return new NotificationViewModel(x, pkObject); - }); + .Select(x => new NotificationViewModel(x)); return Json(new { diff --git a/Backend/Hubs/ChatHub.cs b/Backend/Hubs/ChatHub.cs index b87424f..af8ab5e 100644 --- a/Backend/Hubs/ChatHub.cs +++ b/Backend/Hubs/ChatHub.cs @@ -146,13 +146,7 @@ public IEnumerable GetNotificationsHistory(int accountId) .OrderByDescending(m => m.CreatedAt) .Take(5) .AsEnumerable() - .Select(x => - { - var pkObject = transactionDetailRepo - .Get().FirstOrDefault(y => y.TransactionDetailId == x.PkId); - - return new NotificationViewModel(x, pkObject); - }); + .Select(x => new NotificationViewModel(x)); } public string ReadNotification(int notificationId) @@ -173,9 +167,13 @@ public string ReadNotification(int notificationId) _context.SaveChanges(); } - if (notification.PkType == (int) NotificationType.Transaction) + switch (notification.PkType) { - return "/Transactions/TransactionsDetails/" + notification.PkId; + case (int) NotificationType.Transaction: + return "/Transactions/TransactionsDetails/" + notification.PkId; + + case (int) NotificationType.Cheque: + return "/Cheques/?ChequeBookId=" + notification.PkId; } } catch (Exception ex) @@ -195,11 +193,8 @@ public void SendNotifications(List notifications) notifications.ForEach(x => { - var pkObject = transactionDetailRepo.Get() - .FirstOrDefault(y => y.TransactionDetailId == x.PkId); - context.Clients.Group("user-" + x.AccountId) - .newNotification(new NotificationViewModel(x, pkObject)); + .newNotification(new NotificationViewModel(x)); // await context.Clients.Group("user-" + x.AccountId) // .historyNotifications(GetNotificationsHistory(GetIntegerAccountId())); }); diff --git a/Backend/Views/Home/Index.cshtml b/Backend/Views/Home/Index.cshtml index 5611886..4cc26df 100644 --- a/Backend/Views/Home/Index.cshtml +++ b/Backend/Views/Home/Index.cshtml @@ -28,9 +28,9 @@ - - + + diff --git a/Backend/Views/Transactions/TransactionsDetails.cshtml b/Backend/Views/Transactions/TransactionsDetails.cshtml index 252c4d4..ad30696 100644 --- a/Backend/Views/Transactions/TransactionsDetails.cshtml +++ b/Backend/Views/Transactions/TransactionsDetails.cshtml @@ -4,7 +4,6 @@ @{ ViewBag.Title = "title"; Layout = "~/Views/Shared/_Layout.cshtml"; - var user = (Accounts) Session["user"]; }
@@ -29,10 +28,8 @@
  • Amount - - @Model.Currency - @(Model.Type == 1 ? "-" : "+") @Model.Amount - + @* @Model.Currency *@ +
  • Beneficiary @Model.ToName @@ -48,4 +45,13 @@
  • - \ No newline at end of file + + +@section scripts{ + +} \ No newline at end of file diff --git a/OnlineBanking.DAL/Common/Status.cs b/OnlineBanking.DAL/Common/Status.cs index 4154cb8..3c0ed46 100644 --- a/OnlineBanking.DAL/Common/Status.cs +++ b/OnlineBanking.DAL/Common/Status.cs @@ -65,7 +65,7 @@ public enum NotificationStatus public enum NotificationType { Transaction = 0, - Message = 1, + Cheque = 1, } public enum TransactionType { diff --git a/OnlineBanking.DAL/ViewModel/NotificationViewModel.cs b/OnlineBanking.DAL/ViewModel/NotificationViewModel.cs index be32842..465b610 100644 --- a/OnlineBanking.DAL/ViewModel/NotificationViewModel.cs +++ b/OnlineBanking.DAL/ViewModel/NotificationViewModel.cs @@ -12,7 +12,7 @@ public NotificationViewModel() { } - public NotificationViewModel(Notifications notification, TransactionDetails transactionDetails) + public NotificationViewModel(Notifications notification) { NotificationId = notification.NotificationId; AccountId = notification.AccountId; @@ -24,10 +24,6 @@ public NotificationViewModel(Notifications notification, TransactionDetails tran PkType = notification.PkType; PkTypeName = ((NotificationType) notification.PkType).ToString(); - var obj = new TransactionsViewModels(transactionDetails, transactionDetails.Transaction); - PkId = obj.TransactionDetailId; - PkObject = obj; - CreatedAt = notification.CreatedAt?.ToString("dd-MM-yyyy HH:mm:ss"); UpdatedAt = notification.UpdatedAt?.ToString("dd-MM-yyyy HH:mm:ss"); } From b5694ed1ca6c81e74a504a8fa68c7b7de0aa5a02 Mon Sep 17 00:00:00 2001 From: Tuan Anh Nguyen Manh Date: Fri, 27 Aug 2021 11:26:19 +0700 Subject: [PATCH 3/3] Update notify cheque admin --- .../Admin/Controllers/ChequesController.cs | 83 ++++++++++++++----- Backend/Controllers/ChequesController.cs | 20 +++-- 2 files changed, 73 insertions(+), 30 deletions(-) diff --git a/Backend/Areas/Admin/Controllers/ChequesController.cs b/Backend/Areas/Admin/Controllers/ChequesController.cs index 81e5d85..2b180c5 100644 --- a/Backend/Areas/Admin/Controllers/ChequesController.cs +++ b/Backend/Areas/Admin/Controllers/ChequesController.cs @@ -114,7 +114,7 @@ public ActionResult PostData(Cheques chequeInformation) var fromBankAccount = _context.BankAccounts.FirstOrDefault(x => x.BankAccountId == chequeInformation.FromBankAccountId); - if (fromBankAccount.Status != (int) BankAccountStatus.Actived) + if (fromBankAccount != null && fromBankAccount.Status != (int) BankAccountStatus.Actived) { errors.Add("FromBankAccountId", "This bank account is not actived"); return Json(new @@ -127,7 +127,7 @@ public ActionResult PostData(Cheques chequeInformation) var chequeBook = _context.ChequeBooks.FirstOrDefault(x => x.ChequeBookId == chequeInformation.ChequeBookId); - if (chequeBook.Status != (int) ChequeBookStatus.Opened) + if (chequeBook != null && chequeBook.Status != (int) ChequeBookStatus.Opened) { return Json(new { @@ -145,7 +145,7 @@ public ActionResult PostData(Cheques chequeInformation) chequeInformation.Code = code; chequeInformation.Status = (int) ChequeStatus.Actived; - if (fromBankAccount.Balance < chequeInformation.Amount) + if (fromBankAccount != null && fromBankAccount.Balance < chequeInformation.Amount) { errors.Add("Amount", "Your balance is not enough"); return Json(new @@ -171,18 +171,23 @@ public ActionResult PostData(Cheques chequeInformation) _context.Cheques.Add(chequeInformation); _context.SaveChanges(); - fromBankAccount.Balance -= chequeInformation.Amount; - // bankAccounts.Update(fromBankAccount); - // _context.Entry(fromBankAccount).State = EntityState.Modified; - _context.SaveChanges(); + List newNotifications = null; + if (fromBankAccount != null) + { + fromBankAccount.Balance -= chequeInformation.Amount; + // bankAccounts.Update(fromBankAccount); + // _context.Entry(fromBankAccount).State = EntityState.Modified; + _context.SaveChanges(); + + var message = "Your account balance -" + chequeInformation.Amount + + ", available balance: " + fromBankAccount.Balance; + + newNotifications = CreateNotification(chequeInformation, message); + } transaction.Commit(); - return Json(new - { - message = "Success", - statusCode = 200, - }, JsonRequestBehavior.AllowGet); + ChatHub.Instance().SendNotifications(newNotifications); } catch (Exception ex) { @@ -193,8 +198,13 @@ public ActionResult PostData(Cheques chequeInformation) message = "error", statuscode = 404 }, JsonRequestBehavior.AllowGet); - throw; } + + return Json(new + { + message = "Success", + statusCode = 200, + }, JsonRequestBehavior.AllowGet); } } } @@ -435,7 +445,7 @@ public ActionResult ChequeExec(ChequesExecViewModel chequeExec) var newNotifications = CreateNotifications(newTransaction); transaction.Commit(); - + ChatHub.Instance().SendNotifications(newNotifications); return Json(new @@ -573,6 +583,8 @@ public ActionResult DeleteData(int chequeId) var fromBankAccount = _context.BankAccounts.FirstOrDefault(x => x.BankAccountId == cheque.FromBankAccountId); + List newNotifications = null; + if (fromBankAccount != null) { fromBankAccount.Balance += cheque.Amount; @@ -580,33 +592,58 @@ public ActionResult DeleteData(int chequeId) // cheques.Delete(cheque); // bankAccounts.Edit(fromBankAccount); - _context.Cheques.Remove(cheque); - _context.SaveChanges(); + + var message = "Your account balance +" + cheque.Amount + + ", available balance: " + cheque.FromBankAccount.Balance; + + newNotifications = CreateNotification(cheque, message); } + _context.Cheques.Remove(cheque); _context.SaveChanges(); transaction.Commit(); - return Json(new - { - message = "Success", - data = "Delete Successfully", - statusCode = 200, - }, JsonRequestBehavior.AllowGet); + ChatHub.Instance().SendNotifications(newNotifications); } catch (Exception ex) { transaction.Rollback(); + return Json(new { data = ex, message = "error", statuscode = 404 }, JsonRequestBehavior.AllowGet); - throw; } + + return Json(new + { + message = "Success", + data = "Delete Successfully", + statusCode = 200, + }, JsonRequestBehavior.AllowGet); } } } + + private List CreateNotification(Cheques cheque, string message) + { + var lstNotification = new List() + { + new Notifications + { + AccountId = cheque.FromBankAccount.AccountId, + Content = message, + Status = (int) NotificationStatus.Unread, + PkType = (int) NotificationType.Cheque, + PkId = cheque.ChequeBookId, + } + }; + + _context.Set().AddRange(lstNotification); + _context.SaveChanges(); + return lstNotification; + } } } \ No newline at end of file diff --git a/Backend/Controllers/ChequesController.cs b/Backend/Controllers/ChequesController.cs index 72656d9..f74e9f5 100644 --- a/Backend/Controllers/ChequesController.cs +++ b/Backend/Controllers/ChequesController.cs @@ -179,7 +179,7 @@ public ActionResult PostData(Cheques chequeInformation) chequeInformation.Code = code; chequeInformation.Status = (int) ChequeStatus.Actived; - if (fromBankAccount.Balance < chequeInformation.Amount) + if (fromBankAccount != null && fromBankAccount.Balance < chequeInformation.Amount) { errors.Add("Amount", "Your balance is not enough"); return Json(new @@ -205,14 +205,18 @@ public ActionResult PostData(Cheques chequeInformation) _context.Cheques.Add(chequeInformation); _context.SaveChanges(); - fromBankAccount.Balance -= chequeInformation.Amount; - // bankAccounts.Update(fromBankAccount); - _context.SaveChanges(); + List newNotifications = null; + if (fromBankAccount != null) + { + fromBankAccount.Balance -= chequeInformation.Amount; + // bankAccounts.Update(fromBankAccount); + _context.SaveChanges(); - var message = "Your account balance -" + chequeInformation.Amount + - ", available balance: " + fromBankAccount.Balance; + var message = "Your account balance -" + chequeInformation.Amount + + ", available balance: " + fromBankAccount.Balance; - var newNotifications = CreateNotification(chequeInformation, message); + newNotifications = CreateNotification(chequeInformation, message); + } transaction.Commit(); @@ -373,7 +377,9 @@ public ActionResult DeleteData(int chequeId) var fromBankAccount = _context.BankAccounts.FirstOrDefault(x => x.BankAccountId == cheque.FromBankAccountId); + List newNotifications = null; + if (fromBankAccount != null) { fromBankAccount.Balance += cheque.Amount;
    Account NumberCurrencyId BalanceStatus CurrencyStatus