1
0
Fork 0
mirror of https://github.com/seanmorley15/AdventureLog.git synced 2025-07-24 07:19:36 +02:00

Add userVisitedAdventures table and API endpoints for visits and userinfo

This commit is contained in:
Sean Morley 2024-04-03 23:55:00 +00:00
parent ba6a5283fe
commit d834cef83b
10 changed files with 417 additions and 10 deletions

View file

@ -0,0 +1,12 @@
CREATE TABLE IF NOT EXISTS "userVisitedAdventures" (
"user_id" text NOT NULL,
"adventure_name" text NOT NULL,
"location" text,
"adventure_visited" text
);
--> statement-breakpoint
DO $$ BEGIN
ALTER TABLE "userVisitedAdventures" ADD CONSTRAINT "userVisitedAdventures_user_id_user_id_fk" FOREIGN KEY ("user_id") REFERENCES "user"("id") ON DELETE no action ON UPDATE no action;
EXCEPTION
WHEN duplicate_object THEN null;
END $$;

View file

@ -0,0 +1,195 @@
{
"id": "ec5af5e4-8522-4383-af47-412fb43c7cc3",
"prevId": "2039600b-1f5f-4f37-84dd-bb40636855e7",
"version": "5",
"dialect": "pg",
"tables": {
"featuredAdventures": {
"name": "featuredAdventures",
"schema": "",
"columns": {
"id": {
"name": "id",
"type": "serial",
"primaryKey": true,
"notNull": true
},
"name": {
"name": "name",
"type": "text",
"primaryKey": false,
"notNull": true
},
"location": {
"name": "location",
"type": "text",
"primaryKey": false,
"notNull": false
}
},
"indexes": {},
"foreignKeys": {},
"compositePrimaryKeys": {},
"uniqueConstraints": {}
},
"session": {
"name": "session",
"schema": "",
"columns": {
"id": {
"name": "id",
"type": "text",
"primaryKey": true,
"notNull": true
},
"user_id": {
"name": "user_id",
"type": "text",
"primaryKey": false,
"notNull": true
},
"expires_at": {
"name": "expires_at",
"type": "timestamp with time zone",
"primaryKey": false,
"notNull": true
}
},
"indexes": {},
"foreignKeys": {
"session_user_id_user_id_fk": {
"name": "session_user_id_user_id_fk",
"tableFrom": "session",
"tableTo": "user",
"columnsFrom": [
"user_id"
],
"columnsTo": [
"id"
],
"onDelete": "no action",
"onUpdate": "no action"
}
},
"compositePrimaryKeys": {},
"uniqueConstraints": {}
},
"sharedAdventures": {
"name": "sharedAdventures",
"schema": "",
"columns": {
"id": {
"name": "id",
"type": "text",
"primaryKey": true,
"notNull": true
},
"data": {
"name": "data",
"type": "json",
"primaryKey": false,
"notNull": true
}
},
"indexes": {},
"foreignKeys": {},
"compositePrimaryKeys": {},
"uniqueConstraints": {}
},
"user": {
"name": "user",
"schema": "",
"columns": {
"id": {
"name": "id",
"type": "text",
"primaryKey": true,
"notNull": true
},
"username": {
"name": "username",
"type": "text",
"primaryKey": false,
"notNull": true
},
"first_name": {
"name": "first_name",
"type": "text",
"primaryKey": false,
"notNull": true
},
"last_name": {
"name": "last_name",
"type": "text",
"primaryKey": false,
"notNull": true
},
"hashed_password": {
"name": "hashed_password",
"type": "varchar",
"primaryKey": false,
"notNull": true
}
},
"indexes": {},
"foreignKeys": {},
"compositePrimaryKeys": {},
"uniqueConstraints": {}
},
"userVisitedAdventures": {
"name": "userVisitedAdventures",
"schema": "",
"columns": {
"user_id": {
"name": "user_id",
"type": "text",
"primaryKey": false,
"notNull": true
},
"adventure_name": {
"name": "adventure_name",
"type": "text",
"primaryKey": false,
"notNull": true
},
"location": {
"name": "location",
"type": "text",
"primaryKey": false,
"notNull": false
},
"adventure_visited": {
"name": "adventure_visited",
"type": "text",
"primaryKey": false,
"notNull": false
}
},
"indexes": {},
"foreignKeys": {
"userVisitedAdventures_user_id_user_id_fk": {
"name": "userVisitedAdventures_user_id_user_id_fk",
"tableFrom": "userVisitedAdventures",
"tableTo": "user",
"columnsFrom": [
"user_id"
],
"columnsTo": [
"id"
],
"onDelete": "no action",
"onUpdate": "no action"
}
},
"compositePrimaryKeys": {},
"uniqueConstraints": {}
}
},
"enums": {},
"schemas": {},
"_meta": {
"columns": {},
"schemas": {},
"tables": {}
}
}

