public ActionResult Create([Bind(Include = "Id,Make,Model,Size,Material,Price,Description,CategoryId")] Product product, HttpPostedFileBase upload)
        {
            if (ModelState.IsValid)
            {
                if (upload != null && upload.ContentLength > 0)
                {
                    var avatar = new File
                    {
                        FileName    = System.IO.Path.GetFileName(upload.FileName),
                        FileType    = FileType.Avatar,
                        ContentType = upload.ContentType
                    };
                    using (var reader = new System.IO.BinaryReader(upload.InputStream))
                    {
                        avatar.Content = reader.ReadBytes(upload.ContentLength);
                    }
                    product.Files = new List <File> {
                        avatar
                    };
                }
                db.Products.Add(product);
                db.SaveChanges();
                return(RedirectToAction("Index"));
            }

            ViewBag.CategoryId = new SelectList(db.Categories, "Id", "Title", product.CategoryId);
            return(View(product));
        }
        public void RemoveUser(string username)
        {
            var user = _marketplaceContext.Users.FirstOrDefault(x => x.Username == username);

            _marketplaceContext.Users.Remove(user);

            _marketplaceContext.SaveChanges();
        }
示例#3
0
 public void Delete(T Entity)
 {
     try{
         _context.Set <T>().Remove(Entity);
         _context.SaveChanges();
     }catch (Exception ex) {
         throw ex;
     }
 }
        public ActionResult Create([Bind(Include = "Id,ShippingAddress,Fullname,ProductId")] Order order)
        {
            if (ModelState.IsValid)
            {
                db.Orders.Add(order);
                db.SaveChanges();
                return(RedirectToAction("Index"));
            }

            ViewBag.ProductId = new SelectList(db.Products, "Id", "Make", order.ProductId);
            return(View(order));
        }
        private async Task SeedCartWithOneProduct()
        {
            var cart = await _marketplaceContext.Cart.FirstAsync(x => x.CustomerUsername.Equals("test"));

            var fetchedProduct = await _marketplaceContext.Product.FirstAsync(x => x.Id == 1);

            CartProduct cp = new CartProduct()
            {
                Cart    = cart,
                Product = fetchedProduct
            };

            _marketplaceContext.CartProducts.Add(cp);
            _marketplaceContext.SaveChanges();
        }
        public ActionResult Edit(Category category, HttpPostedFileBase upload)
        {
            if (ModelState.IsValid)
            {
                using (var database = new MarketplaceContext())
                {
                    //if (upload != null && upload.ContentLength > 0)
                    //{
                    //    if (category.Files.Any(f => f.FileType == FileType.Avatar))
                    //    {
                    //        database.Files.Remove(category.Files.First(f => f.FileType == FileType.Avatar));
                    //    }
                    //    var avatar = new File
                    //    {
                    //        FileName = System.IO.Path.GetFileName(upload.FileName),
                    //        FileType = FileType.Avatar,
                    //        ContentType = upload.ContentType
                    //    };
                    //    using (var reader = new System.IO.BinaryReader(upload.InputStream))
                    //    {
                    //        avatar.Content = reader.ReadBytes(upload.ContentLength);
                    //    }
                    //    category.Files = new List<File> { avatar };
                    //}
                    database.Entry(category).State = EntityState.Modified;
                    database.SaveChanges();

                    return(RedirectToAction("Index"));
                }
            }
            return(View(category));
        }
示例#7
0
        public ActionResult AddProduct(int entityId, Product product)
        {
            if (!authorizationService.HasRole(entityId, EntityRole.Owner))
            {
                return(Unauthorized());
            }

            if (!ModelState.IsValid)
            {
                return(BadRequest());
            }

            context.Products.Add(product);
            context.SaveChanges();
            return(Created("", product));
        }
示例#8
0
        public bool LeaveComment(Comment comment)
        {
            MarketplaceContext context = new MarketplaceContext();

            context.Comments.Add(comment);
            return(context.SaveChanges() > 0);
        }
