Пример #1
0
 public void Post(ModelDetailsViewModel model)
 {
     if (model.Id == 0)
     {
         AddNewModel(model);
     }
     else
     {
         UpdateExistingModel(model);
     }
     try {
         EntityStore.SaveChanges();
     }
     catch (DbEntityValidationException ex) {
         StringBuilder sb = new StringBuilder();
         foreach (DbEntityValidationResult validationErrors in ex.EntityValidationErrors)
         {
             foreach (DbValidationError validationError in validationErrors.ValidationErrors)
             {
                 sb.Append(string.Format("{0} - {1}", validationError.PropertyName, validationError.ErrorMessage));
             }
         }
         throw new Exception(sb.ToString());
     }
 }
Пример #2
0
        public ModelDetailsViewModel Get(int id, int?makeId)
        {
            ModelDetailsViewModel viewModel = ModelDetailsViewModel.FromModel(EntityStore.Models.Include("Make").Include("Images").FirstOrDefault(x => x.Id == id));

            viewModel.Makes = EntityStore.Makes.Select(x => new LookupItemViewModel {
                Id = x.Id, Text = x.Name
            }).OrderBy(x => x.Text).ToList();
            if (id == 0)
            {
                viewModel.MakeId = !makeId.HasValue ? null : (makeId.Value == 0?(int?)null: makeId.Value);
            }
            return(viewModel);
        }
Пример #3
0
        private void UpdateExistingModel(ModelDetailsViewModel model)
        {
            Model m = EntityStore.Models.Include("Images").First(x => x.Id == model.Id);

            m.Name        = model.Name;
            m.TopSpeed    = model.TopSpeed;
            m.Year        = model.Year;
            m.ZeroToSixty = model.ZeroToSixty;
            m.BasePrice   = model.BasePrice;
            m.Description = model.Description;
            m.EngineType  = model.EngineType;
            m.Description = model.Description;
            HandleImages(model, m);
        }
Пример #4
0
        private void HandleImages(ModelDetailsViewModel model, Model m)
        {
            IList <ModelImage> toAdd = new List <ModelImage>();

            foreach (ModelImageViewModel image in model.Images)
            {
                if (image.Id != 0)
                {
                    ModelImage o = m.Images.Find(x => x.Id == image.Id);
                    o.Order             = image.Order;
                    o.HighResolutionUrl = image.HighResolutionUrl;
                    o.LowResolutionUrl  = image.LowResolutionUrl;
                    o.ShortDescription  = image.ShortDescription;
                    o.LongDescription   = image.ShortDescription;
                    o.ModelId           = image.ModelId;
                }
                else
                {
                    image.ModelId = model.Id;
                    toAdd.Add(ModelImageViewModel.ToModel(image));
                }
            }
            if (toAdd.Count != 0)
            {
                foreach (ModelImage mi in toAdd)
                {
                    EntityStore.ModelImages.Add(mi);
                }
            }
            IList <ModelImage> toDelete = (from modelImage in m.Images let mivw = model.Images.FirstOrDefault(x => x.Id == modelImage.Id) where mivw == null select modelImage).ToList();

            if (toDelete.Count != 0)
            {
                foreach (ModelImage mi in toDelete)
                {
                    EntityStore.ModelImages.Remove(mi);
                }
            }
        }
Пример #5
0
        private void AddNewModel(ModelDetailsViewModel model)
        {
            Model m = ModelDetailsViewModel.ToModel(model);

            EntityStore.Models.Add(m);
        }
Пример #6
0
 // GET api/<controller>/5
 public ModelDetailsViewModel Get(int id)
 {
     return(ModelDetailsViewModel.FromModel(EntityStore.Models.Include("Images").Include("Make")
                                            .FirstOrDefault(x => x.Id == id)));
 }