Skip to content

Commit

Permalink
chore: Some parts of pokedetails
Browse files Browse the repository at this point in the history
  • Loading branch information
felixtanhm committed May 6, 2024
1 parent 690fde3 commit 240ae98
Show file tree
Hide file tree
Showing 3 changed files with 60 additions and 9 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@ const mongoose = require("mongoose");
const Schema = mongoose.Schema;

const PokeDetailsSchema = new Schema({
height: { type: String, required: true },
height: { type: Number, required: true },
weight: { type: Number, required: true },
has_gender: { type: Boolean, required: true },
hp: { type: Number, required: true },
Expand All @@ -12,8 +12,19 @@ const PokeDetailsSchema = new Schema({
specialAttack: { type: Number, required: true },
specialDefense: { type: Number, required: true },
speed: { type: Number, required: true },
abilities: [{ type: String }],
evolvesTo: [{ type: Schema.Types.ObjectId, ref: "Pokemon" }],
abilities: {
type: [String],
default: () => {
return null;
},
},
evolvesTo: {
type: [Schema.Types.ObjectId],
ref: "Pokemon",
default: () => {
return null;
},
},
});

// Export model
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,12 @@ const Schema = mongoose.Schema;
const PokeSchema = new Schema({
name: { type: String, required: true },
dexId: { type: Number, required: true },
types: { type: [String], default: undefined },
types: {
type: [String],
default: () => {
return null;
},
},
avatar: { type: String, required: true },
details: { type: Schema.Types.ObjectId, ref: "PokeDetails", required: true },
});
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -45,26 +45,61 @@ async function main() {
// console.log("-----LIST START-----");
// console.log(expandList);
// console.log("-----LIST END-----");
console.log(expandList.length);
console.log(`List Length: ${expandList.length}`);

await createPokemon(expandList[0]);
const { newPokemon, newPokeDetails } = processPokeData(expandList[0]);

const detailsRef = await createPokeDetails(newPokeDetails);

await createPokemon(newPokemon, detailsRef);
// await createGenres();
// await createAuthors();

console.log("Debug: Closing mongoose");
mongoose.connection.close();
}

async function createPokemon(data) {
const newPoke = {
function processPokeData(data) {
console.log("Processing Pokemon:");
console.log(data);

const newPokemon = {
name: data.name,
dexId: data.order,
avatar: data.sprites.front_default,
types: data.types.map((typeObj) => {
return typeObj.type.name;
}),
};
const pokemon = new Pokemon(newPoke);

const newPokeDetails = {
height: data.height,
weight: data.weight,
// has_gender: data.height,
hp: data.stats[0].base_stat,
attack: data.stats[1].base_stat,
defense: data.stats[2].base_stat,
specialAttack: data.stats[3].base_stat,
specialDefense: data.stats[4].base_stat,
speed: data.stats[5].base_stat,
abilities: data.abilities.map((aObj) => {
return aObj.ability.name;
}),
// evolvesTo: [{ type: Schema.Types.ObjectId, ref: "Pokemon" }],
};
return { newPokemon, newPokeDetails };
}

async function createPokeDetails(newPokeDetails) {
const pokeDetails = new PokeDetails(newPokeDetails);
console.log(`\n Poke Details:`);
console.log(pokeDetails);
return pokeDetails;
}

async function createPokemon(newPokemon, detailsRef) {
const pokemon = new Pokemon({ ...newPokemon, details: detailsRef });
console.log(`\n Pokemon:`);
console.log(pokemon);
}

Expand Down

0 comments on commit 240ae98

Please sign in to comment.