private static void ProcessMessage(PhonebookEntities context, JsonMessage messege) { if (messege.Content == null) { throw new ArgumentException("Content is required!"); } if (messege.DateTime == null) { throw new ArgumentException("DateTime is required!"); } if (messege.Recipient == null) { throw new ArgumentException("Recipient is required!"); } if (messege.Sender == null) { throw new ArgumentException("Sender is required!"); } var userMessage = new UserMessage() { Content = messege.Content, DateTime = (DateTime)messege.DateTime, Sender = context.Users.FirstOrDefault(u => u.Username == messege.Sender), Recipiant = context.Users.FirstOrDefault(u => u.Username == messege.Recipient), }; context.UserMessages.AddOrUpdate(userMessage); context.SaveChanges(); Console.WriteLine("Message \"{0}\" Imported", userMessage.Content); }
private static void AddContact(JToken contact, PhonebookEntities db) { if (contact["name"] == null) { throw new ArgumentException("Name is required"); } var newContact = new Contact { Name = contact["name"].ToString() }; if (contact["company"] != null) { newContact.Company = contact["company"].ToString(); } if (contact["position"] != null) { newContact.Position = contact["position"].ToString(); } if (contact["site"] != null) { newContact.Site = contact["site"].ToString(); } if (contact["notes"] != null) { newContact.Notes = contact["notes"].ToString(); } if (contact["phones"] != null) { foreach (var phone in contact["phones"]) { newContact.Phones.Add(new Phone { PhoneNumber = phone.ToString() }); } } if (contact["emails"] != null) { foreach (var email in contact["emails"]) { newContact.Emails.Add(new Email { EmailAddress = email.ToString() }); } } db.Contacts.Add(newContact); db.SaveChanges(); }
private ImportRecordResponse ImportContact(PhonebookRecord record, bool batchMode) { if (!batchMode) { numbers = GetContactsPhoneNumbers(); } ImportRecordResponse response = new ImportRecordResponse(); ImportRecordResponseError errorResponse = new ImportRecordResponseError(); #region Validate if (String.IsNullOrEmpty(record.Company)) { errorResponse.Description = "Jméno musí být uvedeno"; errorResponse.RecordId = record.RecordId; errorResponse.ErrorType = ImportErrorType.Required; errorResponse.ColumnId = 0; hostLogger?.Error("Error: 'Name' required [recordId:" + record.RecordId + "]"); throw new FaultException <ImportRecordResponseError>(errorResponse, commonErrorMessage); } if (String.IsNullOrEmpty(record.Number)) { errorResponse.Description = "Tel.číslo musí být uvedeno"; errorResponse.RecordId = record.RecordId; errorResponse.ErrorType = ImportErrorType.Required; errorResponse.ColumnId = 1; hostLogger?.Error("Error: 'Number' required [recordId:" + record.RecordId + "]"); throw new FaultException <ImportRecordResponseError>(errorResponse, commonErrorMessage); } if (numbers != null && numbers.Count > 0) { // TODO iteligentnejsi kontrola, napr. i varianty zacinajici na '+narodnikod' atd // Dalsi moznost je nastaveni indexu bez duplicit na sloupci number v phonebook // a odchytit exception pri db.Save. Toto by bylo rychlejsi a jednodussi, ale nebylo // by mozne resit duplicity importu zapisu cisel v ruznem formatu... // Ale je mozne, ze toto je uz nejak reseno na urovni pripravy importniho CSV // protoze v ukazkach byla jen cisla bez mezinarodniho predcisli. Pak by bylo // lepsi zvolit variantu hlidani duplicit na urovni SQL if (numbers.Contains(record.Number.Trim())) { errorResponse.Description = "Tel.číslo již v seznamu existuje"; errorResponse.RecordId = record.RecordId; errorResponse.ErrorType = ImportErrorType.DuplicateNumber; errorResponse.ColumnId = 1; // TODO na string jmeno hostLogger?.Error("Error: 'Number' duplicated [recordId:" + record.RecordId + "]"); throw new FaultException <ImportRecordResponseError>(errorResponse, commonErrorMessage); } } #endregion phonebook phonebookEntity = PhonebookRecordToEntity(record); try { db.phonebook.Add(phonebookEntity); if (!batchMode) { db.SaveChanges(); } response.Success = true; } catch (Exception e) { errorResponse.RecordId = record.RecordId; errorResponse.ErrorType = ImportErrorType.Unknown; errorResponse.Description = e.Message; throw new FaultException <ImportRecordResponseError>(errorResponse, commonErrorMessage); } return(response); }