Mutations - Portfolio

Menu
Przejdź do treści
Przykład dodania użytkownika
Wykasowanie użytkownika.

Edycja użytkownika
Sprawdzeni czy edycja została zapisana w bazie.
public class MutationUsers
   {
       private readonly IPasswordHasher<User> _passwordHasher;

       public MutationUsers(IPasswordHasher<User> passwordHasher)
       {
           _passwordHasher = passwordHasher;
       }

       public bool AddUser(UserDto userDto, [Service] ApplicationDbContext applicationDbContext)
       {
           User user = new User();

           user.dateCreate = DateTime.Now;
           user.password = _passwordHasher.HashPassword(user, userDto.password);
           user.lastName = userDto.lastName;
           user.name = userDto.name;
           user.email = userDto.email;

           applicationDbContext.users.Add(user);
           applicationDbContext.SaveChanges();
           return true;
       }
       public bool DelUser(string email, [Service] ApplicationDbContext applicationDbContext)
       {
           var delUser = applicationDbContext.users.FirstOrDefault(x => x.email == email);
           if(delUser is null)
           {
               return false;
           }
           applicationDbContext.users.Remove(delUser);
           applicationDbContext.SaveChanges();
           return true;
       }
       public User EditUser(UserDto userDto, [Service] ApplicationDbContext applicationDbContext)
       {
           var editUser = applicationDbContext.users.FirstOrDefault(f => f.email == userDto.email);
           if(editUser is null)
           {
               return editUser;
           }
           
           editUser.password = _passwordHasher.HashPassword(editUser, userDto.password);
           editUser.lastName = userDto.lastName;
           editUser.name = userDto.name;
           editUser.email = userDto.email;
           applicationDbContext.SaveChanges();
           return editUser;

       }
   }
Wróć do spisu treści