示例#9
0
        public bool AddBid(Bid bid)
        {
            MarketplaceContext context = new MarketplaceContext();

            context.Bids.Add(bid);
            return(context.SaveChanges() > 0);
        }
        public ActionResult DeleteConfirmed(int?id)
        {
            if (id == null)
            {
                return(new HttpStatusCodeResult(HttpStatusCode.BadRequest));
            }

            using (var database = new MarketplaceContext())
            {
                // Get article from database
                var products = database.Products
                               .Where(a => a.Id == id)
                               .Include(c => c.Category)
                               .Include(c => c.Files)
                               .First();

                // Check if article exists
                if (products == null)
                {
                    return(HttpNotFound());
                }

                // Delete article from database
                database.Files.Remove(products.Files.First(f => f.FileType == FileType.Avatar));
                database.Products.Remove(products);
                database.SaveChanges();

                // Redirect to index page
                return(RedirectToAction("Index"));
            }
        }
        public ActionResult Create(Category category, HttpPostedFileBase upload)
        {
            if (ModelState.IsValid)
            {
                using (var database = new MarketplaceContext())
                {
                    //if (upload == null)
                    //{
                    //    return RedirectToAction("Create");
                    //}
                    //if (upload != null && upload.ContentLength > 0)
                    //{
                    //    var avatar = new File
                    //    {
                    //        FileName = System.IO.Path.GetFileName(upload.FileName),
                    //        FileType = FileType.Avatar,
                    //        ContentType = upload.ContentType
                    //    };
                    //    using (var reader = new System.IO.BinaryReader(upload.InputStream))
                    //    {
                    //        avatar.Content = reader.ReadBytes(upload.ContentLength);
                    //    }
                    //    category.Files = new List<File> { avatar };
                    //}

                    database.Categories.Add(category);
                    database.SaveChanges();

                    return(RedirectToAction("Index"));
                }
            }
            return(View(category));
        }
        public void DeleteAuction(Auction auction)
        {
            MarketplaceContext context = new MarketplaceContext();

            context.Entry(auction).State = System.Data.Entity.EntityState.Deleted;

            context.SaveChanges();
        }
        public void SaveAuction(Auction auction)
        {
            MarketplaceContext context = new MarketplaceContext();

            context.Auctions.Add(auction);

            context.SaveChanges();
        }
        public void DeleteCategory(Category category)
        {
            MarketplaceContext context = new MarketplaceContext();

            context.Entry(category).State = System.Data.Entity.EntityState.Deleted;

            context.SaveChanges();
        }
        public void SaveCategory(Category category)
        {
            MarketplaceContext context = new MarketplaceContext();

            context.Categories.Add(category);

            context.SaveChanges();
        }
示例#16
0
        public int SavePicture(Picture picture)
        {
            MarketplaceContext context = new MarketplaceContext();

            context.Pictures.Add(picture);
            context.SaveChanges();

            return(picture.ID);
        }
示例#17
0
        private void Seed()
        {
            var customer = new Customer()
            {
                Username = "******", Password = "******"
            };

            _marketplaceContext.Add(customer);
            _marketplaceContext.SaveChanges();
        }
        public ActionResult Edit(ProductViewModel model, HttpPostedFileBase upload)
        {
            // Check if model state is valid
            if (ModelState.IsValid)
            {
                using (var database = new MarketplaceContext())
                {
                    // Get article from database
                    var product = database.Products
                                  .FirstOrDefault(a => a.Id == model.Id);

                    // Set article properties
                    model.Id          = product.Id;
                    model.Model       = product.Model;
                    model.Make        = product.Make;
                    model.Material    = product.Material;
                    model.Price       = product.Price;
                    model.Description = product.Description;
                    model.Categories  = database.Categories
                                        .OrderBy(c => c.Title)
                                        .ToList();
                    model.Files = product.Files;

                    if (upload != null && upload.ContentLength > 0)
                    {
                        if (product.Files.Any(f => f.FileType == FileType.Avatar))
                        {
                            database.Files.Remove(product.Files.First(f => f.FileType == FileType.Avatar));
                        }
                        var avatar = new File
                        {
                            FileName    = System.IO.Path.GetFileName(upload.FileName),
                            FileType    = FileType.Avatar,
                            ContentType = upload.ContentType
                        };
                        using (var reader = new System.IO.BinaryReader(upload.InputStream))
                        {
                            avatar.Content = reader.ReadBytes(upload.ContentLength);
                        }
                        product.Files = new List <File> {
                            avatar
                        };
                    }

                    // Save article state in database
                    database.Entry(product).State = EntityState.Modified;
                    database.SaveChanges();
                }
            }

            // Redirect to the index page
            return(RedirectToAction("Index"));
        }
        public void UpdateAuction(Auction auction)
        {
            MarketplaceContext context = new MarketplaceContext();

            var existingAuction = context.Auctions.Find(auction.ID);

            context.AuctionPictures.RemoveRange(existingAuction.AuctionPictures);

            context.Entry(existingAuction).CurrentValues.SetValues(auction);

            context.AuctionPictures.AddRange(auction.AuctionPictures);

            context.SaveChanges();
        }
        private void Seed()
        {
            var category = new Category()
            {
                Id = 1, Name = "Computers"
            };
            var category1 = new Category()
            {
                Id = 2, Name = "Toys"
            };

            _marketplaceContext.Category.Add(category);
            _marketplaceContext.Category.Add(category1);
            _marketplaceContext.SaveChanges();
        }
