Example #1
0
        //刪除
        public OperationResult DeleteOrder(string id)
        {
            OperationResult operationResult = new OperationResult();
            var             dbContext       = new XbooxLibraryDBContext();

            using (var transaction = dbContext.Database.BeginTransaction())
            {
                try
                {
                    var orderRepo       = new GeneralRepository <Order>(dbContext);
                    var orderDetailRepo = new GeneralRepository <OrderDetails>(dbContext);
                    var productRepo     = new GeneralRepository <Product>(dbContext);
                    var orderDetails    = orderDetailRepo.GetAll().Where(item => item.OrderId.ToString() == id);
                    var order           = orderRepo.GetFirst(item => item.OrderId.ToString() == id);
                    if (order != null && orderDetails != null)
                    {
                        foreach (var item in orderDetails)
                        {
                            orderDetailRepo.Delete(item);
                        }
                        orderRepo.Delete(order);
                        productRepo.SaveContext();
                        operationResult.IsSuccessful = true;
                        transaction.Commit();
                    }
                }
                catch (Exception ex)
                {
                    operationResult.IsSuccessful = false;
                    operationResult.exception    = ex;
                    transaction.Rollback();
                }
            }
            return(operationResult);
        }
Example #2
0
 public CartController()
 {
     if (context == null)
     {
         context = new XbooxLibraryDBContext();
     }
 }
Example #3
0
 public OrderController()
 {
     orderservice        = new OrderService();
     shoppingCartService = new ShoppingCartService();
     _context            = new XbooxLibraryDBContext();
     filterOrderService  = new FilterOrderDataService();
 }
Example #4
0
        public IEnumerable <SalesRevenueViewModel> GetSalesRevenue()
        {
            XbooxLibraryDBContext            db      = new XbooxLibraryDBContext();
            GeneralRepository <Order>        otable  = new GeneralRepository <Order>(db);
            GeneralRepository <OrderDetails> odtable = new GeneralRepository <OrderDetails>(db);
            GeneralRepository <Product>      ptable  = new GeneralRepository <Product>(db);

            var temp = from od in odtable.GetAll().AsEnumerable()
                       join o in otable.GetAll().AsEnumerable()
                       on od.OrderId equals o.OrderId
                       join p in ptable.GetAll().AsEnumerable()
                       on od.ProductId equals p.ProductId
                       where o.Paid == true       //paid 付款狀態 註解掉可以取得較多data
                       select new
            {
                Year  = o.OrderDate.Year,
                Month = o.OrderDate.Month,
                Total = p.Price * od.Quantity
            };
            var Revenue = from t in temp
                          group t by new { t.Month, t.Year } into g
                select new SalesRevenueViewModel
            {
                Year    = g.Key.Year,
                Month   = g.Key.Month,
                Revenue = g.Sum(x => x.Total)
            };

            return(Revenue);
        }
Example #5
0
 /// <summary>
 /// 取得所有訂單
 /// </summary>
 /// <returns></returns>
 public List <OrderViewModel> GetOrder()
 {
     using (var dbContext = new XbooxLibraryDBContext())
     {
         var orderRepo = new GeneralRepository <Order>(dbContext);
         var userRepo  = new GeneralRepository <AspNetUsers>(dbContext);
         var orderList = (from o in orderRepo.GetAll().AsEnumerable()
                          join user in userRepo.GetAll().AsEnumerable()
                          on o.UserId equals user.Id
                          select new OrderViewModel
         {
             OrderId = o.OrderId,
             OrderDate = (DateTime)TimeCheckerService.GetTaipeiTime(o.OrderDate),
             UserName = user.UserName,
             PurchaserName = o.PurchaserName,
             PurchaserEmail = o.PurchaserEmail,
             PurchaserAddress = $"{o.City}{o.District}{o.Road}",
             PurchaserPhone = o.PurchaserPhone,
             Payment = o.Payment,
             Paid = o.Paid,
             PayDate = TimeCheckerService.GetTaipeiTime(o.PayDate),
             Build = o.Build
         }).OrderBy(item => item.OrderDate).ToList();
         return(orderList);
     }
 }
