public ActionResult Create(DriverOffence driveroffence)
 {
     if (ModelState.IsValid)
     {
         // db.DriverOffences.Add(driveroffence);
         // db.SaveChanges();
         // return RedirectToAction("Index");
         DriverOffenceDAL dal = new DriverOffenceDAL();
         dal.CreateDriverOffence(driveroffence);
         return RedirectToAction("Index");
     }
     return View(driveroffence);
 }
        /// <method>
        /// Insert offenceUI object into database
        /// </method>
        public void CreateOffence(DriverOffenceUI driverOffenceUI)
        {
            // declare a new driver offence object.
            // if driver does not exist declare a new driver.
            // if vehicle does not exists declare a new vehicle.
            // if driver exists, use existing driver object
            // if vehicle exists, use existing vehicle object
            // retrieve existing offence object from list of offences
            // update driver offence object
            // insert the new driver offence object into the database.

            DriverOffence _driverOffenceDB = new DriverOffence();

            //assign driver offence information to new driver offence
            _driverOffenceDB.doStatus = driverOffenceUI.status.ToString();
            _driverOffenceDB.doLocation = driverOffenceUI.location.ToString();
            _driverOffenceDB.doGardaId= driverOffenceUI.gardaId.ToString();
            _driverOffenceDB.doOffenceDate = driverOffenceUI.offenceDate ;

            //Check to see if driver exists in database
            DriverDetailDAL driverDAL = new DriverDetailDAL();
            List<DriverDetail> driversDB = driverDAL.ListOfDriverDetails();

            int driverExists = 0;
            foreach (DriverDetail driver in driversDB)
            {
                if (driver.DdLicenceNo == driverOffenceUI.LicenceNo)
                {
                    driverExists = driver.Id;
                    break;
                }
            }

            //If driver does not exist declare a new driver object
            if (driverExists == 0)
            {
                _driverOffenceDB.DriverDetail = new DriverDetail();
                //update driver attributes
                _driverOffenceDB.DriverDetail.DdLicenceNo = driverOffenceUI.LicenceNo.ToString();
                _driverOffenceDB.DriverDetail.DdFName = driverOffenceUI.FName.ToString();
                _driverOffenceDB.DriverDetail.DdSName = driverOffenceUI.SName.ToString();
                _driverOffenceDB.DriverDetail.DdAddress1 = driverOffenceUI.Address1.ToString();
                _driverOffenceDB.DriverDetail.DdAddress2 = driverOffenceUI.Address2.ToString();
                _driverOffenceDB.DriverDetail.DdAddress3 = driverOffenceUI.Address3.ToString();
                _driverOffenceDB.DriverDetail.DdLicenceStatus = driverOffenceUI.LicenceStatus.ToString();
                _driverOffenceDB.DriverDetail = driverDAL.CreateDriverDetail(_driverOffenceDB.DriverDetail);
            }

            //if driver existed assign driver details to the new driver offence object;
            if (driverExists > 0)
            {
                _driverOffenceDB.DriverDetail = driverDAL.DriverDetailFindById(driverExists);
                //update driver attributes
                _driverOffenceDB.DriverDetail.DdLicenceNo = driverOffenceUI.LicenceNo.ToString();
                _driverOffenceDB.DriverDetail.DdFName = driverOffenceUI.FName.ToString();
                _driverOffenceDB.DriverDetail.DdSName = driverOffenceUI.SName.ToString();
                _driverOffenceDB.DriverDetail.DdAddress1 = driverOffenceUI.Address1.ToString();
                _driverOffenceDB.DriverDetail.DdAddress2 = driverOffenceUI.Address2.ToString();
                _driverOffenceDB.DriverDetail.DdAddress3 = driverOffenceUI.Address3.ToString();
                _driverOffenceDB.DriverDetail.DdLicenceStatus = driverOffenceUI.LicenceStatus.ToString();
                //_driverOffenceDB.DriverDetail = driverDAL.DriverDetailFindById(driverDAL.EditDriverDetail(_driverOffenceDB.DriverDetail));
                driverDAL.EditDriverDetail(_driverOffenceDB.DriverDetail);
            }

            //Check to see if vehicle exists in database
            VehicleDetailDAL vehicleDAL = new VehicleDetailDAL();
            List<VehicleDetail> vehiclesDB = vehicleDAL.ListOfVehicleDetails();

            int vehicleExists = 0;
            foreach (VehicleDetail vehicle in vehiclesDB)
            {
                if (vehicle.VdRegistration == driverOffenceUI.Registration)
                {
                    vehicleExists = vehicle.Id;
                    break;
                }
            }

            //If vehicle does not exist declare a new vehicle object
            if (vehicleExists == 0)
            {
                _driverOffenceDB.VehicleDetails = new VehicleDetail();
                //update vechile attributes
                _driverOffenceDB.VehicleDetails.VdRegistration = driverOffenceUI.Registration.ToString();
                _driverOffenceDB.VehicleDetails.VdMake = driverOffenceUI.Make.ToString();
                _driverOffenceDB.VehicleDetails.VdType = driverOffenceUI.Type.ToString();
                _driverOffenceDB.VehicleDetails.VdCubicCapacity = driverOffenceUI.Capacity;
                _driverOffenceDB.VehicleDetails = vehicleDAL.CreateVehicleDetail(_driverOffenceDB.VehicleDetails);
            }

            //if vehicle existed assign vehicle details to the vehicle offence object;
            if (vehicleExists > 0)
            {
                _driverOffenceDB.VehicleDetails = vehicleDAL.VehicleDetailFindById(vehicleExists);
                //update vechile attributes
                _driverOffenceDB.VehicleDetails.VdRegistration = driverOffenceUI.Registration.ToString();
                _driverOffenceDB.VehicleDetails.VdMake = driverOffenceUI.Make.ToString();
                _driverOffenceDB.VehicleDetails.VdType = driverOffenceUI.Type.ToString();
                _driverOffenceDB.VehicleDetails.VdCubicCapacity = driverOffenceUI.Capacity;
                //_driverOffenceDB.VehicleDetails = vehicleDAL.VehicleDetailFindById( vehicleDAL.EditVehicleDetail(_driverOffenceDB.VehicleDetails));
                vehicleDAL.EditVehicleDetail(_driverOffenceDB.VehicleDetails);
            }

            //assign offence details to the new driver offence object
            ListedOffenceDAL offenceDAL = new ListedOffenceDAL();
            List<ListedOffence> offencesDB = offenceDAL.ListOfListedOffences();

            foreach (ListedOffence offence in offencesDB)
            {
                if ((offence.LoDesc == driverOffenceUI.description) && (offence.LoStatus))
                {
                    _driverOffenceDB.ListedOffence = offenceDAL.ListedOffenceFindById(offence.Id);
                    break;
                }

            }

            //Create a new driver offence
            int driverOffenceID = _DAL.CreateDriverOffence(_driverOffenceDB);
            //_driverOffenceDB = _DAL.DriverOffenceFindById(driverOffenceID);
            //Update driver offence object in database
            //_DAL.EditDriverOffence(_driverOffenceDB);
        }
        public ActionResult Edit(DriverOffence driveroffence)
        {
            if (ModelState.IsValid)
            {
                db.Entry(driveroffence).State = EntityState.Modified;
                //db.SaveChanges();
                //return RedirectToAction("Index");

                DriverOffenceDAL dal = new DriverOffenceDAL();
                dal.EditDriverOffence(driveroffence);
                return RedirectToAction("Index");
            }
            return View(driveroffence);
        }