public async Task <IHttpActionResult> UpdateOne(string id, [NotNull] ArticleCreateOrUpdateOneRequestDto requestDto) { var article = await _dbContext.Articles.FindAsync(id); if (article == null) { return(NotFound()); } var userId = User.Identity.GetUserId(); if (article.AuthorId != userId && !User.IsInRole(KeylolRoles.Operator)) { return(Unauthorized()); } article.Title = requestDto.Title; article.Subtitle = string.IsNullOrWhiteSpace(requestDto.Subtitle) ? string.Empty : requestDto.Subtitle; article.Content = SanitizeRichText(requestDto.Content); article.UnstyledContent = PlainTextFormatter.FlattenHtml(article.Content, true); article.CoverImage = SanitizeCoverImage(requestDto.CoverImage); var targetPoint = await _dbContext.Points.Where(p => p.Id == requestDto.TargetPointId) .Select(p => new { p.Id, p.Type }).SingleOrDefaultAsync(); if (targetPoint == null) { return(this.BadRequest(nameof(requestDto), nameof(requestDto.TargetPointId), Errors.NonExistent)); } if (targetPoint.Type == PointType.Game || targetPoint.Type == PointType.Hardware) { article.Rating = requestDto.Rating; article.Pros = JsonConvert.SerializeObject(requestDto.Pros ?? new List <string>()); article.Cons = JsonConvert.SerializeObject(requestDto.Cons ?? new List <string>()); } else { article.Rating = null; article.Pros = string.Empty; article.Cons = string.Empty; } article.ReproductionRequirement = requestDto.ReproductionRequirement == null ? string.Empty : JsonConvert.SerializeObject(requestDto.ReproductionRequirement); await _dbContext.SaveChangesAsync(); var oldAttachedPoints = Helpers.SafeDeserialize <List <string> >(article.AttachedPoints) ?? new List <string>(); if (requestDto.TargetPointId != article.TargetPointId || !requestDto.AttachedPointIds.OrderBy(s => s).SequenceEqual(oldAttachedPoints.OrderBy(s => s))) { article.TargetPointId = targetPoint.Id; requestDto.AttachedPointIds = requestDto.AttachedPointIds.Select(pointId => pointId.Trim()) .Where(pointId => pointId != targetPoint.Id.Trim()).Distinct().ToList(); article.AttachedPoints = JsonConvert.SerializeObject(requestDto.AttachedPointIds); await _dbContext.SaveChangesAsync(); _mqChannel.SendMessage(string.Empty, MqClientProvider.PushHubRequestQueue, new PushHubRequestDto { Type = ContentPushType.Article, ContentId = article.Id }); } _mqChannel.SendMessage(string.Empty, MqClientProvider.ImageGarageRequestQueue, new ImageGarageRequestDto { ContentType = ImageGarageRequestContentType.Article, ContentId = article.Id }); return(Ok()); }
public async Task <IHttpActionResult> CreateOne([NotNull] ArticleCreateOrUpdateOneRequestDto requestDto) { var userId = User.Identity.GetUserId(); if (!await _coupon.CanTriggerEventAsync(userId, CouponEvent.发布文章)) { return(Unauthorized()); } var article = new Models.Article { AuthorId = userId, Title = requestDto.Title, Content = SanitizeRichText(requestDto.Content), CoverImage = SanitizeCoverImage(requestDto.CoverImage) }; article.UnstyledContent = PlainTextFormatter.FlattenHtml(article.Content, true); if (!string.IsNullOrWhiteSpace(requestDto.Subtitle)) { article.Subtitle = requestDto.Subtitle; } var targetPoint = await _dbContext.Points.Where(p => p.Id == requestDto.TargetPointId).SingleOrDefaultAsync(); if (targetPoint == null) { return(this.BadRequest(nameof(requestDto), nameof(requestDto.TargetPointId), Errors.NonExistent)); } targetPoint.LastActivityTime = DateTime.Now; article.TargetPointId = targetPoint.Id; requestDto.AttachedPointIds = requestDto.AttachedPointIds.Select(id => id.Trim()) .Where(id => id != targetPoint.Id).Distinct().ToList(); article.AttachedPoints = JsonConvert.SerializeObject(requestDto.AttachedPointIds); if (targetPoint.Type == PointType.Game || targetPoint.Type == PointType.Hardware) { article.Rating = requestDto.Rating; article.Pros = JsonConvert.SerializeObject(requestDto.Pros ?? new List <string>()); article.Cons = JsonConvert.SerializeObject(requestDto.Cons ?? new List <string>()); } if (requestDto.ReproductionRequirement != null) { article.ReproductionRequirement = JsonConvert.SerializeObject(requestDto.ReproductionRequirement); } _dbContext.Articles.Add(article); article.SidForAuthor = await _dbContext.Articles.Where(a => a.AuthorId == article.AuthorId) .Select(a => a.SidForAuthor) .DefaultIfEmpty(0) .MaxAsync() + 1; await _dbContext.SaveChangesAsync(); await _coupon.UpdateAsync(await _userManager.FindByIdAsync(userId), CouponEvent.发布文章, new { ArticleId = article.Id }); _mqChannel.SendMessage(string.Empty, MqClientProvider.PushHubRequestQueue, new PushHubRequestDto { Type = ContentPushType.Article, ContentId = article.Id }); _mqChannel.SendMessage(string.Empty, MqClientProvider.ImageGarageRequestQueue, new ImageGarageRequestDto { ContentType = ImageGarageRequestContentType.Article, ContentId = article.Id }); SteamCnProvider.TriggerArticleUpdate(); return(Ok(article.SidForAuthor)); }