public HttpResponseMessage Create(HttpRequestMessage request, ProductQuantityViewModel productQuantityVm)
        {
            if (ModelState.IsValid)
            {
                var newQuantity = new ProductQuantity();
                try
                {
                    if (_productQuantityService.CheckExist(productQuantityVm.ProductId, productQuantityVm.SizeId, productQuantityVm.ColorId))
                    {
                        return(request.CreateErrorResponse(HttpStatusCode.BadRequest, "Màu sắc kích thước cho sản phẩm này đã tồn tại"));
                    }
                    else
                    {
                        newQuantity.UpdateProductQuantity(productQuantityVm);

                        _productQuantityService.Add(newQuantity);
                        _productQuantityService.Save();
                        return(request.CreateResponse(HttpStatusCode.OK, productQuantityVm));
                    }
                }
                catch (Exception dex)
                {
                    return(request.CreateErrorResponse(HttpStatusCode.BadRequest, dex.Message));
                }
            }
            else
            {
                return(request.CreateErrorResponse(HttpStatusCode.BadRequest, ModelState));
            }
        }
 public HttpResponseMessage Create(HttpRequestMessage request, ProductQuantityViewModel productQuantityVm)
 {
     return(CreateHttpResponse(request, () =>
     {
         HttpResponseMessage response = null;
         if (!ModelState.IsValid)
         {
             response = request.CreateResponse(HttpStatusCode.BadRequest, ModelState);
         }
         else
         {
             var newQuantity = new ProductQuantity();
             if (_productQuantityService.CheckExist(productQuantityVm.ProductId, productQuantityVm.SizeId, productQuantityVm.ColorId))
             {
                 return request.CreateErrorResponse(HttpStatusCode.BadRequest, "Màu sắc kích thước cho sản phẩm này đã tồn tại");
             }
             else
             {
                 newQuantity.UpdateProductQuantity(productQuantityVm);
                 _productQuantityService.Add(newQuantity);
                 _productQuantityService.Save();
             }
         }
         return response;
     }));
 }
        public async Task <IActionResult> Add([FromBody] ProductQuantityViewModel productQuantityVm)
        {
            var hasPermission = await _authorizationService.AuthorizeAsync(User, "PRODUCT", Operations.Create);

            if (hasPermission.Succeeded == false)
            {
                return(new BadRequestObjectResult(CommonConstants.Forbidden));
            }
            if (ModelState.IsValid)
            {
                if (_productQuantityService.CheckExist(productQuantityVm.ProductId, productQuantityVm.SizeId, productQuantityVm.ColorId))
                {
                    return(new BadRequestObjectResult(ModelState));
                }
                _productQuantityService.Add(productQuantityVm);
                _productQuantityService.SaveChanges();
                return(new OkObjectResult(productQuantityVm));
            }
            return(new BadRequestObjectResult(ModelState));
        }
Пример #4
0
        public HttpResponseMessage Create(HttpRequestMessage request, ProductQuantityViewModel productQuantityVm)
        {
            if (ModelState.IsValid)
            {
                var newQuantity            = new ProductQuantity();
                var identity               = (ClaimsIdentity)User.Identity;
                IEnumerable <Claim> claims = identity.Claims;
                try
                {
                    if (_productQuantityService.CheckExist(productQuantityVm.ProductId, productQuantityVm.SizeId, productQuantityVm.ColorId))
                    {
                        return(request.CreateErrorResponse(HttpStatusCode.BadRequest, "Màu sắc kích thước cho sản phẩm này đã tồn tại"));
                    }
                    else
                    {
                        newQuantity.UpdateProductQuantity(productQuantityVm);

                        _productQuantityService.Add(newQuantity);
                        _productQuantityService.Save();
                        Log log = new Log()
                        {
                            AppUserId = claims.FirstOrDefault().Value,
                            Content   = Notification.CREATE_PRODUCTQUANTITY,
                            Created   = DateTime.Now
                        };
                        _logService.Create(log);
                        _logService.Save();
                        return(request.CreateResponse(HttpStatusCode.OK, productQuantityVm));
                    }
                }
                catch (Exception dex)
                {
                    return(request.CreateErrorResponse(HttpStatusCode.BadRequest, dex.Message));
                }
            }
            else
            {
                return(request.CreateErrorResponse(HttpStatusCode.BadRequest, ModelState));
            }
        }