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))); }
public ServiceEntityResponse <CustomerProductModel> GetSelectedProductsByIdentificationNumber(string identificationNumber) { ServiceEntityResponse <CustomerProductModel> serviceEntityResponse = new ServiceEntityResponse <CustomerProductModel>(); try { using (CustomerProductContext container = (Context as CustomerProductContext)) { var customerProducts = (from cp in container.CustomerProduct join p in container.Products on cp.ProductId equals p.ProductId join cu in container.Customers on cp.IdentificationNumber equals cu.IdentificationNumber where cp.IdentificationNumber.Equals(identificationNumber) select new CustomerProductModel { Customer = cu, Product = p }).ToList(); if (customerProducts.Any()) { serviceEntityResponse.ResponseCode = (int)Enums.EntityResponseCodes.Successfull; serviceEntityResponse.EntityDataList = customerProducts; } else { serviceEntityResponse.ResponseCode = (int)Enums.EntityResponseCodes.NoRecordFound; } } } catch (Exception ex) { serviceEntityResponse.ResponseCode = (int)Enums.EntityResponseCodes.DbError; serviceEntityResponse.ResponseMessage = ex.InnerException != null ? ex.InnerException.Message : ex.Message; } return(serviceEntityResponse); }
public GenericRepository(DbContext dbContext) { Context = dbContext; _serviceEntityResponse = new ServiceEntityResponse <T>(); _servicePrimitiveResponse = new ServicePrimitiveResponse(); }