shoc-backend/SeaHaven.DataServices/Implementation/CategoryDataService.cs

57 lines
1.6 KiB
C#
Raw Normal View History

2026-04-28 18:55:14 -05:00
using Data.SeaHavenIndustries;
using Microsoft.EntityFrameworkCore;
using SeaHaven.DataServices.Interfaces;
namespace SeaHaven.DataServices.Implementation
{
public class CategoryDataService : ICategoryDataService
{
private readonly ApplicationDbContext _context;
public CategoryDataService(ApplicationDbContext context)
{
_context = context;
}
public async Task<Category?> GetByIdAsync(int id)
{
return await _context.Categories.FindAsync(id);
}
public async Task<IEnumerable<Category>> GetAllAsync()
{
return await _context.Categories.ToListAsync();
}
public async Task<Category> AddAsync(Category category)
{
category.CreatedDate = DateTime.UtcNow;
2026-04-28 18:55:14 -05:00
await _context.Categories.AddAsync(category);
await _context.SaveChangesAsync();
return category;
}
public async Task UpdateAsync(Category category)
{
category.LastModificationTime = DateTime.UtcNow;
2026-04-28 18:55:14 -05:00
_context.Categories.Update(category);
await _context.SaveChangesAsync();
}
public async Task DeleteAsync(int id)
{
var entity = await GetByIdAsync(id);
if (entity != null)
{
_context.Categories.Remove(entity);
await _context.SaveChangesAsync();
}
}
public async Task<bool> ExistsAsync(int id)
{
return await _context.Categories.AnyAsync(c => c.Id == id);
}
}
}