public async Task <IActionResult> Put([Required] string id, [FromBody] TagModel item) { if (ModelState.IsValid && User.Identity.IsAuthenticated) { ApplicationUser user = await _authenticationManager.UserManager.FindByNameAsync(User.Identity.Name); FanficDTO fanfic = await _fanficService.GetById(id); if (fanfic.ApplicationUserId == user.Id || await _authenticationManager.UserManager.IsInRoleAsync(user, "Admin")) { TagDTO tag = _tagService.GetTagByName(item.tagName); if (tag == null) { tag = await _tagService.Create(new TagDTO() { TagName = item.tagName, Uses = 1 }); } await _fanficTagsService.Create(new FanficTagsDTO() { TagId = tag.Id, FanficId = id }); return(Ok()); } } return(BadRequest(ModelState)); }
public async Task <IActionResult> CreateChapterForFanfic([Required] string id, [FromBody] ChapterModel item) { if (ModelState.IsValid && User.Identity.IsAuthenticated) { ApplicationUser user = await _authenticationManager.UserManager.FindByNameAsync(User.Identity.Name); FanficDTO fanfic = await _fanficService.GetById(id); if (fanfic == null) { return(NotFound()); } if (fanfic.ApplicationUserId == user.Id || await _authenticationManager.UserManager.IsInRoleAsync(user, "Admin")) { ChapterDTO newChapter = _mapper.Map <ChapterModel, ChapterDTO>(item); newChapter.Id = null; newChapter.FanficId = id; newChapter = await _chapterService.Create(newChapter); return(Ok(_mapper.Map <ChapterDTO, ChapterModel>(newChapter))); } } return(BadRequest(ModelState)); }
private void RecalculateFanficRating(int fanficId) { FanficDTO fanfic = _fabric.CreateFanficsRepository().Get(fanficId).ToModel(); fanfic.RatingValue = fanfic.Ratings.Average(r => r.Value); _fabric.CreateFanficsRepository().Update(fanfic.ToEntity()); }
public async Task <IActionResult> DeleteChapterFromFanfic([Required] string id) { if (ModelState.IsValid && User.Identity.IsAuthenticated) { ApplicationUser user = await _authenticationManager.UserManager.FindByNameAsync(User.Identity.Name); ChapterDTO chapter = await _chapterService.GetById(id); FanficDTO fanfic = await _fanficService.GetById(chapter.FanficId); if (fanfic == null) { return(NotFound()); } if (fanfic.ApplicationUserId == user.Id || await _authenticationManager.UserManager.IsInRoleAsync(user, "Admin")) { await DeleteChapterRatings(id); ChapterDTO deletedChapter = await _chapterService.Delete(id); return(Ok(_mapper.Map <ChapterDTO, ChapterModel>(deletedChapter))); } } return(BadRequest(ModelState)); }
public IActionResult Edit(int fanficId, string newName, string newDescription) { FanficDTO fanfic = _fanficService.Get(fanficId); fanfic.Name = newName; fanfic.Description = newDescription; _fanficService.Update(fanfic); return(RedirectToAction("Read", "Fanfic", new { id = fanficId })); }
public void Create(string name, string text, int fanficId, byte[] image) { FanficDTO fanfic = _fabric.CreateFanficsRepository().Get(fanficId).ToModel(); int chapterNumber = fanfic.Chapters.Count + 1; _fabric.CreateChaptersRepository().Create(name, text, chapterNumber, fanficId, image); fanfic.LastUpdateTimestamp = DateTime.Now; _fabric.CreateFanficsRepository().Update(fanfic.ToEntity()); }
public async Task <FanficDTO> Create(FanficDTO item) { Fanfic fanfic = _mapper.Map <FanficDTO, Fanfic>(item); fanfic.DateOfCreation = DateTime.Now; fanfic.LastModifyingDate = DateTime.Now; fanfic = await _database.FanficRepository.Create(fanfic); return(_mapper.Map <Fanfic, FanficDTO>(fanfic)); }
public async Task <IActionResult> GetAsync([Required] string id) { FanficDTO fanfic = await _fanficService.GetById(id); if (fanfic == null) { return(NotFound()); } return(Ok(await GetFanficModelFromDTO(fanfic))); }
private FanficDTO GetFanficDTOForUpdating(FanficDTO fanfic, FanficModel item) { fanfic.Id = null; fanfic.Title = item.title; fanfic.Description = item.description; fanfic.PictureURL = item.picture_url; fanfic.GenreId = _genreService.GetByName(item.genre).Id; fanfic.DateOfCreation = DateTime.Parse(item.creation_date); fanfic.LastModifyingDate = DateTime.Now; return(fanfic); }
public void ChangeChapterNumber(ChapterDTO chapter, int newNumber) { FanficDTO fanfic = chapter.Fanfic; List <ChapterDTO> chapters = fanfic.Chapters; ChapterDTO chapterToSwapWith = chapters.FirstOrDefault(c => c.ChapterNumber == newNumber); chapterToSwapWith.ChapterNumber = chapter.ChapterNumber; chapter.ChapterNumber = newNumber; Update(chapter); Update(chapterToSwapWith); }
public async Task <FanficDTO> Update(FanficDTO item) { Fanfic fanfic = _mapper.Map <FanficDTO, Fanfic>(item); string oldGenreId = fanfic.GenreId; fanfic.LastModifyingDate = DateTime.Now; fanfic = await _database.FanficRepository.Update(fanfic); if (fanfic.GenreId != oldGenreId) { // ToQueueElasticUpdate } return(_mapper.Map <Fanfic, FanficDTO>(fanfic)); }
public async Task <IActionResult> Post([FromBody] CreateFanfic item) { if (ModelState.IsValid && User.Identity.IsAuthenticated) { if (_fanficService.CheckUniqueName(item.title)) { FanficDTO fanfic = await GetFanficDTO(item); fanfic = await _fanficService.Create(fanfic); await CreateTags(item.tags.ToList(), fanfic.Id); return(Ok(fanfic.Id)); } return(BadRequest("title is already exists")); } return(BadRequest(ModelState)); }
private async Task <FanficModel> GetFanficModelFromDTO(FanficDTO fanfic) { UserDTO user = await _userService.GetUserById(fanfic.ApplicationUserId); return(new FanficModel() { id = fanfic.Id, title = fanfic.Title, picture_url = fanfic.PictureURL, creation_date = fanfic.DateOfCreation.ToString(), last_modifying_date = fanfic.LastModifyingDate.ToString(), description = fanfic.Description, genre = (await _genreService.GetById(fanfic.GenreId)).GenreName, author_username = user.Username, author_picture_url = user.PictureURL, tags = await GetFanficTags(fanfic.Id) }); }
public async Task <IActionResult> Delete([Required] string id, [FromBody] TagModel item) { if (ModelState.IsValid && User.Identity.IsAuthenticated) { ApplicationUser user = await _authenticationManager.UserManager.FindByNameAsync(User.Identity.Name); FanficDTO fanfic = await _fanficService.GetById(id); if (fanfic.ApplicationUserId == user.Id || await _authenticationManager.UserManager.IsInRoleAsync(user, "Admin")) { TagDTO tag = _tagService.GetTagByName(item.tagName); if (tag != null) { FanficTagsDTO fanficTag = _fanficTagsService.GetFanficTagsByFanficId(id).FirstOrDefault(x => x.TagId == tag.Id); await _fanficTagsService.Delete(fanficTag.Id); } return(Ok()); } } return(BadRequest(ModelState)); }
public async Task <IActionResult> Delete([Required] string id) { if (ModelState.IsValid && User.Identity.IsAuthenticated) { FanficDTO fanfic = await _fanficService.GetById(id); ApplicationUser user = await _authenticationManager.UserManager.FindByNameAsync(User.Identity.Name); if (fanfic.ApplicationUserId == user.Id || await _authenticationManager.UserManager.IsInRoleAsync(user, "Admin")) { await DeleteFanficTagsByFanficId(id); await DeleteFanficChapters(id); await DeleteFanficComments(id); fanfic = await _fanficService.Delete(id); return(Ok(fanfic)); } } return(BadRequest(ModelState)); }
public async Task <IActionResult> Put([FromBody] FanficModel item) { if (ModelState.IsValid && User.Identity.IsAuthenticated) { ApplicationUser user = await _authenticationManager.UserManager.FindByNameAsync(User.Identity.Name); FanficDTO fanfic = await _fanficService.GetById(item.id); if (fanfic.ApplicationUserId == user.Id || await _authenticationManager.UserManager.IsInRoleAsync(user, "Admin")) { fanfic = GetFanficDTOForUpdating(fanfic, item); await DeleteFanficTagsByFanficId(item.id); await CreateTags(item.tags.ToList(), item.id); await _fanficService.Update(fanfic); return(Ok(fanfic)); } } return(BadRequest(ModelState)); }
public static Fanfic ToEntity(this FanficDTO fanfic) { return(new Fanfic { Id = fanfic.Id, Name = fanfic.Name, Description = fanfic.Description, CreationTimestamp = fanfic.CreationDate, LastUpdateTimestamp = fanfic.LastUpdateTimestamp, AuthorId = fanfic.Author.Id, GenreId = fanfic.Genre.Id, RatingValue = fanfic.RatingValue, }); /* * var config = new MapperConfiguration(cfg => cfg.CreateMap<FanficDTO, Fanfic>() * .ForMember("AuthorId", opt => opt.MapFrom(f => f.Author.Id)) * .ForMember("Author", opt => opt.Ignore()) * .ForMember("GenreId", opt => opt.MapFrom(f => f.Genre.Id)) * .ForMember("Genre", opt => opt.Ignore())); * return new Mapper(config).Map<FanficDTO, Fanfic>(fanfic); */ }
public void Update(FanficDTO fanfic) { _fabric.CreateFanficsRepository().Update(fanfic.ToEntity()); }
public IActionResult Edit(int fanficId) { FanficDTO fanfic = _fanficService.Get(fanficId); return(View(fanfic)); }