public void EditLeasedVehicles(LeasedVehicle order)
        {
            using (ModelContext context = new ModelContext())
            {
                context.LeasedVehicles.Add(order);
                context.Entry(order).State = EntityState.Modified;
                context.SaveChanges();
                //bool saveFailed;
                //do
                //{
                //    saveFailed = false;

                //    try
                //    {
                //        context.SaveChanges();
                //    }
                //    catch (DbUpdateConcurrencyException ex)
                //    {
                //        saveFailed = true;

                //        // Update the values of the entity that failed to save from the store
                //        //ex.Entries.Single().Reload();
                //        var emp = ex.Entries.Single();
                //        emp.OriginalValues.SetValues(emp.GetDatabaseValues());
                //        DB.SaveChanges();

                //    }

                //} while (saveFailed);
            }
        }
Example #2
0
        /// <summary>
        /// After the user confirms is order this enter it into the DB.
        /// </summary>
        /// <param name="carNum">The car number</param>
        /// <param name="userID">the user ID who create this order</param>
        /// <param name="startDate">The start date</param>
        /// <param name="endDate">The end date</param>
        public void OrderingVehicle(string carNum, int userID, DateTime startDate, DateTime endDate)
        {
            var vehichleID = DB.VehiclesFleets.Where(v => v.CarNumber == carNum).FirstOrDefault();

            var leasedVehicleToLeaseAfter    = DB.LeasedVehicles.Where(l => l.CarNumber == carNum && (l.SoughtReturnDate >= startDate && l.RentalStartDate <= endDate)).FirstOrDefault();
            var leasedVehicleToLeaseBefore   = DB.LeasedVehicles.Where(l => l.CarNumber == carNum && (l.RentalStartDate <= startDate && l.SoughtReturnDate >= endDate)).FirstOrDefault();
            var leasedVehicleToLeaseExternal = DB.LeasedVehicles.Where(l => l.CarNumber == carNum && (l.RentalStartDate >= startDate && l.SoughtReturnDate <= endDate)).FirstOrDefault();

            if (leasedVehicleToLeaseAfter != null || leasedVehicleToLeaseBefore != null || leasedVehicleToLeaseExternal != null)
            {
                throw new Exception();
            }
            else
            {
                LeasedVehicle leasedVehicleToAdd = new LeasedVehicle
                {
                    CarNumber        = carNum,
                    UserID           = userID,
                    RentalStartDate  = startDate,
                    SoughtReturnDate = endDate,
                    VehiclesFleet    = vehichleID,
                };

                DB.LeasedVehicles.Attach(leasedVehicleToAdd);
                DB.Entry(leasedVehicleToAdd).State = EntityState.Added;
                DB.SaveChanges();
            }
        }
        public GetUserOrders_Result AddToUsersOrder(LeasedVehicle leasedVehicle)
        {
            GetUserOrders_Result ordersForUser = new GetUserOrders_Result();

            ordersForUser.CarNamber        = leasedVehicle.CarNumber;
            ordersForUser.RentalStartDate  = leasedVehicle.RentalStartDate.ToShortDateString();
            ordersForUser.SoughtReturnDate = leasedVehicle.SoughtReturnDate.ToShortDateString();
            ordersForUser.ActualReturnDate = leasedVehicle.ActualReturnDate.ToString();
            return(ordersForUser);
        }
        public ActionResult Edit(LeasedVehicle editDate)
        {
            try
            {
                if (!ModelState.IsValid)
                {
                    return(View());
                }

                LeasedVehicleLogic logic = new LeasedVehicleLogic();
                logic.EditLeasedVehicles(editDate);

                return(RedirectToAction("Index", "ReturnVehicle"));
            }
            catch (Exception)
            {
                ViewBag.Error = "We have problem on the server, Please try again later!";
                return(View(new LeasedVehicle()));
            }
        }
        public ActionResult Delete(LeasedVehicle deleteLeasedVehicle)
        {
            try
            {
                if (!ModelState.IsValid)
                {
                    return(View());
                }

                using (LeasedVehicleLogic logic = new LeasedVehicleLogic())
                {
                    logic.DeleteLeasedVehicles(deleteLeasedVehicle);
                }

                return(RedirectToAction("Index", "MangeHome"));
            }
            catch
            {
                ViewBag.Error = "We have problem on the server, Please try again later!";
                return(View(new LeasedVehicle()));
            }
        }
 /// <summary>
 /// Delete car from the orders
 /// </summary>
 /// <param name="vehicle">The details of the car to delete</param>
 public void DeleteLeasedVehicles(LeasedVehicle order)
 {
     DB.Entry(order).State = EntityState.Deleted;
     DB.SaveChanges();
 }