public async Task <IActionResult> PutFavoritePost(int id, FavoritePost favoritePost) { if (id != favoritePost.PostId) { return(BadRequest()); } _context.Entry(favoritePost).State = EntityState.Modified; try { await _context.SaveChangesAsync(); } catch (DbUpdateConcurrencyException) { if (!FavoritePostExists(id)) { return(NotFound()); } else { throw; } } return(NoContent()); }
public async Task <IActionResult> CreateFavoritePost([FromBody] CreateFavoritePost favoritePost) { if (!ModelState.IsValid) { return(BadRequest(ModelState)); } var favPost = new FavoritePost { PostId = favoritePost.PostId, UserId = favoritePost.UserId }; var createFavoritePostVm = await _favoritePostsRepository.CreateFavoritePost(favPost); return(Ok(createFavoritePostVm)); }
public async Task <IActionResult> FavoriteAsync(int PostId, bool IsAdd) { if (HttpContext.Session.GetInt32("AccountId") == null) { return(Redirect("/login")); } HttpClient httpClient = new HttpClient(); if (IsAdd) { FavoritePost favorite = new FavoritePost { PostId = PostId, AccountId = HttpContext.Session.GetInt32("AccountId").Value }; var json = JsonConvert.SerializeObject(favorite); var stringContent = new StringContent(json, UnicodeEncoding.UTF8, "application/json"); using var response = await httpClient.PostAsync(BaseAPI + "FavoritePosts", stringContent); if (response.IsSuccessStatusCode) { string ApiResponse = await response.Content.ReadAsStringAsync(); return(Ok(JsonConvert.DeserializeObject <FavoritePost>(ApiResponse))); } else { return(BadRequest()); } } else { using var response = await httpClient.DeleteAsync(BaseAPI + "FavoritePosts?PostId=" + PostId + "&AccountId=" + HttpContext.Session.GetInt32 ("AccountId").Value); if (response.IsSuccessStatusCode) { return(Ok()); } else { return(BadRequest()); } } }
public async Task FavorAsync(string postId, string userId) { var query = this.favoritePostsRepository .All() .FirstOrDefault(x => x.PostId == postId && x.UserId == userId); if (query == null) { query = new FavoritePost { PostId = postId, UserId = userId, }; } await this.favoritePostsRepository.AddAsync(query); await this.favoritePostsRepository.SaveChangesAsync(); }
public async Task <ActionResult <FavoritePost> > PostFavoritePost(FavoritePost favoritePost) { _context.FavoritePost.Add(favoritePost); try { await _context.SaveChangesAsync(); } catch (DbUpdateException) { if (FavoritePostExists(favoritePost.PostId)) { return(Conflict()); } else { throw; } } return(CreatedAtAction("GetFavoritePost", new { id = favoritePost.PostId }, favoritePost)); }
public async Task <CreateFavoritePostVm> CreateFavoritePost(FavoritePost favoritePost) { var createFavoritePostVm = new CreateFavoritePostVm(); var exist = await _context.FavoritePosts .SingleOrDefaultAsync(f => f.PostId == favoritePost.PostId && f.UserId == favoritePost.UserId); if (exist == null) { await _context.FavoritePosts.AddAsync(favoritePost); createFavoritePostVm.type = "AddFavorite"; } else { _context.FavoritePosts.Remove(exist); createFavoritePostVm.type = "RemoveFavorite"; } await _context.SaveChangesAsync(); return(createFavoritePostVm); }