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())); }