mirror of
https://github.com/Sea-Haven-Industries/shoc-backend.git
synced 2026-09-30 10:43:13 +00:00
56 lines
1.6 KiB
C#
56 lines
1.6 KiB
C#
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.Now;
|
|
await _context.Categories.AddAsync(category);
|
|
await _context.SaveChangesAsync();
|
|
return category;
|
|
}
|
|
|
|
public async Task UpdateAsync(Category category)
|
|
{
|
|
category.LastModificationTime = DateTime.Now;
|
|
_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);
|
|
}
|
|
}
|
|
}
|