Skip to content

Commit

Permalink
Register route (#31)
Browse files Browse the repository at this point in the history
* Add username to Prisma schema

* Add route for registration

* Attempt to fix build issue

* Add types to register route

* Add test for register route
  • Loading branch information
Advayp authored Jan 17, 2025
1 parent b194162 commit c5e9eea
Show file tree
Hide file tree
Showing 6 changed files with 73 additions and 0 deletions.
2 changes: 2 additions & 0 deletions .github/workflows/build-and-test.yml
Original file line number Diff line number Diff line change
Expand Up @@ -27,6 +27,8 @@ jobs:
node-version: 23
- name: Install dependencies
run: npm install --legacy-peer-deps
- name: Install prisma
run: npx prisma generate
- name: Build codebase
run: npm run build

Expand Down
5 changes: 5 additions & 0 deletions backend/__test__/utils.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
export const generateMockRequest = (payload: object) => {
return {
json: async () => payload,
};
};
28 changes: 28 additions & 0 deletions backend/app/api/user/register/route.test.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,28 @@
import { prismaMock } from '@/__test__/singleton';
import { User } from '@prisma/client';
import { POST } from './route';
import { generateMockRequest } from '@/__test__/utils';
import { NextRequest } from 'next/server';

test('Register route returns intended user', async () => {
const temporaryUser: User = {
id: 1,
username: 'test',
createdAt: new Date(),
updatedAt: new Date(),
};

prismaMock.user.create.mockResolvedValue(temporaryUser);

const req = generateMockRequest(temporaryUser);
const res = await POST(req as NextRequest);
const data = await res.json();

const expected = {
...temporaryUser,
updatedAt: temporaryUser.updatedAt.toISOString(),
createdAt: temporaryUser.createdAt.toISOString(),
};

expect(data.user).toEqual(expected);
});
25 changes: 25 additions & 0 deletions backend/app/api/user/register/route.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,25 @@
import prisma from '@/lib/prisma';
import { User } from '@prisma/client';
import { NextRequest, NextResponse } from 'next/server';

type PostRequest = {
username: string;
};

type PostResponse = {
user: User;
};

export const POST = async (
req: NextRequest
): Promise<NextResponse<PostResponse>> => {
const { username }: PostRequest = await req.json();

const user = await prisma.user.create({
data: {
username,
},
});

return NextResponse.json({ user });
};
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
/*
Warnings:
- Added the required column `username` to the `User` table without a default value. This is not possible if the table is not empty.
*/
-- AlterTable
ALTER TABLE "User" ADD COLUMN "username" TEXT NOT NULL;
5 changes: 5 additions & 0 deletions backend/prisma/schema.prisma
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,10 @@ datasource db {
url = env("DATABASE_URL")
}

generator client {
provider = "prisma-client-js"
}

model Genre {
id Int @id @default(autoincrement())
value String @unique
Expand All @@ -11,6 +15,7 @@ model Genre {

model User {
id Int @id @default(autoincrement())
username String
createdAt DateTime @default(now())
updatedAt DateTime @updatedAt
preferences Genre[]
Expand Down

0 comments on commit c5e9eea

Please sign in to comment.