Beispiel #1
0
        public HttpResponseMessage PostProduct(Product product)
        {
            var createdProduct = ProductServices.CreateProduct(product);

            // If product already exists within database, return 409.
            if (createdProduct == null)
            {
                var alreadyExistsResponse = Request.CreateResponse
                                                (HttpStatusCode.Conflict, "(409) Product already exists", Configuration.Formatters.JsonFormatter);

                alreadyExistsResponse.Headers.Add("API-Version", apiVersion);
                alreadyExistsResponse.Headers.Add("ERROR", "(409) Resource already exists");

                return(alreadyExistsResponse);
            }
            // Product doesn't exist inside the database, proceed.
            else
            {
                var response = Request.CreateResponse(HttpStatusCode.Created, product, Configuration.Formatters.JsonFormatter);
                response.Headers.Add("API-Version", apiVersion);
                response.Headers.Add("HTTP-Method", "POST");

                if (Request.RequestUri.AbsoluteUri.EndsWith("/"))
                {
                    response.Headers.Add("New-Product-URL", Request.RequestUri.AbsoluteUri + createdProduct.ID);
                }
                else
                {
                    response.Headers.Add("New-Product-URL", Request.RequestUri.AbsoluteUri + "/" + createdProduct.ID);
                }
                return(response);
            }
        }
 public ActionResult <ResponseContext> Create(Product product)
 {
     try
     {
         if ((bool)HttpContext.Items["isLoggedInOtherDevice"])
         {
             return(Ok(new ResponseContext
             {
                 code = (int)Common.ResponseCode.IS_LOGGED_IN_ORTHER_DEVICE,
                 message = Common.Message.IS_LOGGED_IN_ORTHER_DEVICE,
                 data = null
             }));
         }
         var newProduct = _productServices.CreateProduct(product);
         return(Ok(new ResponseContext
         {
             code = (int)Common.ResponseCode.SUCCESS,
             message = Common.Message.SUCCESS,
             data = newProduct
         }));
     }
     catch (Exception ex)
     {
         _logger.LogError(ex, ex.Message);
         return(StatusCode(StatusCodes.Status500InternalServerError, new ResponseMessage {
             status = "ERROR", message = ex.Message
         }));
     }
 }
Beispiel #3
0
        public async Task <IActionResult> ProductRegister([FromBody] RegisterProductRequest request)
        {
            var createStore = await _service.CreateProduct(request);

            if (createStore is null)
            {
                return(BadRequest());
            }
            return(Ok());
        }
        public ActionResult Create([Bind(Include = "Id,Name,Cost,Price,Quantity")] ProductDto productDto)
        {
            if (ModelState.IsValid)
            {
                _productAppService.CreateProduct(productDto);
                return(RedirectToAction("Index"));
            }

            return(View(productDto));
        }
Beispiel #5
0
 public IActionResult CreateProduct([FromBody] ProductDto productDto)
 {
     try
     {
         return(Ok(_productServices.CreateProduct(productDto)));
     }
     catch (ArgumentException e)
     {
         return(BadRequest(e.Message));
     }
 }