public ActionResult Edit(int?id) { if (id != null) { ApplicationDbContext context = new ApplicationDbContext(); var user = context.Users.Find(User.Identity.GetUserId()); if (user != null && context.BlogDatas.Any(blog => blog.ApplicationUserId == user.Id && blog.BlogDataId == id)) { var blogdata = context.BlogDatas.Where(blog => blog.ApplicationUserId == user.Id && blog.BlogDataId == id).ToList().Single(); List <Element> elements = GenerateElements(blogdata.GalleryModels, blogdata.ImageModels, blogdata.TextModels, blogdata.VideoModels); if (deletetempService.GetCurrentBlogId() == id && deletetempService.Retrieve() != null && deletetempService.Retrieve().Count >= 0) { elements = deletetempService.Retrieve(); } else { elements = elements.OrderBy(x => x.position).ToList(); deletetempService.Clear(); deletetempService.Initialize(elements); deletetempService.SetCurrentBlogId(id.GetValueOrDefault()); } var viewmodel = new ExternBlogViewModel { Title = blogdata.Title, Subtitle = blogdata.Subtitle, Elements = elements }; return(View(viewmodel)); } } return(RedirectToAction("Overview", "Blog")); }
public HttpResponseMessage UpdateBlog(HttpRequestMessage request) { var stringdata = request.Content.ReadAsStringAsync().Result; if (stringdata == null) { return(new HttpResponseMessage(HttpStatusCode.BadRequest) { Content = new StringContent("Post data is empty") }); } else { string externaluserid = ""; Guid externalid = new Guid(); var blogdata = JsonConvert.DeserializeObject <BlogWrapper>(stringdata); if (editService.GetGMapsMarker().Count >= 0) { blogdata.blog.gmapsMarker = editService.GetGMapsMarker(); } var updateBlog = blogdata.blog; ApplicationDbContext context = new ApplicationDbContext(); var user = context.Users.Find(User.Identity.GetUserId()); int id = editService.GetCurrentBlogId(); //delete old blog if (context.BlogDatas.Any(b => b.ApplicationUserId == user.Id && b.BlogDataId == id)) { var oldblog = context.BlogDatas.Where(b => b.ApplicationUserId == user.Id && b.BlogDataId == id).ToList().Single(); if (oldblog != null) { //keep external links externaluserid = oldblog.ExternalUser; externalid = oldblog.ExternalId; //delete old blog context.BlogDatas.Remove(oldblog); context.SaveChanges(); } } List <TextModel> texts = new List <TextModel>(); List <ImageModel> images = new List <ImageModel>(); List <VideoModel> videos = new List <VideoModel>(); List <GalleryModel> gallerys = new List <GalleryModel>(); List <GMapsMarkerModel> gmapsmarker = new List <GMapsMarkerModel>(); int textExisting = 0; var stillexistingelements = editService.Retrieve(); //old data, that wasnt deleted, add first foreach (var img in stillexistingelements) { if (img is TextElement) { var text = img as TextElement; texts.Add(new TextModel { Text = text.value, Position = text.position }); textExisting++; } else if (img is ImageElement) { var image = img as ImageElement; images.Add(new ImageModel { Base64 = image.base64, Position = image.position }); } else if (img is GalleryElement) { var gallery = img as GalleryElement; var galleryImage = new List <GalleryImageModel>(); gallery.Images.ForEach(image => { galleryImage.Add(new GalleryImageModel { Base64 = image.base64 }); }); gallerys.Add(new GalleryModel { Position = gallery.position, ClassName = gallery.ClassName, GalleryImageModels = galleryImage }); } else if (img is VideoElement) { var video = img as VideoElement; videos.Add(new VideoModel { Source = video.Src, Position = video.position }); } } //to match the array indexing start at 0 textExisting--; while (textExisting >= 0) { updateBlog.text.RemoveAt(textExisting); textExisting--; } //new data added trough drag n drop int alreadyfiledPositions = stillexistingelements.Count; foreach (var text in updateBlog.text) { texts.Add(new TextModel { Text = text.value, Position = text.position + alreadyfiledPositions }); } //add image and slideshow data into elements List <Element> imageElements = mapImages(editService.GetImages(), updateBlog.images.ToList(), editService.GetGallery()); foreach (var img in imageElements) { if (img is ImageElement) { var image = img as ImageElement; if (image.base64 != null && image.position != 0) { images.Add(new ImageModel { Base64 = image.base64, Position = image.position + alreadyfiledPositions }); } } else if (img is GalleryElement) { var gallery = img as GalleryElement; var galleryImage = new List <GalleryImageModel>(); gallery.Images.ForEach(image => { galleryImage.Add(new GalleryImageModel { Base64 = image.base64 }); }); gallerys.Add(new GalleryModel { Position = gallery.position + alreadyfiledPositions, ClassName = gallery.ClassName, GalleryImageModels = galleryImage }); } } //add video into elements foreach (var video in updateBlog.videos) { videos.Add(new VideoModel { Source = video.src, Position = video.position + alreadyfiledPositions }); } foreach (var marker in updateBlog.gmapsMarker) { gmapsmarker.Add(new GMapsMarkerModel { Latitude = marker.Latitude, Longitude = marker.Longitude }); } //add everything to db BlogData blog = new BlogData { ApplicationUser = user, Title = blogdata.blog.titel, Subtitle = blogdata.blog.subtitel, GalleryModels = gallerys, ImageModels = images, TextModels = texts, GMapsMarkerModels = gmapsmarker, ExternalId = externalid, ExternalUser = externaluserid, IsVisibleFromOutside = false, VideoModels = videos }; context.BlogDatas.Add(blog); context.SaveChanges(); context.Dispose(); editService.Clear(); return(new HttpResponseMessage(HttpStatusCode.OK)); } }