public async Task <CreatedActionResult <ItemResponse> > PostAddRandomItemToMonsterInventoryAsync( [FromServices] NaheulbookExecutionContext executionContext, [FromRoute] int monsterId, CreateRandomItemRequest request ) { try { var item = await _monsterService.AddRandomItemToMonsterAsync(executionContext, monsterId, request); return(_mapper.Map <ItemResponse>(item)); } catch (ForbiddenAccessException ex) { throw new HttpErrorException(StatusCodes.Status403Forbidden, ex); } catch (MonsterNotFoundException ex) { throw new HttpErrorException(StatusCodes.Status404NotFound, ex); } catch (ItemTemplateSubCategoryNotFoundException ex) { throw new HttpErrorException(StatusCodes.Status400BadRequest, ex); } }
public async Task <Item> AddRandomItemToMonsterAsync(NaheulbookExecutionContext executionContext, int monsterId, CreateRandomItemRequest 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.AddRandomItemToAsync(ItemOwnerType.Monster, monsterId, request, executionContext.UserId); var session = _notificationSessionFactory.CreateSession(); session.NotifyMonsterAddItem(monsterId, item); await session.CommitAsync(); return(item); }