public CommandResult Execute(UpdateUserCommmand command, CommandContext context)
        {
            if (command.Pin != null)
            {
                if (command.Pin.IsNullOrEmpty() || !command.Pin.ContainsDigitsOnly() || !command.Pin.HasLengthBetween(4, 8))
                {
                    return(CommandResult.WithValidationError(Localization.ValidationMessage.IncorrectPinError));
                }
            }

            if (command.Password != null)
            {
                if (command.Password.IsNullOrEmpty())
                {
                    return(CommandResult.WithValidationError(Localization.ValidationMessage.PasswordCannotBeEmpty));
                }
            }

            _userUpdater.Update(new UserUpdaterInput
            {
                UserId       = command.UserId,
                PasswordHash = command.Password?.CalculateHash(),
                PinHash      = command.Pin?.CalculateHash(),
                Permissions  = command.Permissions
            });

            return(CommandResult.Empty);
        }
예제 #2
0
        public async Task <IServiceResult <StudentViewModel> > Update(
            int id,
            StudentRegistration newData)
        {
            if (newData.ProposalId != null)
            {
                var proposal = await _context.Proposals.FirstOrDefaultAsync(p => p.Id == newData.ProposalId);

                if (proposal == null)
                {
                    return(ServiceResult <StudentViewModel> .Error(
                               $"The proposal with id {newData.ProposalId} does not exist"
                               ));
                }
            }

            var isIndexNumberTaken = await IsIndexNumberTaken(id, newData.IndexNumber);

            if (isIndexNumberTaken)
            {
                return(ServiceResult <StudentViewModel> .Error(
                           $"Index number {newData.IndexNumber} is already taken"
                           ));
            }

            var existingStudent =
                await _context
                .Students
                .FirstOrDefaultAsync(p => p.Id == id);

            if (existingStudent == null)
            {
                return(ServiceResult <StudentViewModel> .Error(
                           $"Student with id {id} does not exist"));
            }

            var credentials = new UserCredentials
            {
                Email    = newData.Email,
                Password = newData.Password
            };

            var result = await _userUpdater.Update(
                existingStudent.UserId,
                credentials,
                new RoleType[] {
                RoleType.Student
            });

            if (!result.Successful())
            {
                var errors = result.GetAggregatedErrors();
                return(ServiceResult <StudentViewModel> .Error(errors));
            }

            existingStudent = _mapper.Map(newData, existingStudent);

            _context.Students.Update(existingStudent);
            await _context.SaveChangesAsync();

            var studentViewModel = _mapper.Map <StudentViewModel>(existingStudent);

            return(ServiceResult <StudentViewModel> .Success(studentViewModel));
        }