/// <summary> /// edit a car info in the db /// </summary> /// <param name="Carlicenceparam">the car licence number</param> /// <param name="carparam">a car modle contains all the car data</param> /// <returns>true if the actions sucseed false if it didnt</returns> public static bool EditCar(string Carlicenceparam, CarModel carparam) { try { using (CarRentalDbV2Entities db = new CarRentalDbV2Entities()) { CarsTable dbcar = db.CarsTables.SingleOrDefault(a => a.CarlicenseNumber == Carlicenceparam); CarsTypesTable dbcarType = db.CarsTypesTables.SingleOrDefault(a => a.Model == carparam.CarType.Model); BranchesTable dbBrance = db.BranchesTables.SingleOrDefault(a => a.BranceName == carparam.CarLocation.BranceName); if (dbcar == null || dbcarType == null || dbBrance == null) { return(false); } dbcar.CarImg = carparam.CarImg; dbcar.CarLocation = dbBrance.ID; dbcar.CarKilometer = carparam.CarKilometer; dbcar.CarlicenseNumber = carparam.CarlicenseNumber; dbcar.CarStatus = carparam.CarStatus; dbcar.CarType = dbcarType.ID; db.SaveChanges(); return(true); } } catch { return(false); } }
private void AddRowsInCarsTable() { CarsTable.Rows.Clear(); foreach (var item in _Cars) { CarsTable.Rows.Add(new object[] { item.CarId, item.Brand, item.Model, item.YearOfIssue, Utilities.GetDescription(item.BodyType), item.EngineVolume + " л", Utilities.GetDescription(item.EngineType), Utilities.GetDescription(item.TransmissionType), Utilities.GetDescription(item.WheelDriveType), item.Mileage + " км", Utilities.GetDescription(item.BodyColor), Utilities.GetDescription(item.InteriorMaterial), Utilities.GetDescription(item.InteriorColor), item.Price + " $", "Купить" }); } CarsTable.ClearSelection(); }
/// <summary> /// add a new car to the db /// </summary> /// <param name="NewCar">the car modle object</param> /// <returns> true if the action sucseed false if it didnt</returns> public static bool AddNewCar(CarModel NewCar) { try { using (CarRentalDbV2Entities db = new CarRentalDbV2Entities()) { BranchesTable dbBrance = db.BranchesTables.FirstOrDefault(a => a.BranceName == NewCar.CarLocation.BranceName); CarsTypesTable dbCarType = db.CarsTypesTables.FirstOrDefault(a => a.Model == NewCar.CarType.Model); if (dbBrance == null || dbCarType == null) { return(false); } CarsTable NewdbCar = new CarsTable { CarLocation = dbBrance.ID, CarType = dbCarType.ID, CarKilometer = NewCar.CarKilometer, CarImg = NewCar.CarImg, CarStatus = NewCar.CarStatus, CarlicenseNumber = NewCar.CarlicenseNumber, }; db.CarsTables.Add(NewdbCar); db.SaveChanges(); return(true); } } catch { return(false); } }
/// <summary> /// set a new rent to the DB /// </summary> /// <param name="newrentparam">a rent model object</param> /// <returns>true if the actions secseed false if it didnt</returns> public static bool AddNewRent(RentModel newrentparam) { try { using (CarRentalDbV2Entities db = new CarRentalDbV2Entities()) { UsersTable dbUser = db.UsersTables.FirstOrDefault(a => a.UserName == newrentparam.UserInfo.UserName); CarsTable dbCar = db.CarsTables.FirstOrDefault(a => a.CarlicenseNumber == newrentparam.CarInfo.CarlicenseNumber); if (dbUser == null || dbCar == null) { return(false); } RentTable NewRent = new RentTable { StartDate = newrentparam.StartDate, EndDate = newrentparam.EndDate, ReturnDate = newrentparam.ReturnDate, UserID = dbUser.ID, CarID = dbCar.ID }; db.RentTables.Add(NewRent); db.SaveChanges(); return(true); } } catch { return(false); } }
/// <summary> /// edit a rent acording to a rent id sent by the cient /// </summary> /// <param name="RentId">the rent id sent by the client</param> /// <param name="rentparam">a rent model contains all the rent data</param> /// <returns>true if the actions secseed false if it didnt</returns> public static bool EditRent(int RentId, RentModel rentparam) { try { using (CarRentalDbV2Entities db = new CarRentalDbV2Entities()) { RentTable dbrent = db.RentTables.SingleOrDefault(a => a.ID == RentId); UsersTable dbuser = db.UsersTables.SingleOrDefault(a => a.UserName == rentparam.UserInfo.UserName); CarsTable dbcar = db.CarsTables.SingleOrDefault(a => a.CarlicenseNumber == rentparam.CarInfo.CarlicenseNumber); if (dbrent == null || dbuser == null || dbcar == null) { return(false); } dbrent.CarID = dbcar.ID; dbrent.UserID = dbuser.ID; dbrent.EndDate = rentparam.EndDate; dbrent.ReturnDate = rentparam.ReturnDate; dbrent.StartDate = rentparam.StartDate; db.SaveChanges(); return(true); } } catch { return(false); } }
// ******************************************************************************************************************************************* // POST: api/cars/admin/add/ -- admin using // function that get 1 parameter from CarsTable type // looking if this carLicenseNumber is already exists // if null and carlicense number != from 0 it adds it to the table and return success // else returns badrequest public IHttpActionResult Post([FromBody] CarsTable car) { using (CarsDBEntities db = new CarsDBEntities()) { var currentCar = db.CarsTables.FirstOrDefault(c => c.CarLicenseNumber == car.CarLicenseNumber); if (currentCar == null && car.CarLicenseNumber != 0) { db.CarsTables.Add(car); db.SaveChanges(); return(Ok("The Car Number " + car.CarLicenseNumber + " added to the table")); } return(BadRequest("Cannot Add This Car The Car Id Number" + car.CarLicenseNumber + "is already exists")); } }
/// <summary> /// get a single car model /// </summary> /// <param name="Carlicenceparam">the car licence number from the client</param> /// <returns>a car model object</returns> public static CarModel GetSpesificCar(string Carlicenceparam) { try { using (DAL.CarRentalDbV2Entities db = new CarRentalDbV2Entities()) { CarsTable dbcar = db.CarsTables.SingleOrDefault(a => a.CarlicenseNumber == Carlicenceparam); if (dbcar == null) { return(null); } return(new CarModel { CarImg = dbcar.CarImg, CarLocation = new BrancheModel { Adress = dbcar.BranchesTable.Adress, PositionX = dbcar.BranchesTable.PositionX, PositionY = dbcar.BranchesTable.PositionY, BranceName = dbcar.BranchesTable.BranceName }, CarKilometer = dbcar.CarKilometer, CarlicenseNumber = dbcar.CarlicenseNumber, CarStatus = dbcar.CarStatus, CarType = new CarTypeModel { CostPerDay = dbcar.CarsTypesTable.CostPerDay, FinePrice = dbcar.CarsTypesTable.FinePrice, IsAutomatic = dbcar.CarsTypesTable.IsAutomatic, Manufacturer = dbcar.CarsTypesTable.Manufacturer, Model = dbcar.CarsTypesTable.Model, Year = dbcar.CarsTypesTable.Year } }); } } catch { return(null); } }
/// <summary> /// delete a car from the db /// </summary> /// <param name="Carlicenceparam">the car licence number</param> /// <returns>true if the actions secseed false if it didnt</returns> public static bool DeleteCar(string Carlicenceparam) { try { using (CarRentalDbV2Entities db = new CarRentalDbV2Entities()) { CarsTable dbcar = db.CarsTables.SingleOrDefault(a => a.CarlicenseNumber == Carlicenceparam); if (dbcar == null) { return(false); } db.CarsTables.Remove(dbcar); db.SaveChanges(); return(true); } } catch { return(false); } }
// ********************************************************************************************************************************************* // PUT: api/Cars/5 // Function that edits values in CarsTable by getting 1 parameter from CarsTable type // if this car.CarLicenseNumber is found to check the values are true and edits the currentcar values and return success // else return bad request public HttpResponseMessage Put([FromBody] CarsTable car) { using (CarsDBEntities db = new CarsDBEntities()) { var currentCar = db.CarsTables.FirstOrDefault(tmpcar => tmpcar.CarLicenseNumber == car.CarLicenseNumber); if (currentCar != null) { currentCar.carmodelID = car.carmodelID; currentCar.CarLicenseNumber = car.CarLicenseNumber; currentCar.currentKm = car.currentKm; currentCar.Image = car.Image; currentCar.isDamaged = car.isDamaged; currentCar.isRentable = car.isRentable; db.SaveChanges(); return(Request.CreateResponse <CarsTable>(HttpStatusCode.OK, currentCar)); } else { return(Request.CreateResponse <string>(HttpStatusCode.BadRequest, "Count find that current car")); } } }