Skip to content

Commit

Permalink
Browse files Browse the repository at this point in the history
…into minh
  • Loading branch information
mizhm committed Aug 26, 2021
2 parents 81036a4 + a63aeca commit 56dbad8
Show file tree
Hide file tree
Showing 26 changed files with 788 additions and 187 deletions.
184 changes: 183 additions & 1 deletion Backend/Areas/Admin/Controllers/HomeController.cs
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text.RegularExpressions;
using System.Web;
using System.Web.Mvc;
using Backend.Areas.Admin.Data;
Expand Down Expand Up @@ -53,6 +54,186 @@ public ActionResult GetData()
}, JsonRequestBehavior.AllowGet);
}

public ActionResult InfoAccountData()
{
if ((Accounts) Session["user"] == null) return RedirectToAction("Login");
var userId = ((Accounts) Session["user"]).AccountId;
var acc = accounts.Get(userId);
var account = new ProfileViewModel
{
Name = acc.Name,
Email = acc.Email,
Phone = acc.Phone,
Birthday = acc.Birthday?.ToString("yyyy-MM-dd"),
RoleName = acc.RoleId != 2 ? acc.Role.Name : null,
NumberId = acc.NumberId,
StatusName = ((AccountStatus) acc.Status).ToString(),
Address = acc.Address,
};

return Json(
new
{
data = account,
message = "Success",
statusCode = 200
}, JsonRequestBehavior.AllowGet);
}

public ActionResult InfoAccount()
{
return View();
}

[HttpPost]
public ActionResult UpdateInfo(ProfileViewModel acc)
{
var errors = new Dictionary<string, string>();
var check = true;
var userId = ((Accounts) Session["user"]).AccountId;

if (!Utils.IsNullOrEmpty(acc.Birthday))
{
if (DateTime.TryParse(acc.Birthday, out var dateTime))
{
var today = DateTime.Today;
var age = today.Year - DateTime.Parse(acc.Birthday).Year;
if (age < 18)
{
check = false;
errors.Add("Birthday", "Your age must be greater than 18");
}
}
else
{
check = false;
errors.Add("Birthday", "Your birthday is not valid!");
}
}

if (accounts.CheckDuplicate(x => x.Email == acc.Email && x.AccountId != userId))
{
check = false;
errors.Add("Email", "Your email has been used!");
}

if (!Utils.IsNullOrEmpty(acc.Phone) &&
accounts.CheckDuplicate(x => x.Phone == acc.Phone && x.AccountId != userId))
{
check = false;
errors.Add("Phone", "Your phone has been used!");
}

if (!Utils.IsNullOrEmpty(acc.NumberId) &&
accounts.CheckDuplicate(x => x.NumberId == acc.NumberId && x.AccountId != userId))
{
check = false;
errors.Add("NumberID", "Your NumberId has been used!");
}

if (ModelState.IsValid && check)
{
var acc1 = accounts.Get(((Accounts) Session["user"]).AccountId);
acc1.Name = acc.Name;
acc1.Email = acc.Email;
acc1.Phone = acc.Phone;
acc1.Birthday = Utils.IsNullOrEmpty(acc.Birthday) ? acc1.Birthday : DateTime.Parse(acc.Birthday);
acc1.Address = acc.Address;
acc1.NumberId = acc.NumberId;
if (!accounts.Edit(acc1))
{
return Json(new
{
statusCode = 400,
message = "Error"
}, JsonRequestBehavior.AllowGet);
}

return Json(new
{
statusCode = 200,
message = "Success"
}, JsonRequestBehavior.AllowGet);
}

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);
}

return Json(new
{
statusCode = 400,
message = "Error",
data = errors
}, JsonRequestBehavior.AllowGet);
}


public ActionResult ChangePassword(ChangePasswordViewModel changePasswordViewModel)
{
var errors = new Dictionary<string, string>();
var user = (Accounts) Session["user"];
var userUpdate = accounts.Get(user.AccountId);
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);
}

if (!ModelState.IsValid)
return Json(new
{
data = errors,
statusCode = 400,
message = "Error",
}, JsonRequestBehavior.AllowGet);

if (!changePasswordViewModel.OldPassword.Equals(userUpdate.Password))
{
errors.Add("OldPassword", "Your password is not correct!");
return Json(new
{
data = errors,
statusCode = 400,
message = "Error",
}, JsonRequestBehavior.AllowGet);
}

if (!changePasswordViewModel.NewPassword.Equals(changePasswordViewModel.ConfirmPassword))
{
errors.Add("ConfirmPassword", "Your confirm is not the same as your new password!");
return Json(new
{
data = errors,
statusCode = 400,
message = "Error",
}, JsonRequestBehavior.AllowGet);
}

userUpdate.Password = changePasswordViewModel.NewPassword;
if (!accounts.Edit(userUpdate))
{
return Json(new
{
data = errors,
statusCode = 400,
message = "Error",
}, JsonRequestBehavior.AllowGet);
}

return Json(new
{
statusCode = 200,
message = "Change Password Successfully",
}, JsonRequestBehavior.AllowGet);
}

public ActionResult GetTransactions(DateTime? startDate, DateTime? endDate)
{
var data = transactionDetail.Get();
Expand All @@ -62,7 +243,8 @@ public ActionResult GetTransactions(DateTime? startDate, DateTime? endDate)
data = data.Where(x => x.CreatedAt >= startDate && x.CreatedAt <= endDate);
}

var result = data.OrderByDescending(x => x.CreatedAt).Select(x => new TransactionsViewModels(x,x.Transaction));
var result = data.OrderByDescending(x => x.CreatedAt)
.Select(x => new TransactionsViewModels(x, x.Transaction));
return Json(new
{
data = result.ToList(),
Expand Down
1 change: 0 additions & 1 deletion Backend/Areas/Admin/Views/Accounts/ProfileAccount.cshtml
Original file line number Diff line number Diff line change
Expand Up @@ -352,7 +352,6 @@
$("#Id").val(res.BankAccountId);
$("#Name").val(res.Name);
}
})
},
post: function (bank) {
Expand Down
Loading

0 comments on commit 56dbad8

Please sign in to comment.