public IActionResult GetAllManagers() { Message _Message = new Message(); EmployeeManagement_POCContext DBFactory = new EmployeeManagement_POCContext(); try { var managesrs = DBFactory.AspNetUserRoles.Where(y => y.RoleId == 2).Select(x => x.UserId).Distinct().ToList().ToArray(); if (managesrs == null || managesrs.Count() == 0) { managesrs = DBFactory.AspNetUserRoles.Where(y => y.RoleId == 1).Select(x => x.UserId).Distinct().ToList().ToArray(); } var managerinfo = DBFactory.AspNetUsers.Where(x => managesrs.Contains(x.Id)).Select(x => new { value = x.Id, label = x.FirstName + ' ' + x.LastName }).ToList(); return(Ok(managerinfo)); } catch (Exception ex) { _Message.Status = "Failure"; _Message.StatusCode = (int)HttpStatusCode.ExpectationFailed; _Message.StatusMessage = ex.Message; } return(Ok()); }
public IActionResult GetAllEmployees(string value) { List <UserRoles> Alldata = new List <UserRoles>(); List <UserRoles> Alldata1 = new List <UserRoles>(); List <UserRoles> Alldata2 = new List <UserRoles>(); Message _Message = new Message(); EmployeeManagement_POCContext DBFactory = new EmployeeManagement_POCContext(); try { if (value != null) { var alldata = (from User in DBFactory.AspNetUsers join WU in DBFactory.AspNetUserRoles on User.Id equals WU.UserId join WR in DBFactory.AspNetRoles on WU.RoleId equals WR.Id where (User.Id == Convert.ToInt32(value)) select new UserRoles { UserId = User.Id, UserName = User.UserName, FirstName = User.FirstName, LastName = User.LastName, Email = User.Email, RoleName = WR.Name, RoleId = WR.Id, ManagerName = DBFactory.AspNetUsers.Where(x => x.Id == User.Managerid).Select(x => x.FirstName + ' ' + x.LastName).FirstOrDefault(), ManagerId = User.Managerid }).FirstOrDefault(); Alldata.Add(alldata); } else { Alldata1 = (from user in DBFactory.AspNetUsers where !DBFactory.AspNetUserRoles.Any(c => user.Id == c.UserId) select new UserRoles { UserId = user.Id, UserName = user.UserName, FirstName = user.FirstName, LastName = user.LastName, Email = user.Email, RoleName = "", RoleId = 0, ManagerName = DBFactory.AspNetUsers.Where(x => x.Id == user.Managerid).Select(x => x.FirstName + ' ' + x.LastName).FirstOrDefault(), ManagerId = user.Managerid }).ToList(); Alldata2 = (from User in DBFactory.AspNetUsers join WU in DBFactory.AspNetUserRoles on User.Id equals WU.UserId join WR in DBFactory.AspNetRoles on WU.RoleId equals WR.Id select new UserRoles { UserId = User.Id, UserName = User.UserName, FirstName = User.FirstName, LastName = User.LastName, Email = User.Email, RoleName = WR.Name, RoleId = WR.Id, ManagerName = DBFactory.AspNetUsers.Where(x => x.Id == User.Managerid).Select(x => x.FirstName + ' ' + x.LastName).FirstOrDefault(), ManagerId = User.Managerid }).ToList(); Alldata.AddRange(Alldata1); Alldata.AddRange(Alldata2); Alldata.OrderBy(x => x.UserId).ToList(); } } catch (Exception ex) { _Message.Status = "Failure"; _Message.StatusCode = (int)HttpStatusCode.ExpectationFailed; _Message.StatusMessage = ex.Message; } return(Ok(Alldata)); }
public IActionResult AssignRoleAndManager(AssignRoleAndManagerModel assignRoleAndManagerModel) { Message _Message = new Message(); EmployeeManagement_POCContext DBFactory = new EmployeeManagement_POCContext(); try { if (assignRoleAndManagerModel.UserId > 0) { var olddata = DBFactory.AspNetUsers.Where(x => x.Id == Convert.ToInt32(assignRoleAndManagerModel.UserId)).FirstOrDefault(); if (olddata != null) { olddata.Managerid = Convert.ToInt32(assignRoleAndManagerModel.Managerid); DBFactory.Entry(olddata).State = EntityState.Modified; DBFactory.SaveChanges(); } } if (assignRoleAndManagerModel.RoleId > 0) { var addRole = DBFactory.AspNetUserRoles.Where(x => x.UserId == Convert.ToInt32(assignRoleAndManagerModel.UserId)).FirstOrDefault(); if (addRole == null) { AspNetUserRoles _aspNetUserRoles = new AspNetUserRoles(); _aspNetUserRoles.RoleId = Convert.ToInt32(assignRoleAndManagerModel.RoleId); _aspNetUserRoles.UserId = Convert.ToInt32(assignRoleAndManagerModel.UserId); DBFactory.AspNetUserRoles.Add(_aspNetUserRoles); var flag = DBFactory.SaveChanges(); if (flag > 0) { _Message.Status = "Success"; _Message.StatusCode = 200; _Message.StatusMessage = "Role Successfully Asigned."; } return(Ok(_Message)); } else { var existRole = DBFactory.AspNetUserRoles.Where(x => x.UserId == Convert.ToInt32(assignRoleAndManagerModel.UserId)).FirstOrDefault(); DBFactory.AspNetUserRoles.Remove(existRole); DBFactory.SaveChanges(); AspNetUserRoles _aspNetUserRoles = new AspNetUserRoles(); _aspNetUserRoles.RoleId = Convert.ToInt32(assignRoleAndManagerModel.RoleId); _aspNetUserRoles.UserId = Convert.ToInt32(assignRoleAndManagerModel.UserId); DBFactory.AspNetUserRoles.Add(_aspNetUserRoles); var flag = DBFactory.SaveChanges(); if (flag > 0) { _Message.Status = "Success"; _Message.StatusCode = 200; _Message.StatusMessage = "Role Successfully updated."; } return(Ok(_Message)); } } } catch (Exception ex) { _Message.Status = "Failure"; _Message.StatusCode = (int)HttpStatusCode.ExpectationFailed; _Message.StatusMessage = ex.Message; } return(Ok(_Message)); }