示例#21
0
        private void Seed()
        {
            var customer = new Customer()
            {
                Username = "******", Password = "******"
            };
            var cart = new Cart()
            {
                Id = 1, CustomerUsername = "******"
            };
            var category = new Category()
            {
                Id = 1, Name = "Computers"
            };
            var category1 = new Category()
            {
                Id = 2, Name = "Toys"
            };

            _marketplaceContext.Category.Add(category);
            _marketplaceContext.Category.Add(category1);
            var product = new Product()
            {
                Id           = 1,
                Name         = "ACER G502",
                Description  = "Lorem",
                Price        = 350,
                ThumbnailUrl =
                    "https://res.cloudinary.com/dxfq3iotg/image/upload/v1571750967/Ecommerce/ef192a21ec96.jpg",
                Stock      = 19,
                CategoryId = 1
            };
            var product2 = new Product()
            {
                Id           = 2, Name = "ACER Predator", Description = "Lorem", Price = 1350,
                ThumbnailUrl = "https://www.komplett.dk/img/p/1200/1168528.jpg", Stock = 5, CategoryId = 1
            };

            _marketplaceContext.Customer.Add(customer);
            _marketplaceContext.Cart.Add(cart);
            _marketplaceContext.Product.Add(product);
            _marketplaceContext.Product.Add(product2);
            _marketplaceContext.SaveChanges();
        }
        public ActionResult DeleteConfirmed(int?id)
        {
            using (var database = new MarketplaceContext())
            {
                var category = database.Categories
                               .FirstOrDefault(c => c.Id == id);
                var categoryArticles = category.Products
                                       .ToList();

                foreach (var article in categoryArticles)
                {
                    database.Products.Remove(article);
                }
                //database.Files.Remove(category.Files.First(f => f.FileType == FileType.Avatar));
                database.Categories.Remove(category);
                database.SaveChanges();

                return(RedirectToAction("Index"));
            }
        }
        public ActionResult DeleteConfirmed(string id)
        {
            if (id == null)
            {
                return(new HttpStatusCodeResult(HttpStatusCode.BadRequest));
            }

            using (var database = new MarketplaceContext())
            {
                // Get user from database
                var user = database.Users
                           .Where(u => u.Id.Equals(id))
                           .First();


                // Delete user and save changes
                database.Users.Remove(user);
                database.SaveChanges();

                return(RedirectToAction("List"));
            }
        }
