public async Task <Line> UpdateLine(int lineId, NewLineDto line) { var lineForUpdate = await _lineRepository.GetLine(lineId); var busesInDb = new List <Bus>(lineForUpdate.Buses); _mapper.Map(line, lineForUpdate); var busesToUpdate = new List <Bus>(lineForUpdate.Buses); lineForUpdate.Buses.Clear(); lineForUpdate.Stations = null; if (await SaveAll()) { lineForUpdate = await _lineRepository.GetLine(lineId); foreach (var bus in busesToUpdate) { busesInDb.Remove(busesInDb.FirstOrDefault(b => b.Id == bus.Id)); } foreach (var bus in busesInDb) { var busToUpdate = await _context.Busses.FirstOrDefaultAsync(b => b.Id == bus.Id); busToUpdate.InUse = false; lineForUpdate.Buses.Remove(busToUpdate); _context.Update(busToUpdate); await SaveAll(); } foreach (var bus in busesToUpdate) { var busToUpdate = await _context.Busses.FirstOrDefaultAsync(b => b.Id == bus.Id); busToUpdate.InUse = true; busToUpdate.LineId = lineForUpdate.Id; _context.Update(busToUpdate); lineForUpdate.Buses.Add(busToUpdate); await SaveAll(); } if (lineForUpdate.Stations == null) { foreach (var station in line.Stations) { await AddStationToLine(station.Id, lineId); } } return(lineForUpdate); } else { return(null); } }
public async Task <IActionResult> UpdateLine(int lineId, NewLineDto line) { var result = await _publicTransportRepository.UpdateLine(lineId, line); if (result != null) { if (line.Stations != null) { foreach (var station in line.Stations) { await _publicTransportRepository.AddStationToLine(station.Id, result.Id); } } return(Ok(result)); } else { return(BadRequest("Failed to update line")); } }
public async Task <IActionResult> AddLine(NewLineDto line) { var lineToAdd = new Line(); _mapper.Map(line, lineToAdd); var result = await _publicTransportRepository.AddLine(lineToAdd); if (result != null) { if (line.Stations != null) { foreach (var station in line.Stations) { await _publicTransportRepository.AddStationToLine(station.Id, result.Id); } } return(Ok(result)); } else { return(BadRequest("Failed while creating new line")); } }