public SavePopDashboardResponse SavePopDashboard(SavePopDashboardRequest request) { var popDashboard = request.MapTo<PopDashboard>(); if (request.Id == 0) { foreach (var attachment in request.AttachmentFiles) { popDashboard.Attachments.Add(new PopDashboardAttachment { Alias = attachment.Alias, Filename = attachment.FileName, Type = attachment.Type }); } DataContext.PopDashboards.Add(popDashboard); } else { popDashboard = DataContext.PopDashboards .Include(x => x.Attachments) .FirstOrDefault(x => x.Id == request.Id); foreach (var attachment in popDashboard.Attachments.ToList()) { if(request.AttachmentFiles.All(x => x.Id != attachment.Id)){ popDashboard.Attachments.Remove(attachment); } } foreach (var attachmentReq in request.AttachmentFiles) { var existingAttachment = popDashboard.Attachments.SingleOrDefault(c => c.Id == attachmentReq.Id); if (existingAttachment != null && existingAttachment.Id != 0) { existingAttachment.Alias = attachmentReq.Alias; existingAttachment.Filename = string.IsNullOrEmpty(attachmentReq.FileName) ? existingAttachment.Filename : attachmentReq.FileName; existingAttachment.Type = string.IsNullOrEmpty(attachmentReq.Type) ? existingAttachment.Type : attachmentReq.Type; } else { var newAttachment = new PopDashboardAttachment() { Alias = attachmentReq.Alias, Type = attachmentReq.Type, Filename = attachmentReq.FileName }; popDashboard.Attachments.Add(newAttachment); } } request.MapPropertiesToInstance<PopDashboard>(popDashboard); } DataContext.SaveChanges(); return new SavePopDashboardResponse { IsSuccess = true, Message = "Project has been saved successfully!" }; }
public SavePopDashboardResponse SavePopDashboard(SavePopDashboardRequest request) { var popDashboard = request.MapTo<PopDashboard>(); if (request.Id == 0) { DataContext.PopDashboards.Add(popDashboard); } else { popDashboard = DataContext.PopDashboards.FirstOrDefault(x => x.Id == request.Id); request.MapPropertiesToInstance<PopDashboard>(popDashboard); } DataContext.SaveChanges(); return new SavePopDashboardResponse { IsSuccess = true, Message = "Pop Dashboard has been saved successfully!" }; }
private void ProcessAttachment(SavePopDashboardViewModel viewModel, SavePopDashboardRequest request) { if (viewModel.Attachments.Count > 0) { var validImageTypes = new string[] { "image/gif", "image/jpeg", "image/pjpeg", "image/png" }; var pdfType = "application/pdf"; var excelTypes = new string[]{ "application/vnd.ms-excel", "application/msexcel", "application/x-msexcel", "application/x-ms-excel", "application/x-excel", "application/x-dos_ms_excel", "application/xls", "application/x-xls", "application/vnd.openxmlformats-officedocument.spreadsheetml.sheet" }; var docTypes = new string[]{ "application/msword", "application/msword", "application/vnd.openxmlformats-officedocument.wordprocessingml.document", "application/vnd.openxmlformats-officedocument.wordprocessingml.template", "application/vnd.ms-word.document.macroEnabled.12", "application/vnd.ms-word.template.macroEnabled.12" }; var pptTypes = new string[]{ "application/vnd.ms-powerpoint", "application/vnd.ms-powerpoint", "application/vnd.ms-powerpoint", "application/vnd.ms-powerpoint", "application/vnd.openxmlformats-officedocument.presentationml.presentation", "application/vnd.openxmlformats-officedocument.presentationml.template", "application/vnd.openxmlformats-officedocument.presentationml.slideshow", "application/vnd.ms-powerpoint.addin.macroEnabled.12", "application/vnd.ms-powerpoint.presentation.macroEnabled.12", "application/vnd.ms-powerpoint.template.macroEnabled.12", "application/vnd.ms-powerpoint.slideshow.macroEnabled.12" }; foreach (var attachment in viewModel.Attachments) { if (attachment.File != null) { //var filename = Path.GetFileName(attachment.File.FileName); string type = null; if (attachment.File.ContentType == pdfType) { type = "pdf"; } else if (validImageTypes.Contains(attachment.File.ContentType)) { type = "image"; } else if (excelTypes.Contains(attachment.File.ContentType)) { type = "excel"; } else if (docTypes.Contains(attachment.File.ContentType)) { type = "doc"; } else if (pptTypes.Contains(attachment.File.ContentType)) { type = "ppt"; } else { type = "blank"; } if (!Directory.Exists(Server.MapPath(PathConstant.PopAttachmentPath))) { Directory.CreateDirectory(Server.MapPath(PathConstant.PopAttachmentPath)); } var filename = attachment.File.FileName; var uniqueFilename = RandomString(8) + MakeValidFileName(attachment.File.FileName).Replace(" ", "_"); var filePath = Path.Combine(Server.MapPath(PathConstant.PopAttachmentPath), uniqueFilename); var url = PathConstant.PopAttachmentPath + "/" + uniqueFilename; attachment.File.SaveAs(filePath); var attachmentReq = new SavePopDashboardRequest.Attachment { Id = attachment.Id, FileName = url, Alias = string.IsNullOrEmpty(attachment.Alias) ? filename : attachment.Alias, Type = type }; request.AttachmentFiles.Add(attachmentReq); } else { if (attachment.Id != 0) { var attachmentReq = new SavePopDashboardRequest.Attachment { Id = attachment.Id, Alias = attachment.Alias, }; request.AttachmentFiles.Add(attachmentReq); } } } } }