public CreateOutput <GameDto, long> Create(CreateGameInput input) { Game newGameEntity = input.Entity.MapTo <Game>(); if (!GamePolicy.CanCreateEntity(newGameEntity)) { throw new CityQuestPolicyException(CityQuestConsts.CQPolicyExceptionCreateDenied, "\"Game\""); } newGameEntity.IsActive = true; newGameEntity.GameImageName = CreateGameImage(Guid.NewGuid().ToString(), input.GameImageData); try { newGameEntity.GameStatusId = GameStatusRepository.GetAll().Where(r => r.IsDefault).Single().Id; } catch { throw new UserFriendlyException("Default game status not found!", "Default game status not found! Please contact your system administrator."); } GameRepository.Includes.Add(r => r.GameTasks); GameRepository.Includes.Add(r => r.Location); GameRepository.Includes.Add(r => r.GameStatus); GameRepository.Includes.Add(r => r.LastModifierUser); GameRepository.Includes.Add(r => r.CreatorUser); long newGameId = GameRepository.InsertAndGetId(newGameEntity); UowManager.Current.Completed += (sender, e) => { GameChangesNotifier.RaiseOnGameAdded(new GameAddedMessage(newGameId)); }; GameDto newGameDto = GameRepository.Get(newGameId).MapTo <GameDto>(); GameRepository.Includes.Clear(); return(new CreateOutput <GameDto, long>() { CreatedEntity = newGameDto }); }
public GameStatusService(DbEntitiesContext context) { gameStatusRepository = new GameStatusRepository(context); }
public GameStatusService() : base() { gameStatusRepository = new GameStatusRepository(); }
/// <summary> /// Is used to update GameStatus for Game entity /// </summary> /// <param name="gameId">game's Id (not required if gameEntity parameter inputed)</param> /// <param name="gameEntity">game's entity (not required if gameId parameter inputed)</param> /// <param name="newGameStatusId">new GameStatus's Id (not required if newGameStatusName parameter inputed)</param> /// <param name="newGameStatusName">new GameStatus's Name (not required if newGameStatusId parameter inputed)</param> /// <returns>new game's status dto</returns> private GameStatusDto UpdateGameStatus(long?gameId, Game gameEntity, long?newGameStatusId, string newGameStatusName) { if (gameEntity == null && gameId != null) { #region Retrieving Game entity GameRepository.Includes.Add(r => r.GameStatus); GameRepository.Includes.Add(r => r.GameStatus.CreatorUser); GameRepository.Includes.Add(r => r.GameStatus.LastModifierUser); gameEntity = GameRepository.Get((long)gameId); GameStatusRepository.Includes.Clear(); #endregion } if (gameEntity == null) { throw new CityQuestItemNotFoundException(CityQuestConsts.CityQuestItemNotFoundExceptionMessageBody, "\"Game\""); } GameStatusDto newGameStatusDto = gameEntity.GameStatus.MapTo <GameStatusDto>(); if (gameEntity.GameStatusId != newGameStatusId) { GameStatusRepository.Includes.Add(r => r.CreatorUser); GameStatusRepository.Includes.Add(r => r.LastModifierUser); GameStatus newGameStatus; try { newGameStatus = newGameStatusId != null?GameStatusRepository.Get((long)newGameStatusId) : GameStatusRepository.Single(r => r.Name == newGameStatusName); } catch { throw new UserFriendlyException("Wrong game status!", "Trying to get wrong game status! Please, contact your system administrator."); } if (!GamePolicy.CanChangeStatusForEntity(gameEntity, gameEntity.GameStatus, newGameStatus)) { throw new CityQuestPolicyException(CityQuestConsts.CQPolicyExceptionChangeStatusDenied, "\"Game\""); } gameEntity.GameStatusId = newGameStatus.Id; gameEntity.GameStatus = null; GameRepository.Update(gameEntity); newGameStatusDto = newGameStatus.MapTo <GameStatusDto>(); UowManager.Current.Completed += (sender, e) => { GameChangesNotifier.RaiseOnGameStatusChanged( new GameStatusChangedMessage(gameEntity.Id, newGameStatus.Id, newGameStatusDto)); }; } return(newGameStatusDto); }