public async Task <ActionResult <ServiceTimerViewModel> > GetServiceTimer(int serviceTimerId) { var timer = await dbcontext.ServiceTimer.FindAsync(serviceTimerId); if (timer == null) { return(NotFound()); } var result = new ServiceTimerViewModel { ServiceTimerId = timer.ServiceTimerId, AircraftId = timer.AircraftId, Status = timer.Status, NextServiceDate = timer.NextServiceDate }; return(Ok(result)); }
public async Task <ActionResult <IEnumerable <ServiceTimerViewModel> > > CreateServiceTimer(int aircraftId, ServiceTimer timer) { if (timer == null) { return(BadRequest()); } if (timer.ServiceTimerId == 0) // default { var maxId = await dbcontext.ServiceTimer.MaxAsync(t => t.ServiceTimerId); timer.ServiceTimerId = maxId + 1; } var aircraft = dbcontext.Aircraft .Where(a => a.AircraftId == aircraftId) .Include(a => a.ServiceTimer) .FirstOrDefault(); if (aircraft == null) { return(NotFound()); } await dbcontext.ServiceTimer.AddAsync(timer); await dbcontext.SaveChangesAsync(); var result = new ServiceTimerViewModel { ServiceTimerId = timer.ServiceTimerId, AircraftId = timer.AircraftId, Status = timer.Status, NextServiceDate = timer.NextServiceDate }; return(CreatedAtAction("GetServiceTimersOfAircraftList", new { aircraftId = timer.AircraftId }, result)); }