Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

edit pub urls without having to delete and readd #1746

Merged
merged 9 commits into from
Jan 21, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 1 addition & 1 deletion TASVideos/Pages/Games/Index.cshtml
Original file line number Diff line number Diff line change
Expand Up @@ -167,7 +167,7 @@
<alert type="warning" condition="!Model.Game.Versions.Any()" class="mt-3">
No records.
</alert>
<table condition="Model.Game.Versions.Any()" class="table table-responsive table-sm">
<table condition="Model.Game.Versions.Any()" class="table table-bordered table-responsive table-sm">
<tr>
<th>Type</th>
<th>Name</th>
Expand Down
2 changes: 1 addition & 1 deletion TASVideos/Pages/Games/Models/GameEditModel.cs
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,7 @@ public class GameEditModel
public string? Aliases { get; set; }

[StringLength(250)]
[Display(Name = "Screenshot Url")]
[Display(Name = "Screenshot URL")]
public string? ScreenshotUrl { get; set; }

[StringLength(300)]
Expand Down
15 changes: 9 additions & 6 deletions TASVideos/Pages/Publications/Edit.cshtml
Original file line number Diff line number Diff line change
Expand Up @@ -44,19 +44,22 @@
}
</table>
<hr />
<a permission="EditPublicationFiles" asp-page="EditUrls" asp-route-id="@Model.Id" class="float-end btn btn-primary btn-sm mb-2"><span class="fa fa-pencil"></span> Edit</a>
<a permission="EditPublicationFiles"
asp-page="Urls/List"
asp-route-publicationId="@Model.Id"
class="float-end btn btn-primary btn-sm mb-2"><span class="fa fa-pencil"></span> Edit</a>
<table class="table table-bordered table-sm">
<tr>
<th>Name</th>
<th>Url</th>
<th>Type</th>
<th>URL</th>
<th>Name</th>
</tr>
@foreach (var url in Model.Publication.Urls.OrderBy(f => f.Type))
@foreach (var url in Model.Publication.Urls.OrderBy(f => f.Type).ThenBy(f => f.DisplayName))
{
<tr>
<td>@url.DisplayName</td>
<td><a href="@url.Url">@url.Url</a></td>
<td>@url.Type</td>
<td><a href="@url.Url">@url.Url</a></td>
<td>@url.DisplayName</td>
</tr>
}
</table>
Expand Down
2 changes: 1 addition & 1 deletion TASVideos/Pages/Publications/EditFiles.cshtml
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,7 @@
<row condition="Model.Files.Any()">
<table class="table table-bordered">
<tr>
<td>Url</td>
<td>URL</td>
<td>Type</td>
<td>Description</td>
<td></td>
Expand Down
62 changes: 0 additions & 62 deletions TASVideos/Pages/Publications/EditUrls.cshtml

This file was deleted.

49 changes: 49 additions & 0 deletions TASVideos/Pages/Publications/Urls/Edit.cshtml
Original file line number Diff line number Diff line change
@@ -0,0 +1,49 @@
@page "/Publications/{publicationId}/Urls/Edit/{urlId?}/{handler?}"
@model EditUrlsModel
@{
string saveBtnName;
string iconClass;
if (Model.UrlId.HasValue)
{
ViewData.SetTitle($"Editing URL #{Model.UrlId} for {Model.Title}");
saveBtnName = "Save";
iconClass = "fa fa-save";
}
else
{
ViewData.SetTitle("Add A New URL");
saveBtnName = "Add";
iconClass = "fa fa-plus";
}
}

<form method="post">
<input type="hidden" asp-for="Title" />
<row>
<column lg="6">
<fieldset>
<label asp-for="UrlType"></label>
<select asp-for="UrlType" asp-items="Model.AvailableTypes" class="form-control"></select>
<span asp-validation-for="UrlType" class="text-danger"></span>
</fieldset>
<fieldset>
<label asp-for="CurrentUrl"></label>
<input type="text" asp-for="CurrentUrl" class="form-control" />
<span asp-validation-for="CurrentUrl" class="text-danger"></span>
</fieldset>
<fieldset>
<label asp-for="DisplayName"></label>
<input type="text" asp-for="DisplayName" class="form-control" />
<span asp-validation-for="DisplayName" class="text-danger"></span>
</fieldset>
<div class="text-center mt-3">
<submit-button class="btn btn-primary"><span class="@iconClass"></span> @saveBtnName</submit-button>
<a asp-page="List" asp-route-PublicationId="@Model.PublicationId" class="btn btn-secondary"><span class="fa fa-times"></span> Cancel</a>
</div>
</column>
</row>
</form>

@section Scripts {
<partial name="_ValidationScriptsPartial" />
}
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,7 @@
using TASVideos.Data;
using TASVideos.Data.Entity;

namespace TASVideos.Pages.Publications;
namespace TASVideos.Pages.Publications.Urls;

[RequirePermission(PermissionTo.EditPublicationFiles)]
public class EditUrlsModel : BasePageModel
Expand Down Expand Up @@ -47,7 +47,10 @@ public EditUrlsModel(
});

[FromRoute]
public int Id { get; set; }
public int PublicationId { get; set; }

[FromRoute]
public int? UrlId { get; set; }

