public ActionResult Post([FromBody] LoanInputModel loan) { var result = _loanService.CreateLoan(loan); return(new CreatedAtRouteResult("GetLoanDetails", new { id = result.Id }, result)); }
public LoanViewModel CreateLoan(LoanInputModel loanInputModel) { var kart = this._uow.KartRepository.GetKartBySessionId(loanInputModel.SessionUserId).ToList(); var loan = new Loan() { StartDate = DateTime.Now, EndDate = DateTime.Now.AddDays(7), ReturnDate = null, UserID = GetUser() }; foreach (var c in kart) { loan.BoLoan.Add(new BookLoan() { BookID = c.BookID }); } _uow.LoanRepository.Add(loan); _uow.Commit(); foreach (var c in kart) { c.LoanID = loan.LoanID; _uow.KartRepository.Update(c); _uow.Commit(); } return(_mapper.Map <LoanViewModel>(loan)); }
public IActionResult UpdateBorrowingInformation([FromBody] LoanInputModel loan, int userId, int tapeId) { if (!ModelState.IsValid) { throw new ModelFormatException("Loan was not properly formatted"); } _loanService.UpdateTapeOnLoan(loan, userId, tapeId); return(NoContent()); }
public ActionResult ReturnBooks(int id, [FromBody] LoanInputModel loan) { if (id != loan.Id) { return(BadRequest()); } var result = _loanService.ReturnBooks(loan.Id); return(new CreatedAtRouteResult("GetLoanDetails", new { id = result.Id }, result)); }
// user/userId/tapes/tapeId: Update loan information public void UpdateTapeOnLoan(LoanInputModel loan, int userId, int tapeId) { var user = _userService.IsValidId(userId); var tape = _tapeService.IsValidId(tapeId); var checkLoan = CheckIfUserHasTape(userId, tapeId); if (checkLoan == null) { throw new LoanException($"Tape with id {tapeId} is not registered as loaned for user with id {userId}"); } var loanFromInput = Mapper.Map <Loan>(loan); checkLoan.BorrowDate = loanFromInput.BorrowDate; _repository.UpdateTapeOnLoan(checkLoan); }
public IActionResult BorrowTheLoan(LoanInputModel postModel) { try { if (postModel.LoanAmount <= 0) { throw new ArgumentOutOfRangeException("LoanAmount"); } if (postModel.LoanDurationYears < 0) { throw new ArgumentOutOfRangeException("LoanDurationYears"); } if (postModel.LoanDurationMonths < 0) { throw new ArgumentOutOfRangeException("LoanDurationMonths"); } if (postModel.LoanDurationMonths == 0 && postModel.LoanDurationYears == 0) { throw new ArgumentOutOfRangeException("LoanDuration"); } LoanModel m = _calc.CalcLoan(postModel); var r = new LoanOutputModel() { MonthlyCost = Math.Round(m.MonthlyCost, 4), TotalInterest = Math.Round(m.TotalInterest, 4), TotalFees = Math.Round(m.TotalFee, 4), APR = Math.Round(m.APR * 100, 4) }; return(new JsonResult(r)); } catch (ArgumentOutOfRangeException aex) { return(StatusCode((int)HttpStatusCode.BadRequest, aex.Message)); } catch (Exception ex) { return(StatusCode(500, ex.Message)); } }
public async Task <IActionResult> LoanBooks() { var url = $"/v1/Loans"; var emprestimo = new LoanInputModel() { SessionUserId = GetKartKey() }; var resposta = await _httpClient.PostAsJsonAsync(url, emprestimo); if (!resposta.IsSuccessStatusCode) { return(BadRequest()); } TempData.Remove("KartKey"); return(RedirectToAction("Index", "Loans")); }
public async Task <IActionResult> ReturnBooks(int?id) { if (id == null) { return(NotFound()); } var emprestimo = new LoanInputModel() { Id = id.Value }; var url = baseUrl + "/ReturnBooks/" + id; var result = await _httpClient.PostAsJsonAsync(url, emprestimo); if (!result.IsSuccessStatusCode) { return(BadRequest()); } return(RedirectToAction(nameof(Index))); }
/// <summary> /// Funkcja do dodawania nowego wypożyczenia /// </summary> /// <param name="loan"></param> /// <returns></returns> // POST: api/Loans public async Task <IHttpActionResult> PostLoan(LoanInputModel loan) { if (loan == null) { return(BadRequest(message: "empty")); } if (!ModelState.IsValid) { return(BadRequest(ModelState)); } loan.LoanStart = DateTime.Now; var result = await _loanRepository.SaveAsync(_mapper.Map <Loan>(loan)); if (!result) { return(InternalServerError()); } return(Ok()); }
/// <summary> /// Funkcja do edycji trwającego wypożyczenia o podanym ID /// </summary> /// <param name="id"></param> /// <param name="inputModel"></param> /// <returns></returns> // PUT: api/Loans/5 public async Task <IHttpActionResult> PutLoan(int id, LoanInputModel inputModel) { if (inputModel == null) { return(BadRequest(message: "empty")); } if (id != inputModel.ID) { return(BadRequest()); } var loan = await _loanRepository.GetByIdAsync(id); if (loan == null) { return(NotFound()); } loan.LoanStart = DateTime.Now; if (inputModel.BookID != default(int)) { loan.BookID = inputModel.BookID; } if (inputModel.BorrowerID != default(int)) { loan.BorrowerID = inputModel.BorrowerID; } var result = await _loanRepository.SaveAsync(loan); if (!result) { return(InternalServerError()); } return(Ok()); }
public LoanModel CalcLoan(LoanInputModel postModel) { return(CalcLoan(postModel.LoanAmount, postModel.LoanDurationYears, postModel.LoanDurationMonths)); }