public GameLoan Save(GameLoan gameLoan) { try { ApplicationDbContext.Database.BeginTransaction(); Model.CheckModel(gameLoan); var game = GameRepository.FindGamesById(gameLoan.GameId); ValidateGameLoan(gameLoan, game); //Setting data game.IsLent = true; gameLoan.LoanDate = DateService.GetCurrentDate(); gameLoan.ReturnDate = null; //Saving GameRepository.Update(game); GameLoanRepository.Save(gameLoan); ApplicationDbContext.Database.CommitTransaction(); } catch (Exception e) { ApplicationDbContext.Database.RollbackTransaction(); throw e; } return(gameLoan); }
public GameLoan Update(long id, GameLoan gameLoan) { try { Model.CheckModel(gameLoan); gameLoan.Id = id; ApplicationDbContext.Database.BeginTransaction(); if (!gameLoan.IsActive) { RegisterGameReturn(gameLoan); } else { UpdateGameLoan(gameLoan); } ApplicationDbContext.Database.CommitTransaction(); } catch (Exception e) { ApplicationDbContext.Database.RollbackTransaction(); throw e; } return(gameLoan); }
private void UpdateGameLoan(GameLoan gameLoan) { var currentGameLoan = GetCurrentGameLoan(gameLoan.Id); //If the game is being changed if (gameLoan.GameId != currentGameLoan.GameId) { //Releasing current game var loanedGame = GameRepository.FindGamesById(currentGameLoan.GameId); loanedGame.IsLent = false; GameRepository.Update(loanedGame); var game = GameRepository.FindGamesById(gameLoan.GameId); ValidateGameLoan(gameLoan, game); gameLoan.LoanDate = currentGameLoan.LoanDate; GameRepository.Update(loanedGame); GameLoanRepository.Update(gameLoan); } else { var game = GameRepository.FindGamesById(gameLoan.GameId); gameLoan.LoanDate = currentGameLoan.LoanDate; GameLoanRepository.Update(gameLoan); } }
public void TEST_CREATE_GAMELOAN() { var userInfo = login(); var game = new Game() { Id = 10, Name = "GameTeste", IsLent = false, MediaType = 1, PlataformName = "PlatformTest" }; createGame(userInfo, game); var gameLoan = new GameLoan() { Id = 1000001, FriendId = 1010, GameId = 10, IsActive = true, LoanDate = DateTime.MinValue }; var retrievedGameLoan = createGameLoan(userInfo, gameLoan); Assert.Equal(gameLoan, retrievedGameLoan); }
void SeedLoans(ModelBuilder modelBuilder) { var loans = new List <GameLoan>(); for (int i = 0; i < GamesExamples.Length; i++) { for (int j = 0; j < FriendsExamples.Length; j++) { var index = i * FriendsExamples.Length + j; var active = FriendsExamples[j].Id == GamesExamples[i].Id; DateTime?returnDate = null; if (!active) { returnDate = DateTime.ParseExact("25/01/2015", "dd/MM/yyyy", CultureInfo.InvariantCulture).AddDays(index); } var loan = new GameLoan() { Id = 1000 + index, FriendId = FriendsExamples[j].Id, GameId = GamesExamples[i].Id, IsActive = active, LoanDate = DateTime.ParseExact("22/01/2015", "dd/MM/yyyy", CultureInfo.InvariantCulture).AddDays(index), ReturnDate = returnDate }; loans.Add(loan); } } modelBuilder.Entity <GameLoan>().HasData(loans.ToArray()); }
public void TEST_CREATE_GAMELOAN_FOR_LOANED_GAME() { var userInfo = login(); var game = new Game() { Id = 11, Name = "GameTeste", IsLent = false, MediaType = 1, PlataformName = "PlatformTest" }; createGame(userInfo, game); var gameLoan = new GameLoan() { Id = 1000011, FriendId = 1010, GameId = 11, IsActive = true, LoanDate = DateTime.MinValue }; createGameLoan(userInfo, gameLoan); gameLoan.Id = 1000012; Assert.Throws <AggregateException>(() => createGameLoan(userInfo, gameLoan)); }
private GameLoan deleteGame(UserInfo userInfo, GameLoan gameLoan) { var url = "/api/GameLoans/" + gameLoan.Id; var result = TestContext.Delete(url, userInfo.Token, gameLoan).Result; Assert.Equal(System.Net.HttpStatusCode.OK, result.StatusCode); return(RetrieveGameLoan(userInfo, gameLoan.Id)); }
public dynamic ToReceive([FromBody] GameLoan gameLoan) { var currentLent = _gameLoanRepository.GetDeliveredByGame(gameLoan.GameId); if (currentLent != null) { currentLent.Status = LentStatus.RECEIVED; currentLent.ReceivedDate = DateTime.Now; _gameLoanRepository.Update(currentLent); _gameLoanRepository.Save(); return(new { sucess = true, id = currentLent.Id }); } return(new { sucess = false, message = "Game já foi devolvido" }); }
public dynamic Lend([FromBody] GameLoan gameLoan) { var currentLent = _gameLoanRepository.GetDeliveredByGame(gameLoan.GameId); if (currentLent == null) { gameLoan.Status = LentStatus.DELIVERED; gameLoan.DeliveredDate = DateTime.Now; _gameLoanRepository.Insert(gameLoan); _gameLoanRepository.Save(); return(new { sucess = true, id = gameLoan.Id }); } return(new { sucess = false, message = "Game já foi emprestado" }); }
private void RegisterGameReturn(GameLoan gameLoan) { var currentGameLoan = GetCurrentGameLoan(gameLoan.Id); if (currentGameLoan.IsActive) { var loanedGame = GameRepository.FindGamesById(currentGameLoan.GameId); loanedGame.IsLent = false; gameLoan.IsActive = false; gameLoan.LoanDate = currentGameLoan.LoanDate; gameLoan.ReturnDate = DateService.GetCurrentDate(); GameRepository.Update(loanedGame); GameLoanRepository.Update(gameLoan); } }
public void Delete(GameLoan gameLoan) { try { ApplicationDbContext.Database.BeginTransaction(); Model.CheckModel(gameLoan); gameLoan = ApplicationDbContext.GameLoans.Find(gameLoan.Id); var game = ApplicationDbContext.Games.Find(gameLoan.GameId); game.IsLent = false; GameRepository.Update(game); GameLoanRepository.Delete(gameLoan); ApplicationDbContext.Database.CommitTransaction(); } catch (Exception e) { ApplicationDbContext.Database.RollbackTransaction(); throw e; } }
private void ValidateGameLoan(GameLoan gameLoan, Game game) { if (game == null) { throw new GameManagerException(new List <ValidationError>() { new ValidationError() { DataField = "GameId", ErrorMsg = "Game inválido" } }); } if (game.IsLent) { throw new GameManagerException(new List <ValidationError>() { new ValidationError() { DataField = "GameId", ErrorMsg = "O Jogo Selecionado já está emprestado" } }); } var friend = FriendRepository.FindFriendById(gameLoan.FriendId); if (friend == null) { throw new GameManagerException(new List <ValidationError>() { new ValidationError() { DataField = "FriendId", ErrorMsg = "Amigo inválido" } }); } }
public void TEST_MARK_GAMELOAN_AS_RETURNED() { var userInfo = login(); var game = new Game() { Id = 13, Name = "GameTeste", IsLent = false, MediaType = 1, PlataformName = "PlatformTest" }; createGame(userInfo, game); var gameLoan = new GameLoan() { Id = 1000015, FriendId = 1010, GameId = 13, IsActive = true, LoanDate = DateTime.MinValue }; createGameLoan(userInfo, gameLoan); gameLoan = new GameLoan() { Id = 1000015, FriendId = 1010, GameId = 13, IsActive = false, LoanDate = DateTime.MinValue, ReturnDate = DateTime.MinValue }; var retrievedGameLoan = updateGameLoan(userInfo, gameLoan); Assert.Equal(gameLoan, retrievedGameLoan); }
public GameLoan Save([FromBody] GameLoan gameLoan) { return(GameLoanService.Save(gameLoan)); }
public GameLoan Update(GameLoan gameLent) { gameLent.UpdateAt = DateTime.Now; _ctx.GameLents.Update(gameLent); return(gameLent); }
public GameLoan Insert(GameLoan gameLent) { gameLent.CreateAt = DateTime.Now; _ctx.GameLents.Add(gameLent); return(gameLent); }
public void Delete(GameLoan gameLent) { gameLent.Removed = true; Update(gameLent); }
public GameLoan Update(long id, [FromBody] GameLoan gameLoan) { return(GameLoanService.Update(id, gameLoan)); }