Exemplo n.º 1
0
        public IActionResult Post([FromBody] ProductInputModel productInputModel)
        {
            if (productInputModel == null)
            {
                return(BadRequest());
            }

            var product = new Products(productInputModel.Description, productInputModel.Price);

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

            return(CreatedAtAction(nameof(GetbyId), new { id = product.ID }, product));
        }
Exemplo n.º 2
0
        public void DoSale()
        {
            using EcommerceDbContent db = new EcommerceDbContent();
            Customer c  = db.Customers.Find(1);
            Product  p1 = db.Products.Find(2);
            Product  p2 = db.Products.Find(4);

            Order order = new Order
            {
                Customer = c,
                Date     = new DateTime(),
            };
            OrderProduct op = new OrderProduct
            {
                Order         = order,
                OrderQuantity = 2,
                Product       = p1
            };

            OrderProduct op2 = new OrderProduct
            {
                Order         = order,
                OrderQuantity = 3,
                Product       = p2
            };



            db.OrderProducts.Add(op);
            db.OrderProducts.Add(op2);
            db.SaveChanges();
        }
        public ReturnData <Customer> UpdateCustomerById(CustomerDto customerdto, int customerid)
        {
            using (EcommerceDbContent db = new EcommerceDbContent())
            {
                Customer updatedcustomer = db.Customers.Find(customerid);

                if (customerdto.Name != null)
                {
                    updatedcustomer.Name = customerdto.Name;
                }

                if (customerdto.Address != null)
                {
                    updatedcustomer.Address = customerdto.Address;
                }

                if (customerdto.Balance != 0)
                {
                    updatedcustomer.Balance = customerdto.Balance;
                }
                if (customerdto.RegistrationDate != null)
                {
                    updatedcustomer.RegistrationDate = customerdto.RegistrationDate;
                }

                db.SaveChanges();

                return(new ReturnData <Customer>
                {
                    Data = updatedcustomer,
                    Error = (updatedcustomer == null) ? 1 : 0,
                    Description = (updatedcustomer == null) ? "there is no such customer" : "OK"
                });
            }
        }
Exemplo n.º 4
0
        public void Delete(int id)
        {
            using EcommerceDbContent db = new EcommerceDbContent();
            Customer = db.Customers.Find(id);
            if (Customer != null)
            {
                db.Customers.Remove(Customer);
            }

            db.SaveChanges();
        }
Exemplo n.º 5
0
        public Customer CreateCustomer(CustomerDto customerDto)
        {
            Customer c = new Customer
            {
                Name    = customerDto.Name,
                Address = customerDto.Address,
            };

            using EcommerceDbContent db = new EcommerceDbContent();
            db.Customers.Add(c);
            db.SaveChanges();
            return(c);
        }
Exemplo n.º 6
0
        public Product CreateProduct(ProductDto productDto)
        {
            Product p = new Product
            {
                Name          = productDto.Name,
                Price         = productDto.Price,
                StockQuantity = productDto.StockQuantity
            };

            using EcommerceDbContent db = new EcommerceDbContent();
            db.Products.Add(p);
            db.SaveChanges();
            return(p);
        }
        public Product CreateProduct(ProductDto productdto)
        {
            Product p = new Product();

            p.Name          = productdto.Name;
            p.Price         = productdto.Price;
            p.StockQuantity = productdto.OrderQuantity;

            using (EcommerceDbContent db = new EcommerceDbContent())
            {
                db.Products.Add(p);
                db.SaveChanges();
            }
            return(p);
        }
