public HttpResponseMessage Delete(int id)
        {
            HttpResponseMessage response;
            ModelsValidation    validation = new ModelsValidation();
            ProductData         data       = new ProductData();

            if (validation.DoesProductExist(id) == false)
            {
                response = Request.CreateResponse(HttpStatusCode.NotFound, new { Message = "There is no product with given Id parameter." });
            }
            else
            {
                try
                {
                    data.DeleteProductById(id);
                    response = Request.CreateResponse(HttpStatusCode.NoContent);
                }
                catch (SqlException sqlException)
                {
                    response = CheckSqlExceptionNumber(sqlException.Number);
                }
            }

            return(response);
        }
        public HttpResponseMessage Put([System.Web.Mvc.Bind(Include = "Id")] int id, [System.Web.Mvc.Bind(Include = "Name, Description,CategoryId, BrandId")] ProductModel product)
        {
            HttpResponseMessage response;
            ModelsValidation    validation = new ModelsValidation();
            ProductData         data       = new ProductData();

            if (ModelState.IsValid)
            {
                if (validation.DoesProductExist(id) == false)
                {
                    response = Request.CreateResponse(HttpStatusCode.NotFound, new { Message = "There is no product with given Id parameter." });
                }
                else
                {
                    response = CanAddProduct(product.CategoryId, product.BrandId);

                    if (response.StatusCode == HttpStatusCode.NoContent)
                    {
                        data.UpdateProductById(id, product);
                    }
                }
            }
            else
            {
                response = Request.CreateResponse(HttpStatusCode.NotFound, new { Message = "Model is invalid.", ModelValidation = "Name must be between 2 - 100 signs. Category and brand id must be greater than 0." });
            }

            return(response);
        }
        public HttpResponseMessage Put(int id, [FromBody][System.Web.Mvc.Bind(Include = "Category")] CategoryModel Category)
        {
            HttpResponseMessage response;
            ModelsValidation    validation = new ModelsValidation();
            CategoryData        data       = new CategoryData();

            if (ModelState.IsValid)
            {
                if (validation.DoesCategoryExist(id) == false)
                {
                    response = Request.CreateResponse(HttpStatusCode.NotFound, new { Message = "There is no category with given Id parameter." });
                }
                else
                {
                    data.UpdateCategoryById(id, Category.Category);
                    response = Request.CreateResponse(HttpStatusCode.NoContent);
                }
            }
            else
            {
                response = Request.CreateResponse(HttpStatusCode.NotFound, new { Message = "Model is invalid.", ModelValidation = "Category name must be between 2 - 64 signs. Cannot be null or empty." });
            }

            return(response);
        }
Exemple #4
0
        public HttpResponseMessage Post(VariantModel variant)
        {
            HttpResponseMessage response;
            ModelsValidation    validation = new ModelsValidation();
            VariantData         data       = new VariantData();

            if (ModelState.IsValid)
            {
                if (validation.DoesProductExist(variant.ProductId) == true)
                {
                    data.SaveVariant(variant);
                    response = Request.CreateResponse(HttpStatusCode.Created);
                }
                else
                {
                    response = Request.CreateResponse(HttpStatusCode.NotFound, new { Message = "There is no product with given Id parameter." });
                }
            }
            else
            {
                response = Request.CreateResponse(HttpStatusCode.NotFound, new { Message = "Model is invalid.", ModelValidation = "Product's id must be greater than 0. BasePrice, Tax and InStock must be a positive numbers." });
            }

            return(response);
        }
Exemple #5
0
        public HttpResponseMessage Put(int id, [FromBody][System.Web.Mvc.Bind(Include = "ProductId, BasePrice, Tax, InStock")] VariantModel variant)
        {
            HttpResponseMessage response;
            ModelsValidation    validation = new ModelsValidation();
            VariantData         data       = new VariantData();

            if (ModelState.IsValid)
            {
                if (validation.DoesVariantExist(id) == false)
                {
                    response = Request.CreateResponse(HttpStatusCode.NotFound, new { Message = "There is no variant with given Id parameter." });
                }
                else
                {
                    if (validation.DoesProductExist(variant.ProductId) == false)
                    {
                        response = Request.CreateResponse(HttpStatusCode.NotFound, new { Message = "There is no product with given Id parameter." });
                    }
                    else
                    {
                        data.UpdateVariantById(id, variant);
                        response = Request.CreateResponse(HttpStatusCode.NoContent);
                    }
                }
            }
            else
            {
                response = Request.CreateResponse(HttpStatusCode.NotFound, new { Message = "Model is invalid.", ModelValidation = "Product's id must be greater than 0. BasePrice, Tax and InStock must be a positive numbers." });
            }

            return(response);
        }
        public HttpResponseMessage Put(int id, [FromBody][System.Web.Mvc.Bind(Include = "Brand")] BrandModel Brand)
        {
            HttpResponseMessage response;
            ModelsValidation    validation = new ModelsValidation();
            BrandData           data       = new BrandData();

            if (ModelState.IsValid)
            {
                if (validation.DoesBrandExist(id) == false)
                {
                    response = Request.CreateResponse(HttpStatusCode.NotFound, new { Message = "There is no brand with given Id parameter." });
                }
                else
                {
                    data.UpdateBrandById(id, Brand.Brand);
                    response = Request.CreateResponse(HttpStatusCode.NoContent);
                }
            }
            else
            {
                response = Request.CreateResponse(HttpStatusCode.NotFound, new { Message = "Model is invalid.", ModelValidation = "Brand name must be between 2 - 64 signs. Cannot be null or empty." });
            }

            return(response);
        }
        private HttpResponseMessage CanAddProduct(int category, int brand)
        {
            HttpResponseMessage response;

            ModelsValidation validation = new ModelsValidation();

            int result = 0;

            if (validation.DoesBrandExist(brand) == false)
            {
                result += 2;
            }

            if (validation.DoesCategoryExist(category) == false)
            {
                result += 1;
            }

            switch (result)
            {
            case 0:
                response = Request.CreateResponse(HttpStatusCode.NoContent);
                break;

            case 1:
                response = Request.CreateResponse(HttpStatusCode.NotFound, new { Message = "There is no category with given Id parameter." });
                break;

            case 2:
                response = Request.CreateResponse(HttpStatusCode.NotFound, new { Message = "There is no brand with given Id parameter." });
                break;

            default:
                response = Request.CreateResponse(HttpStatusCode.NotFound, new { Message = "There are no category and brand with given Id parameters." });
                break;
            }

            return(response);
        }