Пример #1
0
        public async Task <IActionResult> AddLecturer(int userId, LecturersDto lecturersDto)
        {
            if (userId != int.Parse(User.FindFirst(ClaimTypes.NameIdentifier).Value))
            {
                return(Unauthorized());
            }

            var lecturersFromRepo = await _repo.GetLecturers();

            foreach (var lecturer in lecturersFromRepo)
            {
                if (lecturer.Name.Trim() == lecturersDto.Name.Trim())
                {
                    return(BadRequest($"lecturer {lecturer.Name} already exists"));
                }
            }

            lecturersDto.Name = lecturersDto.Name.Trim();
            var newLecturer = _mapper.Map <Lecturer>(lecturersDto);

            _repo.Add(newLecturer);
            if (await _repo.SaveAll())
            {
                var lecturerToReturn = _mapper.Map <LecturersDto>(newLecturer);
                return(CreatedAtRoute("GetLecturer"
                                      , new { userId = userId, id = newLecturer.Id }
                                      , lecturerToReturn));
            }
            return(BadRequest("Failed to add the lecturer"));
        }
Пример #2
0
        public async Task <IActionResult> UpdateLecturer(int id, int userId, LecturersDto lecturersDto)
        {
            if (userId != int.Parse(User.FindFirst(ClaimTypes.NameIdentifier).Value))
            {
                return(Unauthorized());
            }

            var lecturerFromRepo = await _repo.GetLecturer(id);

            lecturersDto.Id = lecturerFromRepo.Id;
            _mapper.Map(lecturersDto, lecturerFromRepo);

            if (await _repo.SaveAll())
            {
                return(NoContent());
            }

            throw new Exception($"Updating lecturer {id} failed on save");
        }