Пример #1
0
        public async Task <IActionResult> Excute()
        {
            var _context = new TGDDContext();
            var order    = new Order();


            order = await _context.Orders.Include(o => o.OrderDetails).Where(order => order.Id == OrderId).FirstOrDefaultAsync();

            order.StatusId = StatusId;

            // update stock , buying time
            if (this.StatusId == 4)
            {
                ProductsAutoUpdate productsAutoUpdate = new ProductsAutoUpdate();
                List <OrderDetail> orderDetails       = order.OrderDetails.ToList();
                foreach (OrderDetail orderDetail in orderDetails)
                {
                    Product product = await _context.Products.FindAsync(orderDetail.ProductId);

                    product.BuyingTimes -= orderDetail.Quantity;
                    product.Stock       += orderDetail.Quantity;

                    _context.Entry(product).State = EntityState.Modified;
                }
                await _context.SaveChangesAsync();
            }

            _context.Entry(order).State = EntityState.Modified;

            try
            {
                await _context.SaveChangesAsync();
            }
            catch (DbUpdateConcurrencyException)
            {
                bool orderExist = _context.Orders.Any(o => o.Id == order.Id);
                if (!orderExist)
                {
                    return(NotFound());
                }
                else
                {
                    throw;
                }
            }

            return(Ok("Chuyển trạng thái thành công !"));
        }
        public async Task <IActionResult> PutUseVoucher(long id, UseVoucher useVoucher)
        {
            if (id != useVoucher.Id)
            {
                return(BadRequest());
            }

            _context.Entry(useVoucher).State = EntityState.Modified;

            try
            {
                await _context.SaveChangesAsync();
            }
            catch (DbUpdateConcurrencyException)
            {
                if (!UseVoucherExists(id))
                {
                    return(NotFound());
                }
                else
                {
                    throw;
                }
            }

            return(NoContent());
        }
Пример #3
0
        public async Task <IActionResult> Excute()
        {
            //if (id != comments.Id)
            //{
            //    return BadRequest();
            //}
            AssigndataUtils AssigndataUtils = new AssigndataUtils();

            var _context = new TGDDContext();

            Comment comment = AssigndataUtils.AssignComment(CommentDTO, CommentId);

            _context.Entry(comment).State = EntityState.Modified;

            try
            {
                await _context.SaveChangesAsync();
            }
            catch (DbUpdateConcurrencyException)
            {
                bool commentExist = _context.Comments.Any(e => e.Id == CommentId);
                if (!commentExist)
                {
                    return(NotFound("Không tìm thấy comment!"));
                }
                else
                {
                    throw;
                }
            }
            return(Ok("Chỉnh sửa thành công !"));
        }
        public async Task <IActionResult> Excute()
        {
            var _context = new TGDDContext();
            var contact  = await _context.Contacts.FindAsync(ContactId);

            contact.IsReply = true;

            _context.Entry(contact).State = EntityState.Modified;

            try
            {
                await _context.SaveChangesAsync();
            }
            catch (DbUpdateConcurrencyException)
            {
                bool contactExist = _context.Contacts.Any(c => c.Id == ContactId);
                if (!contactExist)
                {
                    return(NotFound());
                }
                else
                {
                    throw;
                }
            }

            return(NoContent());
        }
Пример #5
0
        public async Task <ActionResult> Excute()
        {
            var _context = new TGDDContext();

            Customer customer = await _context.Customers.FindAsync(CustomerId);

            customer.Password = SecurityUtils.CreateMD5(NewPassword);

            //OldPassword = SecurityUtils.CreateMD5(OldPassword);

            //if(OldPassword == customer.Password)
            //{
            //    customer.Password = SecurityUtils.CreateMD5(NewPassword);
            //}
            //else
            //{
            //    return BadRequest("Mật khẩu hiện tại không đúng !");
            //}

            _context.Entry(customer).State = EntityState.Modified;

            try
            {
                await _context.SaveChangesAsync();
            }
            catch
            {
                throw;
            };

            return(Ok("Đổi mật khẩu thành công !"));
        }