示例#24
0
        public ActionResult Delete(int imageId)
        {
            var foundImage = marketplaceContext.Images.SingleOrDefault(r => r.Id == imageId);

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

            switch (foundImage)
            {
            case EntityImage img:
            {
                if (!authorizationService.HasRole(img.EntityId, EntityRole.Owner) && !authorizationService.HasRole(GlobalRole.Admin))
                {
                    return(Unauthorized());
                }
                break;
            }

            case ProductImage img:
            {
                var productForImage = marketplaceContext.Products.Single(r => r.Id == img.ProductId);
                if (!authorizationService.HasRole(productForImage.EntityId, EntityRole.Owner) && !authorizationService.HasRole(GlobalRole.Admin))
                {
                    return(Unauthorized());
                }
                break;
            }

            default: throw new Exception("Yikes");
            }

            marketplaceContext.Images.Remove(foundImage);
            marketplaceContext.SaveChanges();
            imageService.DeleteImage(imageId);
            return(Ok());
        }
        public ActionResult Edit(string id, EditUserViewModel viewModel)
        {
            //Chek if model is valid
            if (ModelState.IsValid)
            {
                using (var database = new MarketplaceContext())
                {
                    //Get User from database
                    var user = database.Users.FirstOrDefault(u => u.Id == id);

                    //Check for user exists
                    if (user == null)
                    {
                        return(HttpNotFound());
                    }

                    //IF password fiesld is not empty, change password
                    if (!string.IsNullOrEmpty(viewModel.Password))
                    {
                        var hasher       = new PasswordHasher();
                        var passwordHash = hasher.HashPassword(viewModel.Password);
                        user.PasswordHash = passwordHash;
                    }

                    //set user properties
                    user.Email    = viewModel.User.Email;
                    user.UserName = viewModel.User.Email;
                    this.SetUserRoles(viewModel, user, database);

                    //Save changes
                    database.Entry(user).State = EntityState.Modified;
                    database.SaveChanges();

                    return(RedirectToAction("List"));
                }
            }
            return(View(viewModel));
        }
        public ActionResult Create(ProductViewModel model, HttpPostedFileBase upload)
        {
            if (!ModelState.IsValid)
            {
                return(RedirectToAction("Create"));
            }

            using (var database = new MarketplaceContext())
            {
                var product = new Product(model.Make, model.Model, model.Size, model.Material, model.Price, model.Description, model.CategoryId);


                //uploading photo
                if (upload != null && upload.ContentLength > 0)
                {
                    var avatar = new File
                    {
                        FileName    = System.IO.Path.GetFileName(upload.FileName),
                        FileType    = FileType.Avatar,
                        ContentType = upload.ContentType
                    };
                    using (var reader = new System.IO.BinaryReader(upload.InputStream))
                    {
                        avatar.Content = reader.ReadBytes(upload.ContentLength);
                    }
                    product.Files = new List <File> {
                        avatar
                    };
                }

                database.Products.Add(product);
                database.SaveChanges();

                return(RedirectToAction("Index"));
            }
        }
示例#27
0
        public static void Initialize(IServiceProvider serviceProvider)
        {
            using (var context = new MarketplaceContext(
                       serviceProvider.GetRequiredService <DbContextOptions <MarketplaceContext> >()))
            {
                context.Trucks.AddRange(
                    new TruckDb()
                {
                    Id           = 1,
                    Km           = 30,
                    LicensePlate = "ABC-1234",
                    Offers       = new List <OfferDb>()
                    {
                        new OfferDb()
                        {
                            Value = 5, TruckId = 1
                        },
                        new OfferDb()
                        {
                            Value = 10, TruckId = 1
                        }
                    }
                },
                    new TruckDb()
                {
                    Id           = 2,
                    Km           = 32424,
                    LicensePlate = "GHF-5435"
                },
                    new TruckDb()
                {
                    Id           = 3,
                    Km           = 543543543,
                    LicensePlate = "THH-5544"
                });

                context.Contacts.AddRange(

                    new ContactDb()
                {
                    Id      = 2,
                    Name    = "Luis",
                    Phone   = "423894389249",
                    TruckId = 1
                },
                    new ContactDb()
                {
                    Id      = 3,
                    Name    = "Luis",
                    Phone   = "423894389249",
                    TruckId = 2
                },
                    new ContactDb()
                {
                    Id      = 4,
                    Name    = "Someone",
                    Phone   = "563465464564",
                    TruckId = 2
                },
                    new ContactDb()
                {
                    Id      = 5,
                    Name    = "Eric Evans",
                    Phone   = "654654646544",
                    TruckId = 2
                }, new ContactDb()
                {
                    Id      = 6,
                    Name    = "Teste",
                    Phone   = "423894389249",
                    TruckId = 3
                },
                    new ContactDb()
                {
                    Id      = 7,
                    Name    = "Someone again",
                    Phone   = "563465464564",
                    TruckId = 3
                },
                    new ContactDb()
                {
                    Id      = 8,
                    Name    = "Vough Vernon",
                    Phone   = "654654646544",
                    TruckId = 3
                });

                context.SaveChanges();
            }
        }
示例#28
0
 public int SaveChanges()
 {
     return(Db.SaveChanges());
 }
示例#29
0
 public void Commit() => _context.SaveChanges();
示例#30
0
 public void Create(Notification notification)
 {
     notifications.Add(notification);
     ctx.SaveChanges();
 }