Example #6
0
        public IQueryable <TopProducts> GetTopProducts()
        {
            XbooxLibraryDBContext            db      = new XbooxLibraryDBContext();
            GeneralRepository <Order>        otable  = new GeneralRepository <Order>(db);
            GeneralRepository <OrderDetails> odtable = new GeneralRepository <OrderDetails>(db);
            GeneralRepository <Product>      ptable  = new GeneralRepository <Product>(db);

            var temp = from od in odtable.GetAll()
                       join o in otable.GetAll()
                       on od.OrderId equals o.OrderId
                       join p in ptable.GetAll()
                       on od.ProductId equals p.ProductId
                       where o.Paid == true           //paid 付款狀態 註解掉可以取得較多data
                       select new
            {
                Year     = o.OrderDate.Year,
                Month    = o.OrderDate.Month,
                Name     = p.Name,
                Quantity = od.Quantity
            };

            var topproducts = from t in temp
                              group t by new { t.Year, t.Month, t.Name } into g
                select new TopProducts
            {
                Month    = g.Key.Month,
                Name     = g.Key.Name,
                Quantity = g.Sum(x => x.Quantity)
            };

            return(topproducts);
        }
Example #7
0
        public void Delete(Guid id)
        {
            XbooxLibraryDBContext           context       = new XbooxLibraryDBContext();
            GeneralRepository <Product>     repository    = new GeneralRepository <Product>(context);
            GeneralRepository <ProductImgs> Imgrepository = new GeneralRepository <ProductImgs>(context);
            GeneralRepository <ProductTags> Ptrepository  = new GeneralRepository <ProductTags>(context);

            var deleteProduct = repository.GetFirst(x => x.ProductId == id);
            var deleteImg     = Imgrepository.GetAll().Where(x => x.ProductId == id);
            var deleteTag     = Ptrepository.GetAll().Where(x => x.ProductId == id);


            if (deleteImg != null)
            {
                foreach (var i in deleteImg)
                {
                    Imgrepository.Delete(i);
                }
            }
            if (deleteTag != null)
            {
                foreach (var i in deleteTag)
                {
                    Ptrepository.Delete(i);
                }
            }
            //刪除product 刪除 tag

            repository.Delete(deleteProduct);
            context.SaveChanges();
        }
Example #8
0
        public void RemoveImg(string imgName)
        {
            XbooxLibraryDBContext           context       = new XbooxLibraryDBContext();
            GeneralRepository <Product>     repository    = new GeneralRepository <Product>(context);
            GeneralRepository <ProductImgs> Imgrepository = new GeneralRepository <ProductImgs>(context);

            var charac = imgName.Split('"');

            foreach (var i in charac)
            {
                Debug.WriteLine(i);
            }
            var temp = charac[1];

            var imgToDelete = Imgrepository.GetAll().FirstOrDefault(x => x.imgLink == temp);

            if (imgToDelete != null)
            {
                Imgrepository.Delete(imgToDelete);
                Imgrepository.SaveContext();
            }
            else
            {
            }
        }
Example #9
0
        public OperationResult CouponsCreate(Coupons input)
        {
            var result = new OperationResult();

            try
            {
                XbooxLibraryDBContext       context = new XbooxLibraryDBContext();
                GeneralRepository <Coupons> couRepo = new GeneralRepository <Coupons>(context);
                Coupons entity = new Coupons()
                {
                    Id         = Guid.NewGuid(),
                    CouponName = input.CouponName,
                    Discount   = input.Discount,
                    CouponCode = input.CouponCode,
                    StartTime  = input.StartTime,
                    EndTime    = input.EndTime
                };
                couRepo.Create(entity);
                couRepo.SaveContext();
                result.IsSuccessful = true;
            }
            catch (Exception ex)
            {
                result.IsSuccessful = false;
                result.exception    = ex;
            }
            return(result);
        }
