-
Notifications
You must be signed in to change notification settings - Fork 1
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Application new feature: edit discussion entry
- Loading branch information
Showing
10 changed files
with
943 additions
and
2 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,46 @@ | ||
using MemCheck.Application.QueryValidation; | ||
using Microsoft.EntityFrameworkCore; | ||
using System; | ||
using System.Threading.Tasks; | ||
|
||
namespace MemCheck.Application.Cards; | ||
|
||
public sealed class EditCardDiscussionEntry : RequestRunner<EditCardDiscussionEntry.Request, EditCardDiscussionEntry.Result> | ||
{ | ||
#region Fields | ||
private readonly DateTime runUtcDate; | ||
#endregion | ||
public EditCardDiscussionEntry(CallContext callContext, DateTime? runUtcDate = null) : base(callContext) | ||
{ | ||
this.runUtcDate = runUtcDate ?? DateTime.UtcNow; | ||
} | ||
protected override async Task<ResultWithMetrologyProperties<Result>> DoRunAsync(Request request) | ||
{ | ||
var previousVersionCreator = new PreviousCardDiscussionEntryVersionCreator(DbContext); | ||
var entry = await previousVersionCreator.RunAsync(request.EntryId, runUtcDate); | ||
entry.Text = request.Text; | ||
await DbContext.SaveChangesAsync(); | ||
return new ResultWithMetrologyProperties<Result>(new Result(), ("EntryId", request.EntryId.ToString())); | ||
} | ||
#region Request & Result records | ||
public sealed record Request(Guid UserId, Guid EntryId, string Text) : IRequest | ||
{ | ||
public async Task CheckValidityAsync(CallContext callContext) | ||
{ | ||
await QueryValidationHelper.CheckUserExistsAsync(callContext.DbContext, UserId); | ||
|
||
var entry = await callContext.DbContext.CardDiscussionEntries.AsNoTracking() | ||
.Include(entry => entry.Creator) | ||
.SingleOrDefaultAsync(entry => entry.Id == EntryId); | ||
if (entry == null) | ||
throw new NonexistentCardDiscussionEntryException(); | ||
|
||
await QueryValidationHelper.CheckCardExistsAsync(callContext.DbContext, entry.Card); // Not really needed, and not checked by a unit test - just a security | ||
CardVisibilityHelper.CheckUserIsAllowedToViewCard(callContext.DbContext, UserId, entry.Card); | ||
QueryValidationHelper.CheckCanCreateCardDiscussionEntryWithText(Text); | ||
QueryValidationHelper.CheckUserCanEditCardDiscussionEntry(UserId, entry); | ||
} | ||
} | ||
public sealed record Result(); | ||
#endregion | ||
} |
45 changes: 45 additions & 0 deletions
45
MemCheck.Application/Cards/PreviousCardDiscussionEntryVersionCreator.cs
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,45 @@ | ||
using MemCheck.Database; | ||
using MemCheck.Domain; | ||
using Microsoft.EntityFrameworkCore; | ||
using System; | ||
using System.Threading.Tasks; | ||
|
||
namespace MemCheck.Application.Cards; | ||
|
||
internal sealed class PreviousCardDiscussionEntryVersionCreator | ||
{ | ||
#region Fields | ||
private readonly MemCheckDbContext dbContext; | ||
#endregion | ||
#region Private methods | ||
private async Task<CardDiscussionEntryPreviousVersion> CreatePreviousVersionAsync(CardDiscussionEntry entry, DateTime? versionUtcDate = null) | ||
{ | ||
var previousVersion = new CardDiscussionEntryPreviousVersion() | ||
{ | ||
Card = entry.Card, | ||
Creator = entry.Creator, | ||
Text = entry.Text, | ||
CreationUtcDate = versionUtcDate ?? entry.CreationUtcDate, | ||
PreviousVersion = entry.PreviousVersion, | ||
}; | ||
await dbContext.CardDiscussionEntryPreviousVersions.AddAsync(previousVersion); | ||
return previousVersion; | ||
} | ||
#endregion | ||
public PreviousCardDiscussionEntryVersionCreator(MemCheckDbContext dbContext) | ||
{ | ||
this.dbContext = dbContext; | ||
} | ||
public async Task<CardDiscussionEntry> RunAsync(Guid entryId, DateTime? newVersionUtcDate = null) | ||
{ | ||
var entry = await dbContext.CardDiscussionEntries | ||
.Include(entry => entry.Creator) | ||
.Include(entry => entry.PreviousVersion) | ||
.SingleAsync(entry => entry.Id == entryId); | ||
|
||
var previousVersion = await CreatePreviousVersionAsync(entry); | ||
entry.PreviousVersion = previousVersion; | ||
entry.CreationUtcDate = newVersionUtcDate ?? DateTime.UtcNow; | ||
return entry; | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
16 changes: 16 additions & 0 deletions
16
MemCheck.Application/QueryValidation/NonexistentCardDiscussionEntryException.cs
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,16 @@ | ||
using System; | ||
|
||
namespace MemCheck.Application.QueryValidation; | ||
|
||
public class NonexistentCardDiscussionEntryException : InvalidOperationException | ||
{ | ||
public NonexistentCardDiscussionEntryException(string message) : base(message) | ||
{ | ||
} | ||
public NonexistentCardDiscussionEntryException(string message, Exception innerException) : base(message, innerException) | ||
{ | ||
} | ||
public NonexistentCardDiscussionEntryException() | ||
{ | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
16 changes: 16 additions & 0 deletions
16
MemCheck.Application/QueryValidation/UserNotAllowedToEditDiscussionEntryException.cs
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,16 @@ | ||
using System; | ||
|
||
namespace MemCheck.Application.QueryValidation; | ||
|
||
public class UserNotAllowedToEditDiscussionEntryException : InvalidOperationException | ||
{ | ||
public UserNotAllowedToEditDiscussionEntryException(string message) : base(message) | ||
{ | ||
} | ||
public UserNotAllowedToEditDiscussionEntryException(string message, Exception innerException) : base(message, innerException) | ||
{ | ||
} | ||
public UserNotAllowedToEditDiscussionEntryException() | ||
{ | ||
} | ||
} |
Oops, something went wrong.