Ejemplo n.º 1
0
        public async Task <IActionResult> UpdateStatus(long id)
        {
            var userId = long.Parse(User.Claims.FirstOrDefault(x => x.Type == "gazeId")?.Value);
            var order  = await _context.Order.FindAsync(id);

            if (order == null || order.UserId != userId)
            {
                return(NotFound());
            }

            order.Status = (int)OrderStatus.Cancelled;
            _context.Order.Update(order);
            await _context.SaveChangesAsync();

            var userDevice = await _context.DeviceInfo.Where(x => x.UserId == order.UserId && x.Status == 1).OrderByDescending(x => x.CreatedDate).FirstOrDefaultAsync();

            if (userDevice != null)
            {
                var notification = new Notification
                {
                    Title       = "Order Notification",
                    UserId      = order.UserId,
                    Content     = $"Order {order.Code} has been cancelled successfully",
                    ObjectId    = order.Id,
                    Type        = (int)NotificationType.Order,
                    Status      = 0,
                    CreatedDate = DateTime.Now
                };

                _context.Notification.Add(notification);
                await _context.SaveChangesAsync();

                // See documentation on defining a message payload.
                var message = new Message()
                {
                    Data  = notification.GetData(),
                    Token = userDevice.RegId,
                };

                // Send a message to the device corresponding to the provided
                // registration token.
                string response = await FirebaseMessaging.DefaultInstance.SendAsync(message);

                // Response is a message ID string.
                Console.WriteLine("Successfully sent message: " + response);
            }

            return(Ok(order));
        }
Ejemplo n.º 2
0
        public async Task <ActionResult <Order> > PostOrder(Order order)
        {
            var id = long.Parse(User.Claims.FirstOrDefault(x => x.Type == "gazeId")?.Value);

            order.UserId = id;

            foreach (var productOrder in order.Cart)
            {
                ProductOption option = await _context.ProductOption.FindAsync(productOrder.OptionId);

                if (option == null)
                {
                    return(BadRequest());
                }

                productOrder.Id       = 0;
                productOrder.Color    = option.Color;
                productOrder.Size     = option.Size;
                productOrder.Discount = option.Discount;
                productOrder.Price    = option.Price;
                productOrder.Option   = null;
            }

            Configuration configuration = await _context.Configuration.OrderByDescending(c => c.CreatedDate).FirstOrDefaultAsync();

            order.Code        = $"#{DateTime.Now:yyMMddHHmmssfff}";
            order.ShippingFee = order.ShippingOption == 1
                ? configuration.StandardShippingFee
                : configuration.PremiumShippingFee;
            order.Status = (int)OrderStatus.Submitted;

            _context.Order.Add(order);

            List <CartItem> cart = await _context.Cart.Where(c => c.UserId == id).ToListAsync();

            _context.Cart.RemoveRange(cart);

            await _context.SaveChangesAsync();

            var userDevice = await _context.DeviceInfo.Where(x => x.UserId == id).OrderByDescending(x => x.CreatedDate).FirstOrDefaultAsync();

            if (userDevice != null)
            {
                var notification = new Notification
                {
                    Title       = "Order Submitted",
                    UserId      = id,
                    Content     = "Your order has been submitted!",
                    ObjectId    = order.Id,
                    Type        = (int)NotificationType.Order,
                    Status      = 0,
                    CreatedDate = DateTime.Now
                };

                _context.Notification.Add(notification);
                await _context.SaveChangesAsync();

                // See documentation on defining a message payload.
                var message = new Message()
                {
                    Data  = notification.GetData(),
                    Token = userDevice.RegId,
                };

                // Send a message to the device corresponding to the provided
                // registration token.
                string response = await FirebaseMessaging.DefaultInstance.SendAsync(message);

                // Response is a message ID string.
                Console.WriteLine("Successfully sent message: " + response);
            }

            return(CreatedAtAction(nameof(PostOrder), new { id = order.Id }, order));
        }
Ejemplo n.º 3
0
        public async Task <IActionResult> UpdateStatus(long id, [FromBody] int status)
        {
            var order = await _context.Order.FindAsync(id);

            if (order == null)
            {
                return(NotFound());
            }

            order.Status = status;
            _context.Order.Update(order);
            await _context.SaveChangesAsync();

            string content = "";

            switch (status)
            {
            case 0:
            {
                content = $"Order {order.Code} has been submitted";
                break;
            }

            case 1:
            {
                content = $"Order {order.Code} has been confirmed and on delivery";
                break;
            }

            case 2:
            {
                content = $"Order {order.Code} has been completed";
                break;
            }

            case 3:
            {
                content = $"Order {order.Code} has been cancelled";
                break;
            }
            }

            var userDevice = await _context.DeviceInfo.Where(x => x.UserId == order.UserId && x.Status == 1).OrderByDescending(x => x.CreatedDate).FirstOrDefaultAsync();

            if (userDevice != null)
            {
                var notification = new Notification
                {
                    Title       = "Order Notification",
                    UserId      = order.UserId,
                    Content     = content,
                    ObjectId    = order.Id,
                    Type        = (int)NotificationType.Order,
                    Status      = 0,
                    CreatedDate = DateTime.Now
                };

                _context.Notification.Add(notification);
                await _context.SaveChangesAsync();

                // See documentation on defining a message payload.
                var message = new Message()
                {
                    Data  = notification.GetData(),
                    Token = userDevice.RegId,
                };

                // Send a message to the device corresponding to the provided
                // registration token.
                string response = await FirebaseMessaging.DefaultInstance.SendAsync(message);

                // Response is a message ID string.
                Console.WriteLine("Successfully sent message: " + response);
            }

            return(Ok(order));
        }