Example #10
0
 /// <summary>
 /// 利用訂單Id取訂單
 /// </summary>
 /// <param name="httpContext"></param>
 /// <param name="orderId"></param>
 /// <returns></returns>
 public List <OrderViewModel> GetOrder(HttpContextBase httpContext, string orderId)
 {
     using (var dbContext = new XbooxLibraryDBContext())
     {
         var orderRepo = new GeneralRepository <Order>(dbContext);
         var userRepo  = new GeneralRepository <AspNetUsers>(dbContext);
         var userId    = httpContext.User.Identity.GetUserId();
         var orderList = (from o in orderRepo.GetAll().AsEnumerable()
                          where o.UserId == userId && o.OrderId.ToString() == orderId
                          join user in userRepo.GetAll().AsEnumerable()
                          on o.UserId equals user.Id
                          select new OrderViewModel
         {
             OrderId = o.OrderId,
             EcpayOrderNumber = o.EcpayOrderNumber,
             OrderDate = (DateTime)TimeCheckerService.GetTaipeiTime(o.OrderDate),
             UserName = user.UserName,
             PurchaserName = o.PurchaserName,
             PurchaserEmail = o.PurchaserEmail,
             City = o.City,
             District = o.District,
             Road = o.Road,
             PurchaserPhone = o.PurchaserPhone,
             Payment = o.Payment,
             PayDate = TimeCheckerService.GetTaipeiTime(o.PayDate),
             Paid = o.Paid,
             Build = o.Build
         }).OrderBy(item => item.OrderDate).ToList();
         return(orderList);
     }
 }
Example #11
0
        /// <summary>
        /// 編輯當筆訂單的付款狀態(ecpayNumber)
        /// </summary>
        /// <param name="ecpayNumber"></param>
        /// <returns></returns>
        public OperationResult EditPaidStateByEcNumber(string ecpayNumber)
        {
            OperationResult operationResult = new OperationResult();

            using (var dbContext = new XbooxLibraryDBContext())
            {
                try
                {
                    var orderRepo = new GeneralRepository <Order>(dbContext);
                    var order     = orderRepo.GetFirst(x => x.EcpayOrderNumber.ToString() == ecpayNumber);
                    if (order != null)
                    {
                        if (!order.Paid)
                        {
                            order.Paid    = true;
                            order.PayDate = DateTime.UtcNow;
                        }
                        else
                        {
                            order.Paid = false;
                        }
                        orderRepo.SaveContext();
                        operationResult.isSuccessful = true;
                    }
                }
                catch (Exception ex)
                {
                    operationResult.isSuccessful = false;
                    operationResult.exception    = ex;
                }
                return(operationResult);
            }
        }
Example #12
0
 /// <summary>
 /// 拿到記住訂單的資訊
 /// </summary>
 /// <param name="httpContext"></param>
 /// <returns></returns>
 public OrderViewModel GetRecordInfo(HttpContextBase httpContext)
 {
     using (var dbContext = new XbooxLibraryDBContext())
     {
         var orderRepo = new GeneralRepository <Order>(dbContext);
         var userRepo  = new GeneralRepository <AspNetUsers>(dbContext);
         var userId    = httpContext.User.Identity.GetUserId();
         var orderList = orderRepo.GetAll()
                         .Where(item => item.UserId == userId)
                         .OrderByDescending(item => item.OrderDate);
         var            order     = orderList.FirstOrDefault();
         OrderViewModel orderInfo = new OrderViewModel();
         if (order != null)
         {
             if (order.Remember)
             {
                 orderInfo.PurchaserName  = order.PurchaserName;
                 orderInfo.City           = order.City;
                 orderInfo.District       = order.District;
                 orderInfo.Road           = order.Road;
                 orderInfo.PurchaserEmail = order.PurchaserEmail;
                 orderInfo.PurchaserPhone = order.PurchaserPhone;
                 orderInfo.Remember       = order.Remember;
             }
         }
         return(orderInfo);
     }
 }
