Esempio n. 1
0
        public async Task <IActionResult> UpdateCommonDifficulty(int userId, int id, CommonDifficultyForCreationDto commonDifficultyForUpdateDto)
        {
            if (userId != int.Parse(User.FindFirst(ClaimTypes.NameIdentifier).Value))
            {
                return(Unauthorized());
            }

            var commonDifficultyFromRepo = await _repo.GetCommonDifficulty(id);

            _mapper.Map(commonDifficultyForUpdateDto, commonDifficultyFromRepo);

            if (await _repo.SaveAll())
            {
                return(CreatedAtRoute("GetCommonDifficulty", new { id = commonDifficultyFromRepo.Id, userId = userId }, commonDifficultyFromRepo));
            }

            throw new Exception($"Updating common difficulty {id} failed on save");
        }
Esempio n. 2
0
        public async Task <IActionResult> AddCommonDifficulty(int userId, CommonDifficultyForCreationDto commonDifficultyForCreation)
        {
            var creator = await _userRepo.GetUser(userId);

            if (creator.Id != int.Parse(User.FindFirst(ClaimTypes.NameIdentifier).Value))
            {
                return(Unauthorized());
            }

            var commonDifficulty = _mapper.Map <CommonDifficulty>(commonDifficultyForCreation);

            commonDifficulty.User = creator;

            _repo.Add(commonDifficulty);

            if (await _repo.SaveAll())
            {
                var jobToReturn = _mapper.Map <CommonDifficultyForReturnDto>(commonDifficulty);
                return(CreatedAtRoute("GetCommonDifficulty", new { id = commonDifficulty.Id, userId = userId }, jobToReturn));
            }

            throw new Exception("Creation of Common difficulty failed on save");
        }