public GroupResult Edit(int id, BGroup bGroup) { var groupResult = new GroupResult(); using (_context = new GroupsManagementDbEntities()) { using (var transaction = _context.Database.BeginTransaction()) { try { Group group = _context.Groups.Find(id); group.Name = bGroup.Name; _context.SaveChanges(); transaction.Commit(); groupResult.bGroups.Add(new BGroup { GroupId = group.GroupId, Name = group.Name }); } catch (Exception e) { groupResult.IsValid = false; groupResult.ErrorMessages.Add(e.Message); } } } return(groupResult); }
public RoleResult Edit(int id, BRole bRole) { RoleResult roleResult = new RoleResult(); using (_context = new GroupsManagementDbEntities()) { try { Role role = _context.Roles.FirstOrDefault(r => r.RoleId == id); if (role != null) { role.RoleName = bRole.Name; _context.SaveChanges(); roleResult.bRoles.Add(Conversions.ConvertRoleToBRole(role)); } else { roleResult.IsValid = false; roleResult.ErrorMessages.Add("This role doesn't exist."); } } catch (Exception e) { roleResult.IsValid = false; roleResult.ErrorMessages.Add(e.Message); } } return(roleResult); }
public RoleResult Add(BRole bRole) { RoleResult roleResult = new RoleResult(); using (_context = new GroupsManagementDbEntities()) { using (var transaction = _context.Database.BeginTransaction()) { try { Role role = new Role() { RoleName = bRole.Name }; _context.Roles.Add(role); _context.SaveChanges(); transaction.Commit(); roleResult.bRoles.Add(Conversions.ConvertRoleToBRole(role)); } catch (Exception e) { roleResult.IsValid = false; roleResult.ErrorMessages.Add(e.Message); } } } return(roleResult); }
public UserResult Edit(int id, BUser bUser) { UserResult userResult = new UserResult(); using (_context = new GroupsManagementDbEntities()) { using (var transaction = _context.Database.BeginTransaction()) { try { User user = _context.Users.Find(id); user.UserName = bUser.Name; user.Email = bUser.Email; user.GroupId = bUser.GroupId; _context.SaveChanges(); transaction.Commit(); userResult.bUsers.Add(Conversions.ConvertUserToBUser(user)); } catch (Exception e) { userResult.IsValid = false; userResult.ErrorMessages.Add(e.Message); } } } return(userResult); }
public UserResult Delete(int id) { UserResult userResult = new UserResult(); using (_context = new GroupsManagementDbEntities()) { using (var transaction = _context.Database.BeginTransaction()) { try { User user = _context.Users.Include("Group").FirstOrDefault(u => u.UserId == id); BUser bUser = Conversions.ConvertUserToBUser(user); bUser.GroupName = user.Group.Name; userResult.bUsers.Add(bUser); _context.Users.Remove(user); _context.SaveChanges(); transaction.Commit(); } catch (Exception e) { userResult.IsValid = false; userResult.ErrorMessages.Add(e.Message); } } } return(userResult); }
public UserResult Add(BUser bUser) { UserResult userResult = new UserResult(); using (_context = new GroupsManagementDbEntities()) { using (var transaction = _context.Database.BeginTransaction()) { try { // check if group exists Group group = _context.Groups.FirstOrDefault(g => g.GroupId == bUser.GroupId); if (group != null) { User user = _context.Users.Add(Conversions.ConvertBUserToUser(bUser)); _context.SaveChanges(); transaction.Commit(); _context.Entry(user).Reference(u => u.Group).Load(); BUser newBUser = Conversions.ConvertUserToBUser(user); userResult.bUsers.Add(newBUser); } else { throw new Exception("Can't create the user because this group doesn't exist"); } } catch (Exception e) { userResult.IsValid = false; userResult.ErrorMessages.Add(e.Message); } } } return(userResult); }