Пример #1
0
        public HttpResponseMessage Post([FromBody] AddEditProductVM objProduct)
        {
            try
            {
                var product = Mapper.Map <Product>(objProduct);
                productService.CreateProduct(product);
                productService.SaveProduct();

                var message = Request.CreateResponse <Product>(HttpStatusCode.Created, product);
                message.Headers.Location = new Uri(Request.RequestUri + product.ProductID.ToString());
                return(message);
            }
            catch (Exception ex)
            {
                return(Request.CreateErrorResponse(HttpStatusCode.InternalServerError, "Error occured during creating product"));
            }
        }
Пример #2
0
        public HttpResponseMessage Put(int id, [FromBody] AddEditProductVM objProduct)
        {
            try
            {
                var product = productService.GetProduct(id);

                if (product == null)
                {
                    return(Request.CreateResponse <Product>(HttpStatusCode.NotFound, product));
                }
                Mapper.Map <AddEditProductVM, Product>(objProduct, product);
                productService.Update(product);
                productService.SaveProduct();

                return(Request.CreateResponse <Product>(HttpStatusCode.OK, null));
            }
            catch (Exception ex)
            {
                return(Request.CreateErrorResponse(HttpStatusCode.InternalServerError, "Error occured during updating product"));
            }
        }
Пример #3
0
        public HttpResponseMessage GetByID(int id)
        {
            try
            {
                var product = productService.GetProduct(id);

                AddEditProductVM obj = new AddEditProductVM();

                obj = Mapper.Map <Product, AddEditProductVM>(product);

                if (obj != null)
                {
                    return(Request.CreateResponse <AddEditProductVM>(HttpStatusCode.OK, obj));
                }
                else
                {
                    return(Request.CreateErrorResponse(HttpStatusCode.NotFound, "No Products found"));
                }
            }
            catch (Exception ex)
            {
                return(Request.CreateErrorResponse(HttpStatusCode.InternalServerError, "Error occured during processing GetAll "));
            }
        }