/// <summary> /// Notifies listeners that a new <see cref="PromotionActionEntity"/> has been created. /// </summary> /// <param name="promotionAction">The <see cref="PromotionActionEntity"/> that was created.</param> /// <returns>A <see cref="Task"/> that will complete when the operation has completed.</returns> internal protected Task RaisePromotionActionCreatedAsync(PromotionActionEntity promotionAction) => _mediator.Publish(new PromotionActionCreated { PromotionActionId = promotionAction.Id, PromotionActionCreationData = new PromotionActionCreationData { Created = promotionAction.Created, CreatedById = promotionAction.CreatedById, GuildId = promotionAction.GuildId, Type = promotionAction.Type } });
/// <summary> /// Notifies <see cref="PromotionActionEventHandlers"/> that a new <see cref="PromotionActionEntity"/> has been created. /// </summary> /// <param name="promotionAction">The <see cref="PromotionActionEntity"/> that was created.</param> /// <returns>A <see cref="Task"/> that will complete when the operation has completed.</returns> internal protected async Task RaisePromotionActionCreatedAsync(PromotionActionEntity promotionAction) { foreach (var handler in PromotionActionEventHandlers) { await handler.OnPromotionActionCreatedAsync(promotionAction.Id, new PromotionActionCreationData() { GuildId = (ulong)promotionAction.GuildId, Type = promotionAction.Type, Created = promotionAction.Created, CreatedById = (ulong)promotionAction.CreatedById }); } }
/// <inheritdoc /> public async Task <PromotionActionSummary> TryUpdateAsync(long commentId, ulong userId, Action <PromotionCommentMutationData> updateAction) { if (updateAction is null) { throw new ArgumentNullException(nameof(updateAction)); } var oldComment = await ModixContext.Set <PromotionCommentEntity>() .Include(x => x.Campaign) .SingleAsync(x => x.Id == commentId); var modifyAction = new PromotionActionEntity { CampaignId = oldComment.CampaignId, Created = DateTimeOffset.Now, CreatedById = userId, GuildId = oldComment.Campaign.GuildId, Type = PromotionActionType.CommentModified, }; await ModixContext.Set <PromotionActionEntity>().AddAsync(modifyAction); await ModixContext.SaveChangesAsync(); var data = PromotionCommentMutationData.FromEntity(oldComment); updateAction(data); var newComment = data.ToEntity(); newComment.CreateActionId = modifyAction.Id; await ModixContext.Set <PromotionCommentEntity>().AddAsync(newComment); await ModixContext.SaveChangesAsync(); modifyAction.OldCommentId = oldComment.Id; modifyAction.NewCommentId = newComment.Id; oldComment.ModifyActionId = modifyAction.Id; newComment.CreateActionId = modifyAction.Id; await ModixContext.SaveChangesAsync(); var actionSummary = await ModixContext.Set <PromotionActionEntity>().AsNoTracking() .Where(x => x.Id == modifyAction.Id) .AsExpandable() .Select(PromotionActionSummary.FromEntityProjection) .FirstAsync(); return(actionSummary); }
/// <inheritdoc /> public async Task <long> TryUpdateAsync(long commentId, ulong userId, Action <PromotionCommentMutationData> updateAction) { if (updateAction is null) { throw new ArgumentNullException(nameof(updateAction)); } var oldComment = await ModixContext.PromotionComments .Include(x => x.Campaign) .SingleAsync(x => x.Id == commentId); var modifyAction = new PromotionActionEntity { CampaignId = oldComment.CampaignId, Created = DateTimeOffset.Now, CreatedById = userId, GuildId = oldComment.Campaign.GuildId, Type = PromotionActionType.CommentModified, }; await ModixContext.PromotionActions.AddAsync(modifyAction); await ModixContext.SaveChangesAsync(); var data = PromotionCommentMutationData.FromEntity(oldComment); updateAction(data); var newComment = data.ToEntity(); newComment.CreateActionId = modifyAction.Id; await ModixContext.PromotionComments.AddAsync(newComment); await ModixContext.SaveChangesAsync(); modifyAction.OldCommentId = oldComment.Id; modifyAction.NewCommentId = newComment.Id; oldComment.ModifyActionId = modifyAction.Id; newComment.CreateActionId = modifyAction.Id; await ModixContext.SaveChangesAsync(); await RaisePromotionActionCreatedAsync(modifyAction); return(newComment.Id); }