Exemple #1
0
        public async Task <IActionResult> PostStarshipCharacter([FromBody] StarshipCharacter starshipCharacter)
        {
            if (!ModelState.IsValid)
            {
                return(BadRequest(ModelState));
            }

            _context.StarshipCharacter.Add(starshipCharacter);
            try
            {
                await _context.SaveChangesAsync();
            }
            catch (DbUpdateException)
            {
                if (StarshipCharacterExists(starshipCharacter.StarshipId))
                {
                    return(new StatusCodeResult(StatusCodes.Status409Conflict));
                }
                else
                {
                    throw;
                }
            }

            return(CreatedAtAction("GetStarshipCharacter", new { id = starshipCharacter.StarshipId }, starshipCharacter));
        }
Exemple #2
0
        public async Task <IActionResult> PutStarshipCharacter([FromRoute] int id, [FromBody] StarshipCharacter starshipCharacter)
        {
            if (!ModelState.IsValid)
            {
                return(BadRequest(ModelState));
            }

            if (id != starshipCharacter.StarshipId)
            {
                return(BadRequest());
            }

            _context.Entry(starshipCharacter).State = EntityState.Modified;

            try
            {
                await _context.SaveChangesAsync();
            }
            catch (DbUpdateConcurrencyException)
            {
                if (!StarshipCharacterExists(id))
                {
                    return(NotFound());
                }
                else
                {
                    throw;
                }
            }

            return(NoContent());
        }
        public Starship ToModel(star_wars_apiContext context)
        {
            Starship starship = context.Starship.Find(this.id);

            bool newObject = false;

            if (starship == null)
            {
                newObject = true;
                starship  = new Starship();
            }

            starship.id               = this.id;
            starship.created          = this.created;
            starship.edited           = this.edited;
            starship.mglt             = this.mglt;
            starship.hyperdriveRating = this.hyperdriveRating;
            starship.starshipClass    = this.starshipClass;
            starship.cargoCapacity    = this.cargoCapacity;
            starship.consumables      = this.consumables;
            starship.costInCredits    = this.costInCredits;
            starship.crew             = this.crew;
            starship.length           = this.length;
            starship.manufacturer     = this.manufacturer;
            starship.model            = this.model;
            starship.name             = this.name;
            starship.passengers       = this.passengers;
            starship.filmIds          = new List <FilmStarship>();
            starship.pilotIds         = new List <StarshipCharacter>();

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

                foreach (int characterId in this.pilotIds)
                {
                    if (context.Character.Find(characterId) != null)
                    {
                        StarshipCharacter starshipCharacter = context.StarshipCharacter.Find(this.id, characterId);
                        if (starshipCharacter == null)
                        {
                            starship.pilotIds.Add(new StarshipCharacter(this.id, characterId));
                        }
                        else
                        {
                            starship.pilotIds.Add(starshipCharacter);
                        }
                    }
                }
            }
            return(starship);
        }
        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);
        }