Example #13
0
 /// <summary>
 ///  拿到訂單裡的產品資訊
 /// </summary>
 /// <param name="orderId"></param>
 /// <returns></returns>
 public List <OrderDetailsViewModel> GetOrderDetails(string orderId)
 {
     using (var dbContext = new XbooxLibraryDBContext())
     {
         var orderDetailRepo = new GeneralRepository <OrderDetails>(dbContext);
         var productRepo     = new GeneralRepository <Product>(dbContext);
         var imgRepo         = new GeneralRepository <ProductImgs>(dbContext);
         var CouponRepo      = new GeneralRepository <Coupons>(dbContext);
         List <OrderDetailsViewModel> orderDetailsList = new List <OrderDetailsViewModel>();
         // 因為多張圖片會重複產品
         var tempList = (from od in orderDetailRepo.GetAll().AsEnumerable()
                         where od.OrderId.ToString() == orderId
                         join pd in productRepo.GetAll().AsEnumerable()
                         on od.ProductId equals pd.ProductId
                         join pi in imgRepo.GetAll().AsEnumerable()
                         on pd.ProductId equals pi.ProductId
                         where pd.ProductId == pi.ProductId
                         select new OrderDetailsViewModel
         {
             Imagelink = pi.imgLink,
             Name = pd.Name,
             Quantity = od.Quantity,
             Price = pd.Price,
             Coupon = CouponRepo.GetFirst(item => item.Id == od.Discount),
             Total = Math.Round(pd.Price * od.Quantity)
         }).GroupBy(item => item.Name);
         foreach (var productList in tempList)
         {
             var firstProductItem = productList.FirstOrDefault(item => !item.Imagelink.Contains("-0"));
             orderDetailsList.Add(firstProductItem);
         }
         return(orderDetailsList);
     }
 }
Example #14
0
        public List <ProductListViewModel> GetProducts()
        {
            XbooxLibraryDBContext       context     = new XbooxLibraryDBContext();
            GeneralRepository <Product> prepository = new GeneralRepository <Product>(context);
            // var tagList = Tagrepository.GetAll().ToList();
            var pList = prepository.GetAll().Select(item => new ProductListViewModel
            {
                Name   = item.Name,
                Author = item.Author,

                CategoryId    = item.CategoryId,
                Intro         = item.Intro,
                ISBN          = item.ISBN,
                Price         = item.Price,
                PublishedDate = item.PublishedDate,
                Publisher     = item.Publisher,
                UnitInStock   = item.UnitInStock,
                Specification = item.Specification,
                ProductId     = item.ProductId,
                Description   = item.Description,
                Language      = item.Language
            }).ToList();

            return(pList);
        }
Example #15
0
        public List <Coupons> GetAllCoupons()
        {
            XbooxLibraryDBContext       context    = new XbooxLibraryDBContext();
            GeneralRepository <Coupons> couponRepo = new GeneralRepository <Coupons>(context);
            var couponList = couponRepo.GetAll().ToList();

            return(couponList);
        }
Example #16
0
 public NavBarHelper()
 {
     if (SideBarCategories == null)
     {
         XbooxLibraryDBContext db = new XbooxLibraryDBContext();
         SideBarCategories = new GeneralRepository <Category>(db);
     }
 }
Example #17
0
 public GeneralRepository(XbooxLibraryDBContext contexts)
 {
     if (contexts == null)
     {
         throw new ArgumentNullException();
     }
     context = contexts;
 }
Example #18
0
        public Product GetSingleProduct(Guid id)
        {
            XbooxLibraryDBContext       context    = new XbooxLibraryDBContext();
            GeneralRepository <Product> repository = new GeneralRepository <Product>(context);
            var temp  = repository.GetAll().FirstOrDefault(x => x.ProductId == id);
            var temps = repository.GetFirst(x => x.ProductId == id);

            return(temps);
        }
Example #19
0
        public List <string> GetPictures(Guid id)
        {
            XbooxLibraryDBContext           context       = new XbooxLibraryDBContext();
            GeneralRepository <Product>     repository    = new GeneralRepository <Product>(context);
            GeneralRepository <ProductImgs> Imgrepository = new GeneralRepository <ProductImgs>(context);

            var imgList = Imgrepository.GetAll().Where(x => x.ProductId == id).Select(x => x.imgLink).ToList();

            return(imgList);
        }
Example #20
0
        public void EditUserDetails(UserDetails userdetails)
        {
            XbooxLibraryDBContext           db   = new XbooxLibraryDBContext();
            GeneralRepository <AspNetUsers> user = new GeneralRepository <AspNetUsers>(db);
            var edit = user.GetAll().FirstOrDefault(u => u.Id == userdetails.Id);

            edit.Email       = userdetails.Email;
            edit.PhoneNumber = userdetails.Phone;
            user.Update(edit);
            user.SaveContext();
        }
