public async Task <ServiceResponse <string> > AddLine(AddLineDTO newLine) { ServiceResponse <string> response = new ServiceResponse <string>(); Enums.LineType type = (Enums.LineType)Enum.Parse(typeof(Enums.LineType), newLine.Type); List <Coordinate> coords = new List <Coordinate>(); try { Line line = new Line { Name = newLine.Name, Type = type }; await _context.Lines.AddAsync(line); await _context.SaveChangesAsync(); foreach (CoordinateDTO coord in newLine.Coordinates) { coords.Add(new Coordinate { LineId = line.Id, XCoordinate = coord.XCoordinate, YCoordinate = coord.YCoordinate }); } await _context.Coordinates.AddRangeAsync(coords); await _context.SaveChangesAsync(); response.Data = line.Name; } catch (Exception e) { response.Success = false; response.Message = e.Message; } return(response); }
public async Task <ServiceResponse <List <LineNameDTO> > > GetLineNames(string lineType) { ServiceResponse <List <LineNameDTO> > response = new ServiceResponse <List <LineNameDTO> >(); List <LineNameDTO> lineNames = new List <LineNameDTO>(); Enums.LineType line = (Enums.LineType)Enum.Parse(typeof(Enums.LineType), lineType); try { List <Line> lines = await _context.Lines.Where(l => l.Type == line).ToListAsync(); foreach (Line temp in lines) { lineNames.Add(new LineNameDTO { Id = temp.Id, Name = temp.Name }); } response.Data = lineNames; } catch (Exception e) { response.Success = false; response.Message = e.Message; } return(response); }
public async Task <ServiceResponse <List <LineDTO> > > UpdateLine(LineDTO newLine) { ServiceResponse <List <LineDTO> > response = new ServiceResponse <List <LineDTO> >(); List <LineDTO> lineDTOs = new List <LineDTO>(); List <LineStation> lineStations = new List <LineStation>(); Enums.LineType type = (Enums.LineType)Enum.Parse(typeof(Enums.LineType), newLine.Type); foreach (int id in newLine.StationIds) { lineStations.Add(new LineStation { StationId = id, LineId = newLine.Id }); } try { Line line = await _context.Lines.Include(l => l.LineStations).FirstOrDefaultAsync(l => l.Id == newLine.Id); _context.RemoveRange(line.LineStations); await _context.SaveChangesAsync(); line.Name = newLine.Name; line.Type = type; line.LineStations = lineStations; _context.Lines.Update(line); await _context.SaveChangesAsync(); List <Line> lines = await _context.Lines.Include(l => l.LineStations).ToListAsync(); foreach (var temp in lines) { LineDTO lineDTO = new LineDTO(); lineDTO.Id = temp.Id; lineDTO.Name = temp.Name; lineDTO.Type = temp.Type.ToString(); lineDTO.StationIds = new List <int>(); foreach (var item in temp.LineStations) { lineDTO.StationIds.Add(item.StationId); } lineDTOs.Add(lineDTO); } response.Data = lineDTOs; } catch (Exception e) { response.Success = false; response.Message = e.Message; } return(response); }