Untitled

mail@pastecode.io avatar
unknown
plain_text
2 months ago
1.6 kB
1
Indexable
Never
using System.Collections.Generic;
using System.Linq;
using System.Threading.Tasks;
using Microsoft.EntityFrameworkCore;

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

public class OrderRepository : IOrderRepository
{
    private readonly YourDbContext _dbContext;

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

    public async Task<List<OrderWithAddress>> FetchOrders(string gmail)
    {
        var ordersWithAddress = await _dbContext.Orders
            .Where(o => o.EmailId == gmail)
            .Join(
                _dbContext.Addresses,
                order => order.AddressId,
                address => address.AddressId,
                (order, address) => new OrderWithAddress
                {
                    OrderId = order.OrderId,
                    ProductId = order.ProductId,
                    EmailId = order.EmailId,
                    InvoiceId = order.InvoiceId,
                    AddressId = order.AddressId,
                    Address = new Address
                    {
                        AddressId = address.AddressId,
                        EmailId = address.EmailId,
                        MobileNumber = address.MobileNumber,
                        City = address.City,
                        State = address.State,
                        Pincode = address.Pincode,
                        Description = address.Description
                    }
                })
            .ToListAsync();

        return ordersWithAddress;
    }
}
Leave a Comment