From bbb052db9c9894921c8b6009039bd22665219632 Mon Sep 17 00:00:00 2001 From: michael-bailey Date: Tue, 18 Mar 2025 13:20:39 +0000 Subject: [PATCH] Added Deletion flow for files. --- .../Controllers/FilesController.cs | 33 ++++++++++++++++--- .../Models/FileHandleDeleteModel.cs | 9 +++++ .../Repositories/FileRepository.cs | 17 ++++++++++ .../Views/Files/ConfirmDelete.cshtml | 20 +++++++++++ .../Views/Files/Index.cshtml | 6 +++- 5 files changed, 79 insertions(+), 6 deletions(-) create mode 100644 FileStorageService.www/Models/FileHandleDeleteModel.cs create mode 100644 FileStorageService.www/Views/Files/ConfirmDelete.cshtml diff --git a/FileStorageService.www/Controllers/FilesController.cs b/FileStorageService.www/Controllers/FilesController.cs index 99bf3b3..0e4645e 100644 --- a/FileStorageService.www/Controllers/FilesController.cs +++ b/FileStorageService.www/Controllers/FilesController.cs @@ -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 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 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"); + } } \ No newline at end of file diff --git a/FileStorageService.www/Models/FileHandleDeleteModel.cs b/FileStorageService.www/Models/FileHandleDeleteModel.cs new file mode 100644 index 0000000..e9f6a5a --- /dev/null +++ b/FileStorageService.www/Models/FileHandleDeleteModel.cs @@ -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; } +} \ No newline at end of file diff --git a/FileStorageService.www/Repositories/FileRepository.cs b/FileStorageService.www/Repositories/FileRepository.cs index d8a1616..5b2bfdd 100644 --- a/FileStorageService.www/Repositories/FileRepository.cs +++ b/FileStorageService.www/Repositories/FileRepository.cs @@ -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 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(); + } } \ No newline at end of file diff --git a/FileStorageService.www/Views/Files/ConfirmDelete.cshtml b/FileStorageService.www/Views/Files/ConfirmDelete.cshtml new file mode 100644 index 0000000..47fc79e --- /dev/null +++ b/FileStorageService.www/Views/Files/ConfirmDelete.cshtml @@ -0,0 +1,20 @@ +@model FileHandleDeleteModel + +@{ + ViewBag.Title = "title"; + Layout = "_Layout"; +} + +

Delete - @Model.FileName

+ +
+ + + +

Are you sure you want this file to be deleted?

+ +

If so, please enter file name here to continue

+ + + +
diff --git a/FileStorageService.www/Views/Files/Index.cshtml b/FileStorageService.www/Views/Files/Index.cshtml index a688613..32cdc40 100644 --- a/FileStorageService.www/Views/Files/Index.cshtml +++ b/FileStorageService.www/Views/Files/Index.cshtml @@ -53,7 +53,11 @@ View... + class="btn btn-primary me-2">View... + Delete... + }