public IActionResult Cancel([FromBody] RentingDto renting, [FromServices] IUserRepository userRepo) { if (renting is null) { return(BadRequest(new ErrorModel { CauseValue = "null", CauseValueName = nameof(renting), Message = "Renting parameter is not valid!" })); } if (!userRepo.Validate(renting.Email, renting.Password)) { return(ValidationProblem("User credentials are incorrect!", "Email or password is incorrect!", 422, "Unprocessable Entity")); } string userId = userRepo.GetUserId(renting.Email, renting.Password); bool rentExists = _repo.RentingExists(renting.CarId, userId); if (rentExists && _repo.CancellRent(userId: userId)) { _repo.Save(); return(Ok("Renting cancelled!")); } return(NoContent()); }
public IActionResult Rent([FromBody] RentingDto renting, [FromServices] IUserRepository userRepo) { if (renting is null) { return(BadRequest(new ErrorModel { CauseValue = "null", CauseValueName = nameof(renting), Message = "Renting parameter is not valid!" })); } if (!userRepo.Validate(renting.Email, renting.Password)) { return(ValidationProblem("User credentials are incorrect!", "Email or password is incorrect!", 422, "Unprocessable Entity")); } string userId = userRepo.GetUserId(renting.Email, renting.Password); Renting rented = _repo.RentCar(renting.CarId, userId, renting.Start, renting.End); _repo.Save(); return(new JsonResult(new { Rented = true, From = renting.Start, To = renting.End, Car = rented.Car.Brand + " " + rented.Car.Model })); }
public IHttpActionResult CreateRentings(RentingDto rentObject) { var customer = _context.Customers.Single(c => c.Id == rentObject.CustomerId); if (customer == null) { return(BadRequest("Not found")); } var movies = _context.Movies.Where(m => rentObject.MoviesId.Contains(m.Id)).ToList(); if (movies == null) { return(BadRequest("Not found")); } foreach (var i in movies) { if (i.NumberAvailable == 0) { return(BadRequest("Movie is not available")); } i.NumberAvailable--; var rentings = new Renting { Customer = customer, Movie = i, DateRented = DateTime.Now, }; _context.Rentings.Add(rentings); } _context.SaveChanges(); return(Ok()); }