public void SaveUser(UserItem user) { if (user == null) { throw new ArgumentException("User can't be null"); } if (user.KeyPair == null) { throw new ArgumentException("KeyPair can't be null"); } using (QRyptoDb db = QryptoDbFactory.GetDb()) { if (db.Users.Count() == 1) { throw new Exception("User already exists"); } db.Users.InsertOnSubmit(new UserModel() { Id = user.Id, KeyPair = user.KeyPair, Name = user.Name }); db.SubmitChanges(); } }
public IEnumerable <IContactModel> GetContacts() { using (QRyptoDb db = QryptoDbFactory.GetDb()) { return(db.Contacts.ToList()); } }
public bool UserExists() { using (QRyptoDb db = QryptoDbFactory.GetDb()) { bool userExists = db.Users.Any(); return(userExists); } }
public void ClearMessages() { using (QRyptoDb db = QryptoDbFactory.GetDb()) { db.Messages.DeleteAllOnSubmit(db.Messages); db.SubmitChanges(); } }
public IEnumerable <Tuple <IContactModel, int> > GetContactsWithNewMessageCount() { using (QRyptoDb db = QryptoDbFactory.GetDb()) { return(db.Contacts.Select( c => new Tuple <IContactModel, int>(c, c.MessagesSentToUser.Count(mm => mm.IsNew) + c.MessagesSentByUser.Count(mm => mm.IsNew))) .ToList()); } }
public IUserModel GetUser() { using (QRyptoDb db = QryptoDbFactory.GetDb()) { UserModel user = db.Users.FirstOrDefault(); if (user == null) { throw new UserNotFoundException("User does not exist"); } return(user); } }
public void SetUserName(string name) { using (QRyptoDb db = QryptoDbFactory.GetDb()) { UserModel user = db.Users.FirstOrDefault(); if (user == null) { throw new UserNotFoundException("User does not exist"); } user.Name = name; db.SubmitChanges(); } }
public void MarkContactsAsNotNew(IEnumerable <int> contactsId) { if (contactsId == null) { return; } using (QRyptoDb db = QryptoDbFactory.GetDb()) { foreach (var contact in db.Contacts.Where(c => contactsId.Contains(c.Id))) { contact.IsNew = false; } db.SubmitChanges(); } }
public void SaveMessages(IEnumerable <MessageItem> messages) { if (messages == null) { return; } using (QRyptoDb db = QryptoDbFactory.GetDb()) { db.Messages.InsertAllOnSubmit(messages.Select(m => new MessageModel() { SenderId = m.SenderId, ReceiverId = m.ReceiverId, IsNew = m.IsNew, Body = m.Body, Date = m.Date })); db.SubmitChanges(); } }
public void SaveContacts(IEnumerable <ContactItem> contacts) { if (contacts == null) { return; } using (QRyptoDb db = QryptoDbFactory.GetDb()) { var filtered = contacts.Where(e => !db.Contacts.Any(c => c.Id == e.Id)).ToList(); db.Contacts.InsertAllOnSubmit(filtered.Select(c => new ContactModel() { Id = c.Id, Name = c.Name, PublicKey = c.PublicKey, IsNew = c.IsNew, MessagesSentByUser = new EntitySet <MessageModel>(), MessagesSentToUser = new EntitySet <MessageModel>() })); db.SubmitChanges(); } }
public IEnumerable <IMessageModel> GetMessages(int contactId) { using (QRyptoDb db = QryptoDbFactory.GetDb()) { ContactModel contact = db.Contacts.FirstOrDefault(c => c.Id == contactId); if (contact == null) { throw new ArgumentException("Contact does not exist"); } List <MessageModel> messages = contact.MessagesSentByUser.ToList(); messages.AddRange(contact.MessagesSentToUser); // mark messages as already seen foreach (MessageModel msg in messages) { msg.IsNew = false; } db.SubmitChanges(); return(messages); } }