Exemple #1
0
        public async Task <ActionResult> Edit(MilageViewModel model)
        {
            using (TransactionScope scope = new TransactionScope(TransactionScopeAsyncFlowOption.Enabled))
            {
                try
                {
                    Milage milage = MilageFactory.MilageViewmodelToEntity(model);

                    if (milage == null)
                    {
                        Danger("Milage cannot be found", true);
                        return(Json(new { success = true }));
                    }
                    _context.Entry(milage).State = EntityState.Modified;
                    await _context.SaveChangesAsync();

                    // get the vehicle movement through their id
                    var getVehicleMovement =
                        _context.VehicleMovements.SingleOrDefault(x => x.Id.Equals(model.VehicleMovementId));

                    var driver = _context.Drivers.SingleOrDefault(x => x.Id.Equals(getVehicleMovement.DriverId));

                    if (driver == null)
                    {
                        Danger("Driver cannot be found", true);
                        return(Json(new { success = true }));
                    }

                    driver.IsAvailable = true;
                    await _context.SaveChangesAsync();

                    var getMottor = _context.Mottors.SingleOrDefault(x => x.Id.Equals(getVehicleMovement.MotoId));
                    if (getMottor == null)
                    {
                        Danger("Mottor cannot be found", true);
                        return(Json(new { success = true }));
                    }

                    getMottor.IsAvailable = true;
                    await _context.SaveChangesAsync();

                    scope.Complete();
                    scope.Dispose();
                    Success("Updated Successfully", true);
                    return(Json(new { success = true }));
                }
                catch (Exception e)
                {
                    Danger(e.Message, true);
                    return(Json(new { success = true }));
                }
            }
        }
        public ActionResult Milage(int id)
        {
            var milage = _dbcontext.Milages.SingleOrDefault(x => x.VehicleMovementId.Equals(id));

            if (milage == null)
            {
                Danger("Vehicle Milage Cannot be found", true);
                return(RedirectToAction("Index"));
            }

            var model = MilageFactory.EntityToViewModel(milage);

            return(View(model));
        }
Exemple #3
0
        public async Task <ActionResult> Edit(int?id)
        {
            if (id == null)
            {
                return(new HttpStatusCodeResult(HttpStatusCode.BadRequest));
            }

            Milage mottor = await _context.Milages.FindAsync(id);

            if (mottor == null)
            {
                Danger("Milage cnnot be found", true);
                return(Json(new { success = true }));
            }
            var model = MilageFactory.EntityToMilageViewModel(mottor);

            return(PartialView("_Edit", model));
        }
        public async Task <ActionResult> Create(VehicleMovementViewModel model)
        {
            // define our transaction scope


            using (TransactionScope scope = new TransactionScope(TransactionScopeAsyncFlowOption.Enabled))
            {
                try
                {
                    var mov    = VehicleMovementFactory.ViewModelToEntity(model);
                    var result = _dbcontext.VehicleMovements.Add(mov);
                    var i      = await _dbcontext.SaveChangesAsync();

                    if (i.Equals(1))
                    {
                        var motto = _dbcontext.Mottors.SingleOrDefault(x => x.Id.Equals(mov.MotoId));
                        if (motto == null)
                        {
                            Danger("Something is wrong Internally, visit the admin");
                            return(View("Create"));
                        }
                        motto.IsAvailable = false;
                        await _dbcontext.SaveChangesAsync();

                        //var savedMoveent = _dbcontext.VehicleMovements.Select().LastOrDefault();

                        var milage = MilageFactory.ViewmodelToEntity(result);
                        _dbcontext.Milages.Add(milage);
                        await _dbcontext.SaveChangesAsync();

                        var driver = _dbcontext.Drivers.SingleOrDefault(x => x.Id == mov.DriverId);
                        if (driver == null)
                        {
                            Danger("Something is wrong Internally, visit the admin");
                            return(View("Create"));
                        }

                        driver.IsAvailable = false;
                        await _dbcontext.SaveChangesAsync();

                        scope.Complete();
                        scope.Dispose();
                    }
                    //Desig
                }
                catch (DbEntityValidationException e)
                {
                    var outputLines = new List <string>();
                    foreach (var eve in e.EntityValidationErrors)
                    {
                        outputLines.Add(string.Format("{0}: Entity of type \"{1}\" in state \"{2}\" has the following validation errors: ", DateTime.Now, eve.Entry.Entity.GetType().Name, eve.Entry.State));
                        foreach (var ve in eve.ValidationErrors)
                        {
                            outputLines.Add(string.Format(" Property: \"{0}\", Error: \"{1}\"", ve.PropertyName, ve.ErrorMessage));
                        }
                    }



                    Danger(outputLines.ToString(), false);
                    System.IO.File.AppendAllLines(@"C:\errors.txt", outputLines);
                    //throw;
                }
            }


            return(RedirectToAction("Index"));
        }