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 void ClearMessages()
 {
     using (QRyptoDb db = QryptoDbFactory.GetDb())
     {
         db.Messages.DeleteAllOnSubmit(db.Messages);
         db.SubmitChanges();
     }
 }
        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);
            }
        }