public async Task <ActionResult <CarBrandOut> > GetCarBrand(int id) { var carBrand = await _context.CarBrands.FindAsync(id); if (carBrand == null) { return(NotFound()); } return(CarBrandOut.fromCarBrand(carBrand)); }
public async Task <ActionResult <CarBrandOut> > PostCarBrand(CarBrandIn carBrand) { if (_context.CarBrands.Any(b => b.Name == carBrand.Name)) { return(BadRequest("Brand name already exists!")); } _context.CarBrands.Add(new CarBrand { Name = carBrand.Name }); await _context.SaveChangesAsync(); CarBrand brand = await _context.CarBrands.Where(b => b.Name == carBrand.Name).FirstAsync(); return(CreatedAtAction("GetCarBrand", new { id = brand.Id }, CarBrandOut.fromCarBrand(brand))); }
public async Task <ActionResult <CarBrandOut> > PutCarBrand(int id, CarBrandIn carBrand) { CarBrand brand = await _context.CarBrands.FindAsync(id); if (brand == null) { return(NotFound()); } if (_context.CarBrands.Any(b => b.Id != id && b.Name == carBrand.Name)) { return(BadRequest("Brand name already exists!")); } brand.Name = carBrand.Name; _context.Entry(brand).State = EntityState.Modified; try { await _context.SaveChangesAsync(); } catch (DbUpdateConcurrencyException) { if (!CarBrandExists(id)) { return(NotFound()); } else { throw; } } //return NoContent(); return(CreatedAtAction("GetCarBrand", new { id = brand.Id }, CarBrandOut.fromCarBrand(brand))); }
public async Task <ActionResult <IEnumerable <CarBrandOut> > > GetCarBrands() { return(await _context.CarBrands.Select(carBrand => CarBrandOut.fromCarBrand(carBrand)).ToListAsync()); }