using AutoMapper; using System; using System.Collections.Generic; using System.Linq; using System.Text; using System.Threading.Tasks; using Microsoft.EntityFrameworkCore; using SytelineSaAppEfDataModel.Dtos; namespace SytelineSaAppEfDataModel.Services { public class CustomerOrderService(SytelineSaAppDbContext context, IMapper mapper) : ICustomerOrderService { public async Task> GetAll() { return await context.CustomerOrders.Select(x => mapper.Map(x)).ToListAsync(); } public async Task GetByOrderNumber(string orderNumber) { CustomerOrderDto? customerOrder = await context.CustomerOrders .Where(x => x.CoNum == orderNumber) .Select(x => mapper.Map(x)).FirstOrDefaultAsync(); if (customerOrder == null) return null; customerOrder.CustomerOrderLines = await context.CustomerOrderLines .Where(x => x.CoNum == orderNumber) .Select(x => mapper.Map(x)).ToListAsync(); foreach (CustomerOrderLineDto customerOrderLine in customerOrder.CustomerOrderLines) { customerOrderLine.CustomerOrderLineItems = await context.CustomerOrderLineItems .Where(x => x.CoNum == orderNumber && x.CoLine == customerOrderLine.CoLine) .Select(x => mapper.Map(x)).ToListAsync(); } return customerOrder; } } }