public HistoryItem(int transferId, int userId)
        {
            SafeSendEntities db = new SafeSendEntities();
            var item = from f in db.FileTransfers
                       join u1 in db.Users on f.SenderId equals u1.UserId
                       join u2 in db.Users on f.ReceiverId equals u2.UserId
                       where f.TransferId == transferId
                       select new {
                           f.TransferId,
                           f.TransferDate,
                           SenderName = u1.Name + " " + u1.Surname,
                           ReceiverName = u2.Name + " " + u2.Surname,
                           f.SenderId,
                           f.ReceiverId,
                           f.Status
                       };

            this.TransferId = item.First().TransferId;
            this.TransferDate = item.First().TransferDate.Value;
            this.SenderName = item.First().SenderName;
            this.ReceiverName = item.First().ReceiverName;
            this.Status = item.First().Status.Value;
            if(userId == item.First().SenderId)
            {
                this.TransferDirection = 1;
            }
            else
            {
                this.TransferDirection = 2;
            }
        }
 public Boolean CheckUser()
 {
     SafeSendEntities db = new SafeSendEntities();
     if (db.Users.Where(x => x.Phone == this.Phone || x.Email == this.Email).Count() > 0)
     {
         return false;
     }
     return true;
 }
        public int Authenticate()
        {
            SafeSendEntities db = new SafeSendEntities();
            if (db.Users.Where(x => x.Email == this.Email && x.Password == this.Password).Count() > 0)
            {
                return db.Users.Where(x => x.Email == this.Email && x.Password == this.Password).First().UserId;
            }

            return -1;
        }
 public static Boolean UpdateTransferStatus(int transferId)
 {
     SafeSendEntities db = new SafeSendEntities();
     FileTransfers transfer = db.FileTransfers.Where(x => x.TransferId == transferId).FirstOrDefault();
     if(transfer != null)
     {
         transfer.Status = 2;
         db.FileTransfers.Attach(transfer);
         db.Entry(transfer).State = System.Data.Entity.EntityState.Modified;
         db.SaveChanges();
         return true;
     }
     else
     {
         return false;
     }
 }
 public static Package DownloadFile(int transferId)
 {
     SafeSendEntities db = new SafeSendEntities();
     FileTransfers transfer = db.FileTransfers.Where(x => x.TransferId == transferId).FirstOrDefault();
     if(transfer != null)
     {
         Package package = new Package();
         package.EncryptionLevel = transfer.EncryptionLevel.Value;
         package.TransferId = transfer.TransferId;
         package.FileContent = transfer.TransferredData;
         package.FileData = transfer.FileData;
         return package;
     }
     else
     {
         return null;
     }
 }
 public static int TransferFile(int senderId, string phone, string data, int level, byte[] fileData)
 {
     SafeSendEntities db = new SafeSendEntities();
     Users user = db.Users.Where(x => x.Phone == phone).FirstOrDefault();
     if(user != null)
     {
         FileTransfers transfer = new FileTransfers();
         transfer.SenderId = senderId;
         transfer.ReceiverId = user.UserId;
         transfer.Status = 1;
         transfer.TransferDate = DateTime.Now;
         transfer.EncryptionLevel = level;
         transfer.TransferredData = data;
         transfer.FileData = fileData;
         db.FileTransfers.Add(transfer);
         db.SaveChanges();
         PushNotification(user.DeviceToken);
         return transfer.TransferId;
     }
     else
     {
         return -1;
     }
 }
        public string GetContacts(string phoneListStr)
        {
            string returnStr = "";
            SafeSendEntities db = new SafeSendEntities();
            string[] tmp = phoneListStr.Split(';');
            List<ContactItem> contactList = new List<ContactItem>();
            for (int i = 0; i < tmp.Length; i++)
            {
                if(!string.IsNullOrEmpty(tmp[i]))
                {
                    string phone = tmp[i];
                    if (db.Users.Where(x => x.Phone == phone && x.UserId != UserId).Count() > 0)
                    {
                        returnStr = returnStr + phone + ";";
                    }
                }
            }

            return returnStr;
        }
 public Boolean Update()
 {
     SafeSendEntities db = new SafeSendEntities();
     Users _user = db.Users.Where(x => x.UserId == this.UserId).First();
     if (_user != null)
     {
         _user.Name = this.Name;
         _user.Surname = this.Surname;
         db.Users.Attach(_user);
         db.Entry(_user).State = System.Data.Entity.EntityState.Modified;
         db.SaveChanges();
         return true;
     }
     return false;
 }
        public Boolean Insert()
        {
            SafeSendEntities db = new SafeSendEntities();
            Users _user = new Users();
            _user.Name = this.Name;
            _user.Surname = this.Surname;
            _user.Email = this.Email;
            _user.Password = this.Password;
            _user.Phone = this.Phone;
            _user.UDID = this.UDID;
            _user.DeviceToken = this.DeviceToken;
            db.Users.Add(_user);
            if (db.SaveChanges() > 0)
            {
                return true;
            }

            return false;
        }
 public void GetUserInfo()
 {
     SafeSendEntities db = new SafeSendEntities();
     Users _user = db.Users.Where(x => x.UserId == this.UserId).FirstOrDefault();
     if(_user != null)
     {
         this.Name = _user.Name;
         this.Surname = _user.Surname;
         this.Email = _user.Email;
         this.Password = _user.Password;
         this.Phone = _user.Phone;
         this.UDID = _user.UDID;
         this.DeviceToken = _user.DeviceToken;
     }
 }
        public List<NotificationItem> GetNotifications()
        {
            SafeSendEntities db = new SafeSendEntities();
            List<NotificationItem> notifications = new List<NotificationItem>();
            var list = from f in db.FileTransfers
                       join u in db.Users on f.SenderId equals u.UserId
                       where f.ReceiverId == this.UserId && f.Status == 1
                       select new { f.TransferId, u.Name, u.Surname };
            foreach (var item in list)
            {
                NotificationItem notificationItem = new NotificationItem();
                notificationItem.TransferId = item.TransferId;
                notificationItem.Explanation = "You have an incoming file from " + item.Name + " " + item.Surname;
                notifications.Add(notificationItem);
            }

            return notifications;
        }
 public List<HistoryItem> GetHistory()
 {
     SafeSendEntities db = new SafeSendEntities();
     List<HistoryItem> history = new List<HistoryItem>();
     var list = db.FileTransfers.Where(x => x.SenderId == this.UserId || x.ReceiverId == this.UserId).ToList().OrderByDescending(x=> x.TransferDate);
     foreach (var item in list)
     {
         HistoryItem historyItem = new HistoryItem(item.TransferId, this.UserId);
         history.Add(historyItem);
     }
     return history;
 }