public async Task<int> Save(long routeId, long stationId, int number) { if(routeId <= 0) throw new ArgumentException("routedId <= 0"); if(stationId <= 0) throw new ArgumentException("stationId <= 0"); using (var db = new Db()) { using (var transtaction = db.Database.BeginTransaction(IsolationLevel.RepeatableRead)) { try { var route = db.Routes.SingleOrDefault(r => r.Id == routeId); if (route == null) throw new NullReferenceException("route"); var station = db.Stations.SingleOrDefault(r => r.Id == stationId); if (station == null) throw new NullReferenceException("station"); var routeStation = new RouteStation() { Number = number, RouteId = routeId, StationId = stationId }; db.Entry(routeStation).State = EntityState.Added; return await db.SaveChangesAsync(); } catch (Exception ex) { Log.Error(ex.Message); transtaction.Rollback(); 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 async Task<int> Delete(long routeId, long stationId) { if(routeId <= 0) throw new ArgumentException("routeId <= 0"); if(stationId <= 0) throw new ArgumentException("stationId <= 0"); using (var db = new Db()) { try { var routeStation = new RouteStation() {RouteId = routeId, StationId = stationId}; db.Entry(routeStation).State = EntityState.Deleted; return await db.SaveChangesAsync(); } catch (Exception ex) { Log.Error(ex.Message); throw; } } }