Exemplo n.º 1
0
        public static async Task <bool> SendNotiBuy(ApplicationDbContext db, string BuyerID, OrderHeader OrderHeader, List <OrderDetails> OrderDetails, DateTime?date = null)
        {
            if (date == null)
            {
                date = DateTime.Now;
            }


            var noti_buy = new NotiBuy()
            {
                Message = "You have bought a total of " + OrderDetails.Count().ToString() +
                          " different products for a total of " + "$" + (OrderHeader.TotalPrice).ToString("F2"),
                NotiDate    = (DateTime)date,
                Seen        = false,
                OrderHeader = OrderHeader,
                SendToUser  = BuyerID,
            };

            await db.NotiBuy.AddAsync(noti_buy);

            await db.SaveChangesAsync();

            return(true);
        }
Exemplo n.º 2
0
        public async Task <IActionResult> Index(string noti_type)
        {
            var claimsIdentity = (ClaimsIdentity)this.User.Identity;
            var claim          = claimsIdentity.FindFirst(ClaimTypes.NameIdentifier);

            bool is_admin = this.User.IsInRole(SD.ManagerUser);

            NotificationViewModel nvm = new NotificationViewModel();

            if (!this.User.Identity.IsAuthenticated)
            {
                return(NotFound());
            }

            if (noti_type == "NotiRole" || noti_type == null)
            {
                var notifications = await _db.NotiRole
                                    .Where(n =>
                                           n.SendToUser == claim.Value ||
                                           (n.SendToUser == "All_A" && is_admin)
                                           )
                                    .Include(nr => nr.User)
                                    .ToListAsync();

                foreach (var n in notifications)
                {
                    NotiRole nr = new NotiRole()
                    {
                        Id         = n.Id,
                        Message    = n.Message,
                        User       = n.User,
                        UserID     = n.UserID,
                        NotiDate   = n.NotiDate,
                        Seen       = n.Seen,
                        SendToUser = n.SendToUser,
                    };
                    nvm.NotiRole.Add(nr);
                    n.Seen = true;
                }
                _db.UpdateRange(notifications);
            }
            if (noti_type == "NotiBuy" || noti_type == null)
            {
                var notifications = await _db.NotiBuy
                                    .Where(n =>
                                           n.SendToUser == claim.Value
                                           )
                                    .Include(n => n.OrderHeader)
                                    .Include(n => n.OrderHeader.User)
                                    .ToListAsync();

                foreach (var n in notifications)
                {
                    n.OrderDetails = await _db.OrderDetails.Where(o => o.OrderId == n.OrderHeaderID).ToListAsync();

                    NotiBuy nr = new NotiBuy()
                    {
                        Id            = n.Id,
                        Message       = n.Message,
                        OrderHeaderID = n.OrderHeaderID,
                        OrderHeader   = n.OrderHeader,
                        OrderDetails  = n.OrderDetails,
                        NotiDate      = n.NotiDate,
                        Seen          = n.Seen,
                        SendToUser    = n.SendToUser,
                    };

                    nvm.NotiBuy.Add(nr);
                    n.Seen = true;
                }

                _db.UpdateRange(notifications);
            }

            if (noti_type == "NotiSell" || noti_type == null)
            {
                var notifications = await _db.NotiSell
                                    .Where(n =>
                                           n.SendToUser == claim.Value
                                           )
                                    .Include(n => n.OrderDetails)
                                    .ToListAsync();

                foreach (var n in notifications)
                {
                    NotiSell ns = new NotiSell()
                    {
                        Id           = n.Id,
                        Message      = n.Message,
                        NotiDate     = n.NotiDate,
                        Seen         = n.Seen,
                        SendToUser   = n.SendToUser,
                        OrderDetails = n.OrderDetails,
                    };
                    nvm.NotiSell.Add(ns);
                    n.Seen = true;
                }
                _db.UpdateRange(notifications);
            }

            if (noti_type == "NotiGeneral" || noti_type == null)
            {
                var notifications = await _db.Notification.OfType <Notification>().Where(n => (n.SendToUser == claim.Value)).ToListAsync();

                foreach (var n in notifications)
                {
                    if (n.GetType() == typeof(Notification))
                    {
                        Notification not = new Notification()
                        {
                            Id         = n.Id,
                            Message    = n.Message,
                            NotiDate   = n.NotiDate,
                            Seen       = n.Seen,
                            SendToUser = n.SendToUser
                        };

                        nvm.NotiGeneral.Add(not);
                        n.Seen = true;
                    }
                }

                _db.UpdateRange(notifications);
            }

            if (noti_type == "NotiAuction" || noti_type == null)
            {
                var notifications = await _db.NotiAuction
                                    .Where(n =>
                                           n.SendToUser == claim.Value
                                           )
                                    .Include(n => n.AuctionHeader)
                                    .ToListAsync();

                foreach (var n in notifications)
                {
                    var products = await _db.AuctionProduct.Where(a => a.AuctionId == n.AuctionHeaderID).Include(a => a.Product).ToListAsync();

                    NotiAuction na = new NotiAuction()
                    {
                        Id              = n.Id,
                        Message         = n.Message,
                        NotiDate        = n.NotiDate,
                        Seen            = n.Seen,
                        SendToUser      = n.SendToUser,
                        AuctionHeader   = n.AuctionHeader,
                        AuctionHeaderID = n.AuctionHeaderID,
                        AuctionProduct  = products,
                    };

                    var auction = await _db.AuctionHeader.FirstOrDefaultAsync(a => a.Id == n.AuctionHeaderID);

                    string status = SD.ActiveStatus;
                    if (DateTime.Compare(DateTime.Now, auction.EndDate) >= 0)
                    {
                        status = SD.PastStatus;
                    }

                    na.Status = status;
                    nvm.NotiAuction.Add(na);
                    n.Seen = true;
                }
                _db.UpdateRange(notifications);
            }

            await _db.SaveChangesAsync();

            nvm.Type = noti_type;
            return(View(nvm));
        }