Ejemplo n.º 1
0
        public async Task <IActionResult> AddItemToFishTank(AddItemToFishTank model)
        {
            // Find fish tank
            FishTank tank = await _context.FishTank.FirstOrDefaultAsync(t => t.Id == model.Id);

            if (tank == null)
            {
                return(BadRequest($"Fish tank with Id {model.Id} not found"));
            }


            // check if it is a fish or an item and get its size
            int newItemSize = model.Fish != null ? model.Fish.Size : model.Item.Size;

            // check if the fish tank has the capacity to hold it
            if (tank.AvailableCapacity < newItemSize)
            {
                return(BadRequest($"Fish tank with Id {model.Id} cannot fit the new item"));
            }

            // add the new item
            if (model.Fish != null)
            {
                tank.AddFish(model.Fish);
            }
            else
            {
                tank.AddItem(model.Item);
            }

            await _context.SaveChangesAsync();

            return(Ok(tank));
        }
Ejemplo n.º 2
0
        public async Task <IActionResult> AddToNextAvailable(AddItemModel model)
        {
            FishTank tank = await NextAvailableFishTankWithCapacity(model.Decoration.Size);

            if (tank == null)
            {
                return(BadRequest("No Tank found that can hold this fish, please add a new tank"));
            }

            tank.AddItem(model.Decoration);

            await _context.SaveChangesAsync();

            return(Ok(tank));
        }