private bool ValidateRequest( string httpMethod, UserLoan value, object param1, out List <string> validationFailureMessages) { bool result = true; validationFailureMessages = new List <string>(); if (HttpMethods.IsPost(httpMethod)) { if (value.Id != 0) { result = false; validationFailureMessages.Add(Constants.Message.ValidationFailedIdShouldBeNull); } } else if (HttpMethods.IsPut(httpMethod)) { if (value.Id.ToString() != param1.ToString()) { result = false; validationFailureMessages.Add(Constants.Message.ValidationFailedIdsShouldMatch); } } _appLogger.LogError($"UserLoanController::Validate(httpMethod: {httpMethod}, <value>, param1: {param1}) >> Result = {result}."); return(result); }
public void Create_Returns400BadRequest() { //Arrange var userLoanInput = new UserLoan { Id = 10, UserLoanNum = "67853423187", InterestAmount = 375, EarlyPaymentFee = 76, Balance = 1990, AppliedForTopup = true, LoanMasterId = 1, CreatedOn = new DateTime(2019, 1, 1), UpdatedOn = new DateTime(2019, 1, 1) }; var mockUserLoanRepository = new Mock <IUserLoanRepository>(); var mockAppLogger = new Mock <IAppLogger>(); //mockUserLoanRepository.Setup(x => x.Add(userLoanInput)).Returns(userLoanInput); var controller = new UserLoanController(mockAppLogger.Object, mockUserLoanRepository.Object); //Act var actual = controller.Create(userLoanInput); //Assert Assert.Null(actual.Value); var badRequestObjectResult = Assert.IsType <BadRequestObjectResult>(actual.Result); Assert.Equal(400, badRequestObjectResult.StatusCode); }
public void Get_Successful() { //Arrange var id = 1; var userId = 1; var userLoan = new UserLoan { Id = 1, UserLoanNum = "67853423187", InterestAmount = 375, EarlyPaymentFee = 76, Balance = 1990, AppliedForTopup = true, LoanMasterId = 1, CreatedOn = new DateTime(2019, 1, 1), UpdatedOn = new DateTime(2019, 1, 1), }; var mockUserLoanRepository = new Mock <IUserLoanRepository>(); var mockAppLogger = new Mock <IAppLogger>(); mockUserLoanRepository.Setup(x => x.GetByLoanIdUserId(id, userId)).Returns(userLoan); var controller = new UserLoanController(mockAppLogger.Object, mockUserLoanRepository.Object); //Act var actual = controller.GetObjectById(userId, id); //Assert Assert.Same(userLoan, actual.Value); }
public ActionResult <UserLoan> Create([FromBody] UserLoan value) { string methodName = nameof(Create), title = Constants.Message.TitleCreateObject; List <string> validationFailureMessages; try { var isValid = ValidateRequest(HttpMethods.Post, value, null, out validationFailureMessages); if (isValid) { return(_userLoanRepository.Add(value)); } else { var errorMessage = GetFlattenedMessage(validationFailureMessages, Constants.Message.ValidationFailed); return(BadRequestError(methodName, title, null, errorMessage)); } } catch (System.Exception ex) { return(InternalServerError(methodName, title, ex)); } }
public async Task <Loan> CreateLoan(User user, Car car, DateTime loanUntil) { var loan = new Loan() { User = user, Car = car, LoanStart = DateTime.UtcNow, LoanEnd = loanUntil }; _context.Add(loan); await _context.SaveChangesAsync(); var userLoan = new UserLoan() { UserId = user.Id, LoanId = loan.Id }; _context.UserLoans.Add(userLoan); await _context.SaveChangesAsync(); return(loan); }