Пример #6
0
        public async Task <IActionResult> Excute()
        {
            AssigndataUtils AssigndataUtils = new AssigndataUtils();
            var             _context        = new TGDDContext();

            Voucher voucherUpdate = AssigndataUtils.AssignVoucher(voucherDTO, voucherDTO.Id);

            _context.Entry(voucherUpdate).State = EntityState.Modified;

            try
            {
                await _context.SaveChangesAsync();
            }
            catch (DbUpdateConcurrencyException)
            {
                bool voucherExist = _context.Vouchers.Any(voucher => voucher.Id == voucherUpdate.Id);
                if (!voucherExist)
                {
                    return(NotFound());
                }
                else
                {
                    throw;
                }
            }

            return(Ok("Update Voucher thành công !"));
        }
Пример #7
0
        public async Task <ActionResult <IEnumerable <Circle> > > GetCircle()
        {
            List <OrderDetail> orderDetails = await _context.OrderDetails.Include(o => o.Product).ToListAsync();

            List <Circle> circles = await _context.Circle.ToListAsync();

            var c = new int[4];

            foreach (OrderDetail orderDetail in orderDetails)
            {
                c[orderDetail.Product.CategoryId.Value - 1] += orderDetail.Quantity.Value;
            }

            int tmp = 0;

            foreach (Circle circle in circles)
            {
                circle.SoldQuantity          = c[tmp];
                _context.Entry(circle).State = EntityState.Modified;
                tmp++;
            }

            await _context.SaveChangesAsync();



            return(await _context.Circle.Include(c => c.Category).ToListAsync());
        }
Пример #8
0
        public async Task <ActionResult <Customer> > Excute()
        {
            var      _context = new TGDDContext();
            Customer customer = _context.Customers.FirstOrDefault(customer => customer.UserName == Username);

            if (customer == null)
            {
                return(BadRequest());
            }
            else
            {
                customer.Password = SecurityUtils.CreateMD5(Password);

                _context.Entry(customer).State = EntityState.Modified;
            }

            try
            {
                await _context.SaveChangesAsync();
            }
            catch (DbUpdateConcurrencyException)
            {
                bool customerExist = _context.Customers.Any(c => c.Id == customer.Id);
                if (!customerExist)
                {
                    return(NotFound());
                }
                else
                {
                    throw;
                }
            }
            return(Ok());
        }
Пример #9
0
        public async Task <IActionResult> PutYearStatistical(long id, YearStatistical yearStatistical)
        {
            if (id != yearStatistical.Id)
            {
                return(BadRequest());
            }

            _context.Entry(yearStatistical).State = EntityState.Modified;

            try
            {
                await _context.SaveChangesAsync();
            }
            catch (DbUpdateConcurrencyException)
            {
                if (!YearStatisticalExists(id))
                {
                    return(NotFound());
                }
                else
                {
                    throw;
                }
            }

            return(NoContent());
        }
Пример #10
0
        public async Task <IActionResult> Excute()
        {
            var _context = new TGDDContext();

            Customer customer = _context.Customers.Where(customer => customer.UserName == Username).FirstOrDefault();

            if (customer == null)
            {
                return(BadRequest("Không tìm thấy tài khoản!"));
            }

            if (customer.Password == Pw)
            {
                customer.Verified = true;

                _context.Entry(customer).State = EntityState.Modified;
                try
                {
                    await _context.SaveChangesAsync();
                }
                catch
                {
                    return(BadRequest());
                }
            }
            else
            {
                return(BadRequest());
            }
            return(Ok("Xác thực thành công !"));
        }
Пример #11
0
        public async Task <IActionResult> PutCarts(long id, Cart carts)
        {
            if (id != carts.Id)
            {
                return(BadRequest());
            }

            _context.Entry(carts).State = EntityState.Modified;

            try
            {
                await _context.SaveChangesAsync();
            }
            catch (DbUpdateConcurrencyException)
            {
                if (!CartsExists(id))
                {
                    return(NotFound());
                }
                else
                {
                    throw;
                }
            }

            return(NoContent());
        }
