Added Deletion flow for files.

This commit is contained in:
michael-bailey 2025-03-18 13:20:39 +00:00
parent dade1ddb62
commit bbb052db9c
5 changed files with 79 additions and 6 deletions

View File

@ -1,16 +1,11 @@
using System.Diagnostics.CodeAnalysis;
using FileStorageService.www.Atttributes;
using FileStorageService.www.Data;
using FileStorageService.www.Models;
using FileStorageService.www.Repositories;
using Microsoft.AspNetCore.Mvc;
using Microsoft.Build.Framework;
using Microsoft.EntityFrameworkCore;
namespace FileStorageService.www.Controllers;
public class FilesController(
ApplicationDbContext context,
FileRepository fileRepository) : Controller
{
private const long MaxFileSize = 7L * 1024L * 1024L * 1024L;
@ -90,5 +85,33 @@ public class FilesController(
{
return View();
}
public async Task<IActionResult> ConfirmDelete(Guid id)
{
var handle = await fileRepository.GetFileHandle(id);
var model = new FileHandleDeleteModel()
{
Id = handle.Id,
FileName = handle.Name,
BlockCount = handle.FileBlockCount,
};
return View(model);
}
[HttpPost]
public async Task<IActionResult> Delete(FileHandleDeleteModel model)
{
if (!ModelState.IsValid)
return RedirectToAction("ConfirmDelete", new {id = model.Id});
if (model.ConfirmFileName != model.FileName)
return RedirectToAction("ConfirmDelete", new {id = model.Id});
await fileRepository.DeleteFileHandle(model.Id);
return RedirectToAction("Index");
}
}

View File

@ -0,0 +1,9 @@
namespace FileStorageService.www.Models;
public class FileHandleDeleteModel
{
public required Guid Id { get; init; }
public required string FileName { get; init; }
public string ConfirmFileName { get; init; } = "";
public int BlockCount { get; set; }
}

View File

@ -1,4 +1,5 @@
using FileStorageService.www.Data;
using Microsoft.AspNetCore.Mvc;
using Microsoft.EntityFrameworkCore;
namespace FileStorageService.www.Repositories;
@ -91,4 +92,20 @@ public class FileRepository(ApplicationDbContext context)
return fileHandle;
}
public async Task<FileHandle> GetFileHandle(Guid id)
{
return await context.FileHandles.FirstAsync(f => f.Id == id);
}
public async Task DeleteFileHandle(Guid id)
{
var handle = await context.FileHandles
.Include(f => f.FileBlocks)
.FirstAsync(f => f.Id == id);
context.FileHandles.Remove(handle);
await context.SaveChangesAsync();
}
}

View File

@ -0,0 +1,20 @@
@model FileHandleDeleteModel
@{
ViewBag.Title = "title";
Layout = "_Layout";
}
<h2>Delete - @Model.FileName </h2>
<form asp-action="Delete" method="post">
<input asp-for="Id" type="hidden" />
<input asp-for="FileName" type="hidden"/>
<p>Are you sure you want this file to be deleted?</p>
<p>If so, please enter file name here to continue</p>
<input asp-for="ConfirmFileName"/>
<input type="submit" class="btn btn-danger"/>
</form>

View File

@ -53,7 +53,11 @@
<th scope="row" class="align-middle">
<a
asp-controller="Files" asp-action="Index" asp-route-id="@file.Id"
class="btn btn-primary">View...</a></th>
class="btn btn-primary me-2">View...</a>
<a
asp-controller="Files" asp-action="ConfirmDelete" asp-route-id="@file.Id"
class="btn btn-danger">Delete...</a>
</th>
</tr>
}
</tbody>