public override void OnActionExecuting(ActionExecutingContext context)
        {
            logger.LogDebug("Start - OnActionExecuting");

            _ = context.ActionArguments.TryGetValue("boardId", out object foreignKey) &&
                foreignKey is int;
            int boardId = (int)foreignKey;

            if (context.ActionArguments.TryGetValue("Id", out object value) &&
                value is int battleshipId)
            {
                var badRequestResult = new BadRequestObjectResult($"Invalid battle ship id {battleshipId}");
                if (battleshipId <= 0)
                {
                    context.Result = badRequestResult;
                }
                else
                {
                    var board = battleShipRepo.Get(boardId, battleshipId);
                    if (board == null)
                    {
                        context.Result = badRequestResult;
                    }
                }
            }

            logger.LogDebug("End - OnActionExecuting");
        }
Example #2
0
        public IActionResult ApiBattleShipGet(int boardId, int id)
        {
            logger.LogDebug($"Start - Request for battle ship {id}");

            var battleShip = battleShipRepo.Get(boardId, id);
            var response   = ResponseFactory.Create(battleShip);

            logger.LogDebug($"End - Request for battle ship {id}");

            return(Ok(response));
        }
Example #3
0
        public BattleShip SaveBattleShip(
            int boardId, List <BattleShipBlock> blocksToAssociate)
        {
            logger.LogDebug($"Start - Save new battle ship");

            // construct battle ship to save in repo
            var battleShipToSave = new BattleShip
            {
                BoardId = boardId
            };
            var battleShipFromRepo = battleShipRepo.Add(battleShipToSave);
            // retrieve battle ship Id
            var battleshipId = battleShipFromRepo.Id;
            // construct battle ship blocks
            // retrieve blockIds for given block numbers on board
            var blocksFromRepo = blockRepo.ListByBoard(boardId);

            blocksToAssociate = blocksFromRepo.Join(
                blocksToAssociate,
                br => br.Number,
                bta => bta.Block.Number,
                (br, bta) =>
                new BattleShipBlock
            {
                BattleShipId = battleshipId,
                BlockId      = br.Id
            })
                                .ToList();
            var blocksAssociatedFromRepo =
                blockRepo.AssociateBlockToBattleShip(blocksToAssociate);

            battleShipFromRepo = battleShipRepo.Get(boardId, battleShipFromRepo.Id);

            logger.LogDebug($"End - Save new battle ship");

            return(battleShipFromRepo);
        }