Example #1
0
        // POST entries/create
        // Creates a new entry for the user.
        public async Task <ActionResult> CreateEntry([FromBody] EntryDto entryDto)
        {
            var currentUser = await _userService.GetUserAsync(User);

            if (!(await IsValidEntry("CreateEntry", currentUser, entryDto)))
            {
                return(new BadRequestResult());
            }

            // Create the entry.
            entryDto.When = new DateTime(
                entryDto.When.Year,
                entryDto.When.Month,
                entryDto.When.Day,
                entryDto.When.Hour,
                entryDto.When.Minute,
                entryDto.When.Second);                 // Remove the miliseconds.
            var newEntry = _entryService.CreateEntry(currentUser, entryDto);

            _selfAssessmentService.CreateSelfAssessments(
                currentUser,
                entryDto.SkillSetId,
                entryDto.AssessmentBundle,
                newEntry);

            return(Json(newEntry));
        }
Example #2
0
        // POST skill-set/{skillSetId}/self-assessments/create
        // Creates a new self assessment for the user.
        public async Task <ActionResult> CreateSelfAssessment(int skillSetId, [FromBody] SelfAssessmentDto selfAssessmentDto)
        {
            var currentUser = await _userService.GetUserAsync(User);

            if (currentUser == null)
            {
                _logger.LogWarning($"CreateSelfAssessment called with invalid user {User.Email()}");
                return(new BadRequestResult());
            }

            // Validation.
            if (selfAssessmentDto.SkillId == 0)
            {
                _logger.LogWarning($"CreateSelfAssessment called with invalid SkillId of {selfAssessmentDto.SkillId}");
                return(new BadRequestResult());
            }
            var skill = await _skillService.GetSkillAsync(skillSetId, selfAssessmentDto.SkillId);

            if (skill == null)
            {
                _logger.LogWarning($"CreateSelfAssessment called with invalid SkillId of {selfAssessmentDto.SkillId} with SkillSetId of {skillSetId}");
                return(new BadRequestResult());
            }
            if (!skill.CanSelfAssess || !skill.SelfAssessmentScaleId.HasValue)
            {
                _logger.LogWarning($"CreateSelfAssessment called with SkillId of {selfAssessmentDto.SkillId} which cannot be self assessed.");
                return(new BadRequestResult());
            }
            var selfAssessmentScales = await _selfAssessmentService.GetSelfAssessmentScalesAsync(skillSetId);

            var selfAssessment      = selfAssessmentScales.FirstOrDefault(s => s.Id == skill.SelfAssessmentScaleId.Value);
            var selfAssessmentLevel = selfAssessment.Levels.FirstOrDefault(l => l.Id == selfAssessmentDto.LevelId);

            if (selfAssessmentLevel == null)
            {
                _logger.LogWarning($"CreateSelfAssessment called with SkillId of {selfAssessmentDto.SkillId} with an invalid LevelId of {selfAssessmentDto.LevelId}");
                return(new BadRequestResult());
            }
            if (selfAssessmentLevel.Score != selfAssessmentDto.Score)
            {
                _logger.LogWarning($"CreateSelfAssessment called with SkillId of {selfAssessmentDto.SkillId} with LevelId of {selfAssessmentDto.LevelId} with an invalid score of {selfAssessmentDto.Score}");
                return(new BadRequestResult());
            }

            // Create the selfAssessment.
            _selfAssessmentService.CreateSelfAssessments(currentUser, skillSetId, new Dictionary <int, SelfAssessmentDto> {
                { skill.Id, selfAssessmentDto }
            });

            return(new OkResult());
        }