mirror of
https://github.com/Sea-Haven-Industries/shoc-backend.git
synced 2026-09-30 06:03:12 +00:00
91 lines
2.6 KiB
C#
91 lines
2.6 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
|
|
.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)
|
|
{
|
|
var query = _context.Assets.AsQueryable();
|
|
|
|
var totalCount = await query.CountAsync();
|
|
var items = await query
|
|
.OrderBy(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();
|
|
}
|
|
}
|
|
}
|