public IActionResult Post([FromBody] RegisterUserCred registerUserCred)
        {
            if (dbContext.Users.Any(x => x.Username == registerUserCred.Username))
            {
                return(Conflict(new { message = $"An existing user with the same username was already found." }));
            }

            var user = new User
            {
                Username     = registerUserCred.Username,
                Password     = registerUserCred.Password,
                CurrencyCode = registerUserCred.CurrencyCode
            };

            if (user.CurrencyCode == null)
            {
                return(Conflict(new { message = $"Invalid/empty currency code in body, please re-check your data." }));
            }

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

            var token = jwtAuthenticationManager.Authenticate(registerUserCred.Username, registerUserCred.Password);

            return(Ok("Your user has been created. Bearer token: " + token));
        }
        public async Task <ActionResult <Order> > ChangeOrderStatus(int orderId, [FromBody] string status)
        {
            var order = dbContext.Orders.FirstOrDefault(x => x.Id == orderId);

            order.Status = (Status)Enum.Parse(typeof(Status), status);

            dbContext.SaveChanges();
            return(CreatedAtAction("ChangeOrderStatus", order));
        }
        static void Main(string[] args)
        {
            string dbName = "eCommerceDatabase.db";

            if (File.Exists(dbName))
            {
                File.Delete(dbName);
            }
            using (var dbContext = new eCommerceDbContext())
            {
                dbContext.Database.EnsureCreated();

                var json        = File.ReadAllText("../../../products.json");
                var productDtos = JsonConvert.DeserializeObject <ImportProductsDto[]>(json);

                var products = new List <Product>();

                foreach (var productDto in productDtos)
                {
                    var product = new Product
                    {
                        Name     = productDto.Name,
                        ImageUrl = productDto.Image,
                        Price    = productDto.Price
                    };

                    products.Add(product);
                }

                dbContext.Products.AddRange(products);

                foreach (var product in dbContext.Products)
                {
                    Console.WriteLine($"product name: {product.Name}, product price: {product.Price}");
                }

                var user = new User
                {
                    Username     = "******",
                    Password     = "******",
                    CurrencyCode = "EUR"
                };

                dbContext.Users.Add(user);
                dbContext.SaveChanges();
            }
        }
Exemplo n.º 4
0
        public async Task <IActionResult> CreateProduct([FromBody] Product product)
        {
            if (dbContext.Products.Any(x => x.Name == product.Name))
            {
                return(Conflict(new { message = $"A product with the same name already exists." }));
            }

            if (string.IsNullOrEmpty(product.Name))
            {
                return(Conflict(new { message = $"Name cannot be null or empty." }));
            }

            if (product.Price <= 0)
            {
                return(Conflict(new { message = $"Price cannot be less or equal to zero." }));
            }

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

            return(Ok($"Your product {product.Name} has been successfully added."));
        }