public async Task AcceptInviteAsync_ShouldDeleteInvitesFromDatabase_AndChangeCharacterGroupId() { const int groupId = 42; const int characterId = 24; var character = new Character(); var executionContext = new NaheulbookExecutionContext(); var groupInvite = new GroupInvite { GroupId = groupId, Character = character }; var otherGroupInvite = new GroupInvite { GroupId = groupId, Character = character }; var groupInvites = new List <GroupInvite> { groupInvite, otherGroupInvite }; _unitOfWorkFactory.GetUnitOfWork().GroupInvites.GetByCharacterIdAndGroupIdWithGroupWithCharacterAsync(groupId, characterId) .Returns(groupInvite); _unitOfWorkFactory.GetUnitOfWork().GroupInvites.GetInvitesByCharacterIdAsync(characterId) .Returns(groupInvites); _unitOfWorkFactory.GetUnitOfWork().When(x => x.SaveChangesAsync()) .Do(_ => character.GroupId.Should().Be(groupId)); await _service.AcceptInviteAsync(executionContext, groupId, characterId); Received.InOrder(() => { _unitOfWorkFactory.GetUnitOfWork().GroupInvites.RemoveRange(groupInvites); _unitOfWorkFactory.GetUnitOfWork().SaveChangesAsync(); }); }
public async Task <List <Character> > GetCharacterListAsync(NaheulbookExecutionContext executionContext) { using (var uow = _unitOfWorkFactory.CreateUnitOfWork()) { return(await uow.Characters.GetForSummaryByOwnerIdAsync(executionContext.UserId)); } }
public async Task CreateLootAsync_CreateALootInDb_AndReturnIt() { const int groupId = 42; var createLootRequest = new CreateLootRequest { Name = "some-name" }; var naheulbookExecutionContext = new NaheulbookExecutionContext(); var group = new Group { Id = groupId }; _unitOfWorkFactory.GetUnitOfWork().Groups.GetAsync(groupId) .Returns(group); var actualLoot = await _service.CreateLootAsync(naheulbookExecutionContext, groupId, createLootRequest); Received.InOrder(() => { _unitOfWorkFactory.GetUnitOfWork().Loots.Add(actualLoot); _unitOfWorkFactory.GetUnitOfWork().SaveChangesAsync(); }); actualLoot.Name.Should().Be("some-name"); actualLoot.Group.Should().BeSameAs(group); }
public async Task <CharacterModifier> AddModifiersAsync(NaheulbookExecutionContext executionContext, int characterId, AddCharacterModifierRequest request) { using (var uow = _unitOfWorkFactory.CreateUnitOfWork()) { var character = await uow.Characters.GetWithGroupAsync(characterId); if (character == null) { throw new CharacterNotFoundException(characterId); } _authorizationUtil.EnsureCharacterAccess(executionContext, character); var characterModifier = _mapper.Map <CharacterModifier>(request); characterModifier.Character = character; uow.CharacterModifiers.Add(characterModifier); uow.CharacterHistoryEntries.Add(_characterHistoryUtil.CreateLogAddModifier(character, characterModifier)); await uow.SaveChangesAsync(); var session = _notificationSessionFactory.CreateSession(); session.NotifyCharacterAddModifier(characterId, characterModifier); await session.CommitAsync(); return(characterModifier); } }
public async Task <CharacterModifier> ToggleModifiersAsync(NaheulbookExecutionContext executionContext, int characterId, int characterModifierId) { using (var uow = _unitOfWorkFactory.CreateUnitOfWork()) { var character = await uow.Characters.GetWithGroupAsync(characterId); if (character == null) { throw new CharacterNotFoundException(characterId); } _authorizationUtil.EnsureCharacterAccess(executionContext, character); var characterModifier = await uow.CharacterModifiers.GetByIdAndCharacterIdAsync(characterId, characterModifierId); if (characterModifier == null) { throw new CharacterModifierNotFoundException(characterModifierId); } _characterModifierUtil.ToggleModifier(character, characterModifier); await uow.SaveChangesAsync(); var session = _notificationSessionFactory.CreateSession(); session.NotifyCharacterUpdateModifier(characterId, characterModifier); await session.CommitAsync(); return(characterModifier); } }
public async Task <ActionResult <CharacterRemoveJobResponse> > PostCharacterRemoveJobAsync( [FromServices] NaheulbookExecutionContext executionContext, [FromRoute] int characterId, CharacterRemoveJobRequest request ) { try { await _characterService.RemoveJobAsync(executionContext, characterId, request); return(new CharacterRemoveJobResponse { JobId = request.JobId }); } catch (CharacterAlreadyKnowThisJobException ex) { throw new HttpErrorException(StatusCodes.Status409Conflict, ex); } catch (JobNotFoundException ex) { throw new HttpErrorException(StatusCodes.Status400BadRequest, ex); } catch (ForbiddenAccessException ex) { throw new HttpErrorException(StatusCodes.Status403Forbidden, ex); } catch (CharacterNotFoundException ex) { throw new HttpErrorException(StatusCodes.Status404NotFound, ex); } }
public async Task <MonsterTemplate> CreateMonsterTemplateAsync(NaheulbookExecutionContext executionContext, MonsterTemplateRequest request) { await _authorizationUtil.EnsureAdminAccessAsync(executionContext); using (var uow = _unitOfWorkFactory.CreateUnitOfWork()) { var subCategory = await uow.MonsterSubCategories.GetAsync(request.SubCategoryId); if (subCategory == null) { throw new MonsterSubCategoryNotFoundException(request.SubCategoryId); } var itemTemplates = await uow.ItemTemplates.GetByIdsAsync(request.Inventory.Select(x => x.ItemTemplateId)); var monsterTemplate = new MonsterTemplate { Data = JsonConvert.SerializeObject(request.Data), Name = request.Name, SubCategory = subCategory, Items = request.Inventory.Where(i => !i.Id.HasValue || i.Id == 0).Select(i => new MonsterTemplateInventoryElement { Chance = i.Chance, ItemTemplate = itemTemplates.First(x => x.Id == i.ItemTemplateId), MaxCount = i.MaxCount, MinCount = i.MinCount }).ToList() }; uow.MonsterTemplates.Add(monsterTemplate); await uow.SaveChangesAsync(); return(monsterTemplate); } }
public async Task <MapMarker> CreateMapMarkerAsync( NaheulbookExecutionContext executionContext, int mapLayerId, MapMarkerRequest request ) { using var uow = _unitOfWorkFactory.CreateUnitOfWork(); var mapLayer = await uow.MapLayers.GetAsync(mapLayerId); if (mapLayer == null) { throw new MapLayerNotFoundException(mapLayerId); } await _authorizationUtil.EnsureCanEditMapLayerAsync(executionContext, mapLayer); var mapMarker = new MapMarker { LayerId = mapLayerId, Name = request.Name, Description = request.Description, Type = request.Type, MarkerInfo = _jsonUtil.SerializeNonNull(request.MarkerInfo) }; uow.MapMarkers.Add(mapMarker); await uow.SaveChangesAsync(); return(mapMarker); }
public void EnsureItemAccess(NaheulbookExecutionContext executionContext, Item item) { if (item.CharacterId != null) { if (item.Character !.OwnerId == executionContext.UserId) { return; } if (item.Character !.GroupId != null && item.Character !.Group !.MasterId == executionContext.UserId) { return; } } if (item.MonsterId != null && item.Monster !.Group.MasterId == executionContext.UserId) { return; } if (item.LootId != null && item.Loot !.Group.MasterId == executionContext.UserId) { return; } throw new ForbiddenAccessException(); }
public async Task <Effect> EditEffectAsync(NaheulbookExecutionContext executionContext, int effectId, EditEffectRequest request) { await _authorizationUtil.EnsureAdminAccessAsync(executionContext); using (var uow = _unitOfWorkFactory.CreateUnitOfWork()) { var effect = await uow.Effects.GetWithModifiersAsync(effectId); if (effect == null) { throw new EffectNotFoundException(); } effect.Name = request.Name; effect.SubCategoryId = request.SubCategoryId; effect.Description = request.Description; effect.Dice = request.Dice; effect.TimeDuration = request.TimeDuration; effect.CombatCount = request.CombatCount; effect.Duration = request.Duration; effect.LapCount = request.LapCount; effect.DurationType = request.DurationType; effect.Modifiers = request.Modifiers.Select(s => new EffectModifier { StatName = s.Stat, Type = s.Type, Value = s.Value }).ToList(); await uow.SaveChangesAsync(); return(effect); } }
public void SetUp() { _effectService = Substitute.For <IEffectService>(); _mapper = Substitute.For <IMapper>(); _effectSubCategoriesController = new EffectSubCategoriesController(_effectService, _mapper); _executionContext = new NaheulbookExecutionContext(); }
public async Task <Effect> CreateEffectAsync(NaheulbookExecutionContext executionContext, int subCategoryId, CreateEffectRequest request) { await _authorizationUtil.EnsureAdminAccessAsync(executionContext); var effect = new Effect { Name = request.Name, SubCategoryId = subCategoryId, Description = request.Description, Dice = request.Dice, TimeDuration = request.TimeDuration, CombatCount = request.CombatCount, Duration = request.Duration, LapCount = request.LapCount, DurationType = request.DurationType, Modifiers = request.Modifiers.Select(s => new EffectModifier { StatName = s.Stat, Type = s.Type, Value = s.Value }).ToList() }; using (var uow = _unitOfWorkFactory.CreateUnitOfWork()) { uow.Effects.Add(effect); await uow.SaveChangesAsync(); } return(effect); }
public void SetUp() { _itemService = Substitute.For <IItemService>(); _mapper = Substitute.For <IMapper>(); _controller = new ItemsController(_itemService, _mapper); _executionContext = new NaheulbookExecutionContext(); }
public async Task CreateMonsterTemplate_InsertNewEntityInDatabase() { const int subCategoryId = 10; const int locationId = 12; var itemTemplateId = Guid.NewGuid(); var executionContext = new NaheulbookExecutionContext(); var monsterSubCategory = CreateMonsterSubCategory(subCategoryId); var request = CreateRequest(subCategoryId, itemTemplateId, locationId); var itemTemplate = new ItemTemplate { Id = itemTemplateId }; var expectedMonsterTemplate = CreateMonsterTemplate(monsterSubCategory, itemTemplate); _unitOfWorkFactory.GetUnitOfWork().MonsterSubCategories.GetAsync(subCategoryId) .Returns(monsterSubCategory); _unitOfWorkFactory.GetUnitOfWork().ItemTemplates.GetByIdsAsync(Arg.Is <IEnumerable <Guid> >(x => x.SequenceEqual(new[] { itemTemplateId }))) .Returns(new List <ItemTemplate> { itemTemplate }); var monsterTemplate = await _service.CreateMonsterTemplateAsync(executionContext, request); var monsterTemplateRepository = _unitOfWorkFactory.GetUnitOfWork().MonsterTemplates; Received.InOrder(() => { monsterTemplateRepository.Add(monsterTemplate); _unitOfWorkFactory.GetUnitOfWork().Received(1).SaveChangesAsync(); }); monsterTemplate.Should().BeEquivalentTo(expectedMonsterTemplate); }
public async Task <ActionResult <ActiveStatsModifier> > PostToggleModifiersAsync( [FromServices] NaheulbookExecutionContext executionContext, [FromRoute] int characterId, [FromRoute] int characterModifierId ) { try { var characterModifier = await _characterService.ToggleModifiersAsync(executionContext, characterId, characterModifierId); return(_mapper.Map <ActiveStatsModifier>(characterModifier)); } catch (ForbiddenAccessException ex) { throw new HttpErrorException(StatusCodes.Status403Forbidden, ex); } catch (CharacterNotFoundException ex) { throw new HttpErrorException(StatusCodes.Status404NotFound, ex); } catch (CharacterModifierNotFoundException ex) { throw new HttpErrorException(StatusCodes.Status404NotFound, ex); } catch (CharacterModifierNotReusableException ex) { throw new HttpErrorException(StatusCodes.Status400BadRequest, ex); } }
public void EnsureIsCharacterOwner(NaheulbookExecutionContext executionContext, Character character) { if (character.OwnerId != executionContext.UserId) { throw new ForbiddenAccessException(); } }
public async Task <ActionResult <CharacterLevelUpResponse> > PostCharacterLevelUpAsync( [FromServices] NaheulbookExecutionContext executionContext, [FromRoute] int characterId, CharacterLevelUpRequest request ) { try { var levelUpResult = await _characterService.LevelUpCharacterAsync(executionContext, characterId, request); return(_mapper.Map <CharacterLevelUpResponse>(levelUpResult)); } catch (SpecialityNotFoundException ex) { throw new HttpErrorException(StatusCodes.Status400BadRequest, ex); } catch (InvalidTargetLevelUpRequestedException ex) { throw new HttpErrorException(StatusCodes.Status400BadRequest, ex); } catch (ForbiddenAccessException ex) { throw new HttpErrorException(StatusCodes.Status403Forbidden, ex); } catch (CharacterNotFoundException ex) { throw new HttpErrorException(StatusCodes.Status404NotFound, ex); } }
public async Task <ItemTemplate> EditItemTemplateAsync(NaheulbookExecutionContext executionContext, Guid itemTemplateId, ItemTemplateRequest request) { using (var uow = _unitOfWorkFactory.CreateUnitOfWork()) { var itemTemplate = await uow.ItemTemplates.GetWithModifiersWithRequirementsWithSkillsWithSkillModifiersWithSlotsWithUnSkillsAsync(itemTemplateId); if (itemTemplate == null) { throw new ItemTemplateNotFoundException(itemTemplateId); } await _authorizationUtil.EnsureCanEditItemTemplateAsync(executionContext, itemTemplate); if (itemTemplate.Source != request.Source) { if (request.Source == "official") { await _authorizationUtil.EnsureAdminAccessAsync(executionContext); itemTemplate.SourceUserId = null; } else { itemTemplate.SourceUserId = executionContext.UserId; } } _itemTemplateUtil.ApplyChangesFromRequest(itemTemplate, request); await uow.SaveChangesAsync(); return((await uow.ItemTemplates.GetWithModifiersWithRequirementsWithSkillsWithSkillModifiersWithSlotsWithUnSkillsAsync(itemTemplateId)) !); } }
public async Task <ActionResult <CharacterResponse> > GetCharacterDetailsAsync( [FromServices] NaheulbookExecutionContext executionContext, [FromRoute] int characterId ) { try { var character = await _characterService.LoadCharacterDetailsAsync(executionContext, characterId); if (executionContext.UserId == character.Group?.MasterId) { return(_mapper.Map <CharacterFoGmResponse>(character)); } return(_mapper.Map <CharacterResponse>(character)); } catch (ForbiddenAccessException ex) { throw new HttpErrorException(StatusCodes.Status403Forbidden, ex); } catch (CharacterNotFoundException ex) { throw new HttpErrorException(StatusCodes.Status404NotFound, ex); } }
public async Task <CreatedActionResult <MonsterResponse> > PostCreateMonsterAsync( [FromServices] NaheulbookExecutionContext executionContext, [FromRoute] int groupId, CreateMonsterRequest request ) { try { var monster = await _monsterService.CreateMonsterAsync(executionContext, groupId, request); return(_mapper.Map <MonsterResponse>(monster)); } catch (ForbiddenAccessException ex) { throw new HttpErrorException(StatusCodes.Status403Forbidden, ex); } catch (GroupNotFoundException ex) { throw new HttpErrorException(StatusCodes.Status404NotFound, ex); } catch (ItemTemplateNotFoundException ex) { throw new HttpErrorException(StatusCodes.Status400BadRequest, ex); } }
public async Task <Item> AddItemToCharacterAsync(NaheulbookExecutionContext executionContext, int characterId, CreateItemRequest request) { using (var uow = _unitOfWorkFactory.CreateUnitOfWork()) { var character = await uow.Characters.GetWithGroupAsync(characterId); if (character == null) { throw new CharacterNotFoundException(characterId); } _authorizationUtil.EnsureCharacterAccess(executionContext, character); } var item = await _itemService.AddItemToAsync(ItemOwnerType.Character, characterId, request); using (var uow = _unitOfWorkFactory.CreateUnitOfWork()) { uow.CharacterHistoryEntries.Add(_characterHistoryUtil.CreateLogAddItem(characterId, item)); await uow.SaveChangesAsync(); } var session = _notificationSessionFactory.CreateSession(); session.NotifyCharacterAddItem(characterId, item); await session.CommitAsync(); return(item); }
public async Task <CreatedActionResult <GroupInviteResponse> > PostCreateInviteAsync( [FromServices] NaheulbookExecutionContext executionContext, [FromRoute] int groupId, CreateInviteRequest request ) { try { var invite = await _groupService.CreateInviteAsync(executionContext, groupId, request); return(_mapper.Map <GroupInviteResponse>(invite)); } catch (ForbiddenAccessException ex) { throw new HttpErrorException(StatusCodes.Status403Forbidden, ex); } catch (GroupNotFoundException ex) { throw new HttpErrorException(StatusCodes.Status404NotFound, ex); } catch (CharacterNotFoundException ex) { throw new HttpErrorException(StatusCodes.Status400BadRequest, ex); } catch (CharacterAlreadyInAGroupException ex) { throw new HttpErrorException(StatusCodes.Status400BadRequest, ex); } }
public async Task DeleteModifiersAsync(NaheulbookExecutionContext executionContext, int characterId, int characterModifierId) { using (var uow = _unitOfWorkFactory.CreateUnitOfWork()) { var character = await uow.Characters.GetWithGroupAsync(characterId); if (character == null) { throw new CharacterNotFoundException(characterId); } _authorizationUtil.EnsureCharacterAccess(executionContext, character); var characterModifier = await uow.CharacterModifiers.GetByIdAndCharacterIdAsync(characterId, characterModifierId); if (characterModifier == null) { throw new CharacterModifierNotFoundException(characterModifierId); } // TODO: workaround, will change after character history rework characterModifier.CharacterId = null; // uow.CharacterModifiers.Remove(characterModifier); uow.CharacterHistoryEntries.Add(_characterHistoryUtil.CreateLogRemoveModifier(characterId, characterModifierId)); await uow.SaveChangesAsync(); var session = _notificationSessionFactory.CreateSession(); session.NotifyCharacterRemoveModifier(characterId, characterModifierId); await session.CommitAsync(); } }
public async Task <ActionResult> PostAcceptInviteAsync( [FromServices] NaheulbookExecutionContext executionContext, [FromRoute] int groupId, [FromRoute] int characterId ) { try { await _groupService.AcceptInviteAsync(executionContext, groupId, characterId); return(new NoContentResult()); } catch (ForbiddenAccessException ex) { throw new HttpErrorException(StatusCodes.Status403Forbidden, ex); } catch (InviteNotFoundException ex) { throw new HttpErrorException(StatusCodes.Status404NotFound, ex); } catch (CharacterAlreadyInAGroupException ex) { throw new HttpErrorException(StatusCodes.Status400BadRequest, ex); } }
public async Task RemoveJobAsync(NaheulbookExecutionContext executionContext, int characterId, CharacterRemoveJobRequest request) { using (var uow = _unitOfWorkFactory.CreateUnitOfWork()) { var character = await uow.Characters.GetWithOriginWithJobsAsync(characterId); if (character == null) { throw new CharacterNotFoundException(characterId); } _authorizationUtil.EnsureCharacterAccess(executionContext, character); var characterJob = character.Jobs.FirstOrDefault(x => x.JobId == request.JobId); if (characterJob == null) { throw new CharacterDoNotKnowJobException(characterId, request.JobId); } character.Jobs.Remove(characterJob); var notificationSession = _notificationSessionFactory.CreateSession(); notificationSession.NotifyCharacterRemoveJob(character.Id, request.JobId); await uow.SaveChangesAsync(); await notificationSession.CommitAsync(); } }
public async Task <CreatedActionResult <ItemResponse> > PostAddItemToCharacterInventoryAsync( [FromServices] NaheulbookExecutionContext executionContext, [FromRoute] int characterId, CreateItemRequest request ) { try { var item = await _characterService.AddItemToCharacterAsync(executionContext, characterId, request); return(_mapper.Map <ItemResponse>(item)); } catch (ForbiddenAccessException ex) { throw new HttpErrorException(StatusCodes.Status403Forbidden, ex); } catch (CharacterNotFoundException ex) { throw new HttpErrorException(StatusCodes.Status404NotFound, ex); } catch (ItemTemplateNotFoundException ex) { throw new HttpErrorException(StatusCodes.Status400BadRequest, ex); } }
public async Task <Character> CreateCharacterAsync(NaheulbookExecutionContext executionContext, CreateCharacterRequest request) { using (var uow = _unitOfWorkFactory.CreateUnitOfWork()) { var character = _characterFactory.CreateCharacter(request); if (request.GroupId.HasValue) { var group = await uow.Groups.GetAsync(request.GroupId.Value); if (group == null) { throw new GroupNotFoundException(request.GroupId.Value); } _authorizationUtil.EnsureIsGroupOwner(executionContext, group); character.Group = group; } character.OwnerId = executionContext.UserId; character.Items = await _itemUtil.CreateInitialPlayerInventoryAsync(request.Money); uow.Characters.Add(character); await uow.SaveChangesAsync(); return(character); } }
public async Task <IActionResult> DeleteModifiersAsync( [FromServices] NaheulbookExecutionContext executionContext, [FromRoute] int characterId, [FromRoute] int characterModifierId ) { try { await _characterService.DeleteModifiersAsync(executionContext, characterId, characterModifierId); return(NoContent()); } catch (ForbiddenAccessException ex) { throw new HttpErrorException(StatusCodes.Status403Forbidden, ex); } catch (CharacterNotFoundException ex) { throw new HttpErrorException(StatusCodes.Status404NotFound, ex); } catch (CharacterModifierNotFoundException ex) { throw new HttpErrorException(StatusCodes.Status404NotFound, ex); } }
public async Task <ActionResult <ItemTemplateResponse> > PutItemTemplateAsync( [FromServices] NaheulbookExecutionContext executionContext, [FromRoute] Guid itemTemplateId, ItemTemplateRequest request ) { try { var itemTemplate = await _itemTemplateService.EditItemTemplateAsync( executionContext, itemTemplateId, request ); return(_mapper.Map <ItemTemplateResponse>(itemTemplate)); } catch (ItemTemplateNotFoundException ex) { throw new HttpErrorException(StatusCodes.Status404NotFound, ex); } catch (ForbiddenAccessException ex) { throw new HttpErrorException(StatusCodes.Status403Forbidden, ex); } }
public void CreateInviteAsync_WhenFromCharacter_EnsureUserIsOwnerOfCharacter() { const int groupId = 42; const int characterId = 24; var naheulbookExecutionContext = new NaheulbookExecutionContext { UserId = 10 }; var request = new CreateInviteRequest { CharacterId = characterId, FromGroup = false }; var group = new Group { Id = groupId }; var character = new Character { Id = characterId }; _unitOfWorkFactory.GetUnitOfWork().Groups.GetAsync(groupId) .Returns(group); _unitOfWorkFactory.GetUnitOfWork().Characters.GetWithOriginWithJobsAsync(characterId) .Returns(character); _authorizationUtil.When(x => x.EnsureIsCharacterOwner(naheulbookExecutionContext, character)) .Throw(new TestException()); Func <Task> act = () => _service.CreateInviteAsync(naheulbookExecutionContext, groupId, request); act.Should().Throw <TestException>(); _unitOfWorkFactory.GetUnitOfWork().DidNotReceive().SaveChangesAsync(); }