public void EditFanfic([FromBody] FanficScriptModel data) { FanficFullModel fanfic = Mapper.Map <FanficFullModel>(FanficRepository.EditFanfic(data.Id, data.Name, data.Description, data.ImgUrl)); foreach (var topic in fanfic.Topics) { bool isDelete = true; foreach (var newTopic in data.Topics) { if (topic.Id == newTopic.Id) { isDelete = false; break; } } if (isDelete) { TopicRepository.DeleteTopicById(topic.Id); } } foreach (var topic in data.Topics) { if (topic.Id != -1) { TopicRepository.EditTopic(topic.Id, topic.Number, topic.Name, topic.ImgUrl, topic.Text); } else { Topic newTopic = new Topic { Name = topic.Name, Number = topic.Number, FanficId = data.Id, Text = topic.Text, ImgUrl = topic.ImgUrl, AverageRating = 0 }; //TopicRepository.AddTopic(newTopic); FanficRepository.AddTopic(fanfic.Id, newTopic); } } FanficRepository.SetAverageRatingById(fanfic.Id); List <FanficTagViewModel> fanficTag = Mapper.Map <List <FanficTagViewModel> >(FanficTagRepository.GetFanficTagByFanficId(data.Id)); foreach (var fanTag in fanficTag) { bool isDelete = true; foreach (var newFanTag in data.Tags) { if (TagRepository.GetTagNameById(fanTag.TagId) == newFanTag.Name) { isDelete = false; } } if (isDelete) { FanficTagRepository.DeleteFanficTag(fanTag.FanficId, fanTag.TagId); TagRepository.SubCountById(fanTag.TagId); } } foreach (var tag in data.Tags) { int idTag = TagRepository.AddOrNull(tag.Name); if (FanficTagRepository.FindByFanficIdTagId(fanfic.Id, idTag) == null) { FanficTagRepository.AddNewFanficTag(data.Id, idTag); TagRepository.AddCountById(idTag); } } }