예제 #1
0
        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();
            }
        }
예제 #2
0
 public IEnumerable <IContactModel> GetContacts()
 {
     using (QRyptoDb db = QryptoDbFactory.GetDb())
     {
         return(db.Contacts.ToList());
     }
 }
예제 #3
0
 public bool UserExists()
 {
     using (QRyptoDb db = QryptoDbFactory.GetDb())
     {
         bool userExists = db.Users.Any();
         return(userExists);
     }
 }
예제 #4
0
 public void ClearMessages()
 {
     using (QRyptoDb db = QryptoDbFactory.GetDb())
     {
         db.Messages.DeleteAllOnSubmit(db.Messages);
         db.SubmitChanges();
     }
 }
예제 #5
0
 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());
     }
 }
예제 #6
0
        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);
            }
        }
예제 #7
0
        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();
            }
        }
예제 #8
0
        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();
            }
        }
예제 #9
0
        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();
            }
        }
예제 #10
0
        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();
            }
        }
예제 #11
0
        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);
            }
        }