/// <inheritdoc cref="ITrainingSessionService.StartTrainingSessionAsync(StartTrainingSessionRequest)"/>
        public async Task <StartTrainingSessionResponse> StartTrainingSessionAsync(StartTrainingSessionRequest startTrainingSessionRequest)
        {
            TrainingRoom trainingRoom;

            if ((trainingRoom = await _trainingRoomRepository.FindSingleOrDefaultAsync(tr => tr.Id.Equals(startTrainingSessionRequest.TrainingRoomId))) == default)
            {
                return(new StartTrainingSessionResponse(startTrainingSessionRequest.Id, null, "Training room does not exist."));
            }
            UserDto user = await _userService.FindUserAsync(startTrainingSessionRequest.UserId);

            if (user is null)
            {
                return(new StartTrainingSessionResponse(startTrainingSessionRequest.Id, null, "User does not exist."));
            }
            if (!trainingRoom.IsUserAuthorized(startTrainingSessionRequest.UserId))
            {
                return(new StartTrainingSessionResponse(startTrainingSessionRequest.Id, null, "User is not authorized"));
            }
            if (!trainingRoom.StartTrainingSession(startTrainingSessionRequest.UserId, out TrainingSession trainingSession))
            {
                return(new StartTrainingSessionResponse(startTrainingSessionRequest.Id, null, "Training session already started."));
            }
            await EntityRepository.CreateAsync(trainingSession);

            TrainingSessionDto trainingSessionDto = Mapper.Map <TrainingSessionDto>(trainingSession);

            return(new StartTrainingSessionResponse(startTrainingSessionRequest.Id, trainingSessionDto, "Successfully started a training session.", true));
        }
        public async Task <TDto> CreateAsync <TDto>(TDto dto, string password, RoleIdentifier roleIdentifier)
        {
            var entity = Mapper.Map <User>(dto);

            var result = await EntityRepository.CreateAsync(entity, password, roleIdentifier);

            return(Mapper.Map <TDto>(result));
        }
Exemple #3
0
        /// <inheritdoc cref="IUserService.RegisterAsync(RegisterRequest)"/>
        public async Task <RegisterResponse> RegisterAsync(RegisterRequest registerRequest)
        {
            if (string.IsNullOrEmpty(registerRequest.Password))
            {
                return(new RegisterResponse(registerRequest.Id, message: "Credentials are null or empty."));
            }

            if (await EntityRepository.ExistsAsync(anyUser => anyUser.Username == registerRequest.Username))
            {
                return(new RegisterResponse(registerRequest.Id, message: "Username already exists."));
            }

            if (!await _credentialTypeRepository.ExistsAsync(ct => ct.Code == registerRequest.CredentialTypeCode))
            {
                return(new RegisterResponse(registerRequest.Id, message: "CredentialType not found."));
            }

            User user = new User
            {
                Username         = registerRequest.Username,
                TimestampCreated = DateTime.UtcNow
            };

            (bool createdSuccess, _) = await EntityRepository.CreateAsync(user);

            if (!createdSuccess)
            {
                return(new RegisterResponse(registerRequest.Id, "Failed to persist data."));
            }

            CredentialType credentialType = await _credentialTypeRepository.FindSingleOrDefaultAsync(ct => ct.Code == registerRequest.CredentialTypeCode);

            byte[]     salt       = _saltGenerator.GenerateSalt();
            Credential credential = new Credential
            {
                UserId           = user.Id,
                CredentialTypeId = credentialType.Id,
                Identifier       = registerRequest.Username,
                Secret           = _hasher.Hash(registerRequest.Password, salt),
                Extra            = Convert.ToBase64String(salt)
            };

            (bool success, _) = await _credentialRepository.CreateAsync(credential);

            return(!success
                ? new RegisterResponse(registerRequest.Id, "Failed to persist data.")
                : new RegisterResponse(registerRequest.Id, "Registration successful!", true));
        }