Exemplo n.º 1
0
        public Species ToModel(star_wars_apiContext context)
        {
            Species species = context.Species.Find(this.id);

            bool newObject = false;

            if (species == null)
            {
                newObject = true;
                species   = new Species();
            }

            species.id              = this.id;
            species.created         = this.created;
            species.edited          = this.edited;
            species.averageHeight   = this.averageHeight;
            species.averageLifespan = this.averageLifespan;
            species.classification  = this.classification;
            species.designation     = this.designation;
            species.homeworldId     = this.homeworldId;
            species.language        = this.language;
            species.name            = this.name;
            species.filmIds         = new List <FilmSpecies>();
            species.peopleIds       = new List <SpeciesCharacter>();
            species.eyeColors       = new List <SpeciesEyeColor>();
            species.hairColors      = new List <SpeciesHairColor>();
            species.skinColors      = new List <SpeciesSkinColor>();

            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)
                    {
                        FilmSpecies filmSpecies = context.FilmSpecies.Find(filmId, this.id);
                        if (filmSpecies == null)
                        {
                            species.filmIds.Add(new FilmSpecies(filmId, this.id));
                        }
                        else
                        {
                            species.filmIds.Add(filmSpecies);
                        }
                    }
                }

                foreach (int characterId in this.peopleIds)
                {
                    if (context.Character.Find(characterId) != null)
                    {
                        SpeciesCharacter speciesCharacter = context.SpeciesCharacter.Find(this.id, characterId);
                        if (speciesCharacter == null)
                        {
                            species.peopleIds.Add(new SpeciesCharacter(this.id, characterId));
                        }
                        else
                        {
                            species.peopleIds.Add(speciesCharacter);
                        }
                    }
                }

                foreach (string eyeColor in this.eyeColors)
                {
                    SpeciesEyeColor speciesEyeColor = context.SpeciesEyeColor.Find(this.id, eyeColor);
                    if (speciesEyeColor == null)
                    {
                        species.eyeColors.Add(new SpeciesEyeColor(this.id, eyeColor));
                    }
                    else
                    {
                        species.eyeColors.Add(speciesEyeColor);
                    }
                }

                foreach (string hairColor in this.hairColors)
                {
                    SpeciesHairColor speciesHairColor = context.SpeciesHairColor.Find(this.id, hairColor);
                    if (speciesHairColor == null)
                    {
                        species.hairColors.Add(new SpeciesHairColor(this.id, hairColor));
                    }
                    else
                    {
                        species.hairColors.Add(speciesHairColor);
                    }
                }

                foreach (string skinColor in this.skinColors)
                {
                    SpeciesSkinColor speciesSkinColor = context.SpeciesSkinColor.Find(this.id, skinColor);
                    if (speciesSkinColor == null)
                    {
                        species.skinColors.Add(new SpeciesSkinColor(this.id, skinColor));
                    }
                    else
                    {
                        species.skinColors.Add(speciesSkinColor);
                    }
                }
            }
            return(species);
        }
        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);
        }