public static int CreatePackage(DataContracts.Package newPackage)
        {
            Contract.Requires(newPackage != null);
            Contract.Requires(GetPackageById(newPackage.Id) == null);
            Contract.Ensures(GetPackageById(newPackage.Id) != null);
            Contract.Ensures(GetPackageById(newPackage.Id).Equals(newPackage));

            using (var db = new DatabaseContext())
            {
                var package = new Package
                {
                    Name = newPackage.Name,
                    Description = newPackage.Description,
                    OwnerEmail = newPackage.OwnerEmail,
                    Owner = db.Users.Find(newPackage.OwnerEmail),
                    Memberships = new List<Membership>()
                };

                db.Packages.Add(package);
                db.SaveChanges();
                foreach (var id in newPackage.FileIds)
                {
                    package.Memberships.Add(new Membership { PackageId = package.Id, FileId = id });
                }
                db.Entry(db.Packages.Find(package.Id)).State = EntityState.Modified;
                db.SaveChanges();
                return package.Id;
            }
        }
        public static void DeleteFileById(int fId)
        {
            Contract.Requires(GetFileInfoById(fId) != null);
            Contract.Ensures(GetFileInfoById(fId) == null);

            using (var db = new DatabaseContext())
            {
                db.Entry(db.Files.Find(fId)).State = EntityState.Deleted;
                db.SaveChanges();
            }
        }
        public static void UpdateRight(DataContracts.Right updatedRight)
        {
            Contract.Requires(updatedRight != null);

            using (var db = new DatabaseContext())
            {
                var outdatedRight = db.Rights.Find(updatedRight.UserEmail, updatedRight.ItemId);
                db.Entry(outdatedRight).CurrentValues.SetValues(updatedRight);
                db.SaveChanges();
            }
        }
        public static void UpdateUser(DataContracts.User updatedUser)
        {
            Contract.Requires(updatedUser.Email != null);
            Contract.Requires(GetUserByEmail(updatedUser.Email) != null);
            Contract.Ensures(GetUserByEmail(updatedUser.Email).Equals(updatedUser));

            using (var db = new DatabaseContext())
            {
                var outdatedUser = db.Users.Find(updatedUser.Email);
                db.Entry(outdatedUser).CurrentValues.SetValues(updatedUser);
                db.SaveChanges();
            }
        }
        public static void UpdateFileData(byte[] updatedData, int fId)
        {
            Contract.Requires(GetFileInfoById(fId) != null);
            Contract.Ensures(DownloadFileById(fId).Equals(updatedData));

            using (var db = new DatabaseContext())
            {
                var outdatedFile = db.Files.Find(fId);
                var updatedFile = (File)((DataContracts.FileInfo)outdatedFile);
                updatedFile.Data = updatedData;

                db.Entry(outdatedFile).CurrentValues.SetValues(updatedFile);
                db.SaveChanges();
            }
        }
        public static void UpdateFileInfo(DataContracts.FileInfo updatedInfo)
        {
            Contract.Requires(GetFileInfoById(updatedInfo.Id) != null);
            Contract.Ensures(GetFileInfoById(updatedInfo.Id).Equals(updatedInfo));

            using (var db = new DatabaseContext())
            {
                var outdatedFile = db.Files.Find(updatedInfo.Id);
                var updatedFile = (File)updatedInfo;
                updatedFile.Data = outdatedFile.Data;

                db.Entry(outdatedFile).CurrentValues.SetValues(updatedFile);
                db.SaveChanges();
            }
        }
        public static void RemoveFromPackage(List<int> fIds, int pId)
        {
            Contract.Requires(fIds != null && pId != null);
            Contract.Ensures(Contract.OldValue(GetPackageById(pId).FileIds.Count) == GetPackageById(pId).FileIds.Count + fIds.Count);

            using (var db = new DatabaseContext())
            {
                foreach (var fId in fIds)
                {
                    db.Entry(db.Memberships.Find(pId, fId)).State = EntityState.Deleted;
                }
                db.SaveChanges();
            }
        }
        public static void DropTag(string text, int iId)
        {
            Contract.Requires(text != null && iId != null);
            Contract.Ensures(

            using (var db = new DatabaseContext())
            {
                if (db.Tags.Find(text, iId) != null)
                {
                    db.Entry(db.Tags.Find(text,iId)).State = EntityState.Deleted;
                    db.SaveChanges();
                }
            }
        }
        public static void DropRight(string email, int itemId)
        {
            Contract.Requires(email != null && itemId != null);
            Contract.Ensures(GetRight(itemId, email) == null);

            using (var db = new DatabaseContext())
            {
                db.Entry(db.Rights.Find(email, itemId)).State = EntityState.Deleted;
                db.SaveChanges();
            }
        }
        public static void DeleteUserByEmail(string email)
        {
            Contract.Requires(email != null);
            Contract.Requires(GetUserByEmail(email) != null);
            Contract.Ensures(GetUserByEmail(email) == null);

            using (var db = new DatabaseContext())
            {
                db.Entry(db.Users.Find(email)).State = EntityState.Deleted;
                db.SaveChanges();
            }
        }
        public static void DeletePackageById(int pId)
        {
            Contract.Requires(pId != null);
            Contract.Ensures(GetPackageById(pId) == null);

            using (var db = new DatabaseContext())
            {
                db.Entry(db.Packages.Find(pId)).State = EntityState.Deleted;
                db.SaveChanges();
            }
        }