Пример #12
0
        public async Task <IActionResult> Excute()
        {
            AssigndataUtils AssigndataUtils = new AssigndataUtils();

            var _context = new TGDDContext();
            var order    = new Order();

            order = await AssigndataUtils.AssignOrder(OrderDTO, Id);

            if (order.StatusId == 4)
            {
                ProductsAutoUpdate productsAutoUpdate = new ProductsAutoUpdate();
                List <OrderDetail> orderDetails       = order.OrderDetails.ToList();
                foreach (OrderDetail orderDetail in orderDetails)
                {
                    Product product = await _context.Products.FindAsync(orderDetail.ProductId);

                    product.BuyingTimes -= orderDetail.Quantity;
                    product.Stock       += orderDetail.Quantity;

                    _context.Entry(product).State = EntityState.Modified;
                }
                await _context.SaveChangesAsync();
            }

            _context.Entry(order).State = EntityState.Modified;

            try
            {
                await _context.SaveChangesAsync();
            }
            catch (DbUpdateConcurrencyException)
            {
                bool orderExist = _context.Orders.Any(o => o.Id == order.Id);
                if (!orderExist)
                {
                    return(NotFound());
                }
                else
                {
                    throw;
                }
            }

            return(Ok("Cập nhật thành công !"));
        }
        public async Task <ActionResult <IEnumerable <TopProduct> > > GetTopProducts()
        {
            List <long> productId = await _context.Products.OrderByDescending(p => p.BuyingTimes).Select(p => p.Id).Take(10).ToListAsync();

            List <TopProduct> topProducts = await _context.TopProducts.ToListAsync();

            int tmp = 0;

            foreach (TopProduct topProduct in topProducts)
            {
                topProduct.ProductId             = productId[tmp];
                _context.Entry(topProduct).State = EntityState.Modified;
                tmp++;
            }

            await _context.SaveChangesAsync();

            return(await _context.TopProducts.Include(top => top.Product).ThenInclude(p => p.Images).ToListAsync());
        }
Пример #14
0
        public void UpdateQuantity(long ProductId, int Quantity)
        {
            var     _context = new TGDDContext();
            Product product  = _context.Products.Find(ProductId);

            if (product != null)
            {
                product.Stock       -= Quantity;
                product.BuyingTimes += Quantity;
            }
            _context.Entry(product).State = EntityState.Modified;

            _context.SaveChangesAsync();
        }
Пример #15
0
        public async Task <ActionResult <Product> > Rating([FromForm] long ProductId, [FromForm] int rate)
        {
            var     _context = new TGDDContext();
            Product tmp      = await _context.Products.FindAsync(ProductId);

            tmp.Rating = (tmp.Rating * tmp.BuyingTimes + rate) / (tmp.BuyingTimes + 1);

            _context.Entry(tmp).State = EntityState.Modified;

            try
            {
                await _context.SaveChangesAsync();
            }
            catch
            {
                return(BadRequest("Lỗi"));
            }
            return(Ok());
        }
Пример #16
0
        public async Task <IActionResult> Excute()
        {
            //if (id != customer.Id)
            //{
            //    return BadRequest();
            //}

            var _context = new TGDDContext();

            Customer customer = await _context.Customers.FindAsync(Id);

            {
                customer.Lastname  = this.Customer.Lastname;
                customer.Firstname = this.Customer.Firstname;
                customer.Address   = this.Customer.Address;
                customer.Phone     = this.Customer.Phone;
                customer.Gender    = this.Customer.Gender;
            }



            _context.Entry(customer).State = EntityState.Modified;

            try
            {
                await _context.SaveChangesAsync();
            }
            catch (DbUpdateConcurrencyException)
            {
                bool customerExist = _context.Customers.Any(c => c.Id == customer.Id);
                if (!customerExist)
                {
                    return(NotFound());
                }
                else
                {
                    throw;
                }
            }
            return(NoContent());
        }
        public async Task <ActionResult <IEnumerable <TopCustomer> > > GetTopCustomers()
        {
            List <Order> orders = await _context.Orders.OrderByDescending(o => o.Date).Take(10).ToListAsync();

            //List<Order> orders = await _context.Orders.OrderBy(o => o.Date).Take(10).ToListAsync();

            List <TopCustomer> customers = await _context.TopCustomers.ToListAsync();

            int tmp = 0;

            foreach (TopCustomer topCustomer in customers)
            {
                topCustomer.CustomerId            = orders[tmp].CustomerId;
                topCustomer.LastBuy               = orders[tmp].Date;
                _context.Entry(topCustomer).State = EntityState.Modified;
                tmp++;
            }

            await _context.SaveChangesAsync();

            return(await _context.TopCustomers.Include(c => c.Customer).ToListAsync());
        }
