public async Task <IActionResult> PutLootVisibilityAsync(
            [FromServices] NaheulbookExecutionContext executionContext,
            [FromRoute] int lootId,
            PutLootVisibilityRequest request
            )
        {
            try
            {
                await _lootService.UpdateLootVisibilityAsync(executionContext, lootId, request);

                return(NoContent());
            }
            catch (ForbiddenAccessException ex)
            {
                throw new HttpErrorException(StatusCodes.Status403Forbidden, ex);
            }
            catch (LootNotFoundException ex)
            {
                throw new HttpErrorException(StatusCodes.Status404NotFound, ex);
            }
        }
Example #2
0
        public async Task UpdateLootVisibilityAsync(NaheulbookExecutionContext executionContext, int lootId, PutLootVisibilityRequest request)
        {
            using (var uow = _unitOfWorkFactory.CreateUnitOfWork())
            {
                var loot = await uow.Loots.GetAsync(lootId);

                if (loot == null)
                {
                    throw new LootNotFoundException(lootId);
                }

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

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

                _authorizationUtil.EnsureIsGroupOwner(executionContext, loot.Group);

                loot.IsVisibleForPlayer = request.VisibleForPlayer;

                var session = _notificationSessionFactory.CreateSession();
                session.NotifyLootUpdateVisibility(lootId, request.VisibleForPlayer);

                if (loot.IsVisibleForPlayer)
                {
                    var fullLootData = await uow.Loots.GetWithAllDataAsync(lootId);

                    foreach (var character in group.Characters)
                    {
                        session.NotifyCharacterShowLoot(character.Id, fullLootData);
                    }
                }
                else
                {
                    foreach (var character in group.Characters)
                    {
                        session.NotifyCharacterHideLoot(character.Id, loot.Id);
                    }
                }

                await uow.SaveChangesAsync();

                await session.CommitAsync();
            }
        }