public ProjectProfile() { CreateMap <Project, ProjectDto>() .ForMember(dest => dest.ProjectPictureDtos, o => o.Ignore()).AfterMap((source, dest) => { if (source.ProjectPictures != null) { var projectPictureDtos = dest.ProjectPictureDtos.ToList(); foreach (var projectPicture in source.ProjectPictures.ToArray()) { ProjectPictureDto projectPictureDto = new ProjectPictureDto() { Id = projectPicture.Id, ProjectId = projectPicture.ProjectId, DisplayOrder = projectPicture.DisplayOrder, FileName = projectPicture.FileName, Title = projectPicture.Title, Caption = projectPicture.Caption, Alt = projectPicture.Alt, Binary = projectPicture.Binary, IsVisible = projectPicture.IsVisible }; projectPictureDtos.Add(projectPictureDto);; } dest.ProjectPictureDtos = projectPictureDtos; } }); CreateMap <ProjectDto, Project>(); }
public JsonResult ProjectPicture_Delete(int id) { ProjectPictureDto projectPicture = _projectPictureService.Get(id); if (projectPicture == null) { return(Json(new { success = false, error = $"Failure: Invalid project picture id: {id}" })); } _projectPictureService.Delete(projectPicture); return(Json(new { success = true })); }
public async Task <JsonResult> FineUploader_Upload(IFormFile imageFile, int projectId) { if (projectId == default(int)) { return(Json(new { success = false, error = $"Failure: Invalid project id: {projectId}" })); } if (imageFile == null || imageFile.Length == 0) { return(Json(new { success = false, error = $"Given file is invalid." })); } var validFormats = new [] { "image/jpeg", "image/png", "image/bmp", "image/gif" }; if (validFormats.All(validFormat => validFormat != imageFile.ContentType)) { return(Json(new { success = false, error = $"Invalid file format." })); } var project = _projectService.Get(projectId); if (project == null) { return(Json(new { success = false, error = $"No content found for given id: {projectId}." })); } string fileName = $"{Guid.NewGuid().ToString()}{Path.GetExtension(imageFile.FileName)}"; string pyhsicalDir = Path.Combine(_webHostEnvironment.WebRootPath, "images", "projects"); string fileFullPath = Path.Combine(pyhsicalDir, fileName); string fileRelativePath = $"/images/projects/{fileName}"; if (!Directory.Exists(pyhsicalDir)) { Directory.CreateDirectory(pyhsicalDir); } try { ProjectPictureDto projectPictureDto = new ProjectPictureDto { ProjectId = projectId, FileName = fileRelativePath, IsVisible = true, Alt = project.Title, Title = project.Title }; using (var memoryStream = new MemoryStream()) { await imageFile.CopyToAsync(memoryStream); projectPictureDto.Binary = memoryStream.ToArray(); await System.IO.File.WriteAllBytesAsync(fileFullPath, projectPictureDto.Binary); } _projectPictureService.Add(projectPictureDto); if (projectPictureDto.Id != 0) { // OK return(Json(new { success = true, pictureId = projectPictureDto.Id })); } else { // FAILURE return(Json(new { success = false, error = $"Internal server error." })); } } catch (Exception) { if (System.IO.File.Exists(fileFullPath)) { System.IO.File.Delete(fileFullPath); } else if (_projectPictureService.GetProjectPictures(projectId).Any(t => t.Title == fileRelativePath)) { var picture = _projectPictureService.GetProjectPictures(projectId).First(t => t.Title == fileRelativePath); _projectPictureService.Delete(picture); } return(Json(new { success = false, error = $"Internal server error." })); } }
public JsonResult ProjectPicturesGrid_Update([DataSourceRequest] DataSourceRequest request, ProjectPictureDto projectPictureDto) { if (projectPictureDto != null && ModelState.IsValid) { _projectPictureService.Update(projectPictureDto); } return(Json(new[] { projectPictureDto }.ToDataSourceResult(request, ModelState))); }