public async Task <ActionResult <CarModelOut> > PutCarModel(int id, CarModelIn carModel)
        {
            CarModel model = await _context.CarModels.FindAsync(id);

            if (model == null)
            {
                return(BadRequest("Car model with id " + id + " does not exist!"));
            }


            CarBrand brand = await _context.CarBrands.FindAsync(carModel.CarBrandId);

            if (brand == null)
            {
                return(NotFound("Brand with id " + carModel.CarBrandId + " does not exist."));
            }

            if (_context.CarModels.Any(c => c.CarBrand.Id == brand.Id && carModel.Name == c.Name))
            {
                return(BadRequest("Car model already exists."));
            }

            model.Name     = carModel.Name;
            model.CarBrand = brand;

            _context.Entry(model).State = EntityState.Modified;

            try
            {
                await _context.SaveChangesAsync();
            }
            catch (DbUpdateConcurrencyException)
            {
                if (!CarModelExists(id))
                {
                    return(NotFound());
                }
                else
                {
                    throw;
                }
            }

            return(CreatedAtAction("GetCarModel", new { id = model.Id }, CarModelOut.fromCarModel(model)));
        }
        public async Task <ActionResult <CarModelOut> > PostCarModel(CarModelIn carModel)
        {
            CarBrand brand = await _context.CarBrands.FindAsync(carModel.CarBrandId);

            if (brand == null)
            {
                return(NotFound("Brand with id " + carModel.CarBrandId + " does not exist."));
            }
            if (_context.CarModels.Any(c => c.CarBrand.Id == brand.Id && carModel.Name == c.Name))
            {
                return(BadRequest("Car model already exists."));
            }
            CarModel model = new CarModel {
                Name = carModel.Name, CarBrand = brand
            };

            _context.CarModels.Add(model);
            await _context.SaveChangesAsync();

            return(CreatedAtAction("GetCarModel", new { id = model.Id }, CarModelOut.fromCarModel(model)));
        }
        public async Task <ActionResult <CarModelOut> > GetCarModel(int id)
        {
            var carModel = await _context.CarModels.Where(c => c.Id == id).Include(c => c.CarBrand).Select(c => CarModelOut.fromCarModel(c)).FirstOrDefaultAsync();   //FindAsync(id);

            if (carModel == null)
            {
                return(NotFound());
            }

            return(carModel);
        }
 public async Task <ActionResult <IEnumerable <CarModelOut> > > GetCarModels()
 {
     return(await _context.CarModels.Include(carModel => carModel.CarBrand).Select(carModel => CarModelOut.fromCarModel(carModel)).ToListAsync());
 }