예제 #1
0
        public Vehicle ToModel(star_wars_apiContext context)
        {
            Vehicle Vehicle = context.Vehicle.Find(this.id);

            bool newObject = false;

            if (Vehicle == null)
            {
                newObject = true;
                Vehicle   = new Vehicle();
            }

            Vehicle.id            = this.id;
            Vehicle.created       = this.created;
            Vehicle.edited        = this.edited;
            Vehicle.vehicleClass  = this.vehicleClass;
            Vehicle.cargoCapacity = this.cargoCapacity;
            Vehicle.consumables   = this.consumables;
            Vehicle.costInCredits = this.costInCredits;
            Vehicle.crew          = this.crew;
            Vehicle.length        = this.length;
            Vehicle.manufacturer  = this.manufacturer;
            Vehicle.model         = this.model;
            Vehicle.name          = this.name;
            Vehicle.passengers    = this.passengers;
            Vehicle.filmIds       = new List <FilmVehicle>();
            Vehicle.pilotIds      = new List <VehicleCharacter>();

            if (newObject == false)
            {
                // many-many mapping classes have the IDs as foreign keys - if the object does not yet exist it cannot be linked to other objects.
                foreach (int filmId in this.filmIds)
                {
                    if (context.Film.Find(filmId) != null)
                    {
                        FilmVehicle filmVehicle = context.FilmVehicle.Find(filmId, this.id);
                        if (filmVehicle == null)
                        {
                            Vehicle.filmIds.Add(new FilmVehicle(filmId, this.id));
                        }
                        else
                        {
                            Vehicle.filmIds.Add(filmVehicle);
                        }
                    }
                }

                foreach (int characterId in this.pilotIds)
                {
                    if (context.Character.Find(characterId) != null)
                    {
                        VehicleCharacter vehicleCharacter = context.VehicleCharacter.Find(this.id, characterId);
                        if (vehicleCharacter == null)
                        {
                            Vehicle.pilotIds.Add(new VehicleCharacter(this.id, characterId));
                        }
                        else
                        {
                            Vehicle.pilotIds.Add(vehicleCharacter);
                        }
                    }
                }
            }
            return(Vehicle);
        }
        public Character ToModel(star_wars_apiContext context)
        {
            Character character = context.Character.Find(this.id);

            bool newObject = false;

            if (character == null)
            {
                newObject = true;
                character = new Character();
            }

            character.id = this.id;
            if (newObject == true)
            {
                character.created = DateTime.Now;
            }
            else
            {
                character.created = this.created;
            }
            character.edited = DateTime.Now;
            if (this.name == null)
            {
                throw new ArgumentException("The character name is mandatory");
            }
            else
            {
                character.name = this.name;
            }
            character.height      = this.height;
            character.hairColor   = this.hairColor;
            character.eyeColor    = this.eyeColor;
            character.birthYear   = this.birthYear;
            character.gender      = this.gender;
            character.homeworldId = this.homeworldId;
            character.filmIds     = new List <FilmCharacter>();    //Mandatory - at least one
            character.speciesIds  = new List <SpeciesCharacter>(); //Mandatory - one only
            character.vehicleIds  = new List <VehicleCharacter>();
            character.starshipIds = new List <StarshipCharacter>();
            if (this.filmIds.Count <= 1)
            {
                throw new ArgumentException("The character must be linked to at least one film");
            }
            if (this.speciesIds.Count != 1)
            {
                throw new ArgumentException("The character must be linked to one and only one species");
            }

            if (newObject == false)
            {
                // many-many mapping classes have the IDs as foreign keys - if the object does not yet exist it cannot be linked to other objects.
                foreach (int filmId in this.filmIds)
                {
                    if (context.Film.Find(filmId) != null)
                    {
                        FilmCharacter filmCharacter = context.FilmCharacter.Find(filmId, this.id);
                        if (filmCharacter == null)
                        {
                            character.filmIds.Add(new FilmCharacter(filmId, this.id));
                        }
                        else
                        {
                            character.filmIds.Add(filmCharacter);
                        }
                    }
                }

                foreach (int speciesId in this.speciesIds)
                {
                    if (context.Species.Find(speciesId) != null)
                    {
                        SpeciesCharacter speciesCharacter = context.SpeciesCharacter.Find(speciesId, this.id);
                        if (speciesCharacter == null)
                        {
                            character.speciesIds.Add(new SpeciesCharacter(speciesId, this.id));
                        }
                        else
                        {
                            character.speciesIds.Add(speciesCharacter);
                        }
                    }
                }

                foreach (int vehicleId in this.vehicleIds)
                {
                    if (context.Vehicle.Find(vehicleId) != null)
                    {
                        VehicleCharacter vehicleCharacter = context.VehicleCharacter.Find(vehicleId, this.id);
                        if (vehicleCharacter == null)
                        {
                            character.vehicleIds.Add(new VehicleCharacter(vehicleId, this.id));
                        }
                        else
                        {
                            character.vehicleIds.Add(vehicleCharacter);
                        }
                    }
                }

                foreach (int starshipId in this.starshipIds)
                {
                    if (context.Starship.Find(starshipId) != null)
                    {
                        StarshipCharacter starshipCharacter = context.StarshipCharacter.Find(starshipId, this.id);
                        if (starshipCharacter == null)
                        {
                            character.starshipIds.Add(new StarshipCharacter(starshipId, this.id));
                        }
                        else
                        {
                            character.starshipIds.Add(starshipCharacter);
                        }
                    }
                }
            }
            return(character);
        }