Пример #18
0
        public async Task <IActionResult> Excute()
        {
            AssigndataUtils AssigndataUtils = new AssigndataUtils();
            var             _context        = new TGDDContext();

            Product pro = await AssigndataUtils.AssignProduct(ProductDTO, id);

            //List<Description> tmp = pro.Descriptions.ToList() ;


            //foreach(Description description in tmp)
            //{
            //    _context.Entry((Description)description) = EntityState.Modified;
            //}

            _context.Entry(pro).State = EntityState.Modified;

            try
            {
                await _context.SaveChangesAsync();
            }
            catch (DbUpdateConcurrencyException)
            {
                bool productExist = _context.Products.Any(p => p.Id == pro.Id);
                if (!productExist)
                {
                    return(NotFound());
                }
                else
                {
                    throw;
                }
            }

            return(StatusCode(200));
        }
Пример #19
0
        public async Task <ActionResult <Order> > Excute()
        {
            AssigndataUtils AssigndataUtils = new AssigndataUtils();

            var _context = new TGDDContext();
            var order    = new Order();

            order = await AssigndataUtils.AssignOrder(OrderDTO, 0);

            // update stock, buying time
            {
                ProductsAutoUpdate productsAutoUpdate = new ProductsAutoUpdate();

                List <OrderDetail> orderDetails = order.OrderDetails.ToList();

                foreach (OrderDetail orderDetail in orderDetails)
                {
                    Product product = await _context.Products.FindAsync(orderDetail.ProductId);

                    product.BuyingTimes += orderDetail.Quantity;
                    product.Stock       -= orderDetail.Quantity;

                    _context.Entry(product).State = EntityState.Modified;
                }
                await _context.SaveChangesAsync();
            }


            //thống kê


            _context.Orders.Add(order);
            try
            {
                await _context.SaveChangesAsync();
            }
            catch (DbUpdateException)
            {
                bool orderExist = _context.Orders.Any(o => o.Id == order.Id);
                if (orderExist)
                {
                    return(Conflict());
                }
                else
                {
                    throw;
                }
            }

            {
                DateTime dateTime = order.Date.Value;

                int day  = dateTime.DayOfYear;
                int week = dateTime.DayOfYear / 7 - 1;
                if (week < 1)
                {
                    week = 1;
                }
                else if (week > 52)
                {
                    week = 52;
                }
                int month = dateTime.Month;
                int year  = dateTime.Year;

                YearStatistical yearStatisticals = await _context.YearStatisticals.FirstOrDefaultAsync(y => y.Year == year);

                if (yearStatisticals != null)
                {
                    yearStatisticals.TotalYear            += order.Total;
                    _context.Entry(yearStatisticals).State = EntityState.Modified;
                    await _context.SaveChangesAsync();


                    MonthStatistical monthStatistical = await _context.MonthStatisticals.FirstOrDefaultAsync(m => m.Month == month && m.YearId == yearStatisticals.Id);

                    if (monthStatistical != null)
                    {
                        monthStatistical.TotalMonth           += order.Total;
                        _context.Entry(monthStatistical).State = EntityState.Modified;
                        await _context.SaveChangesAsync();

                        WeekStatistical weekStatistical = await _context.WeekStatisticals.FirstOrDefaultAsync(w => w.Week == week && yearStatisticals.Id == w.YearId);

                        if (weekStatistical != null)
                        {
                            weekStatistical.TotalWeek            += order.Total;
                            _context.Entry(weekStatistical).State = EntityState.Modified;
                            await _context.SaveChangesAsync();

                            //DayStatistical dayStatistical = await _context.DayStatisticals.FirstOrDefaultAsync(d => d.Day == day && d.YearId == yearStatisticals.Id);
                        }
                        else
                        {
                            long MonthId = _context.MonthStatisticals.Max(m => m.Id) + 1;
                            long WeekId  = _context.WeekStatisticals.Max(w => w.Id) + 1;

                            newWeek(order, _context, yearStatisticals.Id, MonthId, week);
                            //newday(order, _context, yearStatisticals.Id, MonthId, WeekId, day);
                        }
                    }
                    else
                    {
                        long MonthId = _context.MonthStatisticals.Max(m => m.Id) + 1;
                        long WeekId  = _context.WeekStatisticals.Max(w => w.Id) + 1;


                        newMonth(order, _context, yearStatisticals.Id, month);
                        newWeek(order, _context, yearStatisticals.Id, MonthId, week);
                        //newday(order, _context, yearStatisticals.Id, MonthId, WeekId, day);
                    }
                }
                else
                {
                    long MonthId = _context.MonthStatisticals.Max(m => m.Id) + 1;
                    long WeekId  = _context.WeekStatisticals.Max(w => w.Id) + 1;

                    newYear(order, _context, year);
                    newMonth(order, _context, yearStatisticals.Id, month);
                    newWeek(order, _context, yearStatisticals.Id, MonthId, week);
                    //newday(order, _context, yearStatisticals.Id, MonthId, WeekId, day);
                }
            }

            return(CreatedAtAction("GetOrders", new { id = order.Id }, order));
        }
