public async Task <IHttpActionResult> CreateOne([NotNull] ActivityCreateOrUpdateOneRequestDto requestDto) { var userId = User.Identity.GetUserId(); var activity = new Models.Activity { AuthorId = userId, Content = requestDto.Content }; if (Helpers.IsTrustedUrl(requestDto.CoverImage, false)) { activity.CoverImage = requestDto.CoverImage; } 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; activity.TargetPointId = targetPoint.Id; if (targetPoint.Type == PointType.Game || targetPoint.Type == PointType.Hardware) { activity.AttachedPoints = JsonConvert.SerializeObject(await(from relationship in _dbContext.PointRelationships where relationship.SourcePointId == targetPoint.Id && (relationship.Relationship == PointRelationshipType.Developer || relationship.Relationship == PointRelationshipType.Manufacturer || relationship.Relationship == PointRelationshipType.Series || relationship.Relationship == PointRelationshipType.Tag) select relationship.TargetPointId).Distinct() .Where(id => id != targetPoint.Id).Take(10).ToListAsync()); activity.Rating = requestDto.Rating; } else { activity.AttachedPoints = "[]"; } _dbContext.Activities.Add(activity); activity.SidForAuthor = await _dbContext.Activities.Where(a => a.AuthorId == activity.AuthorId) .Select(a => a.SidForAuthor) .DefaultIfEmpty(0) .MaxAsync() + 1; await _dbContext.SaveChangesAsync(); _mqChannel.SendMessage(string.Empty, MqClientProvider.PushHubRequestQueue, new PushHubRequestDto { Type = ContentPushType.Activity, ContentId = activity.Id }); return(Ok(activity.SidForAuthor)); }
public async Task <IHttpActionResult> UpdateOne(string id, [NotNull] ActivityCreateOrUpdateOneRequestDto requestDto) { var activity = await _dbContext.Activities.FindAsync(id); if (activity == null) { return(NotFound()); } var userId = User.Identity.GetUserId(); if (activity.AuthorId != userId && !User.IsInRole(KeylolRoles.Operator)) { return(Unauthorized()); } activity.Content = requestDto.Content; activity.CoverImage = Helpers.IsTrustedUrl(requestDto.CoverImage, false) ? requestDto.CoverImage : string.Empty; 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) { activity.Rating = requestDto.Rating; } else { activity.Rating = null; } await _dbContext.SaveChangesAsync(); if (requestDto.TargetPointId != activity.TargetPointId) { activity.TargetPointId = targetPoint.Id; if (targetPoint.Type == PointType.Game || targetPoint.Type == PointType.Hardware) { activity.AttachedPoints = JsonConvert.SerializeObject(await(from relationship in _dbContext.PointRelationships where relationship.SourcePointId == targetPoint.Id && (relationship.Relationship == PointRelationshipType.Developer || relationship.Relationship == PointRelationshipType.Manufacturer || relationship.Relationship == PointRelationshipType.Series || relationship.Relationship == PointRelationshipType.Tag) select relationship.TargetPointId).Distinct() .Where(pointId => pointId != targetPoint.Id).Take(10).ToListAsync()); } else { activity.AttachedPoints = "[]"; } await _dbContext.SaveChangesAsync(); _mqChannel.SendMessage(string.Empty, MqClientProvider.PushHubRequestQueue, new PushHubRequestDto { Type = ContentPushType.Activity, ContentId = activity.Id }); } return(Ok()); }