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!" }; }