Пример #20
0
        public async Task <Order> AssignOrder(OrderDTO orderDTO, long id)
        {
            var _context        = new TGDDContext();
            var order           = new Order();
            var listOrderDetail = new List <OrderDetail>();

            if (id == 0)
            {
                id = _context.Orders.Max(o => o.Id) + 1;
            }
            else
            {
                listOrderDetail = await _context.OrderDetails.Where(orderDetail => orderDetail.OrderId == id).ToListAsync();

                foreach (OrderDetail orderDetail in listOrderDetail)
                {
                    foreach (OrderDetailDTO orderDetailDTO in orderDTO.OrderDetails)
                    {
                        if (orderDetailDTO.ProductId == orderDetail.ProductId)
                        {
                            orderDetail.Quantity = orderDetailDTO.Quantity;

                            _context.Entry(orderDetail).State = EntityState.Modified;

                            break;
                        }
                    }
                }

                await _context.SaveChangesAsync();
            }

            order.Id              = id;
            order.Date            = DateTime.Now;
            order.CustomerId      = orderDTO.CustomerId;
            order.Discount        = orderDTO.Discount;
            order.Total           = orderDTO.Total;
            order.ShippingAddress = orderDTO.ShippingAddress;
            order.StatusId        = orderDTO.StatusId;
            order.Note            = orderDTO.Note;
            order.PaymentMethod   = orderDTO.PaymentMethod;

            var orderDetailId = _context.OrderDetails.Max(od => od.Id);

            // OrderDetail od ;

            foreach (var orderDetail in orderDTO.OrderDetails)
            {
                if (listOrderDetail.Count > 0)
                {
                    break;
                }

                var od = new OrderDetail();
                orderDetailId++;
                od.Id        = orderDetailId;
                od.Quantity  = orderDetail.Quantity;
                od.ProductId = orderDetail.ProductId;

                Product product = _context.Products.Find(od.ProductId);


                od.CurrentPrice = product.Price;

                od.OrderId = id;

                order.OrderDetails.Add(od);
            }
            ;

            return(order);
        }