View file

@ -57,6 +57,13 @@
"when": 1712167204757, "when": 1712167204757,
"tag": "0007_nervous_scalphunter", "tag": "0007_nervous_scalphunter",
"breakpoints": true "breakpoints": true
},
{
"idx": 8,
"version": "5",
"when": 1712186591227,
"tag": "0008_romantic_maria_hill",
"breakpoints": true
} }
] ]
} }

View file

@ -38,3 +38,12 @@ export const sessionTable = pgTable("session", {
mode: "date", mode: "date",
}).notNull(), }).notNull(),
}); });
export const userVisitedAdventures = pgTable("userVisitedAdventures", {
userId: text("user_id")
.notNull()
.references(() => userTable.id),
adventureName: text("adventure_name").notNull(),
location: text("location"),
adventureVistied: text("adventure_visited"),
});

View file

@ -10,3 +10,77 @@ export function generateRandomString() {
} }
return randomString; return randomString;
} }
const inspirationalQuotes = [
"Believe you can and you're halfway there. - Theodore Roosevelt",
"The only way to do great work is to love what you do. - Steve Jobs",
"In the middle of every difficulty lies opportunity. - Albert Einstein",
"The future belongs to those who believe in the beauty of their dreams. - Eleanor Roosevelt",
"It does not matter how slowly you go as long as you do not stop. - Confucius",
"Success is not final, failure is not fatal: It is the courage to continue that counts. - Winston Churchill",
"The only limit to our realization of tomorrow will be our doubts of today. - Franklin D. Roosevelt",
"Don't watch the clock; do what it does. Keep going. - Sam Levenson",
"You are never too old to set another goal or to dream a new dream. - C.S. Lewis",
"The only person you are destined to become is the person you decide to be. - Ralph Waldo Emerson",
"Happiness is not something ready-made. It comes from your own actions. - Dalai Lama",
"Life is what happens when you're busy making other plans. - John Lennon",
"You miss 100% of the shots you don't take. - Wayne Gretzky",
"The best time to plant a tree was 20 years ago. The second best time is now. - Chinese Proverb",
"The only way to achieve the impossible is to believe it is possible. - Charles Kingsleigh",
"Don't count the days, make the days count. - Muhammad Ali",
"You don't have to be great to start, but you have to start to be great. - Zig Ziglar",
"You can't go back and change the beginning, but you can start where you are and change the ending. - C.S. Lewis",
"Dream big and dare to fail. - Norman Vaughan",
"The secret of getting ahead is getting started. - Mark Twain",
"Everything you can imagine is real. - Pablo Picasso",
"You must be the change you wish to see in the world. - Mahatma Gandhi",
"If you want to lift yourself up, lift up someone else. - Booker T. Washington",
"Believe in yourself and all that you are. Know that there is something inside you that is greater than any obstacle. - Christian D. Larson",
"The journey of a thousand miles begins with one step. - Lao Tzu",
"Life isn't about waiting for the storm to pass, it's about learning to dance in the rain. - Vivian Greene",
"You are never too old to set another goal or to dream a new dream. - Les Brown",
"Your time is limited, don't waste it living someone else's life. - Steve Jobs",
"Don't let yesterday take up too much of today. - Will Rogers",
"The only thing standing between you and your goal is the story you keep telling yourself as to why you can't achieve it. - Jordan Belfort",
"The future belongs to those who prepare for it today. - Malcolm X",
"The greatest glory in living lies not in never falling, but in rising every time we fall. - Nelson Mandela",
"It's not what happens to you, but how you react to it that matters. - Epictetus",
"The only way to do great work is to love what you do. - Steve Jobs",
"When one door of happiness closes, another opens, but often we look so long at the closed door that we do not see the one that has been opened for us. - Helen Keller",
"The only thing that stands between you and your dream is the will to try and the belief that it is actually possible. - Joel Brown",
"Success is walking from failure to failure with no loss of enthusiasm. - Winston Churchill",
"Believe in yourself! Have faith in your abilities! Without a humble but reasonable confidence in your own powers you cannot be successful or happy. - Norman Vincent Peale",
"The greatest adventure is what lies ahead. - J.R.R. Tolkien",
"The only way to do great work is to love what you do. - Steve Jobs",
"What you get by achieving your goals is not as important as what you become by achieving your goals. - Zig Ziglar",
"To be yourself in a world that is constantly trying to make you something else is the greatest accomplishment. - Ralph Waldo Emerson",
"What lies behind us and what lies before us are tiny matters compared to what lies within us. - Ralph Waldo Emerson",
"The only person you are destined to become is the person you decide to be. - Ralph Waldo Emerson",
"The best and most beautiful things in the world cannot be seen or even touched - they must be felt with the heart. - Helen Keller",
"The only limit to our realization of tomorrow will be our doubts of today. - Franklin D. Roosevelt",
"It always seems impossible until it is done. - Nelson Mandela",
"I can't change the direction of the wind, but I can adjust my sails to always reach my destination. - Jimmy Dean",
"Believe you can and you're halfway there. - Theodore Roosevelt",
"The only way to achieve the impossible is to believe it is possible. - Charles Kingsleigh",
"If you're going through hell, keep going. - Winston Churchill",
"Nothing is impossible, the word itself says 'I'm possible'! - Audrey Hepburn",
"The only thing standing in the way between you and your goal is the story you keep telling yourself as to why you can't achieve it. - Jordan Belfort",
"The future belongs to those who believe in the beauty of their dreams. - Eleanor Roosevelt",
"Success is not final, failure is not fatal: It is the courage to continue that counts. - Winston Churchill",
"Keep your face always toward the sunshine - and shadows will fall behind you. - Walt Whitman",
"Success is not the key to happiness. Happiness is the key to success. If you love what you are doing, you will be successful. - Albert Schweitzer",
"Don't watch the clock; do what it does. Keep going. - Sam Levenson",
"You are never too old to set another goal or to dream a new dream. - C.S. Lewis",
"You are never too old to set another goal or to dream a new dream. - C.S. Lewis",
"The only person you are destined to become is the person you decide to be. - Ralph Waldo Emerson",
"Happiness is not something ready-made. It comes from your own actions. - Dalai Lama",
"Life is what happens when you're busy making other plans. - John Lennon",
"You miss 100% of the shots you don't take. - Wayne Gretzky",
"The best time to plant a tree was 20 years ago. The second best time is now. - Chinese Proverb",
"The only way to achieve the impossible is to believe it is possible. - Charles Kings",
];
export function getRandomQuote() {
const randomIndex = Math.floor(Math.random() * inspirationalQuotes.length);
return inspirationalQuotes[randomIndex];
}

