예제 #1
0
        public ActionResult <ApiResponse <int> > Delete([FromBody] CustomerProductDeleteRequest request)
        {
            ServicePrimitiveResponse deleteCustomerProductResponse = new ServicePrimitiveResponse();

            //Fluent Validation Controls
            CustomerProductValidator validator = new CustomerProductValidator();
            ValidationResult         results   = validator.Validate(request);

            if (results.IsValid)
            {
                //Check IdentificationNo IsExist
                bool isExistCustomer = _customerRepository.FindFirstBy(p => p.IdentificationNumber.Equals(request.IdentificationNumber)).ResponseCode == (int)EntityResponseCodes.NoRecordFound;
                if (isExistCustomer)
                {
                    return(BadRequest(new ApiResponse {
                        Code = (int)ApiResponseCodes.ValidationError, ErrorMessage = " Identification Number Is Not Exist!"
                    }));
                }

                //Check ProductId IsExist
                bool isExistProduct = _productRepository.Find(request.ProductId).ResponseCode == (int)EntityResponseCodes.NoRecordFound;
                if (isExistProduct)
                {
                    return(BadRequest(new ApiResponse {
                        Code = (int)ApiResponseCodes.ValidationError, ErrorMessage = "Product Id Is Not Exist!"
                    }));
                }


                //Get CustomerProductInfo
                ServiceEntityResponse <Data.Entities.CustomerProduct> serviceEntityResponse = _customerProductRepository.FindFirstBy(p => p.IdentificationNumber.Equals(request.IdentificationNumber) && p.ProductId == request.ProductId);

                if (serviceEntityResponse.ResponseCode == (int)Enums.EntityResponseCodes.Successfull)
                {
                    _customerProductRepository.Delete(serviceEntityResponse.EntityData);

                    deleteCustomerProductResponse = _customerProductRepository.Save();

                    if (deleteCustomerProductResponse.ResponseCode == (int)EntityResponseCodes.Successfull)
                    {
                        return(Ok(new ApiResponse {
                            Code = (int)ApiResponseCodes.Ok
                        }));
                    }
                    else
                    {
                        throw new ApiException((int)ApiResponseCodes.DbError, deleteCustomerProductResponse.ResponseMessage);
                    }
                }
            }

            return(BadRequest(GetValidationErrors(results)));
        }
예제 #2
0
        public ActionResult <ApiResponse <CustomerProductResponse> > Post([FromBody] CustomerProductCreateRequest request)
        {
            ServicePrimitiveResponse insertCustomerProductResult = new ServicePrimitiveResponse();

            //Fluent Validation Controls
            CustomerProductValidator validator = new CustomerProductValidator();
            ValidationResult         results   = validator.Validate(request);

            if (results.IsValid)
            {
                //Check IdentificationNo IsExist
                bool isExistCustomer = _customerRepository.FindFirstBy(p => p.IdentificationNumber.Equals(request.IdentificationNumber)).ResponseCode == (int)EntityResponseCodes.NoRecordFound;
                if (isExistCustomer)
                {
                    return(BadRequest(new ApiResponse {
                        Code = (int)ApiResponseCodes.ValidationError, ErrorMessage = "Identification Number Is Not Exist!"
                    }));
                }

                //Check ProductId IsExist
                bool isExistProduct = _productRepository.Find(request.ProductId).ResponseCode == (int)EntityResponseCodes.NoRecordFound;
                if (isExistProduct)
                {
                    return(BadRequest(new ApiResponse {
                        Code = (int)ApiResponseCodes.ValidationError, ErrorMessage = "Product Id Is Not Exist!"
                    }));
                }

                insertCustomerProductResult = _customerProductRepository.InsertCustomerProduct(request.IdentificationNumber, request.ProductId);

                if (insertCustomerProductResult.ResponseCode == (int)EntityResponseCodes.Successfull)
                {
                    return(Get(request.IdentificationNumber)); //If Success Call Get ProductInfo endpoint
                }
            }
            else
            {
                return(BadRequest(GetValidationErrors(results)));
            }

            throw new ApiException((int)ApiResponseCodes.DbError, insertCustomerProductResult.ResponseMessage);
        }