mirror of
https://github.com/Sea-Haven-Industries/shoc-backend.git
synced 2026-09-30 11:53:12 +00:00
116 lines
3.4 KiB
C#
116 lines
3.4 KiB
C#
|
|
using Data.SeaHavenIndustries;
|
||
|
|
using Microsoft.EntityFrameworkCore;
|
||
|
|
using SeaHaven.DataServices.Interfaces;
|
||
|
|
|
||
|
|
namespace SeaHaven.DataServices.Implementation
|
||
|
|
{
|
||
|
|
public class DispatchDataService : IDispatchDataService
|
||
|
|
{
|
||
|
|
private readonly ApplicationDbContext _context;
|
||
|
|
|
||
|
|
public DispatchDataService(ApplicationDbContext context)
|
||
|
|
{
|
||
|
|
_context = context;
|
||
|
|
}
|
||
|
|
|
||
|
|
public async Task<Dispatch?> GetByIdAsync(int id)
|
||
|
|
{
|
||
|
|
return await _context.Dispatches.FindAsync(id);
|
||
|
|
}
|
||
|
|
|
||
|
|
public async Task<Dispatch?> GetByIdWithDetailsAsync(int id)
|
||
|
|
{
|
||
|
|
return await _context.Dispatches
|
||
|
|
.Include(d => d.Vendor)
|
||
|
|
.FirstOrDefaultAsync(d => d.Id == id);
|
||
|
|
}
|
||
|
|
|
||
|
|
public async Task<IEnumerable<Dispatch>> GetAllAsync()
|
||
|
|
{
|
||
|
|
return await _context.Dispatches.ToListAsync();
|
||
|
|
}
|
||
|
|
|
||
|
|
public async Task<IEnumerable<Dispatch>> GetByVendorIdAsync(int vendorId)
|
||
|
|
{
|
||
|
|
return await _context.Dispatches
|
||
|
|
.Where(d => d.VendorId == vendorId)
|
||
|
|
.ToListAsync();
|
||
|
|
}
|
||
|
|
|
||
|
|
public async Task<IEnumerable<Dispatch>> GetByStatusAsync(string status)
|
||
|
|
{
|
||
|
|
return await _context.Dispatches
|
||
|
|
.Where(d => d.Status == status)
|
||
|
|
.ToListAsync();
|
||
|
|
}
|
||
|
|
|
||
|
|
public async Task<(IEnumerable<Dispatch> Items, int TotalCount)> GetPagedAsync(
|
||
|
|
int page,
|
||
|
|
int pageSize,
|
||
|
|
string? search = null,
|
||
|
|
string? status = null)
|
||
|
|
{
|
||
|
|
var query = _context.Dispatches.AsQueryable();
|
||
|
|
|
||
|
|
if (!string.IsNullOrWhiteSpace(search))
|
||
|
|
{
|
||
|
|
query = query.Where(d => d.DispatchNumber!.Contains(search));
|
||
|
|
}
|
||
|
|
|
||
|
|
if (!string.IsNullOrWhiteSpace(status))
|
||
|
|
{
|
||
|
|
query = query.Where(d => d.Status == status);
|
||
|
|
}
|
||
|
|
|
||
|
|
var totalCount = await query.CountAsync();
|
||
|
|
var items = await query
|
||
|
|
.OrderByDescending(d => d.CreatedDate)
|
||
|
|
.Skip((page - 1) * pageSize)
|
||
|
|
.Take(pageSize)
|
||
|
|
.ToListAsync();
|
||
|
|
|
||
|
|
return (items, totalCount);
|
||
|
|
}
|
||
|
|
|
||
|
|
public async Task<Dispatch> AddAsync(Dispatch dispatch)
|
||
|
|
{
|
||
|
|
dispatch.CreatedDate = DateTime.Now;
|
||
|
|
await _context.Dispatches.AddAsync(dispatch);
|
||
|
|
await _context.SaveChangesAsync();
|
||
|
|
return dispatch;
|
||
|
|
}
|
||
|
|
|
||
|
|
public async Task UpdateAsync(Dispatch dispatch)
|
||
|
|
{
|
||
|
|
dispatch.LastModificationTime = DateTime.Now;
|
||
|
|
_context.Dispatches.Update(dispatch);
|
||
|
|
await _context.SaveChangesAsync();
|
||
|
|
}
|
||
|
|
|
||
|
|
public async Task DeleteAsync(int id)
|
||
|
|
{
|
||
|
|
var entity = await GetByIdAsync(id);
|
||
|
|
if (entity != null)
|
||
|
|
{
|
||
|
|
_context.Dispatches.Remove(entity);
|
||
|
|
await _context.SaveChangesAsync();
|
||
|
|
}
|
||
|
|
}
|
||
|
|
|
||
|
|
public async Task<bool> ExistsAsync(int id)
|
||
|
|
{
|
||
|
|
return await _context.Dispatches.AnyAsync(d => d.Id == id);
|
||
|
|
}
|
||
|
|
|
||
|
|
public async Task<string> GenerateNextDispatchNumberAsync()
|
||
|
|
{
|
||
|
|
return await Task.FromResult($"DISP-{DateTime.Now:yyyyMMdd}-{new Random().Next(1000, 9999)}");
|
||
|
|
}
|
||
|
|
|
||
|
|
public async Task<int> CountAsync()
|
||
|
|
{
|
||
|
|
return await _context.Dispatches.CountAsync();
|
||
|
|
}
|
||
|
|
}
|
||
|
|
}
|