Ejemplo n.º 1
0
        public Film ToModel(star_wars_apiContext context)
        {
            Film film = context.Film.Find(this.id);

            bool newObject = false;

            if (film == null)
            {
                newObject         = true;
                film              = new Film();
                film.characterIds = new List <FilmCharacter>();
                film.planetIds    = new List <FilmPlanet>();
                film.speciesIds   = new List <FilmSpecies>();
                film.starshipIds  = new List <FilmStarship>();
                film.vehicleIds   = new List <FilmVehicle>();
            }

            film.id           = this.id;
            film.created      = this.created;
            film.edited       = this.edited;
            film.director     = this.director;
            film.episodeId    = this.episodeId;
            film.openingCrawl = this.openingCrawl;
            film.producer     = this.producer;
            film.releaseDate  = this.releaseDate;
            film.title        = this.title;

            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 characterId in this.characterIds)
                {
                    if (context.Character.Find(characterId) != null)
                    {
                        FilmCharacter filmCharacter = context.FilmCharacter.Find(this.id, characterId);
                        if (filmCharacter == null)
                        {
                            filmCharacter = new FilmCharacter(this.id, characterId);
                        }
                        if (film.characterIds.Contains(filmCharacter) == false)
                        {
                            film.characterIds.Add(filmCharacter);
                        }
                    }
                }

                foreach (int planetId in this.planetIds)
                {
                    if (context.Planet.Find(planetId) != null)
                    {
                        FilmPlanet filmPlanet = context.FilmPlanet.Find(this.id, planetId);
                        if (filmPlanet == null)
                        {
                            filmPlanet = new FilmPlanet(this.id, planetId);
                        }
                        if (film.planetIds.Contains(filmPlanet) == false)
                        {
                            film.planetIds.Add(filmPlanet);
                        }
                    }
                }

                foreach (int speciesId in this.speciesIds)
                {
                    if (context.Species.Find(speciesId) != null)
                    {
                        FilmSpecies filmSpecies = context.FilmSpecies.Find(this.id, speciesId);
                        if (filmSpecies == null)
                        {
                            filmSpecies = new FilmSpecies(this.id, speciesId);
                        }
                        if (film.speciesIds.Contains(filmSpecies) == false)
                        {
                            film.speciesIds.Add(filmSpecies);
                        }
                    }
                }

                foreach (int starshipId in this.starshipIds)
                {
                    if (context.Starship.Find(starshipId) != null)
                    {
                        FilmStarship filmStarship = context.FilmStarship.Find(this.id, starshipId);
                        if (filmStarship == null)
                        {
                            filmStarship = new FilmStarship(this.id, starshipId);
                        }
                        if (film.starshipIds.Contains(filmStarship) == false)
                        {
                            film.starshipIds.Add(filmStarship);
                        }
                    }
                }

                foreach (int vehicleId in this.vehicleIds)
                {
                    if (context.Vehicle.Find(vehicleId) != null)
                    {
                        FilmVehicle filmVehicle = context.FilmVehicle.Find(this.id, vehicleId);
                        if (filmVehicle == null)
                        {
                            filmVehicle = new FilmVehicle(this.id, vehicleId);
                        }
                        if (film.vehicleIds.Contains(filmVehicle) == false)
                        {
                            film.vehicleIds.Add(filmVehicle);
                        }
                    }
                }
            }
            return(film);
        }
Ejemplo n.º 2
0
        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);
        }