shoc-backend/SeaHaven.DataServices/Implementation/QuotesDataService.cs
2026-05-14 11:00:12 -05:00

75 lines
2 KiB
C#

using Data.SeaHavenIndustries;
using Microsoft.EntityFrameworkCore;
using SeaHaven.DataServices.Interfaces;
namespace SeaHaven.DataServices.Implementation
{
public class QuotesDataService : IQuotesDataService
{
private readonly ApplicationDbContext _context;
public QuotesDataService(ApplicationDbContext context)
{
_context = context;
}
public async Task<Quotes?> GetByIdAsync(int id)
{
return await _context.Quotes.FindAsync(id);
}
public async Task<IEnumerable<Quotes>> GetAllAsync()
{
return await _context.Quotes.ToListAsync();
}
public async Task<IEnumerable<Quotes>> GetByWorkOrderIdAsync(int workOrderId)
{
return await _context.Quotes
.Where(q => q.WorkorderId == workOrderId)
.ToListAsync();
}
public async Task<Quotes> AddAsync(Quotes quote)
{
await _context.Quotes.AddAsync(quote);
await _context.SaveChangesAsync();
return quote;
}
public async Task UpdateAsync(Quotes quote)
{
_context.Quotes.Update(quote);
await _context.SaveChangesAsync();
}
public async Task DeleteAsync(int id)
{
var entity = await GetByIdAsync(id);
if (entity != null)
{
_context.Quotes.Remove(entity);
await _context.SaveChangesAsync();
}
}
public async Task<bool> ExistsAsync(int id)
{
return await _context.Quotes.AnyAsync(q => q.Id == id);
}
public async Task UnlinkFromWorkOrderAsync(int workOrderId)
{
var quotes = await _context.Quotes
.Where(q => q.WorkorderId == workOrderId)
.ToListAsync();
foreach (var quote in quotes)
{
quote.WorkorderId = null;
}
await _context.SaveChangesAsync();
}
}
}