Пример #1
0
        public static List <UploadRecord> GetAllUploadRecords(int userId)
        {
            List <UploadRecord> result = null;

            //using (BillingContext ctx = new BillingContext())
            //{
            //    ctx.Configuration.LazyLoadingEnabled = false;

            //    return ctx.BillingRecords.Include(i => i.Currency).AsNoTracking()
            //                .Where(br => br.SubscriptionId.Equals(subscriptionId))
            //                .OrderByDescending(i => i.Year)
            //                .ThenByDescending(i => i.Month)
            //                .ToList();
            //}

            //int userId = 1;

            using (UploadContext ctx = new UploadContext())
            {
                ctx.Configuration.LazyLoadingEnabled = false;
                var share = ctx.Shares
                            .Where(s => s.UserId == userId)
                            .FirstOrDefault();

                result = ctx.UploadRecords
                         .Where(br => br.ShareId.Equals(share.ShareId))
                         .OrderByDescending(i => i.CreatedDate)
                         .ToList();
            }

            return(result);
        }
Пример #2
0
        public static bool CreateShare(string shareName, int totalSpace, string networkSharePath, int userId, string userName)
        {
            bool result = false;

            using (UploadContext ctx = new UploadContext())
            {
                ctx.Configuration.LazyLoadingEnabled = false;

                var location = new Share
                {
                    ShareName        = shareName,
                    TotalSpace       = totalSpace,
                    FreeSpace        = totalSpace, // When we start, all space is free.
                    NetworkSharePath = networkSharePath,
                    UserName         = userName,
                    UserId           = userId
                };

                ctx.Shares.Add(location);
                ctx.SaveChanges();
                result = true;
            }

            return(result);
        }
Пример #3
0
        public static bool CreateUploadRecords(string storageFileName, long size, int shareId)
        {
            bool result = false;

            using (UploadContext ctx = new UploadContext())
            {
                Share        share     = ctx.Shares.Where(s => s.ShareId == shareId).FirstOrDefault();
                UploadRecord file      = new UploadRecord();
                string[]     arrayName = storageFileName.Split('.');
                file.ShareId       = shareId;
                file.CloudId       = 1;
                file.Container     = string.Empty;
                file.Description   = "Description";
                file.Extension     = arrayName.Length > 1 ? arrayName[1] : string.Empty;
                file.FileCloudName = "Azure Pack";
                file.IsHidden      = false;
                file.IsShared      = false;
                file.Name          = arrayName.Length > 0 ? arrayName[0] : storageFileName;
                file.MimeType      = arrayName.Length > 1 ? arrayName[1]: string.Empty;
                file.Size          = size;
                file.CreatedDate   = DateTime.Now;
                file.ExpiredDate   = file.CreatedDate.AddDays(7);

                ctx.UploadRecords.Add(file);
                ctx.SaveChanges();

                //Update Share with new freeSpace
                int newFreeSpace = share.FreeSpace - (int)size;
                UpdateShare(share.ShareId, newFreeSpace);

                result = true;
            }

            return(result);
        }
Пример #4
0
        public static Share GetShare(int userId)
        {
            Share result;

            using (UploadContext ctx = new UploadContext())
            {
                ctx.Configuration.LazyLoadingEnabled = false;
                result = ctx.Shares.Where(i => i.UserId == userId)
                         .SingleOrDefault();
            }

            return(result);
        }
Пример #5
0
        public static bool UpdateShare(int id, int newFreeSpace)
        {
            bool result = false;

            using (UploadContext ctx = new UploadContext())
            {
                ctx.Configuration.LazyLoadingEnabled = false;

                // Delete from Database
                ctx.Database.ExecuteSqlCommand("UPDATE Share SET FreeSpace=@newFreeSpace WHERE ShareId=@id", new SqlParameter("@id", id), new SqlParameter("@newFreeSpace", newFreeSpace));
                result = true;
            }

            return(result);
        }
Пример #6
0
        public static bool DeleteShare(int id)
        {
            bool result = false;

            using (UploadContext ctx = new UploadContext())
            {
                ctx.Configuration.LazyLoadingEnabled = false;

                // Delete from Database
                ctx.Database.ExecuteSqlCommand("Delete from Share where ShareId=@id", new SqlParameter("@id", id));
                result = true;
            }

            return(result);
        }
Пример #7
0
        public static List <Share> GetAllShares()
        {
            List <Share> result;

            using (UploadContext ctx = new UploadContext())
            {
                ctx.Configuration.LazyLoadingEnabled = false;

                result = ctx.Shares
                         .OrderBy(i => i.ShareId)
                         .ToList();
            }

            return(result);
        }
Пример #8
0
        public static bool DeleteUploadRecord(int id)
        {
            bool result = false;

            using (UploadContext ctx = new UploadContext())
            {
                var uploadFile = ctx.UploadRecords.Where(f => f.Id == id).FirstOrDefault();
                if (uploadFile != null)
                {
                    var    share    = ctx.Shares.Where(s => s.ShareId == uploadFile.ShareId).FirstOrDefault();
                    string pathFile = share.NetworkSharePath + "/" + uploadFile.Name + "." + uploadFile.Extension;

                    if (!File.Exists(pathFile))
                    {
                        return(false);
                    }

                    bool isDeleted = false;
                    while (!isDeleted)
                    {
                        try
                        {
                            File.Delete(pathFile);
                            isDeleted = true;
                        }
                        catch (Exception e)
                        {
                        }
                        Thread.Sleep(50);
                    }

                    // Delete from Database
                    ctx.Database.ExecuteSqlCommand("Delete from UploadRecord where id=@id", new SqlParameter("@id", id));

                    // Share with new freeSpace
                    int newFreeSpace = share.FreeSpace + (int)uploadFile.Size;
                    UpdateShare(share.ShareId, newFreeSpace);

                    result = true;
                }
            }

            return(result);
        }