public DeleteOutput <long> Delete(DeleteInput <long> input) { Game gameEntityForDelete = GameRepository.Get(input.EntityId); if (gameEntityForDelete == null) { throw new CityQuestItemNotFoundException(CityQuestConsts.CityQuestItemNotFoundExceptionMessageBody, "\"Game\""); } if (!GamePolicy.CanDeleteEntity(gameEntityForDelete)) { throw new CityQuestPolicyException(CityQuestConsts.CQPolicyExceptionDeleteDenied, "\"Game\""); } DeleteGameImage(gameEntityForDelete.GameImageName); GameRepository.Delete(gameEntityForDelete); UowManager.Current.Completed += (sender, e) => { GameChangesNotifier.RaiseOnGameDeleted(new GameDeletedMessage(input.EntityId)); }; return(new DeleteOutput <long>() { DeletedEntityId = input.EntityId }); }
public ChangeActivityOutput <GameDto, long> ChangeActivity(ChangeActivityInput input) { GameRepository.Includes.Add(r => r.Location); GameRepository.Includes.Add(r => r.GameStatus); GameRepository.Includes.Add(r => r.GameTasks); GameRepository.Includes.Add(r => r.LastModifierUser); GameRepository.Includes.Add(r => r.CreatorUser); Game gameEntity = GameRepository.Get(input.EntityId); if (gameEntity == null) { throw new CityQuestItemNotFoundException(CityQuestConsts.CityQuestItemNotFoundExceptionMessageBody, "\"Game\""); } if (!GamePolicy.CanChangeActivityForEntity(gameEntity)) { throw new CityQuestPolicyException(CityQuestConsts.CQPolicyExceptionChangeActivityDenied, "\"Game\""); } gameEntity.IsActive = input.IsActive == null ? !gameEntity.IsActive : (bool)input.IsActive; GameDto newGameDto = (gameEntity).MapTo <GameDto>(); GameRepository.Update(gameEntity); if (newGameDto.IsActive) { UowManager.Current.Completed += (sender, e) => { GameChangesNotifier.RaiseOnGameActivated(new GameActivatedMessage(input.EntityId)); }; } else { UowManager.Current.Completed += (sender, e) => { GameChangesNotifier.RaiseOnGameDeactivated(new GameDeactivatedMessage(input.EntityId)); }; } GameRepository.Includes.Clear(); return(new ChangeActivityOutput <GameDto, long>() { Entity = newGameDto }); }
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 UpdateOutput <GameDto, long> Update(UpdateGameInput input) { GameRepository.Includes.Add(r => r.Location); GameRepository.Includes.Add(r => r.GameStatus); GameRepository.Includes.Add(r => r.GameTasks); GameRepository.Includes.Add(r => r.LastModifierUser); GameRepository.Includes.Add(r => r.CreatorUser); Game existGameEntity = GameRepository.Get(input.Entity.Id); if (existGameEntity == null) { throw new CityQuestItemNotFoundException(CityQuestConsts.CityQuestItemNotFoundExceptionMessageBody, "\"Game\""); } if (!GamePolicy.CanUpdateEntity(existGameEntity)) { throw new CityQuestPolicyException(CityQuestConsts.CQPolicyExceptionUpdateDenied, "\"Game\""); } UpdateGameEntity(existGameEntity, input.Entity); if (!string.IsNullOrEmpty(input.GameImageData)) { DeleteGameImage(existGameEntity.GameImageName); existGameEntity.GameImageName = CreateGameImage(Guid.NewGuid().ToString(), input.GameImageData); } GameDto newGameDto = (GameRepository.Get(input.Entity.Id)).MapTo <GameDto>(); UowManager.Current.Completed += (sender, e) => { GameChangesNotifier.RaiseOnGameUpdated(new GameUpdatedMessage(newGameDto.Id)); }; GameRepository.Includes.Clear(); return(new UpdateOutput <GameDto, long>() { UpdatedEntity = newGameDto }); }
/// <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); }