public async Task<UserDto> AddNewUser(NewUserCommandDto newUserCommandDto) { using (var context = new PicturesDbContext()) { var newUser = new User { FirstName = newUserCommandDto.FirstName, LastName = newUserCommandDto.LastName, PhotoRec = newUserCommandDto.PhotoRec, Email = newUserCommandDto.Email }; if (!newUserCommandDto.IsLocalUser) { var login = new Login { LoginType = newUserCommandDto.LoginType, ExternalUserId = newUserCommandDto.ExternalLoginId, User = newUser }; context.Logins.Add(login); } else { newUser.Login = newUserCommandDto.Login; newUser.Password = newUserCommandDto.Password; } context.Users.Add(newUser); await context.SaveChangesAsync(); if (newUserCommandDto.IsLocalUser) { var user = context.Users.Include(i => i.Logins).First(i => i.Login == newUserCommandDto.Login); return user.CreateUserDto(user.Logins); } var user1 = context.Logins.Include(i => i.User).First( i => i.LoginType == newUserCommandDto.LoginType && i.ExternalUserId == newUserCommandDto.ExternalLoginId).User; return user1.CreateUserDto(user1.Logins); } }
public async Task UpdateUserInfo(long id, string firstName, string lastName) { using (var context = new PicturesDbContext()) { var userEntity = new User {Id = id}; var entity = context.Users.Attach(userEntity); entity.FirstName = firstName; entity.LastName = lastName; context.Entry(entity).Property(item => item.FirstName).IsModified = true; context.Entry(entity).Property(item => item.LastName).IsModified = true; await context.SaveChangesAsync(); } }
public void UpdateExternalLoginValues(UserDto userDto, string firstName, string lastName, string photoRec) { using (var context = new PicturesDbContext()) { var userEntity = new User {Id = userDto.Id}; var entity = context.Users.Attach(userEntity); var wasModified = false; if (userDto.PhotoRec != photoRec) { entity.PhotoRec = photoRec; //context.Entry(entity).Property((item) => item.PhotoRec).IsModified = true; wasModified = true; } if (userDto.FirstName != firstName) { entity.FirstName = photoRec; //context.Entry(entity).Property((item) => item.FirstName).IsModified = true; wasModified = true; } if (userDto.LastName != lastName) { entity.LastName = lastName; //context.Entry(entity).Property((item) => item.LastName).IsModified = true; } if (wasModified) context.SaveChanges(); } }