public void Delete(ModelImageViewModel viewModel) { ModelImage toRemove = EntityStore.ModelImages.Find(viewModel.Id); EntityStore.ModelImages.Remove(toRemove); EntityStore.SaveChanges(); }
public static ModelDetailViewModel FromModel(Model model) { return(new ModelDetailViewModel { Id = model.Id, Name = model.Name, Year = model.Year, Description = model.Description, BasePrice = model.BasePrice, EngineType = model.EngineType, BreakHorsepower = model.BreakHorsepower, ZeroToSixty = model.ZeroToSixty, TopSpeed = model.TopSpeed, MakeId = model.MakeId, MakeName = model.Make.Name, MakeImageUrl = model.Make.ImageUrl, Images = model.Images.Select(x => ModelImageViewModel.FromModel(x)).ToList() }); }
public async Task <IActionResult> Create(ModelImageViewModel viewModel) { Logger.LogInformation($"Request: {Request.GetDisplayUrl()}"); Logger.LogInformation($"Post Product Create page"); if (ModelState.IsValid) { try { var model = new EntityImageViewModel { Name = viewModel.Name }; using (var stream = new MemoryStream()) { await viewModel.Image.CopyToAsync(stream); model.Image = stream.ToArray(); } model = await Client.SaveEntity(Options.Products, model); Logger.LogTrace("Saved model: {SavedModel}", model); return(RedirectToAction(nameof(Index))); } catch (ClientException e) { Logger.LogWarning("{ClientException}", e.Message); //set the error in the view return(View()); } catch (Exception e) { Logger.LogCritical("Critical error: {CriticalErrorStackTrace}", e.StackTrace); return(RedirectToAction("Error", "Error")); } } else { return(View(viewModel)); } }
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); } } }
public IEnumerable <ModelImageViewModel> Get() { return(EntityStore.ModelImages.AsEnumerable().Select(x => ModelImageViewModel.FromModel(x))); }
public IEnumerable <ModelImageViewModel> Get(int id) { return(EntityStore.ModelImages.Where(x => x.ModelId == id).AsEnumerable().Select(x => ModelImageViewModel.FromModel(x))); }