-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
feat: create MyProject view, with updated controller with findByUser
add tests, include to router and authentication clean console logs from tests
- Loading branch information
Showing
12 changed files
with
174 additions
and
12 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,72 @@ | ||
<script lang="ts" setup> | ||
import { ref, onMounted } from 'vue' | ||
import { trpc } from '@/trpc' | ||
import type { Selectable } from 'kysely' | ||
import type { Projects } from '@server/shared/types' | ||
import { FwbButton } from 'flowbite-vue' | ||
import ProjectCard from '@/components/ProjectCard.vue' | ||
import { authUserId, isLoggedIn, username } from '@/stores/user' | ||
const projects = ref<Selectable<Projects>[]>([]) | ||
const fetchProjects = async () => { | ||
try { | ||
if (!isLoggedIn.value) { | ||
// Handle the case where the user is not logged in | ||
console.log('User is not logged in. Redirecting or showing appropriate message.') | ||
return | ||
} | ||
// Fetch projects for the authenticated user | ||
if (authUserId.value) { | ||
projects.value = await trpc.projects.findByUser.query({ userId: authUserId.value }) | ||
} else { | ||
console.error('User ID is not available.') | ||
} | ||
} catch (err) { | ||
console.error('Failed to fetch projects:', err) | ||
} | ||
} | ||
onMounted(fetchProjects) | ||
</script> | ||
|
||
<template> | ||
<div class="dark:bg-gray-800"> | ||
<div v-if="!isLoggedIn" class="rounded-md bg-white px-6 py-8"> | ||
<div class="items-center lg:flex"> | ||
<div class="lg:w-1/2"> | ||
<h2 class="text-4xl font-bold text-gray-800 dark:text-gray-100">DIY-Platform</h2> | ||
<p class="mt-4 text-gray-500 dark:text-gray-400 lg:max-w-md"> | ||
A place where you can share your crafting ideas with others. | ||
</p> | ||
<div class="mt-6 flex items-center gap-2"> | ||
<FwbButton component="RouterLink" tag="router-link" href="/signup">Sign up</FwbButton> | ||
<FwbButton component="RouterLink" tag="router-link" color="alternative" href="/login"> | ||
Log in | ||
</FwbButton> | ||
</div> | ||
</div> | ||
<div class="mt-8 lg:mt-0 lg:w-1/2"> | ||
<div class="flex items-center justify-center lg:justify-end"> | ||
<div class="max-w-lg"> | ||
<picture> | ||
<source srcset="../assets/illustration.webp" type="image/webp" /> | ||
<img class="h-64 w-full rounded-md object-cover object-center" | ||
src="../assets/illustration.png" alt="Person typing" /> | ||
</picture> | ||
</div> | ||
</div> | ||
</div> | ||
</div> | ||
</div> | ||
<div v-if="isLoggedIn" class="mt-12 text-center"> | ||
<p>Logged in as: {{ username }}</p> | ||
<h3 class="text-2xl font-bold text-gray-800 dark:text-gray-100">My Projects</h3> | ||
<div v-if="projects.length" class="mt-6 grid gap-6 lg:grid-cols-3"> | ||
<ProjectCard v-for="project in projects" :key="project.id" :project="project" /> | ||
</div> | ||
<div v-else class="text-center text-gray-500 dark:text-gray-400">No projects yet!</div> | ||
</div> | ||
</div> | ||
</template> |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,28 @@ | ||
// server/src/controllers/projects/findByUser.ts | ||
|
||
import { publicProcedure } from "@server/trpc"; | ||
import provideRepos from "@server/trpc/provideRepos"; | ||
import { TRPCError } from "@trpc/server"; | ||
import { projectRepository } from "@server/repositories/projectRepository"; | ||
import { z } from "zod"; | ||
|
||
export default publicProcedure | ||
.use( | ||
provideRepos({ | ||
projectRepository, | ||
}) | ||
) | ||
.input(z.object({ userId: z.number() })) // Ensure input is defined correctly | ||
.query(async ({ input, ctx }) => { | ||
const { userId } = input; | ||
|
||
if (!userId) { | ||
throw new TRPCError({ | ||
code: "UNAUTHORIZED", | ||
message: "User not authenticated", | ||
}); | ||
} | ||
|
||
// Fetch projects for the authenticated user | ||
return ctx.repos.projectRepository.findByUser(userId); | ||
}); |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,43 @@ | ||
import { authContext } from "@server/tests/utils/context"; | ||
import { fakeProject, fakeUser } from "@server/tests/utils/fakes"; | ||
import { createTestDatabase } from "@server/tests/utils/database"; | ||
import { createCallerFactory } from "@server/trpc"; | ||
import { wrapInRollbacks } from "@server/tests/utils/transactions"; | ||
import { insertAll } from "@server/tests/utils/records"; | ||
import projectRouter from ".."; | ||
|
||
const createCaller = createCallerFactory(projectRouter); | ||
const db = await wrapInRollbacks(createTestDatabase()); | ||
|
||
const [user1, user2] = await insertAll(db, "users", [fakeUser(), fakeUser()]); | ||
|
||
const [project1, project2, project3] = await insertAll(db, "projects", [ | ||
fakeProject({ userId: user1.id }), | ||
fakeProject({ userId: user1.id }), // Another project for user1 | ||
fakeProject({ userId: user2.id }), // Project for user2 | ||
]); | ||
|
||
const { findByUser } = createCaller(authContext({ db }, user1)); | ||
|
||
describe("findByUser", () => { | ||
it("should return projects for the authenticated user", async () => { | ||
const projectsResponse = await findByUser({ userId: user1.id }); // Pass an object with userId | ||
expect(projectsResponse).toHaveLength(2); // Expecting 2 projects for user1 | ||
expect(projectsResponse).toEqual( | ||
expect.arrayContaining([ | ||
expect.objectContaining({ id: project1.id, userId: user1.id }), | ||
expect.objectContaining({ id: project2.id, userId: user1.id }), | ||
]) | ||
); | ||
}); | ||
|
||
it("should return an empty array if the user has no projects", async () => { | ||
const projectsResponse = await findByUser({ userId: user2.id }); // Pass an object with userId | ||
expect(projectsResponse).toHaveLength(1); | ||
expect(projectsResponse).toEqual( | ||
expect.arrayContaining([ | ||
expect.objectContaining({ id: project3.id, userId: user2.id }), | ||
]) | ||
); | ||
}); | ||
}); |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters