Exemple #1
0
 public void Execute(CreateUserDto request)
 {
     if (request == null)
     {
         throw new NullReferenceException();
     }
     else
     {
         if (Context.Users.Any(u => u.Username == request.Username))
         {
             throw new EntityAlreadyExistsException("User");
         }
         try
         {
             Context.Users.Add(new Domain.User
             {
                 FirstName = request.FirstName,
                 LastName  = request.LastName,
                 Password  = HashPasswordCommand.MD5Hash(request.Password),
                 Username  = request.Username,
                 Email     = request.Email,
                 RoleId    = 2,
                 CreatedAt = DateTime.Now
             });
             Context.SaveChanges();
         }
         catch
         {
             throw new NullReferenceException();
         }
     }
 }
Exemple #2
0
        public UserDto Execute(UserSearch request)
        {
            if (request != null)
            {
                var user     = Context.Users.Include(x => x.Role).AsQueryable();
                var password = HashPasswordCommand.MD5Hash(request.Password);
                var founded  = Context.Users.Include(x => x.Role).Where(x => x.Username == request.Username && x.Password == password).FirstOrDefault();
                if (founded == null)
                {
                    throw new EntityNotFoundException("User");
                }

                return(new UserDto
                {
                    FirstName = founded.FirstName,
                    LastName = founded.LastName,
                    Email = founded.Email,
                    Username = founded.Username,
                    Id = founded.Id,
                    RoleName = founded.Role.Name
                });
            }
            else
            {
                throw new EntityNotFoundException("User");
            }
        }
Exemple #3
0
        public void Execute(CreateUserDto request)
        {
            var user = Context.Users.Find(request.UserId);

            if (user == null)
            {
                throw new EntityNotFoundException("User");
            }
            if (Context.Users.Where(x => x.Id != request.UserId).Any(x => x.Username == request.Username))
            {
                throw new EntityAlreadyExistsException("User");
            }
            user.FirstName  = request.FirstName;
            user.LastName   = request.LastName;
            user.Email      = request.Email;
            user.Username   = request.Username;
            user.RoleId     = request.RoleId;
            user.Password   = HashPasswordCommand.MD5Hash(request.Password);
            user.ModifiedAt = DateTime.Now;
            Context.SaveChanges();
        }