예제 #1
0
        public void Execute(RentGameDto req)
        {
            if (!Context.Games.Any(g => g.Id == req.GameId))
            {
                throw new GameDoesntExistException();
            }
            if (!Context.Users.Any(u => u.Id == req.UserId))
            {
                throw new UserDoesntExistException();
            }

            if (!Context.Rents.Any(r => r.Id == req.Id))
            {
                throw new RentNotFoundException();
            }
            var rent = Context.Rents.Find(req.Id);

            if (rent.GameId != req.GameId || rent.UserId != req.UserId)
            {
                throw new BadDataException();
            }
            if (rent.ReturnedAt.HasValue)
            {
                throw new GameReturnedException();
            }
            rent.ReturnedAt = DateTime.Now;
            Context.SaveChanges();
        }
예제 #2
0
 public IActionResult Post([FromBody] RentGameDto dto)
 {
     try
     {
         _rentGame.Execute(dto);
         return(StatusCode(201, "Game rented!"));
     }
     catch (GameNotAvailableException)
     {
         return(StatusCode(409, "Game not available for renting!"));
     }
     catch (UserDoesntExistException)
     {
         return(StatusCode(404, "User with provided ID doesn't exist!"));
     }
     catch (GameDoesntExistException)
     {
         return(StatusCode(404, "Game with provided ID doesn't exist"));
     }
     catch (ActiveRentException)
     {
         return(StatusCode(409, "You have an active rent!"));
     }
     catch (DeletedException)
     {
         return(StatusCode(404, "Game has been deleted!"));
     }
     catch (Exception)
     {
         return(StatusCode(500, "Something is wrong on our server! Please try again later"));
     }
 }
예제 #3
0
 public IActionResult Put(int id, [FromBody] RentGameDto dto)
 {
     dto.Id = id;
     try
     {
         _returnGame.Execute(dto);
         return(StatusCode(201, "Game returned!"));
     }
     catch (GameDoesntExistException)
     {
         return(StatusCode(404, "Game doesnt exists"));
     }
     catch (UserDoesntExistException)
     {
         return(StatusCode(404, "User doesnt exist"));
     }
     catch (RentNotFoundException)
     {
         return(StatusCode(404, "Rent with that Id not found"));
     }
     catch (BadDataException)
     {
         return(StatusCode(422, "Wrong data provided!"));
     }
     catch (GameReturnedException)
     {
         return(StatusCode(409, "You have already returned this game!"));
     }
     catch (Exception)
     {
         return(StatusCode(500, "Something is wrong on our server! Please try again later"));
     }
 }
예제 #4
0
        public void Execute(RentGameDto req)
        {
            if (!Context.Games.Any(g => g.Id == req.GameId))
            {
                throw new GameDoesntExistException();
            }

            if (Context.Games.Find(req.GameId).DeletedAt.HasValue)
            {
                throw new DeletedException();
            }

            if (!Context.Users.Any(u => u.Id == req.UserId))
            {
                throw new UserDoesntExistException();
            }

            var UserRents = Context.Rents
                            .Any(r => r.UserId == req.UserId && !r.ReturnedAt.HasValue);

            //.Where(r => r.GameId == req.GameId && r.ReturnedAt == null).
            //.FirstOrDefault();
            Console.WriteLine("user rents");
            Console.WriteLine(UserRents);
            if (UserRents)
            {
                throw new ActiveRentException();
            }

            var game = Context.Rents
                       .Where(r => r.GameId == req.GameId)
                       .OrderByDescending(r => r.Id)
                       .FirstOrDefault();

            if (game != null && !game.ReturnedAt.HasValue)
            {
                Console.WriteLine("Igrica nije vracena");
                throw new GameNotAvailableException();
            }
            Context.Rents.Add(new Rent
            {
                UserId = req.UserId,
                GameId = req.GameId
            });
            Context.SaveChanges();
        }