MVP #6.1 #82
|
@ -5,11 +5,13 @@ const users = new Collection('63ded6c18e8493bffc83', 'Users')
|
||||||
const interests = new Collection('6417cf1de159d094b370', '6417cf29f2118829b3b4')
|
const interests = new Collection('6417cf1de159d094b370', '6417cf29f2118829b3b4')
|
||||||
const travel_with = new Collection('6417cf1de159d094b370', '6417d0429843609a2f49')
|
const travel_with = new Collection('6417cf1de159d094b370', '6417d0429843609a2f49')
|
||||||
const recommended_by = new Collection('6417cf1de159d094b370', '6417d00e40701375978b')
|
const recommended_by = new Collection('6417cf1de159d094b370', '6417d00e40701375978b')
|
||||||
|
const usersAnswers = new Collection('63cef30d6da945dd4250', 'users-answers')
|
||||||
|
|
||||||
export default {
|
export default {
|
||||||
experiences,
|
experiences,
|
||||||
users,
|
users,
|
||||||
interests,
|
interests,
|
||||||
travel_with,
|
travel_with,
|
||||||
recommended_by
|
recommended_by,
|
||||||
|
usersAnswers,
|
||||||
}
|
}
|
||||||
|
|
|
@ -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'
|
|
|
@ -1,9 +1,13 @@
|
||||||
import { databases } from '$lib/appwrite'
|
import { databases, user as userStore } from '$lib/appwrite'
|
||||||
import { Query } from 'appwrite'
|
import { Account, Permission, Query, Role } from 'appwrite'
|
||||||
import database from 'svelte-appwrite-client/src/lib/database'
|
import database from 'svelte-appwrite-client/src/lib/database'
|
||||||
import { getLocationDataFromLatAndLong } from '../locations'
|
import { getLocationDataFromLatAndLong } from '../locations'
|
||||||
import { writable } from 'svelte/store'
|
import { writable } from 'svelte/store'
|
||||||
import { CheckPoint, Experience } from '$lib/TStypes/experiences'
|
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
|
//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) => {
|
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
|
return experience
|
||||||
}
|
}
|
||||||
//Fetch of Rating
|
//Fetch of Rating
|
||||||
const getRating = async (expId: string) => {
|
const getRating = async (expId: string) => {
|
||||||
const { documents, total } = await databases.listDocuments('63cef30d6da945dd4250', '63ee6353ebb174cf815d', [Query.equal('ExpID', expId)])
|
const { documents, total } = await databases.listDocuments('63cef30d6da945dd4250', '63ee6353ebb174cf815d', [Query.equal('ExpID', expId)])
|
||||||
const sum = documents.reduce((accumulator, rating) => accumulator + rating.ExpUserRating, 0)
|
const sum = documents.reduce((accumulator, rating) => accumulator + rating.ExpUserRating, 0)
|
||||||
return sum / total || 0
|
return sum / total || 0
|
||||||
}
|
}
|
||||||
// Fetch of answers to the checkpoints
|
// Fetch of answers to the checkpoints
|
||||||
export const answer = async (checkPointId: string, answer: any) => {
|
export const answer = async (experienceId: string, checkPointId: string, answer: any) => {
|
||||||
const checkPoint = await databases.getDocument('63cef30d6da945dd4250', '63cef84d908acf805758', checkPointId)
|
try {
|
||||||
console.log({ checkPoint })
|
const checkPoint = await databases.getDocument('63cef30d6da945dd4250', '63cef84d908acf805758', checkPointId)
|
||||||
const { CPType, CPAnswerID } = checkPoint
|
console.log({ checkPoint })
|
||||||
const correctAnswer = (await databases.getDocument('63cef30d6da945dd4250', '63dd5c2b764061e40025', CPAnswerID)).CPAnswer
|
const { CPType, CPAnswerID } = checkPoint
|
||||||
|
const correctAnswer = (await databases.getDocument('63cef30d6da945dd4250', '63dd5c2b764061e40025', CPAnswerID)).CPAnswer
|
||||||
|
|
||||||
if (CPType === 'CHECKBOX') return JSON.stringify(answer) === JSON.stringify(correctAnswer)
|
let correct: boolean = false
|
||||||
if (CPType === 'RADIO' || CPType === 'NUMBER' || CPType === 'TEXT') return answer === correctAnswer[0]
|
|
||||||
|
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
|
// Fetch of all experiences
|
||||||
export const getExperiences = async () => {
|
export const getExperiences = async () => {
|
||||||
|
@ -80,8 +98,3 @@ export const getExperiencesAsStore = () => {
|
||||||
}
|
}
|
||||||
|
|
||||||
// !pridat trideni podle kategorie! export const getExperiencesByCategory = async (category: string) => {}
|
// !pridat trideni podle kategorie! export const getExperiencesByCategory = async (category: string) => {}
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
|
|
|
@ -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
|
|
||||||
}
|
|
||||||
}
|
|
|
@ -62,7 +62,7 @@
|
||||||
let myAnswer: string | string[]
|
let myAnswer: string | string[]
|
||||||
|
|
||||||
const checkAnswer = async () => {
|
const checkAnswer = async () => {
|
||||||
const res = await answer(checkPoint.$id, myAnswer)
|
const res = await answer(gameData.$id, checkPoint.$id, myAnswer)
|
||||||
if (res) {
|
if (res) {
|
||||||
control = 'correct'
|
control = 'correct'
|
||||||
client.points += 2
|
client.points += 2
|
||||||
|
|
Loading…
Reference in New Issue