Пример #1
0
        public ContentResult AddProduct(ProductInput inputData)
        {
            try
            {
                var pro = dbContext.Products.FirstOrDefault(x => x.ProductName == inputData.ProductName);
                if (pro is null)
                {
                    var product = new Product()
                    {
                        ProductId   = Guid.NewGuid(),
                        ProductName = inputData.ProductName,
                        Quantity    = inputData.Quantity,
                        IsActive    = true
                    };

                    dbContext.Products.Add(product);
                    var result = dbContext.SaveChanges();

                    if (result is 1)
                    {
                        return(new ContentResult
                        {
                            Content = JsonConvert.SerializeObject("Success"),
                            ContentType = "application/json",
                            StatusCode = 200
                        });
                    }
                    else
                    {
                        return(new ContentResult
                        {
                            Content = JsonConvert.SerializeObject("Fail"),
                            ContentType = "application/json",
                            StatusCode = 204
                        });
                    }
                }
                else
                {
                    return(new ContentResult
                    {
                        Content = JsonConvert.SerializeObject("Entered Product Name Already Exists"),
                        ContentType = "application/json",
                        StatusCode = 204
                    });
                }
            }
            catch (Exception ex)
            {
                return(new ContentResult
                {
                    Content = JsonConvert.SerializeObject(ex.InnerException.ToString()),
                    ContentType = "application/json",
                    StatusCode = 417
                });
            }
        }
        public ContentResult AddOrder(OrderInput inputData)
        {
            try
            {
                var order = new Order()
                {
                    OrderId    = Guid.NewGuid(),
                    ProductId  = inputData.ProductId,
                    CustomerId = inputData.CustomerId,
                    Quantity   = inputData.Quantity,
                    IsCancel   = false,
                };

                dbContext.Orders.Add(order);

                var getProduct = dbContext.Products.Where(x => x.ProductId == inputData.ProductId).FirstOrDefault();

                if (getProduct is null || inputData.Quantity > getProduct.Quantity)
                {
                    return(new ContentResult
                    {
                        Content = JsonConvert.SerializeObject("Product Unavailable"),
                        ContentType = "application/json",
                        StatusCode = 204
                    });
                }
                else
                {
                    getProduct.Quantity -= inputData.Quantity; //Reduced Product Quantity
                    dbContext.Products.Update(getProduct);     //Update Product

                    var result = dbContext.SaveChanges();

                    if (result is 2)
                    {
                        return(new ContentResult
                        {
                            Content = JsonConvert.SerializeObject("Success"),
                            ContentType = "application/json",
                            StatusCode = 200
                        });
                    }
                    return(new ContentResult
                    {
                        Content = JsonConvert.SerializeObject("Fail"),
                        ContentType = "application/json",
                        StatusCode = 204
                    });
                }
            }
Пример #3
0
        public ContentResult AddCustomer(CustomerInput inputData)
        {
            try
            {
                var customer = new Customer()
                {
                    CustomerId   = Guid.NewGuid(),
                    CustomerName = inputData.CustomerName,
                    EmailID      = inputData.EmailID,
                    Mobile       = inputData.Mobile
                };

                dbContext.Customers.Add(customer);
                var result = dbContext.SaveChanges();

                if (result is 1)
                {
                    return(new ContentResult
                    {
                        Content = JsonConvert.SerializeObject("Success"),
                        ContentType = "application/json",
                        StatusCode = 200
                    });
                }
                else
                {
                    return(new ContentResult
                    {
                        Content = JsonConvert.SerializeObject("Fail"),
                        ContentType = "application/json",
                        StatusCode = 204
                    });
                }
            }
            catch (Exception ex)
            {
                return(new ContentResult
                {
                    Content = JsonConvert.SerializeObject(ex.InnerException.ToString()),
                    ContentType = "application/json",
                    StatusCode = 417
                });
            }
        }