Example #21
0
        private void PutImgs(Product product)
        {
            XbooxLibraryDBContext           context = new XbooxLibraryDBContext();
            GeneralRepository <ProductImgs> ImgRepo = new GeneralRepository <ProductImgs>(context);

            //   var imgList = getImg().Split(',').Where(x=>x!="").ToList();


            //用list傳的
            foreach (var i in GetImg())
            {
                ProductImgs productImgs = new ProductImgs()
                {
                    // ProductImgId = 0,
                    imgLink   = i,
                    ProductId = product.ProductId,
                };
                ImgRepo.Create(productImgs);
                // context.ProductImgs.Add(productImgs);
            }



            //   用string 傳的
            //     foreach (var i in imgList)
            //    {
            //        productImgs = new ProductImgs()
            ////        {
            // ProductImgId = 0,
            //             imgLink = i,
            //             ProductId = product.ProductId,
            //          };
            //         context.ProductImgs.Add(productImgs);
            //productImgs.Add(new ProductImgs() { imgLink = i.ToString(), ProductId = product.ProductId });
            //    }


            // context.SaveChanges();

            //  var PiList = new List<ProductImgs>();
            // var img = productImgs
            ///    var productImgs = productImgs.Where(x => x.ProductId == product.ProductId);
            //    freach (var i in productImgs)
            //   {
            //   product.ProductImgId = i.ProductImgId + ",";
            //product.ProductImgId = Convert.ToInt64(product.ProductImgId) + ",";
            //   }

            //    imgString = null;
            ImgstringList = null;
            ImgRepo.SaveContext();
        }
Example #22
0
        public List <CreateListViewModel.CategoryViewModel> GetCatecory()
        {
            XbooxLibraryDBContext        context            = new XbooxLibraryDBContext();
            GeneralRepository <Category> Categoryrepository = new GeneralRepository <Category>(context);

            var cateList = Categoryrepository.GetAll().Select(item => new CreateListViewModel.CategoryViewModel
            {
                Name       = item.Name,
                CategoryId = item.CategoryId
            }).ToList();

            return(cateList);
        }
Example #23
0
        public List <TagViewModel> GetTags()
        {
            XbooxLibraryDBContext    context       = new XbooxLibraryDBContext();
            GeneralRepository <Tags> Tagrepository = new GeneralRepository <Tags>(context);
            // var tagList = Tagrepository.GetAll().ToList();
            var tagList = Tagrepository.GetAll().Select(item => new TagViewModel
            {
                TagId   = item.TagId,
                TagName = item.TagName
            }).ToList();

            return(tagList);
        }
Example #24
0
        //取消訂單
        public OperationResult CancelOrder(string id)
        {
            OperationResult operationResult = new OperationResult();
            var             dbContext       = new XbooxLibraryDBContext();

            using (var transaction = dbContext.Database.BeginTransaction())
            {
                try
                {
                    var orderRepo       = new GeneralRepository <Order>(dbContext);
                    var orderDetailRepo = new GeneralRepository <OrderDetails>(dbContext);
                    var productRepo     = new GeneralRepository <Product>(dbContext);
                    var orderDetails    = orderDetailRepo.GetAll().Where(item => item.OrderId.ToString() == id);
                    var order           = orderRepo.GetFirst(item => item.OrderId.ToString() == id);
                    if (order != null && orderDetails != null)
                    {
                        if (order.Paid == false)
                        {
                            order.Build = false;
                            foreach (var item in orderDetails)
                            {
                                var products = productRepo.GetAll().Where(pd => pd.ProductId == item.ProductId).OrderBy(pd => pd.PublishedDate);
                                foreach (var pd in products)
                                {
                                    if (item.Quantity <= 0)
                                    {
                                        break;
                                    }
                                    else

                                    {
                                        pd.UnitInStock = pd.UnitInStock + item.Quantity;
                                    }
                                }
                            }
                            productRepo.SaveContext();
                            operationResult.IsSuccessful = true;
                            transaction.Commit();
                        }
                    }
                }

                catch (Exception ex)
                {
                    operationResult.IsSuccessful = false;
                    operationResult.exception    = ex;
                    transaction.Rollback();
                }
            }
            return(operationResult);
        }
