public async Task <ActionResult <IEnumerable <Cars> > > GetCars() { var brandsS = new BrandsService(_context); var brands = await brandsS.getBrandsWithModels(); var cars = await getcarsFull(); return(new OkObjectResult(cars)); }
public async Task <List <Cars> > getcarsFull(int carId = 0) { var brandsS = new BrandsService(_context); var brands = await brandsS.getBrandsWithModels(); List <Cars> cars = new List <Cars>(); var carsAll = await _context.Cars.Join(brands, car => car.BrandId, brand => brand.Id, (c, b) => new { Car = c, Brand = b }) .Select(c => new Cars { Id = c.Car.Id, Brand = new Brands { Name = c.Car.Brand.Name, Id = c.Car.Brand.Id }, Model = c.Brand.Models.Where(m => m.Id == c.Car.ModelId).Select(m => new WeRentCar123.Models.Models { Name = m.Name, Id = m.Id }).FirstOrDefault(), Year = c.Car.Year, DailyPrice = c.Car.DailyPrice, ClientId = c.Car.ClientId, Notes = c.Car.Notes, BrandId = c.Car.BrandId, ModelId = c.Car.ModelId, Color = c.Car.Color, ImageUrl = c.Car.ImageUrl }) .ToListAsync(); if (carId > 0) { cars = carsAll.Where(c => c.Id == carId).ToList(); } else { cars = carsAll; } return(cars); }