public override bool AddContact(string login, string contact_login, string contact_nickname)
        {
            using (DataEntities ctx = new DataEntities())
            {
                user usr = ctx.users.FirstOrDefault(u => u.login == login);
                user usr_ct = ctx.users.FirstOrDefault(u => u.login == contact_login);

                if (usr == null || usr_ct == null)
                    return false;

                if (ctx.contacts.FirstOrDefault(u => u.user_id == usr.user_id && u.contact_id == usr_ct.user_id) != null)
                    return false;

                contact ct = new contact()
                {
                    user_id = usr.user_id,
                    contact_id = usr_ct.user_id,
                    name = contact_nickname
                };

                ctx.contacts.Add(ct);
                ctx.SaveChanges();
                return true;
            }
        }
        public override void AddMessage(DateTime time, List<int> users, int author, string message)
        {
            using (DataEntities ctx = new DataEntities())
            {
                if (ctx.users.FirstOrDefault(u => u.user_id == author) == null)
                {
                    throw new UserNotExistingException();
                }
                foreach (int id in users)
                {
                    if (ctx.users.FirstOrDefault(u => u.user_id == id) == null)
                    {
                        throw new UserNotExistingException();
                    }
                }

                message msg = new message
                {
                    author_id = author,
                    time = time,
                    message1 = message
                };

                foreach (int id in users)
                {
                    msg.users.Add(ctx.users.FirstOrDefault(u => u.user_id == id));
                }

                ctx.messages.Add(msg);
                ctx.SaveChanges();
            }
        }
        public override int CreateUser(string login, string password)
        {
            using (DataEntities ctx = new DataEntities())
            {
                if (ctx.users.FirstOrDefault(u => u.login == login) == null)
                {
                    user new_user = new user()
                    {
                        login = login,
                        password = password,
                        status = 0,
                        description = "",
                        last_status_update = DateTime.UtcNow,
                        last_messages_download = DateTime.UtcNow
                    };
                    ctx.users.Add(new_user);
                    ctx.SaveChanges();

                    return ctx.users.FirstOrDefault(u => u.login == login).user_id;
                }

                throw new UserAlreadyExistsException();
            }
        }
 public override List<Message> GetArchive(DateTime from, DateTime to, List<int> ids)
 {
     using (DataEntities ctx = new DataEntities())
     {
         List<message> msgs = ctx.messages.Where(m => m.users.Select(u => u.user_id).Intersect(ids).Count() == ids.Count()).Where(m2 => m2.time >= from && m2.time <= to).ToList();
         return Convert(msgs);
     }
 }
        public override bool SetStatus(string login, EUserStatus status, string description)
        {
            using (DataEntities ctx = new DataEntities())
            {
                user usr = ctx.users.FirstOrDefault(u => u.login == login);

                if (usr == null)
                    return false;

                usr.status = (int)status;
                usr.description = description;
                usr.last_status_update = DateTime.UtcNow;

                ctx.SaveChanges();
                return true;
            }
        }
 public override void SetLastMessageUpdate(int user_id, DateTime time)
 {
     using (DataEntities ctx = new DataEntities())
     {
         user usr = ctx.users.FirstOrDefault(u => u.user_id == user_id);
         if (usr == null)
         {
             throw new UserNotExistingException();
         }
         usr.last_messages_download = time;
         ctx.SaveChanges();
     }
 }
 public override List<string> GetUsersWithLoginInContactList(string login)
 {
     using (DataEntities ctx = new DataEntities())
     {
         return ctx.users.Where(u => u.contacts.FirstOrDefault(c => c.contact_.login == login) != null).Select(s => s.login).ToList();
     }
 }
 public override User GetUser(string login)
 {
     using (DataEntities ctx = new DataEntities())
     {
         return Convert(ctx.users.FirstOrDefault(u => u.login == login));
     }
 }
 public override User GetUser(int user_id)
 {
     using (DataEntities ctx = new DataEntities())
     {
         return Convert(ctx.users.FirstOrDefault(u => u.user_id == user_id));
     }
 }
 public override List<Message> GetMessagesSince(int user, DateTime time)
 {
     using (DataEntities ctx = new DataEntities())
     {
         List<Message> msgs = new List<Message>();
         foreach (message msg in ctx.messages.Where(m => m.author_id != user && m.users.FirstOrDefault(u => u.user_id == user) != null && m.time >= time).ToList())
         {
             msgs.Add(Convert(msg));
         }
         return msgs;
     }
 }
 public override DateTime GetLastMessageUpdate(int user_id)
 {
     using (DataEntities ctx = new DataEntities())
     {
         user usr = ctx.users.FirstOrDefault(u => u.user_id == user_id);
         if (usr == null)
         {
             throw new UserNotExistingException();
         }
         return usr.last_messages_download;
     }
 }