[BindProperty]
public string Title { get; set; } = "";
Expand All @@ -62,8 +65,8 @@ public EditUrlsModel(
[Required]
[BindProperty]
[Url]
[Display(Name = "Url")]
public string PublicationUrl { get; set; } = "";
[Display(Name = "URL")]
public string CurrentUrl { get; set; } = "";

[Required]
[BindProperty]
Expand All @@ -73,7 +76,7 @@ public EditUrlsModel(
public async Task<IActionResult> OnGet()
{
var title = await _db.Publications
.Where(p => p.Id == Id)
.Where(p => p.Id == PublicationId)
.Select(p => p.Title)
.SingleOrDefaultAsync();

Expand All @@ -84,16 +87,34 @@ public async Task<IActionResult> OnGet()

Title = title;
CurrentUrls = await _db.PublicationUrls
.Where(u => u.PublicationId == Id)
.Where(u => u.PublicationId == PublicationId)
.ToListAsync();

if (!UrlId.HasValue)
{
return Page();
}

var url = CurrentUrls
.SingleOrDefault(u => u.Id == UrlId.Value);

if (url is null || url.Url is null)
{
return NotFound();
}

PublicationId = url.PublicationId;
DisplayName = url.DisplayName;
UrlType = url.Type;
CurrentUrl = url.Url;
Comment on lines +106 to +109
Copy link
Collaborator Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Not sure if there needs to be new.


return Page();
}

public async Task<IActionResult> OnPost()
{
var publication = await _db.Publications
.Where(p => p.Id == Id)
.Where(p => p.Id == PublicationId)
.Select(p => new
{
Title,
Expand All @@ -110,47 +131,64 @@ public async Task<IActionResult> OnPost()
return NotFound();
}

var publicationWiki = await _wikiPages.PublicationPage(Id);
var publicationWiki = await _wikiPages.PublicationPage(PublicationId);

CurrentUrls = publication.PublicationUrls;

if (CurrentUrls.Any(u => u.Type == UrlType && u.Url == PublicationUrl))
if (CurrentUrls.Any(u => u.Type == UrlType && u.Url == CurrentUrl && u.Id != UrlId))
{
ModelState.AddModelError($"{nameof(PublicationUrl)}", $"The {UrlType} url: {PublicationUrl} already exists");
ModelState.AddModelError($"{nameof(CurrentUrl)}", $"The {UrlType} URL: {CurrentUrl} already exists");
}

if (!ModelState.IsValid)
{
return Page();
}

var publicationUrl = new PublicationUrl
string[] logWording;

if (UrlId.HasValue)
{
var url = CurrentUrls
.Single(u => u.Id == UrlId.Value);

url.PublicationId = PublicationId;
url.DisplayName = DisplayName;
url.Type = UrlType;
url.Url = CurrentUrl;

logWording = new[] { "Chang", "chang" };
}
else
{
PublicationId = Id,
Url = PublicationUrl,
Type = UrlType,
DisplayName = DisplayName
};
_db.PublicationUrls.Add(new PublicationUrl
{
PublicationId = PublicationId,
Url = CurrentUrl,
Type = UrlType,
DisplayName = DisplayName
});

_db.PublicationUrls.Add(publicationUrl);
logWording = new[] { "Add", "add" };
}

string log = $"Added {DisplayName} {UrlType} url {PublicationUrl}";
await _publicationMaintenanceLogger.Log(Id, User.GetUserId(), log);
var result = await ConcurrentSave(_db, log, "Unable to add url.");
string log = $"{logWording[0]}ed {DisplayName} {UrlType} URL {CurrentUrl}";
await _publicationMaintenanceLogger.Log(PublicationId, User.GetUserId(), log);
var result = await ConcurrentSave(_db, log, $"Unable to {logWording[1]} URL.");
if (result)
{
await _publisher.SendPublicationEdit(
$"{Id}M edited by {User.Name()}",
$"[{Id}M]({{0}}) edited by {User.Name()}",
$"Added {UrlType} url | {Title}",
$"{Id}M");
$"{PublicationId}M edited by {User.Name()}",
$"[{PublicationId}M]({{0}}) edited by {User.Name()}",
$"{logWording[0]}ed {UrlType} URL | {Title}",
$"{PublicationId}M");

if (UrlType == PublicationUrlType.Streaming && _youtubeSync.IsYoutubeUrl(PublicationUrl))
if (UrlType == PublicationUrlType.Streaming && _youtubeSync.IsYoutubeUrl(CurrentUrl))
{
YoutubeVideo video = new(
Id,
PublicationId,
publication.CreateTimestamp,
PublicationUrl,
CurrentUrl,
DisplayName,
publication.Title,
publicationWiki!,
Expand All @@ -161,32 +199,6 @@ await _publisher.SendPublicationEdit(
}
}

return RedirectToPage("EditUrls", new { Id });
}

public async Task<IActionResult> OnPostDelete(int publicationUrlId)
{
var url = await _db.PublicationUrls
.SingleOrDefaultAsync(pf => pf.Id == publicationUrlId);

if (url != null)
{
_db.PublicationUrls.Remove(url);
string log = $"Deleted {url.DisplayName} {url.Type} url {url.Url}";
await _publicationMaintenanceLogger.Log(url.PublicationId, User.GetUserId(), log);
var result = await ConcurrentSave(_db, log, "Unable to remove url.");
if (result)
{
await _publisher.SendPublicationEdit(
$"{Id}M edited by {User.Name()}",
$"[{Id}M]({{0}}) edited by {User.Name()}",
$"Deleted {url.Type} url",
$"{Id}M");

await _youtubeSync.UnlistVideo(url.Url!);
}
}

return RedirectToPage("EditUrls", new { Id });
return RedirectToPage("List", new { PublicationId });
}
}
Loading
Loading