Ejemplo n.º 1
0
        public IActionResult Create([FromBody] Cart cart)
        {
            if (cart == null)
            {
                return(BadRequest());
            }

            // Check if user exists
            User user = _context.Users.FirstOrDefault(t => t.UserId == cart.UserId);

            if (user == null)
            {
                return(NotFound("User not found: " + user.UserId));
            }

            // Check if the products exist
            string[] productIdentifiers = cart.ProductIds.Split(",");
            for (int i = 0; i < productIdentifiers.Length; i++)
            {
                Product tmp = _context.Products.FirstOrDefault(t => t.ProductId == int.Parse(productIdentifiers[i]));
                if (tmp == null)
                {
                    return(NotFound("Product not found : " + productIdentifiers[i]));
                }
            }

            _context.Carts.Add(cart);
            _context.SaveChanges();

            return(CreatedAtRoute("GetCart", new { id = cart.CartId }, cart));
        }
        public IActionResult Create([FromBody] Category category)
        {
            if (category == null)
            {
                return(BadRequest());
            }

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

            return(CreatedAtRoute("GetCategory", new { id = category.CategoryId }, category));
        }
Ejemplo n.º 3
0
        public IActionResult Create([FromBody] User user)
        {
            if (user == null)
            {
                return(BadRequest());
            }

            // TODO : No time to implement proper encryption but you get the idea
            user.Token = user.Firstname + user.Lastname;

            _context.Users.Add(user);
            _context.SaveChanges();

            return(CreatedAtRoute("GetUser", new { id = user.UserId }, user));
        }
        public IActionResult Create([FromBody] Product product)
        {
            if (product == null)
            {
                return(BadRequest("Couldn't create product from request body"));
            }

            // Check if category exists
            Category category = _context.Categories.FirstOrDefault(t => t.CategoryId == product.CategoryId);

            if (category == null)
            {
                return(NotFound("Missing Category"));
            }

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

            return(CreatedAtRoute("GetProduct", new { id = product.ProductId }, product));
        }
Ejemplo n.º 5
0
        private static void addMockData(NetshopDbContext context)
        {
            // Mock users
            context.Users.Add(new User {
                Login = "******", Password = "******", Firstname = "Marc", Lastname = "Picaud", Admin = true, Token = "admin"
            });
            context.Users.Add(new User {
                Login = "******", Password = "******", Firstname = "Jean", Lastname = "Dupont", Admin = false, Token = "client"
            });

            // Mock categories
            context.Categories.Add(new Category {
                Name = "category 1"
            });
            context.Categories.Add(new Category {
                Name = "category 2"
            });

            // Mock products
            context.Products.Add(new Product {
                Name = "product 1", Price = 4.2
            });
            context.Products.Add(new Product {
                Name = "product 2", Price = 8.4
            });

            // Mock carts
            context.Carts.Add(new Cart {
                UserId = 1, ProductIds = "1,2"
            });
            context.Carts.Add(new Cart {
                UserId = 2, ProductIds = "1"
            });

            // Mock orders
            // TODO

            context.SaveChanges();
        }