public async Task <IActionResult> UpdateTopic(TopicForUpdateDto topicForUpdateDto) { if (!await _repo.IsExistTopic(topicForUpdateDto.Id)) { return(BadRequest("Topic doesn't Exist")); } if (!await _repo.IsExistChapter(topicForUpdateDto.ChapterId)) { return(BadRequest("Chapter doesn't Exist")); } var chapterFromRepo = await _repo.GetChapter(topicForUpdateDto.ChapterId); var topicFromRepo = await _repo.GetTopicsById(topicForUpdateDto.Id); var updatedTopic = _mapper.Map(topicForUpdateDto, topicFromRepo); updatedTopic.ChapterName = chapterFromRepo.Name; updatedTopic.ClassName = chapterFromRepo.ClassName; updatedTopic.ClassId = chapterFromRepo.ClassId; updatedTopic.SubjctName = chapterFromRepo.SubjectName; updatedTopic.SubjectIdInClass = chapterFromRepo.SubjectForClassId; if (await _repo.SaveAll()) { return(Ok()); } throw new Exception($"Updating Topic {topicForUpdateDto.Id} failed on save"); }
public async Task <IActionResult> UpdateTopic(TopicForUpdateDto topicForUpdateDto) { var topic = await _repository.Get(topicForUpdateDto.Id); if (topic == null) { return(NotFound()); } var AR = await _authorizationService.AuthorizeAsync(HttpContext.User, topic, "Permission"); if (!AR.Succeeded) { return(Forbid()); } _mapper.Map(topicForUpdateDto, topic); _repository.Update(topic); await _repository.SaveChangesAsync(); return(NoContent()); }