public async Task <IActionResult> PatchCharacterAsync(
            [FromServices] NaheulbookExecutionContext executionContext,
            [FromRoute] int characterId,
            PatchCharacterRequest request
            )
        {
            try
            {
                await _characterService.UpdateCharacterAsync(executionContext, characterId, request);

                return(NoContent());
            }
            catch (ForbiddenAccessException ex)
            {
                throw new HttpErrorException(StatusCodes.Status403Forbidden, ex);
            }
            catch (CharacterNotFoundException ex)
            {
                throw new HttpErrorException(StatusCodes.Status404NotFound, ex);
            }
        }
Beispiel #2
0
        public async Task UpdateCharacterAsync(NaheulbookExecutionContext executionContext, int characterId, PatchCharacterRequest request)
        {
            var notificationSession = _notificationSessionFactory.CreateSession();

            using (var uow = _unitOfWorkFactory.CreateUnitOfWork())
            {
                var character = await uow.Characters.GetWithGroupAsync(characterId);

                if (character == null)
                {
                    throw new CharacterNotFoundException(characterId);
                }

                _authorizationUtil.EnsureCharacterAccess(executionContext, character);

                _characterUtil.ApplyCharactersChange(executionContext, request, character, notificationSession);

                await uow.SaveChangesAsync();
            }

            await notificationSession.CommitAsync();
        }
Beispiel #3
0
        public void ApplyCharactersChange(NaheulbookExecutionContext executionContext, PatchCharacterRequest request, Character character, INotificationSession notificationSession)
        {
            if (request.Debilibeuk.HasValue)
            {
                _authorizationUtil.EnsureIsGroupOwner(executionContext, character.Group);
                var gmData = _jsonUtil.Deserialize <CharacterGmData>(character.GmData) ?? new CharacterGmData();
                gmData.Debilibeuk = request.Debilibeuk.Value;
                character.GmData  = _jsonUtil.Serialize(gmData);
                notificationSession.NotifyCharacterGmChangeData(character, gmData);
            }

            if (request.Mankdebol.HasValue)
            {
                _authorizationUtil.EnsureIsGroupOwner(executionContext, character.Group);
                var gmData = _jsonUtil.Deserialize <CharacterGmData>(character.GmData) ?? new CharacterGmData();
                gmData.Mankdebol = request.Mankdebol.Value;
                character.GmData = _jsonUtil.Serialize(gmData);
                notificationSession.NotifyCharacterGmChangeData(character, gmData);
            }

            if (request.IsActive.HasValue)
            {
                _authorizationUtil.EnsureIsGroupOwner(executionContext, character.Group);
                character.IsActive = request.IsActive.Value;
                notificationSession.NotifyCharacterGmChangeActive(character);
            }

            if (request.Color != null)
            {
                _authorizationUtil.EnsureIsGroupOwner(executionContext, character.Group);
                character.Color = request.Color;
                notificationSession.NotifyCharacterGmChangeColor(character);
            }

            if (request.OwnerId != null)
            {
                _authorizationUtil.EnsureIsGroupOwner(executionContext, character.Group);
                character.OwnerId = request.OwnerId.Value;
            }

            if (request.Target != null)
            {
                _authorizationUtil.EnsureIsGroupOwner(executionContext, character.Group);
                if (request.Target.IsMonster)
                {
                    character.TargetedCharacterId = null;
                    character.TargetedMonsterId   = request.Target.Id;
                }
                else
                {
                    character.TargetedMonsterId   = null;
                    character.TargetedCharacterId = request.Target.Id;
                }

                notificationSession.NotifyCharacterGmChangeTarget(character, request.Target);
            }

            if (request.Ev.HasValue)
            {
                character.AddHistoryEntry(_characterHistoryUtil.CreateLogChangeEv(character, character.Ev, request.Ev));
                character.Ev = request.Ev;
                notificationSession.NotifyCharacterChangeEv(character);
            }

            if (request.Ea.HasValue)
            {
                character.AddHistoryEntry(_characterHistoryUtil.CreateLogChangeEa(character, character.Ea, request.Ea));
                character.Ea = request.Ea;
                notificationSession.NotifyCharacterChangeEa(character);
            }

            if (request.FatePoint.HasValue)
            {
                character.AddHistoryEntry(_characterHistoryUtil.CreateLogChangeFatePoint(character, character.FatePoint, request.FatePoint));
                character.FatePoint = request.FatePoint.Value;
                notificationSession.NotifyCharacterChangeFatePoint(character);
            }

            if (request.Experience.HasValue)
            {
                character.AddHistoryEntry(_characterHistoryUtil.CreateLogChangeExperience(character, character.Experience, request.Experience));
                character.Experience = request.Experience.Value;
                notificationSession.NotifyCharacterChangeExperience(character);
            }

            if (request.Sex != null)
            {
                character.AddHistoryEntry(_characterHistoryUtil.CreateLogChangeSex(character, character.Sex, request.Sex));
                character.Sex = request.Sex;
                notificationSession.NotifyCharacterChangeSex(character);
            }

            if (request.Name != null)
            {
                character.AddHistoryEntry(_characterHistoryUtil.CreateLogChangeName(character, character.Name, request.Name));
                character.Name = request.Name;
                notificationSession.NotifyCharacterChangeName(character);
            }

            if (request.Notes != null)
            {
                character.Notes = request.Notes;
                notificationSession.NotifyCharacterChangeNotes(character);
            }
        }