Esempio n. 1
0
        public async Task<IActionResult> Index(int id, CarLoanModel model)
        {
            if (await _loanService.IsCarLoaned(id))
                return BadRequest();

            var car = await _carService.GetCar(id);

            // map car to CarViewModel
            model.Car = _mapper.Map<CarViewModel>(car);

            if (!ModelState.IsValid)
                return View(model);

            // parse LoanUntil and set the type to UTC date
            var loanUntil = DateTime.SpecifyKind(DateTime.Parse(model.LoanUntil), DateTimeKind.Utc);
            if (loanUntil < DateTime.UtcNow) // check that the loan end date is in the future, at least 1 day
            {
                ModelState.AddModelError("LoanUntil", "Date must be in the future.");
                return View(model);
            }

            var user = await HttpContext.GetCurrentUser(_userService);

            // create loan
            await _loanService.CreateLoan(user, car, loanUntil);

            // redirect
            return LocalRedirect("/cars");
        }
Esempio n. 2
0
        public ActionResult Post([FromBody] LoanInputModel loan)
        {
            var result = _loanService.CreateLoan(loan);

            return(new CreatedAtRouteResult("GetLoanDetails",
                                            new { id = result.Id }, result));
        }
        public async Task <IActionResult> Add([FromBody] LoanRequest request)
        {
            if (request.InterestRate < 0 || request.InterestRate > 1)
            {
                return(BadRequest("Interest rate should be between 0 and 1"));
            }

            if (request.LoanStart > request.LoanEnd)
            {
                return(BadRequest("Loan end date should be later than loan start date"));
            }

            return(HandleResponse(await _loanService.CreateLoan(request)));
        }
Esempio n. 4
0
        public async Task <IActionResult> AcceptRequest(int?id)
        {
            if (id != null)
            {
                var loanRequest = await _loanRequestService.GetById((int)id);

                if (loanRequest != null)
                {
                    await _loanService.CreateLoan(loanRequest.Id);
                }
            }

            return(RedirectToAction(nameof(Index)));
        }
        public IActionResult CreateLoan(CreateLoanDto createLoan)
        {
            if (_userService.CheckUserExistsById(createLoan.BorrowerId) && _userService.CheckUserExistsById(createLoan.LenderId))
            {
                var loanId = _loanService.CreateLoan(createLoan);
                if (loanId != 0)
                {
                    return(NoContent());
                }
                else
                {
                    return(StatusCode((int)Reponse.InternalServerError));
                }
            }

            return(BadRequest());
        }
Esempio n. 6
0
        public IActionResult LoanBooks(LoanViewModel model)
        {
            var bookIsAvailable = _loanService.CheckIfBookIsAvailable(model.SelectedBook);
            var userHasTheBook  = _loanService.CheckIfUserHasTheBook(model.SelectedBook);
            var moreThan3       = _loanService.LoanedMoreThan3();

            if (!bookIsAvailable)
            {
                ModelState.AddModelError("CustomError", "The book isn't available.");
                return(View(model));
            }
            else if (userHasTheBook)
            {
                ModelState.AddModelError("CustomError", "You have already loaned the book .");
                return(View(model));
            }
            else if (moreThan3)
            {
                ModelState.AddModelError("CustomError", "You have already loaned 3 books, can't have more ");
                return(View(model));
            }
            _loanService.CreateLoan(model);
            return(RedirectToAction("LogIn", "User"));
        }
Esempio n. 7
0
        public IActionResult LoanBooks(LoanViewModel model)
        {
            var bookIsAvailable = _loanService.CheckIfBookIsAvailable(model.SelectedBook);
            var userHasTheBook  = _loanService.CheckIfUserHasTheBook(model.SelectedBook);
            var moreThan5       = _loanService.LoanedMoreThan5();

            if (!bookIsAvailable)
            {
                ModelState.AddModelError("CustomError", "The book isn't available.Please select another book.");
                return(View(model));
            }
            else if (userHasTheBook)
            {
                ModelState.AddModelError("CustomError", "You have already loaned the book that you have selected.Please select another book.");
                return(View(model));
            }
            else if (moreThan5)
            {
                ModelState.AddModelError("CustomError", "You have already loaned 5 Books. Please return one book so you can loan new book.");
                return(View(model));
            }
            _loanService.CreateLoan(model);
            return(RedirectToAction("LogIn", "User"));
        }