diff --git a/src/lib/collections.ts b/src/lib/collections.ts index 115f385..57ccfdc 100644 --- a/src/lib/collections.ts +++ b/src/lib/collections.ts @@ -5,11 +5,13 @@ const users = new Collection('63ded6c18e8493bffc83', 'Users') const interests = new Collection('6417cf1de159d094b370', '6417cf29f2118829b3b4') const travel_with = new Collection('6417cf1de159d094b370', '6417d0429843609a2f49') const recommended_by = new Collection('6417cf1de159d094b370', '6417d00e40701375978b') +const usersAnswers = new Collection('63cef30d6da945dd4250', 'users-answers') export default { experiences, users, interests, travel_with, - recommended_by + recommended_by, + usersAnswers, } diff --git a/src/lib/utils/api.js b/src/lib/utils/api.js deleted file mode 100644 index 2271932..0000000 --- a/src/lib/utils/api.js +++ /dev/null @@ -1,55 +0,0 @@ -async function send ({ method, path, body, token, headers }) { - const opts = { method, headers: new Headers(), mode: 'cors' } - opts.headers.append('Content-Type', 'application/json') - opts.headers.append('Access-Control-Allow-Origin', '*') - - if (body) { - opts.body = JSON.stringify(body) - } - - if (headers) { - for (const [k, v] of Object.entries(headers)) { - opts.headers.append(k, v) - } - } - - if (token) { - opts.headers.Authorization = `Bearer ${token}` - } - - const res = fetch(path, opts) - .then(async (r) => { - if (r.status >= 200 && r.status < 400) { - return await r.text() - } else { - return await r.text() - } - }) - .then((str) => { - try { - return JSON.parse(str) - } catch (err) { - return str - } - }) - - return res -} - -export function get (path, token = null) { - return send({ method: 'GET', path, body: null, token }) -} - -export function del (path, token = null) { - return send({ method: 'DELETE', path, body: null, token }) -} - -export function post (path, body, token = null) { - return send({ method: 'POST', path, body, token }) -} - -export function put (path, body, token = null, headers = []) { - return send({ method: 'PUT', path, body, token, headers }) -} - -export const hostName = 'https://erant.cz/api' diff --git a/src/lib/utils/database/experience.ts b/src/lib/utils/database/experience.ts index 665bc86..3e2133d 100644 --- a/src/lib/utils/database/experience.ts +++ b/src/lib/utils/database/experience.ts @@ -1,9 +1,13 @@ -import { databases } from '$lib/appwrite' -import { Query } from 'appwrite' +import { databases, user as userStore } from '$lib/appwrite' +import { Account, Permission, Query, Role } from 'appwrite' import database from 'svelte-appwrite-client/src/lib/database' import { getLocationDataFromLatAndLong } from '../locations' import { writable } from 'svelte/store' import { CheckPoint, Experience } from '$lib/TStypes/experiences' +import collections from '$lib/collections' + +let user: { $id: string } +userStore.subscribe((res) => (user = res)) //Loading of checkpoints and rating is done in the same function to prevent multiple requests to the database export const load = async (pathName: string, previewQuestionsCount?: number, preview?: Function) => { @@ -31,21 +35,35 @@ export const load = async (pathName: string, previewQuestionsCount?: number, pre return experience } -//Fetch of Rating +//Fetch of Rating const getRating = async (expId: string) => { const { documents, total } = await databases.listDocuments('63cef30d6da945dd4250', '63ee6353ebb174cf815d', [Query.equal('ExpID', expId)]) const sum = documents.reduce((accumulator, rating) => accumulator + rating.ExpUserRating, 0) return sum / total || 0 } // Fetch of answers to the checkpoints -export const answer = async (checkPointId: string, answer: any) => { - const checkPoint = await databases.getDocument('63cef30d6da945dd4250', '63cef84d908acf805758', checkPointId) - console.log({ checkPoint }) - const { CPType, CPAnswerID } = checkPoint - const correctAnswer = (await databases.getDocument('63cef30d6da945dd4250', '63dd5c2b764061e40025', CPAnswerID)).CPAnswer +export const answer = async (experienceId: string, checkPointId: string, answer: any) => { + try { + const checkPoint = await databases.getDocument('63cef30d6da945dd4250', '63cef84d908acf805758', checkPointId) + console.log({ checkPoint }) + const { CPType, CPAnswerID } = checkPoint + const correctAnswer = (await databases.getDocument('63cef30d6da945dd4250', '63dd5c2b764061e40025', CPAnswerID)).CPAnswer - if (CPType === 'CHECKBOX') return JSON.stringify(answer) === JSON.stringify(correctAnswer) - if (CPType === 'RADIO' || CPType === 'NUMBER' || CPType === 'TEXT') return answer === correctAnswer[0] + let correct: boolean = false + + if (CPType === 'CHECKBOX') correct = JSON.stringify(answer) === JSON.stringify(correctAnswer) + if (CPType === 'RADIO' || CPType === 'NUMBER' || CPType === 'TEXT') correct = answer === correctAnswer[0] + + collections.usersAnswers.createDocument({ + userId: user.$id, + checkPoint: checkPointId, + answer, + experience: experienceId, + }, [Permission.read(Role.user(user.$id))]) + return correct + } catch (error) { + //operation only for sveltekit 401, 500, 403 + } } // Fetch of all experiences export const getExperiences = async () => { @@ -80,8 +98,3 @@ export const getExperiencesAsStore = () => { } // !pridat trideni podle kategorie! export const getExperiencesByCategory = async (category: string) => {} - - - - - diff --git a/src/lib/utils/parseQuestion.js b/src/lib/utils/parseQuestion.js deleted file mode 100644 index 35d728d..0000000 --- a/src/lib/utils/parseQuestion.js +++ /dev/null @@ -1,15 +0,0 @@ -export default (question, questionType) => { - switch (questionType.toLowerCase()) { - case 'text': - return question - case 'number': - return Number.parseFloat(question) - case 'choice': - return question.split(/;\s*/).map((item) => ({ - label: item.startsWith('*') ? item.substring(1) : item, - value: !!item.startsWith('*') - })) - default: - return null - } -} diff --git a/src/routes/game/Forms/Renderer.svelte b/src/routes/game/Forms/Renderer.svelte index 3acb574..1d9f3df 100644 --- a/src/routes/game/Forms/Renderer.svelte +++ b/src/routes/game/Forms/Renderer.svelte @@ -62,7 +62,7 @@ let myAnswer: string | string[] const checkAnswer = async () => { - const res = await answer(checkPoint.$id, myAnswer) + const res = await answer(gameData.$id, checkPoint.$id, myAnswer) if (res) { control = 'correct' client.points += 2