View file

@ -16,8 +16,9 @@
<article class="prose"> <article class="prose">
{#if data.user && data.user.username != ""} {#if data.user && data.user.username != ""}
<h1 class="mb-4">Welcome {data.user.first_name}. Let's get Exploring!</h1> <h1 class="mb-4">Welcome {data.user.first_name}. Let's get Exploring!</h1>
{:else}
<h1 class="mb-4">Welcome. Let's get Exploring!</h1>
{/if} {/if}
<h1 class="mb-4">Welcome. Let's get Exploring!</h1>
</article> </article>
<img src={campingDrawing} class="w-1/4 mb-4" alt="Logo" /> <img src={campingDrawing} class="w-1/4 mb-4" alt="Logo" />
<button on:click={navToLog} class="btn btn-primary">Open Log</button> <button on:click={navToLog} class="btn btn-primary">Open Log</button>
@ -33,6 +34,6 @@
{#if data.user} {#if data.user}
<form method="post" use:enhance> <form method="post" use:enhance>
<button>Sign out</button> <button class="btn btn-primary">Sign out</button>
</form> </form>
{/if} {/if}

View file

@ -0,0 +1,39 @@
import { lucia } from "$lib/server/auth";
import type { RequestEvent } from "@sveltejs/kit";
export async function GET(event: RequestEvent): Promise<Response> {
if (!event.locals.user) {
return new Response(JSON.stringify({ error: "No user found" }), {
status: 401,
headers: {
"Content-Type": "application/json",
},
});
}
try {
return new Response(
JSON.stringify({
message: "Welcome user info page!",
userId: event.locals.user.id,
username: event.locals.user.username,
firstName: event.locals.user.first_name,
lastName: event.locals.user.last_name,
}),
{
status: 200,
headers: {
"Content-Type": "application/json",
},
}
);
} catch (e) {
console.error(e);
return new Response(JSON.stringify({ error: "Internal server error" }), {
status: 500,
headers: {
"Content-Type": "application/json",
},
});
}
}

View file

@ -0,0 +1,35 @@
import { lucia } from "$lib/server/auth";
import type { RequestEvent } from "@sveltejs/kit";
import { userVisitedAdventures } from "$lib/db/schema";
import { db } from "$lib/db/db.server";
import { eq } from "drizzle-orm";
// Gets all the adventures that the user has visited
export async function GET(event: RequestEvent): Promise<Response> {
if (!event.locals.user) {
return new Response(JSON.stringify({ error: "No user found" }), {
status: 401,
headers: {
"Content-Type": "application/json",
},
});
}
let result = await db
.select()
.from(userVisitedAdventures)
.where(eq(userVisitedAdventures.userId, event.locals.user.id))
.execute();
return new Response(
JSON.stringify({
result: result,
}),
{
status: 200,
headers: {
"Content-Type": "application/json",
},
}
);
}

View file

@ -21,6 +21,12 @@ export const actions: Actions = {
const username = formData.get("username"); const username = formData.get("username");
const password = formData.get("password"); const password = formData.get("password");
if (!username || !password) {
return fail(400, {
message: "Invalid request",
});
}
if ( if (
typeof username !== "string" || typeof username !== "string" ||
username.length < 3 || username.length < 3 ||

View file

@ -1,13 +1,42 @@
<!-- routes/login/+page.svelte --> <!-- routes/login/+page.svelte -->
<script lang="ts"> <script lang="ts">
import { enhance } from "$app/forms"; import { enhance } from "$app/forms";
import { getRandomQuote } from "$lib";
import { onMount } from "svelte";
let quote: string = "";
onMount(async () => {
quote = getRandomQuote();
});
</script> </script>
<h1>Sign in</h1> <article class="text-center text-4xl font-extrabold">
<form method="post" use:enhance> <h1>Sign in</h1>
<label for="username">Username</label> </article>
<input name="username" id="username" /><br />
<label for="password">Password</label> <div class="flex justify-center">
<input type="password" name="password" id="password" /><br /> <form method="post" use:enhance class="w-full max-w-xs">
<button>Continue</button> <label for="username">Username</label>
</form> <input
name="username"
id="username"
class="block mb-2 input input-bordered w-full max-w-xs"
/><br />
<label for="password">Password</label>
<input
type="password"
name="password"
id="password"
class="block mb-2 input input-bordered w-full max-w-xs"
/><br />
<button class="py-2 px-4 rounded btn btn-primary">Login</button>
</form>
</div>
<div class="flex justify-center mt-12 mr-25 ml-25">
<blockquote class="w-80 text-center text-lg break-words">
{#if quote != ""}
"{quote}"
{/if}
<!-- <footer class="text-sm">- Steve Jobs</footer> -->
</blockquote>
</div>