public Message SaveMessage(Message message) { using (var repositoriesContainer = new LmPlatformRepositoriesContainer()) { repositoriesContainer.MessageRepository.Save(message); if (message.Attachments != null) { FilesManagementService.SaveFiles(message.Attachments, message.AttachmentsPath.ToString()); foreach (var attach in message.Attachments) { repositoriesContainer.AttachmentRepository.Save(attach); } } repositoriesContainer.ApplyChanges(); } return message; }
public ResultViewData Save(string subject, string body, string recipients, string attachments) { try { var attachmentsList = JsonConvert.DeserializeObject<List<Attachment>>(attachments).ToList(); var recipientsList = JsonConvert.DeserializeObject<List<int>>(recipients).ToList(); var fromId = WebSecurity.CurrentUserId; if (!string.IsNullOrEmpty(subject) && subject.Length > 50) { subject = subject.Substring(0, 50); } var msg = new Message(body, subject); if (attachmentsList.Any()) { msg.AttachmentsPath = Guid.NewGuid(); attachmentsList.ForEach(a => a.PathName = msg.AttachmentsPath.ToString()); msg.Attachments = attachmentsList; } MessageManagementService.SaveMessage(msg); ////if (ToAdmin) ////{ //// var admin = UserManagementService.GetAdmin(); //// var userMsg = new UserMessages(admin.Id, FromId, msg.Id); //// MessageManagementService.SaveUserMessages(userMsg); ////} ////else ////{ foreach (var recipientId in recipientsList) { var userMsg = new UserMessages(recipientId, fromId, msg.Id); MessageManagementService.SaveUserMessages(userMsg); } ////} return new ResultViewData { Message = "Сообщение отправлено", Code = "200" }; } catch (Exception e) { return new ResultViewData { Message = "Произошла ошибка при сохранение сообщения", Code = "500" }; } }