예제 #1
0
        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);
        }
예제 #2
0
        public async Task <Item> AddItemToMonsterAsync(NaheulbookExecutionContext executionContext, int monsterId, CreateItemRequest request)
        {
            using (var uow = _unitOfWorkFactory.CreateUnitOfWork())
            {
                var monster = await uow.Monsters.GetAsync(monsterId);

                if (monster == null)
                {
                    throw new MonsterNotFoundException(monsterId);
                }

                var group = await uow.Groups.GetGroupsWithCharactersAsync(monster.GroupId);

                if (group == null)
                {
                    throw new GroupNotFoundException(monster.GroupId);
                }

                _authorizationUtil.EnsureIsGroupOwner(executionContext, monster.Group);
            }

            var item = await _itemService.AddItemToAsync(ItemOwnerType.Monster, monsterId, request);

            var session = _notificationSessionFactory.CreateSession();

            session.NotifyMonsterAddItem(monsterId, item);
            await session.CommitAsync();

            return(item);
        }
        public async Task AddItemToCharacterAsync_ShouldCall_ItemService()
        {
            const int characterId = 4;
            var       character   = new Character {
                Id = characterId
            };
            var executionContext = new NaheulbookExecutionContext();
            var request          = new CreateItemRequest();
            var expectedItem     = new Item();

            _unitOfWorkFactory.GetUnitOfWork().Characters.GetWithGroupAsync(characterId)
            .Returns(character);
            _itemService.AddItemToAsync(ItemOwnerType.Character, characterId, request)
            .Returns(expectedItem);

            var item = await _service.AddItemToCharacterAsync(executionContext, characterId, request);

            item.Should().BeSameAs(expectedItem);
        }