public IActionResult CreateAccount([FromBody] GeneralInformationWithRoles generalInfoWithRoles, [FromHeader] string Authorization, [FromHeader] string Role) { if (CheckToken(Authorization) == true && CheckPermission(Role) == true) { if (ModelState.IsValid) { Account account = new Account(); account.GeneralInformation = generalInfoWithRoles.GeneralInformation; foreach (var id in generalInfoWithRoles.RoleIds) { var role = _context.Role.Find(id); RoleAccount roleAccount = new RoleAccount { Role = role, Account = account }; _context.Add(roleAccount); } _context.Add(account); _context.Add(account.GeneralInformation); _context.SaveChanges(); account.RollNumber = "B19APTECH" + account.AccountId.ToString("D4"); account.Password = account.GeneralInformation.Dob.ToString("ddMMyy"); account.EncryptPassword(account.Password); _context.Update(account); _context.SaveChanges(); return(new JsonResult(account.GeneralInformation.Dob.ToString("ddMMyy"))); } //return new JsonResult(generalInfoWithRoles); } return(Unauthorized()); }
public IActionResult EditAccount([FromBody] GeneralInformationWithRoles generalInfoWithRoles, [FromHeader] string Authorization) { if (CheckToken(Authorization) == true) { var account = _context.Account.Find(generalInfoWithRoles.AccountId); var generalInfo = _context.GeneralInformation.Find(generalInfoWithRoles.AccountId); account.RollNumber = "B19APTECH" + account.AccountId.ToString("D4"); generalInfo.Phone = generalInfoWithRoles.GeneralInformation.Phone; generalInfo.Address = generalInfoWithRoles.GeneralInformation.Address; generalInfo.Email = generalInfoWithRoles.GeneralInformation.Email; account.UpdatedAt = DateTime.Today; if (generalInfoWithRoles.Password != null) { account.EncryptPassword(generalInfoWithRoles.Password); } if (generalInfoWithRoles.RoleIds != null) { var OldRoleAccount = _context.RoleAccount.Where(ora => ora.AccountId == generalInfoWithRoles.AccountId); _context.RoleAccount.RemoveRange(OldRoleAccount); } foreach (var roleId in generalInfoWithRoles.RoleIds) { var role = _context.Role.Find(roleId); RoleAccount roleAccount = new RoleAccount { Role = role, Account = account }; _context.RoleAccount.Add(roleAccount); } _context.Account.Update(account); _context.GeneralInformation.Update(generalInfo); _context.SaveChanges(); return(new JsonResult(generalInfoWithRoles)); } return(Unauthorized()); }