private static void ImportAnomalyToDatabase(AnomalyDto anomalyDto) { if (anomalyDto.OriginPlanet == null) { throw new ArgumentNullException("Origin planet cannot be null!"); } else if (anomalyDto.TeleportPlanet == null) { throw new ArgumentNullException("Teleport planet cannot be null!"); } using (var db = new MassDefectDatabaseContext()) { var originPlanet = db.Planets.FirstOrDefault(p => p.Name == anomalyDto.OriginPlanet); var teleportPlanet = db.Planets.FirstOrDefault(p => p.Name == anomalyDto.TeleportPlanet); if (originPlanet == null || teleportPlanet == null) { throw new ArgumentNullException("Origin and teleport planet doesn't exist in dbo.Planets"); } var newAnomaly = new Anomaly() { OriginPlanet = originPlanet, TeleportPlanet = teleportPlanet }; db.Anomalies.Add(newAnomaly); db.SaveChanges(); } }
public IAnomalyDto GetAnomalyById(int anomalyId) { Anomaly anomaly = this.unitOfWork.AnomaliesDbRepository.Find(anomalyId); IAnomalyDto anomalyDto = new AnomalyDto() { Id = anomaly.Id, OriginPlanetId = anomaly.OriginPlanetId, TeleportPlanetId = anomaly.TeleportPlanetId }; return(anomalyDto); }
public IEnumerable <IAnomalyDto> GetAnomaliesThatTeleportTo(int planetId) { IList <Anomaly> anomalies = this.unitOfWork.AnomaliesDbRepository.GetAll(a => a.TeleportPlanetId == planetId).ToList(); IList <IAnomalyDto> anomalyDtos = new List <IAnomalyDto>(anomalies.Count); foreach (Anomaly anomaly in anomalies) { IAnomalyDto anomalyDto = new AnomalyDto() { Id = anomaly.Id, OriginPlanetId = anomaly.OriginPlanetId, TeleportPlanetId = anomaly.TeleportPlanetId }; anomalyDtos.Add(anomalyDto); } return(anomalyDtos); }
public IEnumerable <IAnomalyDto> GetPersonAnomalies(int personId) { Person person = this.unitOfWork.PersonsDbRepository.Find(personId); IList <Anomaly> anomalies = person.Anomalies.ToList(); IList <IAnomalyDto> anomaliDtos = new List <IAnomalyDto>(anomalies.Count); foreach (Anomaly anomaly in anomalies) { IAnomalyDto anomalyDto = new AnomalyDto() { Id = anomaly.Id, OriginPlanetId = anomaly.OriginPlanetId, TeleportPlanetId = anomaly.TeleportPlanetId }; anomaliDtos.Add(anomalyDto); } return(anomaliDtos); }