public InfoLiecViewModel ContentModelToInfoLiecViewmodel(ContentModel c) { var info = new InfoLiecViewModel { Id = c.Id.ToString(), Context = c.Context, DarkTheme = c.GetTheme().DarkColorCode, CreationDate = c.CreationDate.ToShortDateString(), ModificationDate = c.ModificationDate.ToShortDateString(), LightTheme = c.GetTheme().LightColorCode, NormalTheme = c.GetTheme().NormalColorCode, Sources = c.Sources, Tags = c.Tags, Theme = c.Theme.ToString(), Title = c.Title, Text = c.Text, Image = c.ImagePath, TwitterUrl = String.IsNullOrEmpty(c.TwitterUrl) ? "https://twitter.com/instantencommun?lang=fr" : c.TwitterUrl, FacebookUrl = String.IsNullOrEmpty(c.FacebookUrl) ? "https://fr-fr.facebook.com/Linstantencommun/" : c.FacebookUrl, InstagramUrl = String.IsNullOrEmpty(c.InstagramUrl) ? "https://www.instagram.com/instantencommun/" : c.InstagramUrl, Creator = c.InstagramUrl }; return(info); }
public async Task <JsonResult> GetContentById([FromBody] string id) { InfoLiecViewModel info = null; try { var content = await MongoDbContext.Content_Get(new ObjectId(id)); info = ContentModelToInfoLiecViewmodel(content); } catch (Exception e) { return(Json(BadRequest(e))); } return(Json(Ok(info))); }
public async Task <JsonResult> EditContent([FromBody] InfoLiecViewModel vm) { var imagePath = Path.Combine(_hostingEnvironment.WebRootPath, "static/Images"); var content = InfoLiecViewmodelToContentModel(vm); var dir = new DirectoryInfo(imagePath); // Tags and Sources to lower for (int i = 0; i < content.Tags.Length; i++) { content.Tags[i] = content.Tags[i].ToLower(); } if (!dir.Exists) { dir.Create(); } // Copy and delete temp image file var cleanPath = content.ImagePath.Remove(0, 1); var tempFilePath = Path.Combine(_hostingEnvironment.WebRootPath, cleanPath); FileInfo imageFile = new FileInfo(tempFilePath); var newFilePath = Path.Combine(dir.FullName, imageFile.Name); imageFile.CopyTo(newFilePath); content.ImagePath = newFilePath.Remove(0, _hostingEnvironment.WebRootPath.Length); imageFile.Delete(); try { var old = await MongoDbContext.Content_Get(content.Id); var diff = await MongoDbContext.Content_Update(content); if (false) { var oldImageFile = new FileInfo(old.ImagePath); oldImageFile.Delete(); } } catch (Exception e) { HttpContext.Response.StatusCode = 400; return(Json(BadRequest(e))); } return(Json(Ok())); }
public ContentModel InfoLiecViewmodelToContentModel(InfoLiecViewModel c) { //var content = new ContentModel(); //content.Context = c.Context; //content.CreationDate = c.CreationDate == null ? DateTime.Now : DateTime.Parse(c.CreationDate); //content.ModificationDate = c.ModificationDate == null ? DateTime.Now : DateTime.Parse(c.ModificationDate); //content.Sources = c.Sources; //content.Tags = c.Tags; //content.Theme = String.IsNullOrEmpty(c.Theme) ? (Themes?)null : Enum.Parse<Themes>(c.Theme); //content.Title = c.Title; //content.Text = c.Text; //content.ImagePath = c.Image; //content.Creator = c.Creator; //content.TwitterUrl = c.TwitterUrl; //content.InstagramUrl = c.InstagramUrl; //content.FacebookUrl = c.FacebookUrl; var content = new ContentModel() { Id = new ObjectId(c.Id), Context = c.Context, CreationDate = String.IsNullOrEmpty(c.CreationDate) ? DateTime.Now : DateTime.Parse(c.CreationDate), ModificationDate = String.IsNullOrEmpty(c.ModificationDate) ? DateTime.Now : DateTime.Parse(c.ModificationDate), Sources = c.Sources, Tags = c.Tags, Theme = String.IsNullOrEmpty(c.Theme) ? (Themes?)null : Enum.Parse <Themes>(c.Theme), Title = c.Title, Text = c.Text, ImagePath = c.Image, Creator = c.Creator, TwitterUrl = c.TwitterUrl, InstagramUrl = c.InstagramUrl, FacebookUrl = c.FacebookUrl, }; return(content); }