public async Task <IHttpActionResult> UpdateOneSpotlightArticle(int id, SpotlightArticleStream.ArticleCategory category, [NotNull] CreateOrUpdateOneSpotlightArticleRequestDto dto) { var feed = await _dbContext.Feeds.FindAsync(id); if (feed == null || feed.StreamName != SpotlightArticleStream.Name(category)) { return(NotFound()); } var article = await _dbContext.Articles.FindAsync(dto.ArticleId); if (article == null) { return(this.BadRequest(nameof(dto), nameof(dto.ArticleId), Errors.NonExistent)); } feed.Entry = article.Id; feed.Properties = JsonConvert.SerializeObject(new SpotlightArticleStream.FeedProperties { Title = string.IsNullOrWhiteSpace(dto.Title) || dto.Title == article.Title ? null : dto.Title, Subtitle = string.IsNullOrWhiteSpace(dto.Subtitle) || dto.Subtitle == article.Subtitle ? null : dto.Subtitle }, new JsonSerializerSettings { NullValueHandling = NullValueHandling.Ignore }); await _dbContext.SaveChangesAsync(); return(Ok()); }
/// <summary> /// 创建 <see cref="SpotlightArticleList"/> /// </summary> /// <param name="currentUserId">当前登录用户 ID</param> /// <param name="page">分页页码</param> /// <param name="recordsPerPage">每页数量</param> /// <param name="spotlightArticleCategory">文章分类</param> /// <param name="dbContext"><see cref="KeylolDbContext"/></param> /// <param name="cachedData"><see cref="CachedDataProvider"/></param> /// <returns><see cref="SpotlightArticleList"/></returns> public static async Task <SpotlightArticleList> CreateAsync(string currentUserId, int page, int recordsPerPage, SpotlightArticleStream.ArticleCategory spotlightArticleCategory, KeylolDbContext dbContext, CachedDataProvider cachedData) { var streamName = SpotlightArticleStream.Name(spotlightArticleCategory); var queryResult = await(from feed in dbContext.Feeds where feed.StreamName == streamName join article in dbContext.Articles on feed.Entry equals article.Id where article.Rejected == false && article.Archived == ArchivedState.None orderby feed.Id descending select new { article.Id, FeedId = feed.Id, FeedProperties = feed.Properties, article.AuthorId, AuthorIdCode = article.Author.IdCode, AuthorAvatarImage = article.Author.AvatarImage, AuthorUserName = article.Author.UserName, article.PublishTime, article.SidForAuthor, article.Title, article.Subtitle, article.CoverImage, PointIdCode = article.TargetPoint.IdCode, PointAvatarImage = article.TargetPoint.AvatarImage, PointType = article.TargetPoint.Type, PointChineseName = article.TargetPoint.ChineseName, PointEnglishName = article.TargetPoint.EnglishName, PointSteamAppId = article.TargetPoint.SteamAppId }) .TakePage(page, recordsPerPage) .ToListAsync(); var result = new SpotlightArticleList(queryResult.Count); foreach (var a in queryResult) { var p = JsonConvert.DeserializeObject <SpotlightArticleStream.FeedProperties>(a.FeedProperties); result.Add(new SpotlightArticle { Id = a.Id, FeedId = a.FeedId, AuthorIdCode = a.AuthorIdCode, AuthorAvatarImage = a.AuthorAvatarImage, AuthorUserName = a.AuthorUserName, AuthorIsFriend = string.IsNullOrWhiteSpace(currentUserId) ? (bool?)null : await cachedData.Users.IsFriendAsync(currentUserId, a.AuthorId), PublishTime = a.PublishTime, SidForAuthor = a.SidForAuthor, Title = p?.Title ?? a.Title, Subtitle = p?.Subtitle ?? a.Subtitle, CoverImage = a.CoverImage, PointIdCode = a.PointIdCode, PointAvatarImage = a.PointAvatarImage, PointType = a.PointType, PointChineseName = a.PointChineseName, PointEnglishName = a.PointEnglishName, PointInLibrary = string.IsNullOrWhiteSpace(currentUserId) || a.PointSteamAppId == null ? (bool?)null : await cachedData.Users.IsSteamAppInLibraryAsync(currentUserId, a.PointSteamAppId.Value) }); } return(result); }