Esempio n. 1
0
        public HttpResponseMessage Create(HttpRequestMessage request, PriceViewModel priceCategoryVm)
        {
            return(CreateHttpResponse(request, () =>
            {
                HttpResponseMessage response = null;
                if (!ModelState.IsValid)
                {
                    response = request.CreateResponse(HttpStatusCode.BadRequest, ModelState);
                }
                else
                {
                    var newprice = new Price();
                    newprice.UpdatePrice(priceCategoryVm);

                    var PriceExists = _priceService.GetPrice(newprice.ProductID, newprice.SizeID, newprice.ColorID);
                    if (PriceExists != null && PriceExists.ID > 0)
                    {
                        response = request.CreateResponse(HttpStatusCode.BadGateway, new { ErrorMesage = "Sản phẩm đã được làm giá." });
                    }
                    else
                    {
                        _priceService.Add(newprice);
                        if (newprice.IsPrimary)
                        {
                            var pricePrimary = new Price();
                            pricePrimary = _priceService.GetByPrimary(newprice.ProductID);
                            if (pricePrimary != null && pricePrimary.ProductID > 0)
                            {
                                pricePrimary.IsPrimary = false;
                                _priceService.Update(pricePrimary);
                            }
                        }
                        else
                        {
                            var priceProduct = _priceService.GetPriceProduct(newprice.ProductID);
                            if (priceProduct == null || priceProduct.Count() == 0)
                            {
                                newprice.IsPrimary = true;
                            }
                        }
                        _priceService.SaveChanges();

                        var responseData = Mapper.Map <Price, PriceViewModel>(newprice);
                        response = request.CreateResponse(HttpStatusCode.Created, responseData);
                    }
                }

                return response;
            }));
        }