public Vehicle Save(long quoteId)
        {
            Vehicle v = new Vehicle();
            v.Id = VehicleViewModel.Id;
            v.AnnualMiles = VehicleViewModel.AnnualMiles;
            v.AntiTheft = VehicleViewModel.HasAntiTheftSystem;
            v.CurrentValue = VehicleViewModel.CurrentValue;
            v.DaysDrivenPerWeek = VehicleViewModel.DaysDrivenPerWeek;
            v.GarageDifferent = VehicleViewModel.ParkedInSeparateGarage;
            v.HasAntilockBrakingSystem = VehicleViewModel.HasAntilockBrakingSystem;
            v.HasDaytimeRunningLights = VehicleViewModel.HasDaylightRunningLights;
            v.Make = VehicleViewModel.Make;
            v.Mileage = VehicleViewModel.Mileage;
            v.MilesDrivenToWork = VehicleViewModel.MilesDrivenToWork;
            v.CarModel = VehicleViewModel.CarModel;
            v.PassiveRestraints = VehicleViewModel.HasPassiveRestraints;
            v.ReduceUse = VehicleViewModel.ReducedUse;
            v.Vin = VehicleViewModel.Vin;
            v.Year = VehicleViewModel.Year;
            v.DriverId = VehicleViewModel.DriverId;
            v.QuoteId = quoteId;
            v.Quote = Helper.Get<Quote>(quoteId);

            return Helper.Put(v, v.Id);
        }
        public IHttpActionResult PostVehicle(Vehicle vehicle)
        {
            if (!ModelState.IsValid)
            {
                return BadRequest(ModelState);
            }

            db.Vehicles.Add(vehicle);

            try
            {
                db.SaveChanges();
            }
            catch (DbUpdateException due)
            {
                if (VehicleExists(vehicle.Id))
                {
                    return Conflict();
                }
                else
                {
                    throw due;
                }
            }
            catch (DbEntityValidationException e)
            {
                var str = String.Empty;
                foreach (var eve in e.EntityValidationErrors)
                {
                    str += String.Format("Entity of type \"{0}\" in state \"{1}\" has the following validation errors:",
                        eve.Entry.Entity.GetType().Name, eve.Entry.State);
                    foreach (var ve in eve.ValidationErrors)
                    {
                        str += String.Format("- Property: \"{0}\", Error: \"{1}\"",
                            ve.PropertyName, ve.ErrorMessage);
                    }
                }
                throw e;
            }

            return CreatedAtRoute("DefaultApi", new { id = vehicle.Id }, vehicle);
        }
        public IHttpActionResult PutVehicle(long id, Vehicle vehicle)
        {
            if (!ModelState.IsValid)
            {
                return BadRequest(ModelState);
            }

            if (id != vehicle.Id)
            {
                return BadRequest();
            }

            db.Entry(vehicle).State = EntityState.Modified;

            try
            {
                db.SaveChanges();
            }
            catch (DbUpdateConcurrencyException)
            {
                if (!VehicleExists(id))
                {
                    return NotFound();
                }
                else
                {
                    throw;
                }
            }
            catch (DbEntityValidationException e)
            {
                var str = String.Empty;
                foreach (var eve in e.EntityValidationErrors)
                {
                    str += String.Format("Entity of type \"{0}\" in state \"{1}\" has the following validation errors:",
                        eve.Entry.Entity.GetType().Name, eve.Entry.State);
                    foreach (var ve in eve.ValidationErrors)
                    {
                        str += String.Format("- Property: \"{0}\", Error: \"{1}\"",
                            ve.PropertyName, ve.ErrorMessage);
                    }
                }
                throw e;
            }

            return StatusCode(HttpStatusCode.NoContent);
        }
Exemple #4
0
        public static VehicleViewModel convertVehicletoVehicleViewModel(Vehicle v)
        {
            VehicleViewModel vvm = new VehicleViewModel();
            vvm.AnnualMiles = (int)v.AnnualMiles;
            vvm.CurrentValue = v.CurrentValue;
            vvm.DaysDrivenPerWeek = v.DaysDrivenPerWeek;
            vvm.HasAntilockBrakingSystem = v.HasAntilockBrakingSystem;
            vvm.HasDaylightRunningLights = v.HasDaytimeRunningLights;
            vvm.Id = v.Id;
            vvm.Make = v.Make;
            vvm.Mileage = (int)v.Mileage;
            vvm.MilesDrivenToWork = (int)v.MilesDrivenToWork;
            vvm.CarModel = v.CarModel;
            vvm.DriverId = v.DriverId;
            vvm.ReducedUse = v.ReduceUse;
            vvm.Vin = v.Vin;
            vvm.Year = v.Year;
            vvm.HasAntiTheftSystem = v.AntiTheft;
            vvm.HasPassiveRestraints = v.PassiveRestraints;
            vvm.ParkedInSeparateGarage = v.GarageDifferent;

            return vvm ;
        }