public async Task <ActionResult <int> > CreateShip([FromBody] CreateShipDto createShipDto)
        {
            try
            {
                var shipId = await _battleShipService.CreateShip(createShipDto);

                return(Ok(shipId));
            }
            catch (NotFoundException ex)
            {
                return(NotFound(ex.Message));
            }
            catch (Exception ex)
            {
                return(BadRequest(ex));
            }
        }
示例#2
0
        public async Task <int> CreateShip(CreateShipDto createShip)
        {
            var boardEntity = await _battleShipDbContext.Boards.FindAsync(createShip.BoardId);

            if (boardEntity == null)
            {
                throw new NotFoundException(nameof(Board), createShip.BoardId);
            }
            List <int> lstX = new List <int>();
            List <int> lstY = new List <int>();

            if (createShip.Orientation == BattleShipOrientation.Horizontal)
            {
                for (int t = 0; t < createShip.ShipLength; t++)
                {
                    lstY.Add(createShip.StartY + t);
                }
            }
            else
            {
                for (int t = 0; t < createShip.ShipLength; t++)
                {
                    lstX.Add(createShip.StartX + t);
                }
            }
            var overridenParts = _battleShipDbContext.BattleShips.Include(x => x.BattleShipCoordinates).
                                 Where(x => x.Board.BoardId == boardEntity.BoardId).SelectMany(x => x.BattleShipCoordinates, (p, q) => new
            {
                q.X,
                q.Y
            }).Where(part => lstX.Contains(part.X) && lstY.Contains(part.Y))?.ToList();

            if (overridenParts.Count > 0)
            {
                throw new BattleShipOverrideException();
            }
            var shipEntity = new BattleShip()
            {
                Board = boardEntity
            };
            await _battleShipDbContext.BattleShips.AddAsync(shipEntity);

            if (createShip.Orientation == BattleShipOrientation.Horizontal)
            {
                foreach (var item in lstX)
                {
                    await _battleShipDbContext.BattleShipCoordinates.AddAsync(new BattleShipCoordinate()
                    {
                        BattleShip = shipEntity,
                        X          = item,
                        Y          = createShip.StartY
                    });
                }
            }
            else
            {
                foreach (var item in lstY)
                {
                    await _battleShipDbContext.BattleShipCoordinates.AddAsync(new BattleShipCoordinate()
                    {
                        BattleShip = shipEntity,
                        X          = createShip.StartX,
                        Y          = item
                    });
                }
            }
            await _battleShipDbContext.SaveChangesAsync();

            return(shipEntity.BattleShipId);
        }
 public async Task <int> CreateShip(CreateShipDto createShip)
 {
     return(await _shipRepo.CreateShip(createShip));
 }