using System; using System.Collections.Generic; using System.Linq; using System.Threading.Tasks; using LCU.API.Internal.RepoApi.Repos.Models; using LCU.API.Internal.RepoApi.Repos.Persistence; using Microsoft.EntityFrameworkCore; namespace LCU.API.Internal.RepoApi.Repos.Repositories { public class ImsAutoFileDataRepository : BaseRepository, IImsAutoFileDataRepository { public ImsAutoFileDataRepository(AppDbContext context) : base(context) { } public async Task> ListAllAsync() { return await _context.ImsAutoFileData.ToListAsync(); } public async Task AddAsync(ImsAutoFileData assignment) { await _context.ImsAutoFileData.AddAsync(assignment); } public async Task FindByIdAsync(string vin, int repoId) { return await _context.ImsAutoFileData.Where(x => x.VIN == vin && x.RepoId == repoId).FirstOrDefaultAsync(); } public async Task FindByAuctionAssignmentIdAsync(int auctionAssignmentId) { return await _context.ImsAutoFileData.Where(x => x.AuctionAssignmentId == auctionAssignmentId) .FirstOrDefaultAsync(); } public async Task GetNonConformingImsFileRecord(string vin, string auctionCustomerNo) { ImsAutoFileData imsAutoFile = null; // Allow for vehicle brought back to auction after sale. it happens! DateTime withinThirtyDays = DateTime.Now.AddDays(-30); imsAutoFile = await _context.ImsAutoFileData.Where(x => (x.VIN.Trim() == vin && x.AuctionCustomerNo == auctionCustomerNo && (x.SoldDate == null || x.SoldDate >= withinThirtyDays))).FirstOrDefaultAsync(); return imsAutoFile; } public void Update(ImsAutoFileData autoImsAutoFileData) { _context.ImsAutoFileData.Update(autoImsAutoFileData); } public void Remove(ImsAutoFileData autoImsAutoFileData) { _context.ImsAutoFileData.Remove(autoImsAutoFileData); } public async Task> GetImsSoldRecordsForPeriod(DateTime lowDateTime, DateTime highDateTime) { return await _context.ImsAutoFileData .Where(x => x.SoldDate != null && x.SoldDate >= lowDateTime && x.SoldDate <= highDateTime) .ToListAsync(); } } }