Skip to content

Commit

Permalink
(BE) Add API for Retrieving a User (#68)
Browse files Browse the repository at this point in the history
* Add base code for users controller

* Add service for user

* Add controller for retrieving user
  • Loading branch information
devleejb authored Jan 18, 2024
1 parent fde4941 commit 2944ade
Show file tree
Hide file tree
Showing 6 changed files with 72 additions and 0 deletions.
3 changes: 3 additions & 0 deletions backend/src/users/types/find-user-response.type.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
import { UserDomain } from "./user-domain.type";

export class FindUserResponse extends UserDomain {}
12 changes: 12 additions & 0 deletions backend/src/users/types/user-domain.type.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,12 @@
import { ApiProperty } from "@nestjs/swagger";

export class UserDomain {
@ApiProperty({ type: String, description: "ID of user" })
id: string;
@ApiProperty({ type: String, description: "Nickname of user" })
nickname: string;
@ApiProperty({ type: Date, description: "Created date of user" })
createdAt: Date;
@ApiProperty({ type: Date, description: "Updated date of user" })
updatedAt: Date;
}
18 changes: 18 additions & 0 deletions backend/src/users/users.controller.spec.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,18 @@
import { Test, TestingModule } from "@nestjs/testing";
import { UsersController } from "./users.controller";

describe("UsersController", () => {
let controller: UsersController;

beforeEach(async () => {
const module: TestingModule = await Test.createTestingModule({
controllers: [UsersController],
}).compile();

controller = module.get<UsersController>(UsersController);
});

it("should be defined", () => {
expect(controller).toBeDefined();
});
});
22 changes: 22 additions & 0 deletions backend/src/users/users.controller.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,22 @@
import { Controller, Get, Req } from "@nestjs/common";
import { ApiBearerAuth, ApiOkResponse, ApiOperation, ApiTags } from "@nestjs/swagger";
import { UsersService } from "./users.service";
import { AuthroizedRequest } from "src/utils/types/req.type";
import { FindUserResponse } from "./types/find-user-response.type";

@ApiTags("Users")
@ApiBearerAuth()
@Controller("users")
export class UsersController {
constructor(private usersService: UsersService) {}

@Get("")
@ApiOperation({
summary: "Retrieve the Users Information (Myself)",
description: "Return the user information",
})
@ApiOkResponse({ type: FindUserResponse })
async findOne(@Req() req: AuthroizedRequest): Promise<FindUserResponse> {
return this.usersService.findOne(req.user.id);
}
}
2 changes: 2 additions & 0 deletions backend/src/users/users.module.ts
Original file line number Diff line number Diff line change
@@ -1,9 +1,11 @@
import { Module } from "@nestjs/common";
import { UsersService } from "./users.service";
import { PrismaService } from "src/db/prisma.service";
import { UsersController } from "./users.controller";

@Module({
providers: [UsersService, PrismaService],
exports: [UsersService],
controllers: [UsersController],
})
export class UsersModule {}
15 changes: 15 additions & 0 deletions backend/src/users/users.service.ts
Original file line number Diff line number Diff line change
@@ -1,11 +1,26 @@
import { Injectable } from "@nestjs/common";
import { User } from "@prisma/client";
import { PrismaService } from "src/db/prisma.service";
import { FindUserResponse } from "./types/find-user-response.type";

@Injectable()
export class UsersService {
constructor(private prismaService: PrismaService) {}

async findOne(userId: string): Promise<FindUserResponse> {
return await this.prismaService.user.findUnique({
select: {
id: true,
nickname: true,
createdAt: true,
updatedAt: true,
},
where: {
id: userId,
},
});
}

async findOrCreate(
socialProvider: string,
socialUid: string,
Expand Down

0 comments on commit 2944ade

Please sign in to comment.