Skip to content

Commit

Permalink
Merge pull request #786 from NemeStats/bugfix/785-played-game-entity-…
Browse files Browse the repository at this point in the history
…does-not-exist

fix EntityDoesNotExistException for non-existent played game #785
  • Loading branch information
HolisticDeveloper committed Jun 3, 2023
2 parents d98147e + 73f45ba commit 7c8d9be
Show file tree
Hide file tree
Showing 3 changed files with 59 additions and 48 deletions.
44 changes: 24 additions & 20 deletions Source/UI/Controllers/PlayedGameController.cs
Original file line number Diff line number Diff line change
Expand Up @@ -16,9 +16,8 @@
// along with this program. If not, see <http://www.gnu.org/licenses/>
#endregion

using System;
using System.Globalization;
using BusinessLogic.DataAccess;
using BusinessLogic.Exceptions;
using BusinessLogic.Logic;
using BusinessLogic.Logic.GameDefinitions;
using BusinessLogic.Logic.PlayedGames;
Expand All @@ -27,12 +26,13 @@
using BusinessLogic.Models.Games;
using BusinessLogic.Models.PlayedGames;
using BusinessLogic.Models.User;
using BusinessLogic.Paging;
using System;
using System.Collections.Generic;
using System.Globalization;
using System.Linq;
using System.Net;
using System.Web.Mvc;
using BusinessLogic.Models.Players;
using BusinessLogic.Paging;
using UI.Attributes.Filters;
using UI.Controllers.Helpers;
using UI.Mappers.Interfaces;
Expand All @@ -53,7 +53,6 @@ public partial class PlayedGameController : BaseController
private readonly IPlayedGameSaver _playedGameSaver;
private readonly IGameDefinitionRetriever _gameDefinitionRetriever;
private readonly IPlayedGameDeleter _playedGameDeleter;
private readonly IPlayerSaver _playerSaver;
private readonly IMapperFactory _mapperFactory;
private readonly ICreatePlayedGameComponent _createPlayedGameComponent;
private readonly ICreateGameDefinitionComponent _createGameDefinitionComponent;
Expand All @@ -68,7 +67,6 @@ public partial class PlayedGameController : BaseController
IGameDefinitionRetriever gameDefinitionRetriever,
IPlayedGameSaver playedGameSaver,
IPlayedGameDeleter playedGameDeleter,
IPlayerSaver playerSaver,
IMapperFactory mapperFactory,
ICreatePlayedGameComponent createPlayedGameComponent,
ICreateGameDefinitionComponent createGameDefinitionComponent)
Expand All @@ -80,7 +78,6 @@ public partial class PlayedGameController : BaseController
_gameDefinitionRetriever = gameDefinitionRetriever;
_playedGameSaver = playedGameSaver;
_playedGameDeleter = playedGameDeleter;
_playerSaver = playerSaver;
_mapperFactory = mapperFactory;
_createPlayedGameComponent = createPlayedGameComponent;
_createGameDefinitionComponent = createGameDefinitionComponent;
Expand All @@ -94,19 +91,26 @@ public virtual ActionResult Details(int? id, ApplicationUser currentUser)
{
return new HttpStatusCodeResult(HttpStatusCode.BadRequest);
}
var playedGame = _playedGameRetriever.GetPlayedGameDetails(id.Value);
if (playedGame == null)

PlayedGame playedGame;

try
{
playedGame = _playedGameRetriever.GetPlayedGameDetails(id.Value);
}
catch (EntityDoesNotExistException<PlayedGame>)
{
return HttpNotFound();
}

var playedGameDetails = _playedGameDetailsBuilder.Build(playedGame, currentUser, true);
return View(MVC.PlayedGame.Views.Details, playedGameDetails);
}

