Add project files
This commit is contained in:
34
T120B165-ImgBoard/Services/FileService.cs
Normal file
34
T120B165-ImgBoard/Services/FileService.cs
Normal file
@@ -0,0 +1,34 @@
|
||||
using Microsoft.EntityFrameworkCore;
|
||||
using T120B165_ImgBoard.Data;
|
||||
using File = T120B165_ImgBoard.Models.File;
|
||||
|
||||
namespace T120B165_ImgBoard.Services;
|
||||
|
||||
public interface IFileService
|
||||
{
|
||||
public Task<File?> GetFileById(long id);
|
||||
Task<File> Update(File file);
|
||||
Task Delete(File file);
|
||||
}
|
||||
|
||||
public class FileService(ImgBoardContext context): IFileService
|
||||
{
|
||||
public async Task<File?> GetFileById(long id)
|
||||
{
|
||||
return await context.Files.Where(f => f.Id == id).FirstOrDefaultAsync();
|
||||
}
|
||||
|
||||
public async Task<File> Update(File file)
|
||||
{
|
||||
context.Files.Update(file);
|
||||
await context.SaveChangesAsync();
|
||||
return file;
|
||||
}
|
||||
|
||||
public async Task Delete(File file)
|
||||
{
|
||||
var actualFile = await GetFileById(file.Id);
|
||||
context.Files.Remove(actualFile);
|
||||
await context.SaveChangesAsync();
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user