public ServicePrimitiveResponse Commit(long userId) { ServicePrimitiveResponse primitiveResponse = new ServicePrimitiveResponse(); try { primitiveResponse.EntityPrimaryKey = context.SaveChanges(true).ToString(); primitiveResponse.ResponseCode = ResponseCode.SUCCESSFUL; } //catch (DbEntityValidationException dbEx) //{ // primitiveResponse.ResponseCode = ResponseCodes.DB_VALIDATION_ERROR; // //while (dbEx.InnerException != null) // primitiveResponse.InnerException = dbEx.InnerException; // throw new Exception(dbEx.DbEntityValidationExceptionToString(), dbEx); //} catch (Exception ex) { primitiveResponse.ResponseCode = ResponseCode.DB_VALIDATION_ERROR; //while (ex.InnerException != null) primitiveResponse.InnerException = ex.InnerException; throw new Exception("UnitOfWork Commit Method", ex); } return(primitiveResponse); }
public ServicePrimitiveResponse IncorrectData(string message) { var servicePrimitiveResponse = new ServicePrimitiveResponse { ResponseCode = ResponseCode.INCORRECT_DATA }; servicePrimitiveResponse.Errors.Add(message); return(servicePrimitiveResponse); }
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 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); }
public ServicePrimitiveResponse InsertCustomerProduct(string identificationNumber, int productId) { ServicePrimitiveResponse primitiveResponse = new ServicePrimitiveResponse(); CustomerProductContext context = (Context as CustomerProductContext); try { if (context != null) { Data.Entities.CustomerProduct customerProduct = new Data.Entities.CustomerProduct { IdentificationNumber = identificationNumber, ProductId = productId }; context.CustomerProduct.Add(customerProduct); context.SaveChanges(); primitiveResponse.ResponseCode = (int)Enums.EntityResponseCodes.Successfull; } else { primitiveResponse.ResponseCode = (int)Enums.EntityResponseCodes.DbError; } } catch (Exception ex) { primitiveResponse.ResponseCode = (int)Enums.EntityResponseCodes.DbError; primitiveResponse.InnerException = ex.InnerException; primitiveResponse.ResponseMessage = ex.InnerException != null ? ex.InnerException.Message : ex.Message; } return(primitiveResponse); }
public GenericRepository(DbContext dbContext) { Context = dbContext; _serviceEntityResponse = new ServiceEntityResponse <T>(); _servicePrimitiveResponse = new ServicePrimitiveResponse(); }