Skip to content

Commit

Permalink
Merge pull request #50 from coding-bootcamps-eu/set-plants
Browse files Browse the repository at this point in the history
Set plants
  • Loading branch information
wiebken-n authored Oct 15, 2023
2 parents be92386 + 44e2abc commit 42dd8c6
Show file tree
Hide file tree
Showing 3 changed files with 1,215 additions and 667 deletions.
129 changes: 73 additions & 56 deletions src/stores/usePlantsStore.js
Original file line number Diff line number Diff line change
Expand Up @@ -43,7 +43,7 @@ export const usePlantsStore = defineStore('plants', () => {
//vorerst nicht vorgesehen

// add new species / variety
async function setSpecies(
function setSpecies(
name,
botanicname,
imagename,
Expand All @@ -65,82 +65,99 @@ export const usePlantsStore = defineStore('plants', () => {
imagename: imagename,
plantfamily: plantfamily,
nutrition: nutrition,
goodNeighbors: goodNeighbors,
badNeighbors: badNeighbors,
goodNeighbors: goodNeighbors.split(', '),
badNeighbors: badNeighbors.split(', '),
description: description
}

// const resp =
await fetch('http://localhost:3000/plantspecies/', {
fetch('http://localhost:3000/plantspecies/', {
method: 'POST',
headers: { 'Content-type': 'application/json' },
body: JSON.stringify(newSpecies)
}).then(async (resp) => {
const data = await resp.json()
state.plantSpecies = data
})
// const savedSpecies = await resp.json()

return true
}

// async function setVariety(
// name,
// botanicname,
// species,
// plantfamily,
// description,
// sowingForPlantingStart,
// sowingForPlantingEnd,
// plantingStart,
// plantingEnd,
// directSowingStart,
// directSowingEnd,
// harvestingStart,
// growingSeason,
// growingSeasonIntern,
// plantingDistance,
// rowDistance,
// nutrition,
// light,
// water,
// sowingDepth,
// cultivationTips,
// imagename
// ) {
// const newVariety = {
// name: name,
// botanicname,
// speciesId: getSpeciesIdFromName(species),
// species,
// plantfamily,
// description,
// sowingForPlantingStart,
// sowingForPlantingEnd,
// plantingStart,
// plantingEnd,
// directSowingStart,
// directSowingEnd,
// harvestingStart,
// growingSeason,
// growingSeasonIntern,
// plantingDistance,
// rowDistance,
// nutrition,
// light,
// water,
// sowingDepth,
// cultivationTips,
// imagename
// }
function setVariety(
name,
botanicname,
species,
plantfamily,
description,
sowingForPlantingStart,
sowingForPlantingEnd,
plantingStart,
plantingEnd,
directSowingStart,
directSowingEnd,
harvestingStart,
cultureDuration,
cultureDurationIntern,
plantingDistance,
rowDistance,
nutrition,
light,
water,
sowingDepth,
cultivationTips,
imagename
) {
const speciesInStore = getSpeciesIdFromName(species)

const newVariety = {
name: name,
botanicname: botanicname, //=== '' ? this.getSpecies(speciesInStore)[0].botanicname : botanicname,
plantspeciesId: speciesInStore,
species: species,
plantfamily: plantfamily, //=== ''? getSpecies(speciesInStore)[0].plantfamily : plantfamily,
description: description,
sowingForPlantingStart: sowingForPlantingStart,
sowingForPlantingEnd: sowingForPlantingEnd,
plantingStart: plantingStart,
plantingEnd: plantingEnd,
directSowingStart: directSowingStart,
directSowingEnd: directSowingEnd,
harvestingStart: harvestingStart,
cultureDuration: cultureDuration,
cultureDurationIntern: cultureDurationIntern,
plantingDistance: plantingDistance,
rowDistance: rowDistance,
nutrition: nutrition, // === '' ? getSpecies(speciesInStore)[0].nutrition : nutrition,
light: light,
water: water,
sowingDepth: sowingDepth,
cultivationTips: cultivationTips,
imagename: imagename //=== '' ? getSpecies(speciesInStore)[0].imagename : imagename
}

fetch('http://localhost:3000/plantvarieties/', {
method: 'POST',
headers: { 'Content-type': 'application/json' },
body: JSON.stringify(newVariety)
}).then(async (resp) => {
const data = await resp.json()
state.plantSpecies = data
})

return true
}

function varietiesBySpecies(speciesId) {
return state.plantVarieties.filter((varietyItem) => varietyItem['speciesId'] === speciesId)
}

function getSpeciesIdFromName(name) {
console.log(state.plantSpecies.filter((plantSpeciesItem) => plantSpeciesItem['name'] === name))
return state.plantSpecies.filter(
(plantSpeciesItem) => plantSpeciesItem['name'] === name
)?.[0]?.['id']
//optional chaining
}

onBeforeMount(() => {
loadPlantSpecies()
loadPlantVarieties()
Expand All @@ -149,7 +166,7 @@ export const usePlantsStore = defineStore('plants', () => {
return {
state,
setSpecies,
// setVariety,
setVariety,
loadPlantSpecies,
loadPlantVarieties,
getAllSpecies,
Expand Down
4 changes: 2 additions & 2 deletions src/views/PlantVarietyView.vue
Original file line number Diff line number Diff line change
Expand Up @@ -101,8 +101,8 @@ const imageUrl = computed(() => {
</li>
<br />
<br />
<li v-if="variety[0].growingSeason">
Zeit in Beet / Kulturdauer: {{ variety[0].growingSeason }} Tage
<li v-if="variety[0].cultureDuration">
Zeit in Beet / Kulturdauer: {{ variety[0].cultureDuration }} Tage
</li>
</ul>
</article>
Expand Down
Loading

0 comments on commit 42dd8c6

Please sign in to comment.