Ejemplo n.º 1
0
        public int CommentOnYardSaleItem([FromBody] Comment comment)
        {
            YardSaleItem item          = context.YardSaleItem.Where(ysi => ysi.YardSaleItemId == comment.YardSaleItemId).SingleOrDefault();
            User         RecievingUser = context.User.Where(u => u.UserId == item.PostingUserId).SingleOrDefault();
            User         SendingUser   = ActiveUser.Instance.User;

            comment.TimePosted = DateTime.Now;
            comment.UserId     = SendingUser.UserId;

            context.Comment.Add(comment);

            if (RecievingUser.UserId != SendingUser.UserId)
            {
                Notification NewNotification = new Notification
                {
                    NotificationText = $"{SendingUser.FirstName} {SendingUser.LastName}, commented on your {item.ItemName} that is up for sale!",
                    NotificationType = "Sale",
                    NotificatonDate  = DateTime.Now,
                    RecievingUserId  = RecievingUser.UserId,
                    YardSaleItemId   = item.YardSaleItemId,
                    Seen             = false,
                    SenderUserId     = SendingUser.UserId
                };
                context.Notification.Add(NewNotification);
            }

            context.SaveChanges();

            return(comment.CommentId);
        }
Ejemplo n.º 2
0
        /**
         * Purpose: Post method that takes a new item and saves it to the DB
         * Arguments:
         *      model - contains all the neccessary properties that are needed to put a new item up for sale
         * Return:
         *      redirects to the main view of yardsale products "YardSale/Index"
         */
        public async Task <IActionResult> AddNewItem(YardSaleNewItemViewModel model)
        {
            var          uploads = Path.Combine(_environment.WebRootPath, "images");
            YardSaleItem NewItem = new YardSaleItem
            {
                DatePosted      = DateTime.Now,
                ItemDescription = model.NewItem.ItemDescription,
                ItemName        = model.NewItem.ItemName,
                ItemPrice       = model.NewItem.ItemPrice,
                PostingUserId   = ActiveUser.Instance.User.UserId,
                ItemImage1      = model.NewItemImages[0].FileName,
                Category        = model.NewItem.Category,
            };

            NewItem.ItemImage2 = (model.NewItemImages.Count > 1) ? model.NewItemImages[1].FileName : null;
            NewItem.ItemImage3 = (model.NewItemImages.Count > 2) ? model.NewItemImages[2].FileName : null;
            NewItem.ItemImage4 = (model.NewItemImages.Count > 3) ? model.NewItemImages[3].FileName : null;

            context.YardSaleItem.Add(NewItem);
            foreach (var image in model.NewItemImages)
            {
                if (image != null && image.ContentType.Contains("image"))
                {
                    using (var fileStream = new FileStream(Path.Combine(uploads, image.FileName), FileMode.Create))
                    {
                        await image.CopyToAsync(fileStream);
                    }
                }
            }

            await context.SaveChangesAsync();

            return(RedirectToAction("Index", "YardSale"));
        }
Ejemplo n.º 3
0
        public void DeleteYardSaleItem([FromBody] int ItemId)
        {
            List <Comment> ItemComments = context.Comment.Where(c => c.YardSaleItemId == ItemId).ToList();

            ItemComments.ForEach(ic => context.Comment.Remove(ic));
            YardSaleItem item = context.YardSaleItem.Where(ysi => ysi.YardSaleItemId == ItemId).SingleOrDefault();

            context.YardSaleItem.Remove(item);
            context.SaveChanges();
        }