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

            if (tank == null)
            {
                return(BadRequest("No Tank found with Id"));
            }

            tank.RemoveFish(model.Fish.Id);

            await _context.SaveChangesAsync();

            return(Ok(tank));
        }
Ejemplo n.º 2
0
        public async Task <IActionResult> ReplaceFishTank([FromBody] UpdateFishTankCapacityModel 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 the old fish can fit in the new capacity
            if (tank.Fishes != null && tank.Fishes.Count() > 0)
            {
                // Find the size of all the fishes
                int fishSize = tank.Fishes.ToList().Select(f => f.Size).Aggregate((sum, val) => sum + val);
                if (fishSize > model.Capacity)
                {
                    return(BadRequest($"Fish tank with Id {model.Id} not big enough to hold the fishes of the old tank"));
                }
            }


            // The new tank to replace the old
            FishTank newTank = new FishTank();

            // deocaration and plants are discarded when replacing the fish tank
            newTank.Capacity = model.Capacity;
            newTank.AddFish(tank.Fishes);

            // remove the old fishes from the old tank
            tank.RemoveFish(tank.Fishes.Select(f => f.Id));
            // add and save the new fish tank
            await _context.FishTank.AddAsync(newTank);

            await _context.SaveChangesAsync();

            return(Ok(newTank));
        }