internal CarListing Delete(int id) { CarListing original = Get(id); _repo.Delete(id); return(original); }
internal CarListing Get(int id) { CarListing car = _repo.Get(id); if (car == null) { throw new Exception("invalid id"); } return(car); }
public ActionResult <CarListing> Create([FromBody] CarListing newCar) { try { return(Ok(_service.Create(newCar))); } catch (System.Exception err) { return(BadRequest(err.Message)); } }
public ActionResult <CarListing> Create([FromBody] CarListing newCar) { try { FakeDB.Cars.Add(newCar); return(Ok(newCar)); } catch (System.Exception err) { return(BadRequest(err.Message)); } }
public ActionResult <CarListing> Edit([FromBody] CarListing editedCar, int id) { try { editedCar.Id = id; return(Ok(_service.Edit(editedCar))); } catch (System.Exception err) { return(BadRequest(err.Message)); } }
internal CarListing Create(CarListing newCar) { string sql = @" INSERT INTO cars (make, model, color, price, mileage, year) VALUES (@Make, @Model, @Color, @Price, @Mileage, @Year); SELECT LAST_INSERT_ID();"; int id = _db.ExecuteScalar <int>(sql, newCar); newCar.Id = id; return(newCar); }
internal CarListing Edit(CarListing carToEdit) { //After you go an update it make sure to go and select it again string sql = @" UPDATE cars SET make = @Make, model = @Model, price = @Price, year = @Year, mileage = @Mileage WHERE id = @Id; SELECT * FROM cars WHERE id = @Id;"; return(_db.QueryFirstOrDefault <CarListing>(sql, carToEdit)); }
internal CarListing Edit(CarListing editCar) { CarListing original = Get(editCar.Id); original.Make = editCar.Make; original.Model = editCar.Model; original.Color = editCar.Color != null ? editCar.Color : original.Color; original.Price = editCar.Price; original.Mileage = editCar.Mileage; // NOTE: if you need to null check a number, you can use the Elvis operator on the type in the class. original.Year = editCar.Year != null ? editCar.Year : original.Year; return(_repo.Edit(original)); }
public ActionResult <string> DeleteCar(string id) { try { CarListing carToRemove = FakeDB.Cars.Find(c => c.Id == id); if (FakeDB.Cars.Remove(carToRemove)) { return(Ok("Car Delorted")); } else { throw new System.Exception("This car does not exist."); } } catch (System.Exception err) { return(BadRequest(err.Message)); } }
public ActionResult <CarListing> GetCar(string carId) { try { CarListing carFound = FakeDB.Cars.Find(c => c.Id == carId); if (carFound == null) { throw new System.Exception("Car does not exist"); } return(Ok(carFound)); //NOTE can use exists to see if a list contains something, but it returns a bool so be cautious when returning. // bool carFound = FakeDB.Cars.Exists(c => c.Id == carId); // if (carFound == false) // { // throw new System.Exception("Car does not exist"); // } } catch (System.Exception err) { return(BadRequest(err.Message)); } }
internal CarListing Create(CarListing newCar) { return(_repo.Create(newCar)); }