Skip to content
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
36 changes: 36 additions & 0 deletions FileStorage/Controllers/FileController.cs
Original file line number Diff line number Diff line change
Expand Up @@ -137,6 +137,42 @@ public async Task<IActionResult> GetDocumentByHash(string hash)
};
}

[HttpDelete("{id}")]
public async Task<IActionResult> DeleteDocument(Guid id)
{
var document = await _context.Documents.FindAsync(id);
if (document == null)
return NotFound("Document not found.");

// Try to delete the file from disk if present
try
{
if (System.IO.File.Exists(document.FilePath))
{
System.IO.File.Delete(document.FilePath);
}
}
catch (Exception ex)
{
Console.WriteLine($"Error deleting file: {ex.Message}");
return StatusCode(500, "Error deleting file from disk.");
}

// Remove database entry
_context.Documents.Remove(document);
try
{
await _context.SaveChangesAsync();
}
catch (Exception ex)
{
Console.WriteLine($"Error deleting document from database: {ex.Message}");
return StatusCode(500, "Error deleting document from database.");
}

return Ok();
}

private string GetMimeType(string filePath)
{
var provider = new FileExtensionContentTypeProvider();
Expand Down
34 changes: 34 additions & 0 deletions FileStorageTest/UnitTest1.cs
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,7 @@
private FileController _controller;
private FileContext _context;
private string _rootPath;
private string _testFilePath;

Check warning on line 17 in FileStorageTest/UnitTest1.cs

View workflow job for this annotation

GitHub Actions / build

Non-nullable field '_testFilePath' must contain a non-null value when exiting constructor. Consider adding the 'required' modifier or declaring the field as nullable.

[SetUp]
public void Setup()
Expand All @@ -31,7 +31,7 @@

// Use reflection to set the private _rootPath field
var rootPathField = typeof(FileController).GetField("_rootPath", BindingFlags.NonPublic | BindingFlags.Instance);
rootPathField.SetValue(_controller, _rootPath);

Check warning on line 34 in FileStorageTest/UnitTest1.cs

View workflow job for this annotation

GitHub Actions / build

Dereference of a possibly null reference.
}

[TearDown]
Expand All @@ -50,7 +50,7 @@
{
var result = await _controller.UploadDocument(null);
Assert.That(result, Is.TypeOf<BadRequestObjectResult>());
Assert.That((result as BadRequestObjectResult).Value, Is.EqualTo("No file uploaded."));

Check warning on line 53 in FileStorageTest/UnitTest1.cs

View workflow job for this annotation

GitHub Actions / build

Dereference of a possibly null reference.
}

[Test]
Expand Down Expand Up @@ -146,5 +146,39 @@

Assert.That(result, Is.TypeOf<FileStreamResult>());
}

[Test]
public async Task DeleteDocument_NonExistentId_ReturnsNotFound()
{
var result = await _controller.DeleteDocument(Guid.NewGuid());

Assert.That(result, Is.TypeOf<NotFoundObjectResult>());
}

[Test]
public async Task DeleteDocument_ExistingId_RemovesFileAndEntry()
{
var documentId = Guid.NewGuid();
var document = new File
{
Id = documentId,
Name = "test.txt",
FilePath = Path.Combine(_rootPath, "test.txt"),
Hash = "hash",
UploadedAt = DateTime.UtcNow
};
_context.Documents.Add(document);
await _context.SaveChangesAsync();

// Create the file
_testFilePath = document.FilePath;
await System.IO.File.WriteAllTextAsync(_testFilePath, "content");

var result = await _controller.DeleteDocument(documentId);

Assert.That(result, Is.TypeOf<OkResult>());
Assert.That(await _context.Documents.FindAsync(documentId), Is.Null);
Assert.That(System.IO.File.Exists(_testFilePath), Is.False);
}
}
}
Loading