import { defineStore } from "pinia"; import { ref } from "vue"; import { fetchBandsBySearchTerm } from "../data/api/bandApi"; import { fetchLocationsBySearchTerm } from "../data/api/locationApi"; import { fetchConcertsBySearchTerm } from "../data/api/concertApi"; import { ConcertApiModel } from "@/data/models/acts/concertApiModel"; import { useExerciseStore } from "./exercise.store"; import { BandApiModel } from "@/data/models/acts/bandApiModel"; import { useBandStore } from "./band.store"; import { useAccountStore } from "./account.store"; import { LocationDetailsApiModel } from "@/data/models/locations/locationDetailsApiModel"; export const useSearchStore = defineStore("searchStore", { state: () => ({ /** Search term */ searchTerm: ref(""), /** Band results */ bands: ref>([]), /** Location results */ locations: ref>([]), /** Concert results */ concerts: ref>([]), /** One or more searches are already performed */ alreadySearched: ref(false), /** Request to server sent, waiting for data response */ fetchInProgress: ref(false) }), actions: { /** * Search for the term in all bands, locations, events */ async startSearch() { const exerciseStore = useExerciseStore() this.alreadySearched = true this.fetchInProgress = true /** * Fetch all bands by this.searchTerm */ await fetchBandsBySearchTerm(this.searchTerm) .then(async result => { // Check for exercise solution if (this.searchTerm.toUpperCase().includes("SELECT")) { // Exercise 2.1 if (result.data[0].type != undefined && result.data[0].type == "table") { exerciseStore.solveExercise(2, 1) console.log("Exercise 2.1 solved") } // Exercise 2.2 else if (result.data[0].username != undefined) { exerciseStore.solveExercise(2, 2) console.log("Exercise 2.2 solved") } // Exercise 2.3 else if (result.data[0].privilegeAdminPanel != undefined) { exerciseStore.solveExercise(2, 3) console.log("Exercise 2.3 solved") } } // Exercise 2.4 else if (this.searchTerm.toUpperCase().includes("UPDATE")) { const accountStore = useAccountStore() await accountStore.refreshAccount() if (accountStore.userAccount.accountRole.privilegeAdminPanel == true) { exerciseStore.solveExercise(2, 4) console.log("Exercise 2.4 solved") } } // Exercise 2.6 else if (this.searchTerm.toUpperCase().includes("DELETE")) { const bandStore = useBandStore() await bandStore.getBand("muse") if (bandStore.band.ratingValues.find( rating => rating.value == 5).count == 0 ) { exerciseStore.solveExercise(2, 6) console.log("Exercise 2.6 solved") } } else { // Normal behaviour this.bands = result.data } }) /** * Fetch all locations by this.searchTerm */ await fetchLocationsBySearchTerm(this.searchTerm) .then(result => { this.locations = result.data }) /** * Fetch all concerts by this.searchTerm */ await fetchConcertsBySearchTerm(this.searchTerm) .then(result => { this.concerts = result.data }) this.fetchInProgress = false } } })