// GET: /PlayedGame/Create
[System.Web.Mvc.Authorize]
[Authorize]
[UserContext]
[System.Web.Mvc.HttpGet]
[HttpGet]
public virtual ActionResult Create(ApplicationUser currentUser)
{
var viewModel = MakeBaseCreatePlayedGameViewModel<CreatePlayedGameViewModel>(currentUser.CurrentGamingGroupId.Value);
Expand Down Expand Up @@ -262,7 +266,7 @@ public virtual ActionResult Edit(int id, ApplicationUser currentUser)
viewModel.PlayerRanks = playedGameInfo.PlayerRanks;

return View(MVC.PlayedGame.Views.CreateOrEdit, viewModel);
//TODO allow editing of a game immediately after saving. No need to disable if currentStep == 5
//TODO allow editing of a game immediately after saving. No need to disable if currentStep == 5
}

private GameResultTypes SetGameType(List<PlayerRankWithName> playerRanks)
Expand All @@ -282,7 +286,7 @@ private GameResultTypes SetGameType(List<PlayerRankWithName> playerRanks)
}


[System.Web.Mvc.HttpGet]
[HttpGet]
public virtual ActionResult ShowRecentlyPlayedGames()
{
var recentlyPlayedGamesFilter = new RecentlyPlayedGamesFilter
Expand All @@ -298,7 +302,7 @@ public virtual ActionResult ShowRecentlyPlayedGames()
}

// GET: /PlayedGame/Delete/5
[System.Web.Mvc.Authorize]
[Authorize]
[UserContext]
public virtual ActionResult Delete(int? id, ApplicationUser currentUser)
{
Expand All @@ -315,8 +319,8 @@ public virtual ActionResult Delete(int? id, ApplicationUser currentUser)
}

// POST: /PlayedGame/Delete/5
[System.Web.Mvc.Authorize]
[System.Web.Mvc.HttpPost, System.Web.Mvc.ActionName("Delete")]
[Authorize]
[HttpPost, ActionName("Delete")]
[ValidateAntiForgeryToken]
[UserContext]
public virtual ActionResult DeleteConfirmed(int id, ApplicationUser currentUser)
Expand All @@ -327,9 +331,9 @@ public virtual ActionResult DeleteConfirmed(int id, ApplicationUser currentUser)
+ "#" + GamingGroupController.SECTION_ANCHOR_RECENT_GAMES);
}

[System.Web.Mvc.Authorize]
[Authorize]
[UserContext]
[System.Web.Mvc.HttpGet]
[HttpGet]
public virtual ActionResult Search(ApplicationUser currentUser)
{
var viewModel = new SearchViewModel
Expand All @@ -343,9 +347,9 @@ public virtual ActionResult Search(ApplicationUser currentUser)
return View(MVC.PlayedGame.Views.Search, viewModel);
}

