Beispiel #1
0
        private bool dbPutRingStation(int id, RingStationRepresentation ringStation)
        {
            var ringStationInDatabase = db.RingStations.Find(id);

            if (ringStationInDatabase == null)
            {
                return(false);
            }

            foreach (var animalType in ringStationInDatabase.AnimalTypes)
            {
                animalType.RingStations.Remove(ringStationInDatabase);
            }

            ringStationInDatabase.AnimalTypes = new List <AnimalType>();

            foreach (var animalType in ringStation.AnimalTypes)
            {
                var animalTypeInDatabase = db.AnimalTypes.FirstOrDefault(elem => elem.Name == animalType.Name);
                if (animalTypeInDatabase == null)
                {
                    animalTypeInDatabase = new AnimalType()
                    {
                        Name = animalType.Name
                    };
                }

                ringStationInDatabase.AnimalTypes.Add(animalTypeInDatabase);
                animalTypeInDatabase.RingStations.Add(ringStationInDatabase);
            }

            if (ringStationInDatabase.Park != null)
            {
                ringStationInDatabase.Park.RingStations.Remove(ringStationInDatabase);
                ringStationInDatabase.Park = null;
            }

            if (ringStation.Park != null)
            {
                var parkInDb = db.Parks.FirstOrDefault(elem => elem.Name == ringStation.Park.Name);
                if (parkInDb == null)
                {
                    parkInDb = new Park()
                    {
                        Name = ringStation.Park.Name
                    };
                }
                ringStationInDatabase.Park = parkInDb;
                parkInDb.RingStations.Add(ringStationInDatabase);
            }

            ringStationInDatabase.Name = ringStation.Name;

            db.SaveChanges();

            return(true);
        }
        private RingStationRepresentation GetRingStation(RingStation ringStation)
        {
            var representation = new RingStationRepresentation()
            {
                Id   = ringStation.Id,
                Name = ringStation.Name
            };

            representation.Links.Add(LinkTemplates.RingStations.DeleteRingStation.CreateLink(new { id = ringStation.Id }));
            representation.Links.Add(LinkTemplates.RingStations.UpdateRingStation.CreateLink(new { id = ringStation.Id }));
            return(representation);
        }
Beispiel #3
0
        private RingStationRepresentation GetRingStation(RingStation ringStation)
        {
            var representation = new RingStationRepresentation()
            {
                Id   = ringStation.Id,
                Name = ringStation.Name
            };

            representation.Links.Add(LinkTemplates.RingStations.DeleteRingStation.CreateLink(new { id = ringStation.Id }));
            representation.Links.Add(LinkTemplates.RingStations.UpdateRingStation.CreateLink(new { id = ringStation.Id }));
            representation.Park        = ringStation.Park == null ? null : GetPark(ringStation.Park);
            representation.AnimalTypes = ringStation.AnimalTypes.Select(animalType => GetAnimalType(animalType)).ToList();
            return(representation);
        }
Beispiel #4
0
        public IHttpActionResult PutRingStation(int id, RingStationRepresentation ringStation)
        {
            if (!ModelState.IsValid)
            {
                return(BadRequest(ModelState));
            }

            if (dbPutRingStation(id, ringStation))
            {
                return(StatusCode(HttpStatusCode.NoContent));
            }
            else
            {
                return(NotFound());
            }
        }
Beispiel #5
0
        private RingStation dbPostRingStation(RingStationRepresentation ringStation)
        {
            var ringStationInDatabase = db.RingStations.FirstOrDefault(elem => elem.Name == ringStation.Name);

            if (ringStationInDatabase != null)
            {
                return(null);
            }
            ringStationInDatabase = new RingStation()
            {
                Name = ringStation.Name
            };
            db.RingStations.Add(ringStationInDatabase);

            foreach (var animalType in ringStation.AnimalTypes)
            {
                var animalTypeInDatabase = db.AnimalTypes.FirstOrDefault(elem => elem.Name == animalType.Name);
                if (animalTypeInDatabase == null)
                {
                    animalTypeInDatabase = new AnimalType()
                    {
                        Name = animalType.Name
                    };
                }
                animalTypeInDatabase.RingStations.Add(ringStationInDatabase);
                ringStationInDatabase.AnimalTypes.Add(animalTypeInDatabase);
            }

            if (ringStation.Park != null)
            {
                var parkInDb = db.Parks.FirstOrDefault(elem => elem.Name == ringStation.Park.Name);
                if (parkInDb == null)
                {
                    parkInDb = new Park()
                    {
                        Name = ringStation.Park.Name
                    };
                }
                ringStationInDatabase.Park = parkInDb;
                parkInDb.RingStations.Add(ringStationInDatabase);
            }

            db.SaveChanges();

            return(ringStationInDatabase);
        }
Beispiel #6
0
        public IHttpActionResult PostRingStation(RingStationRepresentation ringStation)
        {
            if (!ModelState.IsValid)
            {
                return(BadRequest(ModelState));
            }

            var ringStationInDatabase = dbPostRingStation(ringStation);

            if (ringStationInDatabase != null)
            {
                return(CreatedAtRoute("DefaultApi", new { id = ringStationInDatabase.Id }, GetRingStation(ringStationInDatabase)));
            }
            else
            {
                return(BadRequest());
            }
        }