public UserGetModel GetById(int id) { User user = context.Users .AsNoTracking() .FirstOrDefault(u => u.Id == id); return(UserGetModel.FromUser(user)); }
public UserGetModel GetById(int id) { User user = context.Users .FirstOrDefault(u => u.Id == id); if (user == null) { return(null); } return(UserGetModel.FromUser(user)); }
public UserGetModel Delete(int id) { var existing = context.Users.FirstOrDefault(u => u.Id == id); if (existing == null) { return(null); } context.Users.Remove(existing); context.SaveChanges(); return(UserGetModel.FromUser(existing)); }
public UserGetModel Delete(int id, User addedBy) { var existing = context .Users .Include(x => x.HistoryUserRole) .ThenInclude(x => x.UserRole) .FirstOrDefault(u => u.Id == id); string addedByCurrentRole = getLatestHistoryUserRole(addedBy.HistoryUserRole).UserRole.Name; string existingCurrentRole = getLatestHistoryUserRole(existing.HistoryUserRole).UserRole.Name; if (existing == null) { return(null); } if (addedByCurrentRole.Equals(RoleConstants.MODERATOR)) { // https://www.aspforums.net/Threads/289493/Get-Number-of-months-between-two-dates-in-C/ var dateRegister = addedBy.DateRegister; var dateCurrent = DateTime.Now; int months = dateCurrent.Subtract(dateRegister).Days / 30; if (months >= 6) { context.Users.RemoveRange(context.Users.Where(u => u.Id == existing.Id)); context.SaveChanges(); context.HistoryUserRoles.RemoveRange(context.HistoryUserRoles.Where(u => u.User.Id == existing.Id)); context.SaveChanges(); context.Users.Remove(existing); // context.SaveChanges(); return(UserGetModel.FromUser(existing)); } return(null); // dbcontext.Users.Update(toAdd); //dbcontext.SaveChanges(); //return toAdd; } if (addedByCurrentRole.Equals(RoleConstants.ADMIN)) { context.Users.RemoveRange(context.Users.Where(u => u.Id == existing.Id)); context.SaveChanges(); context.HistoryUserRoles.RemoveRange(context.HistoryUserRoles.Where(u => u.User.Id == existing.Id)); context.SaveChanges(); context.Users.Remove(existing); //context.SaveChanges(); return(UserGetModel.FromUser(existing)); } return(null); }
public UserGetModel Upsert(int id, UserPostModel userPostModel) { var existing = context.Users.AsNoTracking().FirstOrDefault(u => u.Id == id); if (existing == null) { User toAdd = UserPostModel.ToUser(userPostModel); context.Users.Add(toAdd); context.SaveChanges(); return(UserGetModel.FromUser(toAdd)); } User toUpdate = UserPostModel.ToUser(userPostModel); toUpdate.Id = id; context.Users.Update(toUpdate); context.SaveChanges(); return(UserGetModel.FromUser(toUpdate)); }
//IMPLEMENTARE CRUD PENTRU USER public IEnumerable <UserGetModel> GetAll() { return(context.Users.Select(user => UserGetModel.FromUser(user))); }
public UserGetModel Upsert(int id, UserPostModel user, User userLogat) { var existing = context .Users .Include(x => x.HistoryUserRole) .ThenInclude(x => x.UserRole) .AsNoTracking().FirstOrDefault(u => u.Id == id); if (existing == null) { return(null); } if (existing.Username.Equals(userLogat.Username)) { return(null); } string existingCurrentRole = getLatestHistoryUserRole(existing.HistoryUserRole).UserRole.Name; string addedByCurrentRole = getLatestHistoryUserRole(userLogat.HistoryUserRole).UserRole.Name; HistoryUserRole currentHistory = getLatestHistoryUserRole(existing.HistoryUserRole); if (existing == null) { //dbcontext.Users.(UserPostModel.ToUser(user)); //dbcontext.SaveChanges(); //return UserGetModel.FromUser(UserPostModel.ToUser(user)); User toReturn = Create(user); return(UserGetModel.FromUser(toReturn)); } User toUpdate = UserPostModel.ToUser(user); toUpdate.Password = existing.Password; toUpdate.DateRegister = existing.DateRegister; toUpdate.Id = id; if (addedByCurrentRole.Equals(RoleConstants.MODERATOR)) { // https://www.aspforums.net/Threads/289493/Get-Number-of-months-between-two-dates-in-C/ var dateRegister = userLogat.DateRegister; var dateCurrent = DateTime.Now; int months = dateCurrent.Subtract(dateRegister).Days / 30; // toAdd.Id = id; if (months >= 6) { //dbcontext.Users.Update(toAdd); //dbcontext.SaveChanges(); //return UserGetModel.FromUser(toAdd); toUpdate.HistoryUserRole = existing.HistoryUserRole; context.Users.Update(toUpdate); context.SaveChanges(); context.Users.Attach(toUpdate); if (existingCurrentRole != user.UserRole) { IEnumerable <UserRole> allRoles = context.UserRoles; List <String> list = new List <string>(); foreach (UserRole userRole in allRoles) { list.Add(userRole.Name); } if (list.Contains(user.UserRole)) { UserRole userRole = searchForRoleByTitle(user.UserRole); HistoryUserRole history = new HistoryUserRole { UserRole = userRole, StartTime = DateTime.Now }; currentHistory.EndTime = DateTime.Now; context.UserRoles.Attach(userRole); toUpdate.HistoryUserRole.Add(history); context.SaveChanges(); return(UserGetModel.FromUser(toUpdate)); } return(null); } return(null); // dbcontext.Users.Update(toAdd); //dbcontext.SaveChanges(); //return toAdd; } return(null); } if (addedByCurrentRole.Equals(RoleConstants.ADMIN)) { //toAdd.Id = id; //dbcontext.Users.Update(toAdd); //dbcontext.SaveChanges(); //return UserGetModel.FromUser(toAdd); toUpdate.HistoryUserRole = existing.HistoryUserRole; context.Users.Update(toUpdate); context.SaveChanges(); context.Users.Attach(toUpdate); if (existingCurrentRole != user.UserRole) { IEnumerable <UserRole> allRoles = context.UserRoles; List <String> list = new List <string>(); foreach (UserRole userRole in allRoles) { list.Add(userRole.Name); } if (list.Contains(user.UserRole)) { UserRole userRole = searchForRoleByTitle(user.UserRole); HistoryUserRole history = new HistoryUserRole { UserRole = userRole, StartTime = DateTime.Now }; currentHistory.EndTime = DateTime.Now; context.UserRoles.Attach(userRole); toUpdate.HistoryUserRole.Add(history); context.SaveChanges(); return(UserGetModel.FromUser(toUpdate)); } return(null); } return(null); } return(null); }