public async Task <ArticleEnvelope> Handle(Command message) { var article = await _context.Articles.FirstOrDefaultAsync(x => x.Slug == message.Slug); if (article == null) { throw new RestException(HttpStatusCode.NotFound); } var person = await _context.Persons.FirstOrDefaultAsync(x => x.Username == _currentUserAccessor.GetCurrentUsername()); var favorite = await _context.ArticleFavorites.FirstOrDefaultAsync(x => x.ArticleId == article.ArticleId && x.PersonId == person.PersonId); if (favorite == null) { favorite = new ArticleFavorite() { Article = article, ArticleId = article.ArticleId, Person = person, PersonId = person.PersonId }; await _context.ArticleFavorites.AddAsync(favorite); await _context.SaveChangesAsync(); } return(new ArticleEnvelope(await _context.Articles.GetAllData() .FirstOrDefaultAsync(x => x.ArticleId == article.ArticleId))); }
/// <summary> /// 收藏或取消收藏文章 /// </summary> /// <param name="articleFavorite">文章收藏模型</param> /// <returns></returns> public async Task <int> AddOrUpdateArticleFavorite(ArticleFavorite articleFavorite) { var oldArticleFavorite = await dbContext.ArticleFavorite.SingleOrDefaultAsync(af => af.PKID == articleFavorite.PKID && af.UserId == articleFavorite.UserId); try { //取消收藏文章 if (oldArticleFavorite != null) { dbContext.ArticleFavorite.Remove(oldArticleFavorite); await dbContext.SaveChangesAsync(); return(Success); } //收藏文章 else { dbContext.ArticleFavorite.Add(articleFavorite); await dbContext.SaveChangesAsync(); return(Success); } } catch (Exception ex) { //记录日志 return(Error); } }
public async Task <ArticleEnvelope> FavoriteAdd(string slug, CancellationToken cancellationToken) { var article = await this.context.Articles .FirstOrDefaultAsync(x => x.Slug.Equals(slug), cancellationToken); if (article == null) { throw new RestException(HttpStatusCode.NotFound); } var person = await this.context.Persons .FirstOrDefaultAsync(x => x.Username.Equals(this.currentUserAccessor.GetCurrentUsername()), cancellationToken); var favorite = await this.context.ArticleFavorites .FirstOrDefaultAsync(x => x.ArticleId == article.ArticleId && x.PersonId == person.PersonId, cancellationToken); if (favorite == null) { favorite = new ArticleFavorite() { Article = article, ArticleId = article.ArticleId, Person = person, PersonId = person.PersonId }; await this.context.ArticleFavorites.AddAsync(favorite, cancellationToken); await this.context.SaveChangesAsync(cancellationToken); } return(new ArticleEnvelope(await this.context.Articles.GetAllData() .FirstOrDefaultAsync(x => x.ArticleId == article.ArticleId, cancellationToken))); }
public async Task <bool> AddFavorite(string slug, string token) { var user = ServiceProvider.GetRequiredService <UserService>().GetCurrentUser(token); if (user == null) { throw new ApplicationException("Not logged in."); } if (user.Favorites.Any(a => a.Article.Slug == slug)) { return(true); } var article = ServiceProvider.GetRequiredService <ArticleService>().GetArticle(slug); if (article == null) { throw new ApplicationException("Not found."); } var newFavorite = new ArticleFavorite { UserUid = user.UserUid, ArticleUid = article.ArticleUid, ArticleFavoriteUid = Guid.NewGuid() }; Context.Favorites.Add(newFavorite); var count = await Context.SaveChangesAsync(); return(count > 0); }
/// <summary> /// 新增或更新文章收藏记录 /// </summary> /// <param name="articleFavorite"></param> /// <returns></returns> public static async Task <int> SaveArticleFavorite(ArticleFavorite articleFavorite) { var article = await articleDal.SelectArticleDetailById(articleFavorite.PKID); if (article == null) { throw new Exception("文章不存在"); } return(await articleFavoriteDal.AddOrUpdateArticleFavorite(articleFavorite)); }
public Task <Option <ArticleModel, Error> > FavoriteAsync(string userId, string slug) => GetUserByIdOrError(userId).FlatMapAsync(user => GetArticleBySlug(slug) .FilterAsync(async article => !article.Favorites.Any(af => af.UserId == userId), "You have already favorited this article.") .FlatMapAsync(async article => { var articleFavorite = new ArticleFavorite { ArticleId = article.Id, UserId = userId }; DbContext.ArticleFavorites.Add(articleFavorite); await DbContext.SaveChangesAsync(); return(await GetBySlugAsync(user.Id.Some(), slug)); }));