public async Task<int> Save(long routeId, Station station, int number) { if(routeId <= 0) throw new ArgumentException("routeId <= 0"); if(station == null) throw new ArgumentNullException("station"); using (var db = new Db()) { using (var transaction = db.Database.BeginTransaction(IsolationLevel.RepeatableRead)) { try { var route = db.Routes.SingleOrDefault(s => s.Id == routeId); if(route == null) throw new NullReferenceException("route"); station = await _stationRepository.GetByLatLng(station.Latitude, station.Longitude); if(station == null) throw new NullReferenceException("station"); var routeStation = new RouteStation(){Number = number,RouteId = routeId,StationId = station.Id}; db.Entry(routeStation).State = EntityState.Added; return await db.SaveChangesAsync(); } catch (Exception ex) { transaction.Rollback(); Log.Error(ex.Message); throw; } } } }
public async Task<int> Delete(long routeId, Station station) { if(routeId <= 0) throw new ArgumentException("routeId <= 0"); if(station == null) throw new ArgumentNullException("station"); using (var db = new Db()) { try { if (station.Id <= 0) { station = await _stationRepository.GetByLatLng(station.Latitude, station.Longitude); if(station == null)throw new NullReferenceException("station"); } var routeStation = new RouteStation() {RouteId = routeId, StationId = station.Id}; db.Entry(routeStation).State = EntityState.Deleted; return await db.SaveChangesAsync(); } catch (Exception ex) { Log.Error(ex.Message); throw; } } }
public Task<int> Save(long routeId, Station[] stations) { throw new System.NotImplementedException(); }