This commit is contained in:
jheaps
2025-10-09 11:09:31 -06:00
parent 5ce1f88372
commit f4c88f83df
5 changed files with 38 additions and 16 deletions
+2 -1
View File
@@ -4,7 +4,8 @@
"Bash(dotnet build)", "Bash(dotnet build)",
"Bash(dotnet run:*)", "Bash(dotnet run:*)",
"Bash(powershell:*)", "Bash(powershell:*)",
"Bash(dotnet script:*)" "Bash(dotnet script:*)",
"Bash(taskkill:*)"
], ],
"deny": [], "deny": [],
"ask": [] "ask": []
+5 -5
View File
@@ -1,4 +1,4 @@
@page @page "{handler?}"
@model Media.JoshHeaps.Net.Pages.IndexModel @model Media.JoshHeaps.Net.Pages.IndexModel
@{ @{
ViewData["Title"] = "Dashboard"; ViewData["Title"] = "Dashboard";
@@ -47,7 +47,7 @@
<label for="Description">Description (optional)</label> <label for="Description">Description (optional)</label>
<textarea id="Description" name="Description" rows="3" placeholder="Add a description..."></textarea> <textarea id="Description" name="Description" rows="3" placeholder="Add a description..."></textarea>
</div> </div>
<button type="submit" class="btn btn-primary">Upload</button> <button type="submit" class="btn btn-primary" asp-page-handler="Add">Upload</button>
</form> </form>
</div> </div>
@@ -64,10 +64,10 @@
} }
<div class="gallery-item-info"> <div class="gallery-item-info">
<span class="gallery-item-date">@media.CreatedAt.ToString("MMM d, yyyy")</span> <span class="gallery-item-date">@media.CreatedAt.ToString("MMM d, yyyy")</span>
<form method="post" asp-page-handler="Delete" style="display:inline;"> <form method="post" style="display:inline;">
@Html.AntiForgeryToken() @Html.AntiForgeryToken()
<input type="hidden" name="mediaId" value="@media.Id" /> <input type="hidden" name="MediaId" value="@media.Id" />
<button type="submit" class="btn-delete" onclick="return confirm('Are you sure you want to delete this image?');">Delete</button> <button type="submit" class="btn-delete" onclick="return confirm('Are you sure you want to delete this image?');" asp-page-handler="Delete">Delete</button>
</form> </form>
</div> </div>
</div> </div>
+21 -7
View File
@@ -17,6 +17,9 @@ namespace Media.JoshHeaps.Net.Pages
[BindProperty] [BindProperty]
public string? Description { get; set; } public string? Description { get; set; }
[BindProperty]
public long? MediaId { get; set; }
public string? UploadMessage { get; set; } public string? UploadMessage { get; set; }
public bool UploadSuccess { get; set; } public bool UploadSuccess { get; set; }
@@ -39,6 +42,17 @@ namespace Media.JoshHeaps.Net.Pages
RequireAuthentication(); RequireAuthentication();
LoadUserSession(); LoadUserSession();
Dashboard = await userService.GetUserDashboardAsync(UserId);
MediaItems = await _mediaService.GetUserMediaAsync(UserId, 0, 20);
return Page();
}
public async Task<IActionResult> OnPostAddAsync()
{
RequireAuthentication();
LoadUserSession();
if (UploadedFile == null || UploadedFile.Length == 0) if (UploadedFile == null || UploadedFile.Length == 0)
{ {
UploadMessage = "Please select a file to upload."; UploadMessage = "Please select a file to upload.";
@@ -69,19 +83,19 @@ namespace Media.JoshHeaps.Net.Pages
} }
} }
// Reload data // Redirect to prevent form resubmission on refresh
Dashboard = await userService.GetUserDashboardAsync(UserId); return RedirectToPage();
MediaItems = await _mediaService.GetUserMediaAsync(UserId, 0, 20);
return Page();
} }
public async Task<IActionResult> OnPostDeleteAsync(long mediaId) public async Task<IActionResult> OnPostDeleteAsync()
{ {
RequireAuthentication(); RequireAuthentication();
LoadUserSession(); LoadUserSession();
var success = await _mediaService.DeleteMediaAsync(mediaId, UserId); if (MediaId.HasValue)
{
var success = await _mediaService.DeleteMediaAsync(MediaId.Value, UserId);
}
return RedirectToPage(); return RedirectToPage();
} }
@@ -0,0 +1,4 @@
@using Media.JoshHeaps.Net
@using Media.JoshHeaps.Net.Pages
@namespace Media.JoshHeaps.Net.Pages
@addTagHelper *, Microsoft.AspNetCore.Mvc.TagHelpers
+6 -3
View File
@@ -89,7 +89,7 @@ function createGalleryItem(media) {
const form = document.createElement('form'); const form = document.createElement('form');
form.method = 'post'; form.method = 'post';
form.action = '/?handler=Delete'; form.action = '?handler=Delete'; // Relative to current page
form.style.display = 'inline'; form.style.display = 'inline';
// Add anti-forgery token // Add anti-forgery token
@@ -101,7 +101,7 @@ function createGalleryItem(media) {
const hiddenInput = document.createElement('input'); const hiddenInput = document.createElement('input');
hiddenInput.type = 'hidden'; hiddenInput.type = 'hidden';
hiddenInput.name = 'mediaId'; hiddenInput.name = 'MediaId'; // Match C# property name exactly
hiddenInput.value = media.id; hiddenInput.value = media.id;
form.appendChild(hiddenInput); form.appendChild(hiddenInput);
@@ -110,7 +110,10 @@ function createGalleryItem(media) {
deleteBtn.className = 'btn-delete'; deleteBtn.className = 'btn-delete';
deleteBtn.textContent = 'Delete'; deleteBtn.textContent = 'Delete';
deleteBtn.onclick = function(e) { deleteBtn.onclick = function(e) {
return confirm('Are you sure you want to delete this image?'); if (!confirm('Are you sure you want to delete this image?')) {
e.preventDefault();
return false;
}
}; };
form.appendChild(deleteBtn); form.appendChild(deleteBtn);