예제 #1
0
        public async Task <IActionResult> MoveFish(int fishId, int tankId)
        {
            var fish = await _context.Fish.FirstOrDefaultAsync(f => f.FishId == fishId);

            var currentTank = await _context.Tank.FirstAsync(t => t.TankId == fish.TankId);

            var destinationTank = await _context.Tank.FirstAsync(t => t.TankId == tankId);


            if (fish == null)
            {
                return(NotFound());
            }

            var tankIsFull = destinationTank.NumberOfFish >= destinationTank.Capacity;

            if (tankIsFull)
            {
                return(BadRequest("Tank full"));
            }


            _context.Movement.Add(new Movement {
                FishId = fishId, TankIdTo = tankId, TankIdFrom = fish.TankId
            });

            currentTank.NumberOfFish--;
            destinationTank.NumberOfFish++;
            fish.TankId = tankId;

            await _context.SaveChangesAsync();

            return(Ok());
        }
예제 #2
0
        public async Task <IActionResult> PutTank(int id, Tank tank)
        {
            if (id != tank.TankId)
            {
                return(BadRequest());
            }

            _context.Entry(tank).State = EntityState.Modified;

            try
            {
                await _context.SaveChangesAsync();
            }
            catch (DbUpdateConcurrencyException)
            {
                if (!TankExists(id))
                {
                    return(NotFound());
                }
                else
                {
                    throw;
                }
            }

            return(NoContent());
        }