35 lines
856 B
C#
35 lines
856 B
C#
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();
|
|
}
|
|
}
|