mirror of
https://github.com/Sea-Haven-Industries/shoc-backend.git
synced 2026-09-30 11:53:12 +00:00
108 lines
3.2 KiB
C#
108 lines
3.2 KiB
C#
using Data.SeaHavenIndustries;
|
|
using Microsoft.EntityFrameworkCore;
|
|
using SeaHaven.DataServices.Interfaces;
|
|
|
|
namespace SeaHaven.DataServices.Implementation
|
|
{
|
|
public class AssetDataService : IAssetDataService
|
|
{
|
|
private readonly ApplicationDbContext _context;
|
|
|
|
public AssetDataService(ApplicationDbContext context)
|
|
{
|
|
_context = context;
|
|
}
|
|
|
|
public async Task<Assets?> GetByIdAsync(int id)
|
|
{
|
|
return await _context.Assets.FindAsync(id);
|
|
}
|
|
|
|
public async Task<Assets?> GetByIdWithDetailsAsync(int id)
|
|
{
|
|
return await _context.Assets
|
|
.Include(a => a.Account)
|
|
.Include(a => a.Location)
|
|
.FirstOrDefaultAsync(a => a.Id == id);
|
|
}
|
|
|
|
public async Task<IEnumerable<Assets>> GetAllAsync()
|
|
{
|
|
return await _context.Assets.ToListAsync();
|
|
}
|
|
|
|
public async Task<IEnumerable<Assets>> GetByLocationIdAsync(int locationId)
|
|
{
|
|
return await _context.Assets
|
|
.Where(a => a.LocationId == locationId)
|
|
.ToListAsync();
|
|
}
|
|
|
|
public async Task<(IEnumerable<Assets> Items, int TotalCount)> GetPagedAsync(
|
|
int page,
|
|
int pageSize,
|
|
string? search = null)
|
|
{
|
|
search ??= "";
|
|
|
|
var query = _context.Assets
|
|
.Include(a => a.Account)
|
|
.Include(a => a.Location)
|
|
.AsQueryable();
|
|
|
|
if (!string.IsNullOrWhiteSpace(search))
|
|
{
|
|
var s = search.ToLower();
|
|
query = query.Where(a =>
|
|
(a.Name != null && a.Name.ToLower().Contains(s)) ||
|
|
(a.AssetCode != null && a.AssetCode.ToLower().Contains(s)) ||
|
|
(a.Description != null && a.Description.ToLower().Contains(s))
|
|
);
|
|
}
|
|
|
|
var totalCount = await query.CountAsync();
|
|
var items = await query
|
|
.OrderByDescending(a => a.Id)
|
|
.Skip((page - 1) * pageSize)
|
|
.Take(pageSize)
|
|
.ToListAsync();
|
|
|
|
return (items, totalCount);
|
|
}
|
|
|
|
public async Task<Assets> AddAsync(Assets asset)
|
|
{
|
|
asset.CreatedDate = DateTime.Now;
|
|
await _context.Assets.AddAsync(asset);
|
|
await _context.SaveChangesAsync();
|
|
return asset;
|
|
}
|
|
|
|
public async Task UpdateAsync(Assets asset)
|
|
{
|
|
asset.LastModificationTime = DateTime.Now;
|
|
_context.Assets.Update(asset);
|
|
await _context.SaveChangesAsync();
|
|
}
|
|
|
|
public async Task DeleteAsync(int id)
|
|
{
|
|
var entity = await GetByIdAsync(id);
|
|
if (entity != null)
|
|
{
|
|
_context.Assets.Remove(entity);
|
|
await _context.SaveChangesAsync();
|
|
}
|
|
}
|
|
|
|
public async Task<bool> ExistsAsync(int id)
|
|
{
|
|
return await _context.Assets.AnyAsync(a => a.Id == id);
|
|
}
|
|
|
|
public async Task<int> CountAsync()
|
|
{
|
|
return await _context.Assets.CountAsync();
|
|
}
|
|
}
|
|
}
|