Exemplo n.º 1
0
 public IActionResult AssignTruckToAction(int actionId, FireTruckToActionDTO request)
 {
     if (request.IdAction != actionId)
     {
         return(BadRequest("Action Id doesn't match!"));
     }
     dbService.AssignFireTruckToAction(request);
     return(Ok("Firetruck assigned to action!"));
 }
Exemplo n.º 2
0
        public void AssignFireTruckToAction(FireTruckToActionDTO request)
        {
            var eq = dbContext.Actions.Where(a => a.IdAction == request.IdAction && a.EndTime == null).FirstOrDefault();

            //Check if action is available
            if (eq == null)
            {
                throw new Exception("Action doesn't exists or is closed");
            }
            int ifNeededSpecialEq = eq.NeedSpecialEquipment;


            //check if truck is available by ID and also if special equipment is not needed
            var truck = dbContext.FireTrucks
                        .Include(fa => fa.FireTruckActions)
                        .ThenInclude(a => a.Action)
                        .Where(ft => ft.IdFireTruck == request.IdFireTruck && ft.SpecialEquipment == ifNeededSpecialEq)
                        .FirstOrDefault();

            if (truck == null)
            {
                throw new Exception("Truck doesn't have special equipment or doesn't exists!");
            }


            //Check if truck is not assigned
            foreach (var rec in truck.FireTruckActions)
            {
                if (rec.Action.EndTime == null)
                {
                    throw new Exception("Truck already assigned to action!");
                }
            }



            dbContext.Attach(new FireTruckAction()
            {
                IdFireTruck   = request.IdFireTruck,
                IdAction      = request.IdAction,
                AssigmentDate = DateTime.Now
            });
            dbContext.SaveChanges();
        }