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 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 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 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();
     }
 }