Skip to content

Commit

Permalink
Games/Versions -> wire up to ExternalMediaPublisher
Browse files Browse the repository at this point in the history
  • Loading branch information
adelikat committed Aug 26, 2024
1 parent 0e6b06b commit 95ab23f
Show file tree
Hide file tree
Showing 2 changed files with 23 additions and 12 deletions.
1 change: 1 addition & 0 deletions TASVideos/Pages/Games/Versions/Edit.cshtml
Original file line number Diff line number Diff line change
Expand Up @@ -89,6 +89,7 @@
<form-button-bar>
<submit-button edit="@Model.Id.HasValue"></submit-button>
<cancel-link asp-page="/Games/Versions/List" asp-route-gameId="@Model.GameId"></cancel-link>
<minor-edit></minor-edit>
</form-button-bar>
</form>
<input id="hash-input" type="file" class="d-none" />
Expand Down
34 changes: 22 additions & 12 deletions TASVideos/Pages/Games/Versions/Edit.cshtml.cs
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@
namespace TASVideos.Pages.Games.Versions;

[RequirePermission(PermissionTo.CatalogMovies)]
public class EditModel(ApplicationDbContext db) : BasePageModel
public class EditModel(ApplicationDbContext db, ExternalMediaPublisher publisher) : BasePageModel
{
private static readonly List<SelectListItem> VersionTypes = Enum
.GetValues<VersionTypes>()
Expand Down Expand Up @@ -113,8 +113,10 @@ public async Task<IActionResult> OnPost()
}

GameVersion version;
var action = "created";
if (Id.HasValue)
{
action = "updated";
version = await db.GameVersions.SingleAsync(r => r.Id == Id.Value);
}
else
Expand All @@ -137,21 +139,22 @@ public async Task<IActionResult> OnPost()
version.SourceDb = Version.SourceDb;
version.Notes = Version.Notes;

try
var saveResult = await db.TrySaveChanges();
if (saveResult == SaveResult.UpdateFailure)
{
await db.SaveChangesAsync();
}
catch (DbUpdateConcurrencyException)
{
ErrorStatusMessage($"Unable to update Game Version {Id}");
ModelState.AddModelError("", "Unable to save");
return Page();
}
catch (DbUpdateException ex)

SetMessage(saveResult, $"Game Version {version.Name} {action}", $"Unable to update Game {version.Name}");
if (saveResult.IsSuccess())
{
ModelState.AddModelError("", ex.InnerException?.Message ?? ex.Message);
return Page();
await publisher.SendGameManagement(
$"Game Version [{version.Name}]({{0}}) {action} by {User.Name()}",
"",
$"/Games/{version.GameId}/Versions/View/{version.Id}");
}

SuccessStatusMessage($"Game Version {Id} updated");
return string.IsNullOrWhiteSpace(HttpContext.Request.ReturnUrl())
? RedirectToPage("List", new { gameId = GameId })
: BaseReturnUrlRedirect(new() { ["SystemId"] = system.Id.ToString(), ["GameId"] = GameId.ToString(), ["GameVersionId"] = version.Id.ToString() });
Expand All @@ -171,7 +174,14 @@ public async Task<IActionResult> OnPostDelete()
}

db.GameVersions.Attach(new GameVersion { Id = Id ?? 0 }).State = EntityState.Deleted;
SetMessage(await db.TrySaveChanges(), $"Game Version {Id} deleted", $"Unable to delete Game Version {Id}");
var saveResult = await db.TrySaveChanges();
string saveMessage = $"Game Version {Id} deleted";
SetMessage(saveResult, saveMessage, $"Unable to delete Game Version {Id}");
if (saveResult.IsSuccess())
{
await publisher.SendMessage(PostGroups.Game, $"{saveMessage} by {User.Name()}");
}

return BasePageRedirect("List", new { gameId = GameId });
}

Expand Down

0 comments on commit 95ab23f

Please sign in to comment.