public async Task <IActionResult> AddShip(Guid boardId, Guid shipId, AddShipRequest addShipRequest) { var boardTask = _boardService.GetBoard(boardId); var shipTask = _shipService.GetShip(shipId); var board = await boardTask; var ship = await shipTask; await _shipService.AddShipOnBoard(boardId, ship, addShipRequest); return(NoContent()); }
public async Task Given_Ship_Must_Not_Be_Placed_If_Its_Already_Placed() { var boardId = Guid.NewGuid(); var shipId = Guid.NewGuid(); _boardService.GetBoard(boardId).Returns(x => new Board { Id = boardId }); _shipService.GetShip(shipId).Returns(x => new Ship { Id = shipId }); _shipService.GetShipsPlaced(boardId).Returns(x => new List <Ship> { new Ship { Id = shipId } }); var response = await _shipController.PlaceShip(boardId, shipId, null); response.Should().BeOfType <BadRequestObjectResult>(); ((BadRequestObjectResult)response).Value.Should().Be("Ship cannot be placed as requested ship already placed"); }
public async Task <IActionResult> PlaceShip(Guid boardId, Guid shipId, PlaceShipRequest placeShipRequest) { var boardTask = _boardService.GetBoard(boardId); var shipTask = _shipService.GetShip(shipId); var shipsPlacedTask = _shipService.GetShipsPlaced(boardId); await Task.WhenAll(boardTask, shipTask, shipsPlacedTask); var board = await boardTask; var ship = await shipTask; var shipsPlaced = await shipsPlacedTask; if (board == null || ship == null) { return(NotFound()); } if (shipsPlaced.Any(x => x.Id == shipId)) { return(BadRequest("Ship cannot be placed as requested ship already placed")); } var isShipToBePlacedWithInBoundaries = IsShipToBePlacedWithInBoundaries(board, ship, placeShipRequest); if (isShipToBePlacedWithInBoundaries == false) { return(BadRequest("Ship cannot be placed as positions should not cross boundaries")); } var shipPlacedPositions = shipsPlaced.SelectMany(x => x.Positions).ToList(); var haveShipsAlreadyOccupiedThePosition = HaveShipsAlreadyOccupiedThePosition(ship, shipPlacedPositions, placeShipRequest); if (haveShipsAlreadyOccupiedThePosition) { return(BadRequest("Ship cannot be placed as the positions are occupied with ships")); } await _shipService.PlaceShip(boardId, ship, placeShipRequest); return(NoContent()); }