Example #25
0
        public List <TagViewModel> GetTags()
        {
            var result = new TagViewModel();

            XbooxLibraryDBContext    context    = new XbooxLibraryDBContext();
            GeneralRepository <Tags> repository = new GeneralRepository <Tags>(context);
            var temp = from t in repository.GetAll()
                       select new TagViewModel()
            {
                TagId   = t.TagId,
                TagName = t.TagName
            };

            return(temp.ToList());
        }
Example #26
0
        public static UserDetails GetUserDetails(HttpContextBase context)
        {
            XbooxLibraryDBContext           db   = new XbooxLibraryDBContext();
            GeneralRepository <AspNetUsers> user = new GeneralRepository <AspNetUsers>(db);
            var details = (from u in user.GetAll()
                           where u.UserName == context.User.Identity.Name
                           select new UserDetails
            {
                Id = u.Id,
                Account = u.UserName,
                Email = u.Email,
                Phone = u.PhoneNumber
            }).FirstOrDefault();

            return(details);
        }
Example #27
0
        //trycatch
        public OperationResult Create(CreateDataModel input)
        {
            var result = new OperationResult();

            try
            {
                XbooxLibraryDBContext       context    = new XbooxLibraryDBContext();
                GeneralRepository <Product> repository = new GeneralRepository <Product>(context);

                //tag && img
                Product entity = new Product()
                {
                    ProductId     = Guid.NewGuid(),
                    Name          = input.Name,
                    CategoryId    = input.CategoryId,
                    ISBN          = input.ISBN,
                    Author        = input.Author,
                    Specification = input.Specification,
                    Intro         = input.Intro,
                    Language      = input.Language,
                    UnitInStock   = input.UnitInStock,
                    PublishedDate = input.PublishedDate,

                    Description = input.Description,

                    Price = input.Price
                };

                PutImgs(entity);
                //加入Img
                repository.Create(entity);
                repository.SaveContext();
                //加入tag
                AddedTag(entity, input.PostedTagIds);
                //context.SaveChanges();
                // repository.SaveContext();

                result.IsSuccessful = true;
            }
            catch (Exception ex)
            {
                result.IsSuccessful = false;
                result.exception    = ex;
            }

            return(result);
        }
Example #28
0
        public List <UserListViewModel> GetAllUsers()
        {
            XbooxLibraryDBContext context = new XbooxLibraryDBContext();
            var userrepo = new GeneralRepository <AspNetUsers>(context);
            var userList = userrepo.GetAll().ToList();
            List <UserListViewModel> viewModel = new List <UserListViewModel>();

            foreach (var item in userList)
            {
                viewModel.Add(new UserListViewModel()
                {
                    Id          = item.Id,
                    Email       = item.Email,
                    UserName    = item.UserName,
                    PhoneNumber = item.PhoneNumber
                });
            }
            return(viewModel);
        }
Example #29
0
        public List <TagViewModel> GetSelectedTags(Product product)
        {
            XbooxLibraryDBContext           context              = new XbooxLibraryDBContext();
            GeneralRepository <Product>     Pdrepository         = new GeneralRepository <Product>(context);
            GeneralRepository <Tags>        Tagrepository        = new GeneralRepository <Tags>(context);
            GeneralRepository <ProductTags> ProductTagrepository = new GeneralRepository <ProductTags>(context);
            List <TagViewModel>             TagLists             = new List <TagViewModel>();
            var temps = ProductTagrepository.GetAll().Where(x => x.ProductId == product.ProductId).Select(x => x.TagId).ToList();

            foreach (var t in temps)
            {
                TagLists.Add(new TagViewModel()
                {
                    TagId = (Guid)t, TagName = context.Tags.Where(x => x.TagId == t).Select(x => x.TagName).FirstOrDefault()
                });
            }
            ;
            return(TagLists);
        }
Example #30
0
        public OperationResult CouponsEdit(Coupons input)
        {
            var result = new OperationResult();

            try
            {
                XbooxLibraryDBContext       context = new XbooxLibraryDBContext();
                GeneralRepository <Coupons> couRepo = new GeneralRepository <Coupons>(context);

                couRepo.Update(input);
                couRepo.SaveContext();
                result.IsSuccessful = true;
            }
            catch (Exception ex)
            {
                result.IsSuccessful = false;
                result.exception    = ex;
            }
            return(result);
        }