Untitled

 avatar
unknown
plain_text
a year ago
2.8 kB
4
Indexable
using System.Collections.Generic;
using System.Linq;
using System.Threading.Tasks;
using Microsoft.EntityFrameworkCore;

public interface IOrderRepository
{
    Task<List<OrderWithAddressAndProduct>> FetchOrders(string gmail);
}

public class OrderRepository : IOrderRepository
{
    private readonly YourDbContext _dbContext;

    public OrderRepository(YourDbContext dbContext)
    {
        _dbContext = dbContext;
    }

    public async Task<List<OrderWithAddressAndProduct>> FetchOrders(string gmail)
    {
        var ordersWithAddressAndProduct = await _dbContext.Orders
            .Where(o => o.EmailId == gmail)
            .Join(
                _dbContext.Addresses,
                order => order.AddressId,
                address => address.AddressId,
                (order, address) => new
                {
                    Order = order,
                    Address = address
                })
            .Join(
                _dbContext.Products,
                orderAddress => orderAddress.Order.ProductId,
                product => product.ProductId,
                (orderAddress, product) => new OrderWithAddressAndProduct
                {
                    OrderId = orderAddress.Order.OrderId,
                    ProductId = orderAddress.Order.ProductId,
                    EmailId = orderAddress.Order.EmailId,
                    InvoiceId = orderAddress.Order.InvoiceId,
                    AddressId = orderAddress.Order.AddressId,
                    Address = new Address
                    {
                        AddressId = orderAddress.Address.AddressId,
                        EmailId = orderAddress.Address.EmailId,
                        MobileNumber = orderAddress.Address.MobileNumber,
                        City = orderAddress.Address.City,
                        State = orderAddress.Address.State,
                        Pincode = orderAddress.Address.Pincode,
                        Description = orderAddress.Address.Description
                    },
                    Product = new Product
                    {
                        ProductId = product.ProductId,
                        Name = product.Name,
                        Price = product.Price,
                        Description = product.Description,
                        Quantity = product.Quantity,
                        Description1 = product.Description1,
                        Description2 = product.Description2,
                        Description3 = product.Description3,
                        Category = product.Category,
                        ImageLink = product.ImageLink
                    }
                })
            .OrderByDescending(o => o.OrderId) // Sort by order ID in descending order
            .ToListAsync();

        return ordersWithAddressAndProduct;
    }
}
Editor is loading...
Leave a Comment