public void CreateFanfic([FromBody] FanficScriptModel data) { string userId = User.Identity.GetUserId(); PreviewUserViewModel user = Mapper .Map <PreviewUserViewModel>(ApplicationUserRepository.GetApplicationUserById(data.UserId)); if (user != null && User.IsInRole("admin")) { userId = user.Id; } Fanfic fanfic = new Fanfic { ApplicationUserId = userId, AverageRating = 0, CategoryId = data.Category, CreateDate = DateTime.Now, Description = data.Description, ImgUrl = data.ImgUrl, Name = data.Name }; int id = FanficRepository.AddFanfic(fanfic); foreach (var topicScript in data.Topics) { Topic topic = new Topic { Name = topicScript.Name, Number = topicScript.Number, FanficId = id, Text = topicScript.Text, ImgUrl = topicScript.ImgUrl, AverageRating = 0 }; TopicRepository.AddTopic(topic); } foreach (var tag in data.Tags) { FanficTagRepository.AddNewFanficTag(id, TagRepository.FindOrAdd(tag.Name)); } }
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); } } }