Exemplo n.º 8
0
 public void Create()
 {
     Customer = new Customer
     {
         Name             = "Dimitris",
         Address          = "Athens",
         RegistrationDate = new DateTime(),
         Balance          = 0
     };
     using (EcommerceDbContent db = new EcommerceDbContent())
     {
         db.Customers.Add(Customer);
         db.SaveChanges();
     }
 }
        public Order NewOrderById(int customerid, List <ProductDto> products)
        {
            using (EcommerceDbContent db = new EcommerceDbContent())
            {
                Customer c = db.Customers.Find(customerid);

                Order order = new Order()
                {
                    Customer = c,

                    CustomerId = customerid,

                    Date = DateTime.Now
                };


                List <OrderProduct> orderProducts = new List <OrderProduct>();

                foreach (var product in products)
                {
                    Product orderedProduct = db.Products.Where(p => p.Name.Equals(
                                                                   product.Name,
                                                                   StringComparison.OrdinalIgnoreCase))
                                             .First();


                    OrderProduct orderedproducts = new OrderProduct()
                    {
                        Product       = orderedProduct,
                        OrderQuantity = product.OrderQuantity,
                        OrderId       = order.Id,
                        ProductId     = orderedProduct.Id
                    };

                    orderProducts.Add(orderedproducts);
                    db.OrderProducts.Add(orderedproducts);
                }


                order.OrderProduct = orderProducts;

                db.Orders.Add(order);
                db.SaveChanges();
                return(order);
            }
        }
        public Customer CreateCustomer(CustomerDto customerdto)
        {
            Customer c = new Customer()
            {
                Name             = customerdto.Name,
                Address          = customerdto.Address,
                Balance          = customerdto.Balance,
                RegistrationDate = customerdto.RegistrationDate
            };

            using (EcommerceDbContent db = new EcommerceDbContent())
            {
                db.Customers.Add(c);
                db.SaveChanges();
            }
            return(c);
        }
Exemplo n.º 11
0
        public ReturnData <Customer> UpdateCustomerById(int customerId, CustomerDto customerDto)
        {
            if (customerDto == null)
            {
                return(new ReturnData <Customer>
                {
                    Error = 1001,
                    Description = "No data were given"
                });
            }

            using EcommerceDbContent db = new EcommerceDbContent();
            Customer customer = db.Customers.Find(customerId);

            if (customer == null)
            {
                return(new ReturnData <Customer>
                {
                    Error = 1002,
                    Description = "No such customer Id"
                });
            }

            if (customerDto.Address != null)
            {
                customer.Address = customerDto.Address;
            }
            if (customerDto.Name != null)
            {
                customer.Name = customerDto.Name;
            }

            db.SaveChanges();
            return(new ReturnData <Customer>
            {
                Data = customer,
                Error = 0,
                Description = "No errors"
            });
        }
Exemplo n.º 12
0
        public void Update(CustomerDto customerDto, int id)
        {
            if (customerDto == null)
            {
                return;
            }
            using EcommerceDbContent db = new EcommerceDbContent();
            Customer = db.Customers.Find(id);
            if (Customer != null)
            {
                if (customerDto.Name != null)
                {
                    Customer.Name = customerDto.Name;
                }
                if (customerDto.Address != null)
                {
                    Customer.Address = customerDto.Address;
                }
            }

            db.SaveChanges();
        }
Exemplo n.º 13
0
        public void CreateData()
        {
            using EcommerceDbContent db = new EcommerceDbContent();


            db.Customers.AddRange(
                new List <Customer>
            {
                new Customer
                {
                    Name             = "Eleana",
                    Address          = "Athina",
                    Balance          = 100,
                    RegistrationDate = new DateTime()
                },
                new Customer
                {
                    Name             = "Dimitra",
                    Address          = "Athina",
                    Balance          = 120,
                    RegistrationDate = new DateTime()
                },
                new Customer
                {
                    Name             = "Theodora",
                    Address          = "Athina",
                    Balance          = 98,
                    RegistrationDate = new DateTime()
                },
            }

                );

            db.Products.AddRange(
                new List <Product>
            {
                new Product
                {
                    Name          = "car",
                    Price         = 99,
                    StockQuantity = 4
                },

                new Product
                {
                    Name          = "yacht",
                    Price         = 29,
                    StockQuantity = 20
                },
                new Product
                {
                    Name          = "iphone",
                    Price         = 39,
                    StockQuantity = 2
                },

                new Product
                {
                    Name          = "drone",
                    Price         = 133,
                    StockQuantity = 2
                },
            }

                );

            db.SaveChanges();
        }