mirror of
https://github.com/Sea-Haven-Industries/shoc-backend.git
synced 2026-09-30 10:43:13 +00:00
94 lines
2.7 KiB
C#
94 lines
2.7 KiB
C#
using Data.SeaHavenIndustries;
|
|
using Microsoft.EntityFrameworkCore;
|
|
using SeaHaven.DataServices.Interfaces;
|
|
|
|
namespace SeaHaven.DataServices.Implementation
|
|
{
|
|
public class VendorDataService : IVendorDataService
|
|
{
|
|
private readonly ApplicationDbContext _context;
|
|
|
|
public VendorDataService(ApplicationDbContext context)
|
|
{
|
|
_context = context;
|
|
}
|
|
|
|
public async Task<Vendor?> GetByIdAsync(int id)
|
|
{
|
|
return await _context.Vendors.FindAsync(id);
|
|
}
|
|
|
|
public async Task<Vendor?> GetByIdWithDetailsAsync(int id)
|
|
{
|
|
return await _context.Vendors
|
|
.FirstOrDefaultAsync(v => v.Id == id);
|
|
}
|
|
|
|
public async Task<IEnumerable<Vendor>> GetAllAsync()
|
|
{
|
|
return await _context.Vendors.ToListAsync();
|
|
}
|
|
|
|
public async Task<(IEnumerable<Vendor> Items, int TotalCount)> GetPagedAsync(
|
|
int page,
|
|
int pageSize,
|
|
string? search = null)
|
|
{
|
|
var query = _context.Vendors.AsQueryable();
|
|
|
|
var totalCount = await query.CountAsync();
|
|
var items = await query
|
|
.OrderBy(v => v.Id)
|
|
.Skip((page - 1) * pageSize)
|
|
.Take(pageSize)
|
|
.ToListAsync();
|
|
|
|
return (items, totalCount);
|
|
}
|
|
|
|
public async Task<Vendor> AddAsync(Vendor vendor)
|
|
{
|
|
await _context.Vendors.AddAsync(vendor);
|
|
await _context.SaveChangesAsync();
|
|
return vendor;
|
|
}
|
|
|
|
public async Task UpdateAsync(Vendor vendor)
|
|
{
|
|
_context.Vendors.Update(vendor);
|
|
await _context.SaveChangesAsync();
|
|
}
|
|
|
|
public async Task DeleteAsync(int id)
|
|
{
|
|
var entity = await GetByIdAsync(id);
|
|
if (entity != null)
|
|
{
|
|
_context.Vendors.Remove(entity);
|
|
await _context.SaveChangesAsync();
|
|
}
|
|
}
|
|
|
|
public async Task<bool> ExistsAsync(int id)
|
|
{
|
|
return await _context.Vendors.AnyAsync(v => v.Id == id);
|
|
}
|
|
|
|
public async Task<VendorAccessToken?> GetActiveTokenAsync(int vendorId)
|
|
{
|
|
return await _context.VendorAccessTokens
|
|
.FirstOrDefaultAsync(t => t.VendorId == vendorId);
|
|
}
|
|
|
|
public async Task<VendorAccessToken?> GetTokenByStringAsync(string token)
|
|
{
|
|
return await _context.VendorAccessTokens
|
|
.FirstOrDefaultAsync(t => t.Token == token);
|
|
}
|
|
|
|
public async Task<int> CountAsync()
|
|
{
|
|
return await _context.Vendors.CountAsync();
|
|
}
|
|
}
|
|
}
|