[System.Web.Mvc.Authorize]
[Authorize]
[UserContext]
[System.Web.Mvc.HttpPost]
[HttpPost]
public virtual ActionResult Search(PlayedGamesFilterViewModel filter, ApplicationUser currentUser)
{
var playedGameFilter = new PlayedGameFilter
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -16,12 +16,13 @@
// along with this program. If not, see <http://www.gnu.org/licenses/>
#endregion

using BusinessLogic.Exceptions;
using BusinessLogic.Logic.PlayedGames;
using BusinessLogic.Models;
using NUnit.Framework;
using Rhino.Mocks;
using System.Net;
using System.Web.Mvc;
using Rhino.Mocks;
using BusinessLogic.Models;
using UI.Models.PlayedGame;
using UI.Transformations;

Expand All @@ -47,9 +48,15 @@ public void ItReturnsBadHttpStatusWhenNoPlayedGameIdGiven()
[Test]
public void ItReturns404StatusWhenNoPlayedGameIsFound()
{
HttpStatusCodeResult actualResult = AutoMocker.ClassUnderTest.Details(-1, null) as HttpStatusCodeResult;
const int nonExistentPlayedGameId = -1;
AutoMocker.Get<IPlayedGameRetriever>().BackToRecord(BackToRecordOptions.All);
AutoMocker.Get<IPlayedGameRetriever>().Expect(mock => mock.GetPlayedGameDetails(nonExistentPlayedGameId))
.Throw(new EntityDoesNotExistException<PlayedGame>(nonExistentPlayedGameId));
AutoMocker.Get<IPlayedGameRetriever>().Replay();

var result = AutoMocker.ClassUnderTest.Details(nonExistentPlayedGameId, CurrentUser) as HttpStatusCodeResult;

Assert.AreEqual((int)HttpStatusCode.NotFound, actualResult.StatusCode);
Assert.That(result.StatusCode, Is.EqualTo((int)HttpStatusCode.NotFound));
}

[Test]
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -22,45 +22,45 @@
using BusinessLogic.Models.User;
using NUnit.Framework;
using Rhino.Mocks;
using StructureMap.AutoMocking;
using System.Collections.Generic;
using System.Web.Mvc;
using StructureMap.AutoMocking;
using UI.Controllers;
using UI.Mappers.Interfaces;
using UI.Models.PlayedGame;

namespace UI.Tests.UnitTests.ControllerTests.PlayedGameControllerTests
{
public class PlayedGameControllerTestBase
{
protected RhinoAutoMocker<PlayedGameController> AutoMocker;
protected string TestUserName = "the test user name";
protected ApplicationUser CurrentUser;
protected const int EXPECTED_GAMING_GROUP_ID = 1;
protected List<GameDefinitionSummary> GameDefinitionSummaries;
protected List<PublicGameSummary> ExpectedViewModel;
protected PlayedGameEditViewModel ExpectedPopulatedCompletedGameViewModel;
protected List<Player> PlayerList;
protected List<SelectListItem> PlayerSelectList;
protected List<GameDefinition> GameDefinitionList;
protected List<SelectListItem> GameDefinitionSelectList;
public class PlayedGameControllerTestBase
{
protected RhinoAutoMocker<PlayedGameController> AutoMocker;
protected string TestUserName = "the test user name";
protected ApplicationUser CurrentUser;
protected const int EXPECTED_GAMING_GROUP_ID = 1;
protected List<GameDefinitionSummary> GameDefinitionSummaries;
protected List<PublicGameSummary> ExpectedViewModel;
protected PlayedGameEditViewModel ExpectedPopulatedCompletedGameViewModel;
protected List<Player> PlayerList;
protected List<SelectListItem> PlayerSelectList;
protected List<GameDefinition> GameDefinitionList;
protected List<SelectListItem> GameDefinitionSelectList;

[SetUp]
public virtual void TestSetUp()
{
[SetUp]
public virtual void TestSetUp()
{
AutoMocker = new RhinoAutoMocker<PlayedGameController>();
AutoMocker.PartialMockTheClassUnderTest();

CurrentUser = new ApplicationUser()
{
CurrentGamingGroupId = EXPECTED_GAMING_GROUP_ID
{
CurrentGamingGroupId = EXPECTED_GAMING_GROUP_ID
};
GameDefinitionSummaries = new List<GameDefinitionSummary>();
GameDefinitionSummaries = new List<GameDefinitionSummary>();
AutoMocker.Get<IGameDefinitionRetriever>().Expect(mock => mock.GetAllGameDefinitions(EXPECTED_GAMING_GROUP_ID))
.Repeat.Once()
.Return(GameDefinitionSummaries);
.Repeat.Once()
.Return(GameDefinitionSummaries);

AutoMocker.ClassUnderTest.Url = MockRepository.GenerateMock<UrlHelper>();
AutoMocker.ClassUnderTest.Url = MockRepository.GenerateMock<UrlHelper>();

var expectedMapper = MockRepository.GenerateMock<ICustomMapper<SavePlayedGameRequest, NewlyCompletedGame>>();
AutoMocker.Get<IMapperFactory>()
Expand All @@ -74,5 +74,5 @@ public virtual void TestSetUp()
.Return(expectedUpdateGameMapper);
expectedUpdateGameMapper.Expect(mock => mock.Map(Arg<SavePlayedGameRequest>.Is.Anything)).Return(new UpdatedGame());
}
}
}
}

0 comments on commit 7c8d9be

Please sign in to comment.