├── src ├── auth │ ├── dto │ │ ├── index.ts │ │ └── auth.dto.ts │ ├── strategy │ │ ├── index.ts │ │ └── jwt.strategy.ts │ ├── guard │ │ ├── index.ts │ │ └── jwt.guard.ts │ ├── decorator │ │ ├── index.ts │ │ └── get-user.decorator.ts │ ├── auth.module.ts │ ├── auth.controller.ts │ └── auth.service.ts ├── user │ ├── dto │ │ ├── index.ts │ │ └── user.dto.ts │ ├── user.module.ts │ ├── user.controller.ts │ └── user.service.ts ├── movie │ ├── dto │ │ ├── index.ts │ │ └── movie.dto.ts │ ├── movie.module.ts │ ├── movie.controller.ts │ └── movie.service.ts ├── prisma │ ├── prisma.module.ts │ └── prisma.service.ts ├── notification │ ├── notification.module.ts │ ├── notification.service.ts │ └── notification.controller.ts ├── app.module.ts └── main.ts ├── .prettierrc ├── tsconfig.build.json ├── .env.example ├── nest-cli.json ├── test ├── jest-e2e.json └── app.e2e-spec.ts ├── docker-compose.yml ├── .gitignore ├── tsconfig.json ├── .eslintrc.js ├── prisma └── schema.prisma ├── package.json └── README.md /src/auth/dto/index.ts: -------------------------------------------------------------------------------- 1 | -------------------------------------------------------------------------------- /src/auth/strategy/index.ts: -------------------------------------------------------------------------------- 1 | -------------------------------------------------------------------------------- /src/user/dto/index.ts: -------------------------------------------------------------------------------- 1 | export * from "./user.dto" -------------------------------------------------------------------------------- /src/auth/guard/index.ts: -------------------------------------------------------------------------------- 1 | export * from "./jwt.guard" -------------------------------------------------------------------------------- /src/movie/dto/index.ts: -------------------------------------------------------------------------------- 1 | export * from "./movie.dto" -------------------------------------------------------------------------------- /src/auth/decorator/index.ts: -------------------------------------------------------------------------------- 1 | export * from './get-user.decorator'; 2 | -------------------------------------------------------------------------------- /.prettierrc: -------------------------------------------------------------------------------- 1 | { 2 | "singleQuote": true, 3 | "trailingComma": "all" 4 | } -------------------------------------------------------------------------------- /tsconfig.build.json: -------------------------------------------------------------------------------- 1 | { 2 | "extends": "./tsconfig.json", 3 | "exclude": ["node_modules", "test", "dist", "**/*spec.ts"] 4 | } 5 | -------------------------------------------------------------------------------- /.env.example: -------------------------------------------------------------------------------- 1 | #App 2 | PORT=3009 3 | SWAGGER_PATH=docs 4 | SECRET_TOKEN="uasrpl" 5 | #PostgreeSql 6 | DATABASE_URL="postgresql://fzrsahi:123@localhost:5434/uas_se?schema=public" -------------------------------------------------------------------------------- /src/auth/guard/jwt.guard.ts: -------------------------------------------------------------------------------- 1 | import { AuthGuard } from '@nestjs/passport'; 2 | 3 | export class JwtGuard extends AuthGuard('jwt') { 4 | constructor() { 5 | super(); 6 | } 7 | } 8 | -------------------------------------------------------------------------------- /nest-cli.json: -------------------------------------------------------------------------------- 1 | { 2 | "$schema": "https://json.schemastore.org/nest-cli", 3 | "collection": "@nestjs/schematics", 4 | "sourceRoot": "src", 5 | "compilerOptions": { 6 | "deleteOutDir": true 7 | } 8 | } 9 | -------------------------------------------------------------------------------- /test/jest-e2e.json: -------------------------------------------------------------------------------- 1 | { 2 | "moduleFileExtensions": ["js", "json", "ts"], 3 | "rootDir": ".", 4 | "testEnvironment": "node", 5 | "testRegex": ".e2e-spec.ts$", 6 | "transform": { 7 | "^.+\\.(t|j)s$": "ts-jest" 8 | } 9 | } 10 | -------------------------------------------------------------------------------- /src/prisma/prisma.module.ts: -------------------------------------------------------------------------------- 1 | import { Module } from '@nestjs/common'; 2 | import { PrismaService } from './prisma.service'; 3 | 4 | @Module({ 5 | providers: [PrismaService], 6 | exports : [PrismaService] 7 | }) 8 | export class PrismaModule {} 9 | -------------------------------------------------------------------------------- /src/prisma/prisma.service.ts: -------------------------------------------------------------------------------- 1 | import { Injectable, OnModuleInit } from '@nestjs/common'; 2 | import { PrismaClient } from '@prisma/client'; 3 | 4 | @Injectable() 5 | export class PrismaService extends PrismaClient implements OnModuleInit { 6 | async onModuleInit() { 7 | await this.$connect(); 8 | } 9 | } -------------------------------------------------------------------------------- /docker-compose.yml: -------------------------------------------------------------------------------- 1 | version: '3.8' 2 | services: 3 | dev-db: 4 | image: postgres:alpine3.18 5 | ports: 6 | - 5434:5432 7 | environment: 8 | POSTGRES_USER: fzrsahi 9 | POSTGRES_PASSWORD: 123 10 | POSTGRES_DB: uas_se 11 | # change the value to your configuration 12 | -------------------------------------------------------------------------------- /src/auth/decorator/get-user.decorator.ts: -------------------------------------------------------------------------------- 1 | import { createParamDecorator, ExecutionContext } from '@nestjs/common'; 2 | 3 | export const GetUser = createParamDecorator( 4 | (data: string | undefined, ctx: ExecutionContext) => { 5 | const request = ctx.switchToHttp().getRequest(); 6 | if (data) { 7 | return request.user[data]; 8 | } 9 | return request.user; 10 | }, 11 | ); 12 | -------------------------------------------------------------------------------- /src/user/user.module.ts: -------------------------------------------------------------------------------- 1 | import { Module } from '@nestjs/common'; 2 | import { UserController } from './user.controller'; 3 | import { UserService } from './user.service'; 4 | import { PrismaModule } from 'src/prisma/prisma.module'; 5 | 6 | @Module({ 7 | controllers: [UserController], 8 | providers: [UserService], 9 | imports: [PrismaModule], 10 | exports: [UserService], 11 | }) 12 | export class UserModule {} 13 | -------------------------------------------------------------------------------- /src/movie/movie.module.ts: -------------------------------------------------------------------------------- 1 | import { Module } from '@nestjs/common'; 2 | import { MovieController } from './movie.controller'; 3 | import { MovieService } from './movie.service'; 4 | import { PrismaService } from 'src/prisma/prisma.service'; 5 | import { PrismaModule } from 'src/prisma/prisma.module'; 6 | 7 | @Module({ 8 | controllers: [MovieController], 9 | providers: [MovieService], 10 | imports: [PrismaModule] 11 | }) 12 | export class MovieModule {} 13 | -------------------------------------------------------------------------------- /src/notification/notification.module.ts: -------------------------------------------------------------------------------- 1 | import { Module } from '@nestjs/common'; 2 | import { NotificationService } from './notification.service'; 3 | import { NotificationController } from './notification.controller'; 4 | import { PrismaModule } from 'src/prisma/prisma.module'; 5 | 6 | @Module({ 7 | providers: [NotificationService], 8 | controllers: [NotificationController], 9 | imports: [PrismaModule], 10 | }) 11 | export class NotificationModule {} 12 | -------------------------------------------------------------------------------- /src/user/dto/user.dto.ts: -------------------------------------------------------------------------------- 1 | import { ApiProperty } from '@nestjs/swagger'; 2 | 3 | export class CreateUserDto { 4 | @ApiProperty({ required: true }) 5 | name: string; 6 | 7 | @ApiProperty({ required: true }) 8 | username: string; 9 | 10 | @ApiProperty({ required: true }) 11 | password: string; 12 | } 13 | 14 | export class UpdateUserDto { 15 | @ApiProperty({ required: false }) 16 | name: string; 17 | 18 | @ApiProperty({ required: false }) 19 | username: string; 20 | } 21 | -------------------------------------------------------------------------------- /.gitignore: -------------------------------------------------------------------------------- 1 | # compiled output 2 | /dist 3 | /node_modules 4 | .env 5 | 6 | # Logs 7 | logs 8 | *.log 9 | npm-debug.log* 10 | pnpm-debug.log* 11 | yarn-debug.log* 12 | yarn-error.log* 13 | lerna-debug.log* 14 | 15 | # OS 16 | .DS_Store 17 | 18 | # Tests 19 | /coverage 20 | /.nyc_output 21 | 22 | # IDEs and editors 23 | /.idea 24 | .project 25 | .classpath 26 | .c9/ 27 | *.launch 28 | .settings/ 29 | *.sublime-workspace 30 | 31 | # IDE - VSCode 32 | .vscode/* 33 | !.vscode/settings.json 34 | !.vscode/tasks.json 35 | !.vscode/launch.json 36 | !.vscode/extensions.json -------------------------------------------------------------------------------- /src/movie/dto/movie.dto.ts: -------------------------------------------------------------------------------- 1 | import { ApiProperty } from '@nestjs/swagger'; 2 | 3 | export class CreateMovieDto { 4 | @ApiProperty({ required: true }) 5 | title: string; 6 | @ApiProperty({ required: true }) 7 | year: string; 8 | @ApiProperty({ required: true }) 9 | description: string; 10 | 11 | created_by?: string; 12 | id?: string; 13 | 14 | } 15 | 16 | export class UpdateMovieDto { 17 | @ApiProperty({ required: false }) 18 | title: string; 19 | @ApiProperty({ required: false }) 20 | year: string; 21 | @ApiProperty({ required: false }) 22 | description: string; 23 | } 24 | -------------------------------------------------------------------------------- /src/auth/auth.module.ts: -------------------------------------------------------------------------------- 1 | import { Module } from '@nestjs/common'; 2 | import { AuthController } from './auth.controller'; 3 | import { AuthService } from './auth.service'; 4 | import { UserModule } from 'src/user/user.module'; 5 | import { JwtModule } from '@nestjs/jwt'; 6 | import { PrismaModule } from 'src/prisma/prisma.module'; 7 | import { JwtStrategy } from './strategy/jwt.strategy'; 8 | 9 | @Module({ 10 | controllers: [AuthController], 11 | providers: [AuthService, JwtStrategy], 12 | imports: [UserModule, PrismaModule, JwtModule.register({})], 13 | exports: [AuthService], 14 | }) 15 | export class AuthModule {} 16 | -------------------------------------------------------------------------------- /tsconfig.json: -------------------------------------------------------------------------------- 1 | { 2 | "compilerOptions": { 3 | "module": "commonjs", 4 | "declaration": true, 5 | "removeComments": true, 6 | "emitDecoratorMetadata": true, 7 | "experimentalDecorators": true, 8 | "allowSyntheticDefaultImports": true, 9 | "target": "ES2021", 10 | "sourceMap": true, 11 | "outDir": "./dist", 12 | "baseUrl": "./", 13 | "incremental": true, 14 | "skipLibCheck": true, 15 | "strictNullChecks": false, 16 | "noImplicitAny": false, 17 | "strictBindCallApply": false, 18 | "forceConsistentCasingInFileNames": false, 19 | "noFallthroughCasesInSwitch": false 20 | } 21 | } 22 | -------------------------------------------------------------------------------- /src/auth/dto/auth.dto.ts: -------------------------------------------------------------------------------- 1 | import { ApiProperty } from '@nestjs/swagger'; 2 | import { 3 | IsEmail, 4 | IsNotEmpty, 5 | IsNumber, 6 | IsOptional, 7 | IsString, 8 | } from 'class-validator'; 9 | 10 | export class CreateUserDto { 11 | @IsString() 12 | @IsNotEmpty() 13 | name: string; 14 | 15 | @IsString() 16 | @IsNotEmpty() 17 | username: string; 18 | 19 | @IsString() 20 | @IsNotEmpty() 21 | password: string; 22 | } 23 | 24 | export class UserLoginDto { 25 | @ApiProperty() 26 | @IsString() 27 | @IsNotEmpty() 28 | username: string; 29 | 30 | @ApiProperty() 31 | @IsString() 32 | @IsNotEmpty() 33 | password: string; 34 | } 35 | -------------------------------------------------------------------------------- /src/notification/notification.service.ts: -------------------------------------------------------------------------------- 1 | import { Injectable } from '@nestjs/common'; 2 | import { CreateMovieDto } from 'src/movie/dto'; 3 | import { PrismaService } from 'src/prisma/prisma.service'; 4 | 5 | @Injectable() 6 | export class NotificationService { 7 | constructor(private readonly prisma: PrismaService) {} 8 | 9 | async getAll() { 10 | return await this.prisma.notifications.findMany({ 11 | include: { 12 | movie: true, 13 | }, 14 | }); 15 | } 16 | 17 | async create(data: CreateMovieDto) { 18 | await this.prisma.notifications.create({ 19 | data: { 20 | movie_id: data.id, 21 | message: `New Movie : ${data.title} Just Posted !`, 22 | }, 23 | }); 24 | } 25 | } 26 | -------------------------------------------------------------------------------- /test/app.e2e-spec.ts: -------------------------------------------------------------------------------- 1 | import { Test, TestingModule } from '@nestjs/testing'; 2 | import { INestApplication } from '@nestjs/common'; 3 | import * as request from 'supertest'; 4 | import { AppModule } from './../src/app.module'; 5 | 6 | describe('AppController (e2e)', () => { 7 | let app: INestApplication; 8 | 9 | beforeEach(async () => { 10 | const moduleFixture: TestingModule = await Test.createTestingModule({ 11 | imports: [AppModule], 12 | }).compile(); 13 | 14 | app = moduleFixture.createNestApplication(); 15 | await app.init(); 16 | }); 17 | 18 | it('/ (GET)', () => { 19 | return request(app.getHttpServer()) 20 | .get('/') 21 | .expect(200) 22 | .expect('Hello World!'); 23 | }); 24 | }); 25 | -------------------------------------------------------------------------------- /.eslintrc.js: -------------------------------------------------------------------------------- 1 | module.exports = { 2 | parser: '@typescript-eslint/parser', 3 | parserOptions: { 4 | project: 'tsconfig.json', 5 | tsconfigRootDir: __dirname, 6 | sourceType: 'module', 7 | }, 8 | plugins: ['@typescript-eslint/eslint-plugin'], 9 | extends: [ 10 | 'plugin:@typescript-eslint/recommended', 11 | 'plugin:prettier/recommended', 12 | ], 13 | root: true, 14 | env: { 15 | node: true, 16 | jest: true, 17 | }, 18 | ignorePatterns: ['.eslintrc.js'], 19 | rules: { 20 | '@typescript-eslint/interface-name-prefix': 'off', 21 | '@typescript-eslint/explicit-function-return-type': 'off', 22 | '@typescript-eslint/explicit-module-boundary-types': 'off', 23 | '@typescript-eslint/no-explicit-any': 'off', 24 | }, 25 | }; 26 | -------------------------------------------------------------------------------- /src/notification/notification.controller.ts: -------------------------------------------------------------------------------- 1 | import { Controller, Get } from '@nestjs/common'; 2 | import { NotificationService } from './notification.service'; 3 | import { EventEmitter2, OnEvent } from '@nestjs/event-emitter'; 4 | import { ApiTags } from '@nestjs/swagger'; 5 | import { CreateMovieDto } from 'src/movie/dto'; 6 | 7 | @ApiTags('Notifications') 8 | @Controller('notifications') 9 | export class NotificationController { 10 | constructor( 11 | private readonly notifService: NotificationService, 12 | private readonly eventService: EventEmitter2, 13 | ) {} 14 | 15 | @OnEvent('movie:created') 16 | createNotif(data: CreateMovieDto) { 17 | return this.notifService.create(data); 18 | } 19 | 20 | @Get() 21 | getAll() { 22 | return this.notifService.getAll(); 23 | } 24 | } 25 | -------------------------------------------------------------------------------- /src/app.module.ts: -------------------------------------------------------------------------------- 1 | import { Module } from '@nestjs/common'; 2 | import { UserModule } from './user/user.module'; 3 | import { MovieModule } from './movie/movie.module'; 4 | import { AuthModule } from './auth/auth.module'; 5 | import { ConfigModule } from '@nestjs/config'; 6 | import { PrismaModule } from './prisma/prisma.module'; 7 | import { EventEmitterModule } from '@nestjs/event-emitter'; 8 | import { NotificationModule } from './notification/notification.module'; 9 | 10 | @Module({ 11 | imports: [ 12 | UserModule, 13 | MovieModule, 14 | AuthModule, 15 | UserModule, 16 | ConfigModule.forRoot({ 17 | isGlobal: true, 18 | }), 19 | PrismaModule, 20 | EventEmitterModule.forRoot(), 21 | NotificationModule, 22 | ], 23 | controllers: [], 24 | providers: [], 25 | }) 26 | export class AppModule {} 27 | -------------------------------------------------------------------------------- /src/auth/auth.controller.ts: -------------------------------------------------------------------------------- 1 | import { Body, Controller, HttpCode, Post } from '@nestjs/common'; 2 | import { AuthService } from './auth.service'; 3 | import { UserLoginDto } from './dto/auth.dto'; 4 | import { ApiTags } from '@nestjs/swagger'; 5 | import { CreateUserDto } from 'src/user/dto'; 6 | import { UserService } from 'src/user/user.service'; 7 | 8 | @ApiTags('Auth') 9 | @Controller('auth') 10 | export class AuthController { 11 | constructor( 12 | private readonly authService: AuthService, 13 | private readonly userService: UserService, 14 | ) {} 15 | 16 | @HttpCode(200) 17 | @Post('login') 18 | login(@Body() dto: UserLoginDto) { 19 | return this.authService.userLogin(dto); 20 | } 21 | 22 | @Post('register') 23 | register(@Body() dto: CreateUserDto) { 24 | return this.userService.create(dto); 25 | } 26 | } 27 | -------------------------------------------------------------------------------- /src/auth/strategy/jwt.strategy.ts: -------------------------------------------------------------------------------- 1 | import { Injectable } from '@nestjs/common'; 2 | import { ConfigService } from '@nestjs/config'; 3 | import { PassportStrategy } from '@nestjs/passport'; 4 | import { ExtractJwt, Strategy } from 'passport-jwt'; 5 | import { PrismaService } from '../../prisma/prisma.service'; 6 | 7 | @Injectable() 8 | export class JwtStrategy extends PassportStrategy(Strategy, 'jwt') { 9 | constructor(config: ConfigService, private prisma: PrismaService) { 10 | super({ 11 | jwtFromRequest: ExtractJwt.fromAuthHeaderAsBearerToken(), 12 | secretOrKey: config.get('SECRET_TOKEN'), 13 | }); 14 | } 15 | 16 | async validate(payload: { sub: string; username: string }) { 17 | const user = await this.prisma.users.findUnique({ 18 | where: { 19 | id: payload.sub, 20 | }, 21 | }); 22 | delete user.password; 23 | return user; 24 | } 25 | } 26 | -------------------------------------------------------------------------------- /prisma/schema.prisma: -------------------------------------------------------------------------------- 1 | // This is your Prisma schema file, 2 | // learn more about it in the docs: https://pris.ly/d/prisma-schema 3 | 4 | generator client { 5 | provider = "prisma-client-js" 6 | } 7 | 8 | datasource db { 9 | provider = "postgresql" 10 | url = env("DATABASE_URL") 11 | } 12 | 13 | model Users { 14 | id String @id @default(uuid()) 15 | name String 16 | username String 17 | password String 18 | movies Movies[] 19 | 20 | created_at DateTime @default(now()) 21 | updated_at DateTime @updatedAt 22 | } 23 | 24 | model Movies { 25 | id String @id @default(uuid()) 26 | title String 27 | year String 28 | description String 29 | 30 | user Users? @relation(fields: [posted_by], references: [id]) 31 | posted_by String 32 | updated_by String? 33 | 34 | created_at DateTime @default(now()) 35 | updated_at DateTime @updatedAt 36 | Notifications Notifications[] 37 | } 38 | 39 | model Notifications { 40 | id String @id @default(uuid()) 41 | movie Movies? @relation(fields: [movie_id], references: [id]) 42 | movie_id String 43 | message String 44 | } 45 | -------------------------------------------------------------------------------- /src/user/user.controller.ts: -------------------------------------------------------------------------------- 1 | import { 2 | BadRequestException, 3 | Body, 4 | Controller, 5 | Delete, 6 | Get, 7 | Param, 8 | Patch, 9 | Post, 10 | UseGuards, 11 | } from '@nestjs/common'; 12 | import { ApiBearerAuth, ApiTags } from '@nestjs/swagger'; 13 | import { UserService } from './user.service'; 14 | import { CreateUserDto, UpdateUserDto } from './dto'; 15 | import { JwtGuard } from 'src/auth/guard'; 16 | import { GetUser } from 'src/auth/decorator'; 17 | 18 | @UseGuards(JwtGuard) 19 | @ApiBearerAuth('JWTAUTH') 20 | @ApiTags('User') 21 | @Controller('users') 22 | export class UserController { 23 | constructor(private readonly userService: UserService) {} 24 | 25 | @Get() 26 | getAll() { 27 | return this.userService.getAll(); 28 | } 29 | 30 | @Get(':id') 31 | findOneById(@Param('id') id: string) { 32 | return this.userService.findOneById(id); 33 | } 34 | 35 | @Post() 36 | async create(@Body() dto: CreateUserDto) { 37 | return this.userService.create(dto); 38 | } 39 | 40 | @Patch(':id') 41 | async updateOneById( 42 | @Param('id') id: string, 43 | @Body() dto: UpdateUserDto, 44 | @GetUser('id') currentUserId: string, 45 | ) { 46 | if (id !== currentUserId) { 47 | throw new BadRequestException('You Cannot Update someone else account'); 48 | } 49 | return await this.userService.update(id, dto); 50 | } 51 | } 52 | -------------------------------------------------------------------------------- /src/movie/movie.controller.ts: -------------------------------------------------------------------------------- 1 | import { 2 | Body, 3 | Controller, 4 | Delete, 5 | Get, 6 | Param, 7 | Patch, 8 | Post, 9 | UseGuards, 10 | } from '@nestjs/common'; 11 | import { MovieService } from './movie.service'; 12 | import { ApiBearerAuth, ApiTags } from '@nestjs/swagger'; 13 | import { CreateMovieDto, UpdateMovieDto } from './dto'; 14 | import { JwtGuard } from 'src/auth/guard'; 15 | import { GetUser } from 'src/auth/decorator'; 16 | 17 | @UseGuards(JwtGuard) 18 | @ApiBearerAuth('JWTAUTH') 19 | @Controller('movies') 20 | @ApiTags('Movie') 21 | export class MovieController { 22 | constructor(private readonly movieService: MovieService) {} 23 | 24 | @Get() 25 | getAll() { 26 | return this.movieService.getAll(); 27 | } 28 | 29 | @Get(':id') 30 | findOneById(@Param('id') id: string) { 31 | return this.movieService.findOneById(id); 32 | } 33 | 34 | @Post() 35 | create(@Body() dto: CreateMovieDto, @GetUser('id') userId: string) { 36 | return this.movieService.create(dto, userId); 37 | } 38 | 39 | @Patch(':id') 40 | updateOneById( 41 | @Param('id') id: string, 42 | @Body() dto: UpdateMovieDto, 43 | @GetUser('id') userId: string, 44 | ) { 45 | return this.movieService.updateOneById(id, dto, userId); 46 | } 47 | 48 | @Delete(':id') 49 | deleteOneById(@Param('id') id: string) { 50 | return this.movieService.deleteOneById(id); 51 | } 52 | } 53 | -------------------------------------------------------------------------------- /src/main.ts: -------------------------------------------------------------------------------- 1 | import { NestFactory } from '@nestjs/core'; 2 | import { SwaggerModule, DocumentBuilder } from '@nestjs/swagger'; 3 | import { AppModule } from './app.module'; 4 | import { ConfigService } from '@nestjs/config'; 5 | import { ValidationPipe } from '@nestjs/common'; 6 | 7 | async function bootstrap() { 8 | const app = await NestFactory.create(AppModule); 9 | const configService = new ConfigService(); 10 | app.useGlobalPipes(new ValidationPipe()); 11 | const PORT = configService.get('PORT'); 12 | const SWAGGER_PATH = configService.get('SWAGGER_PATH'); 13 | 14 | const config = new DocumentBuilder() 15 | .setTitle('UAS SE Movie REST API Documentations') 16 | .setDescription('untuk matakuliah software engineer') 17 | .setVersion('1.0') 18 | .addBearerAuth( 19 | { 20 | type: 'http', 21 | scheme: 'bearer', 22 | bearerFormat: 'JWT', 23 | name: 'JWT', 24 | description: 'Enter Jwt Token', 25 | in: 'header', 26 | }, 27 | 'JWTAUTH', 28 | ) 29 | .build(); 30 | const document = SwaggerModule.createDocument(app, config); 31 | SwaggerModule.setup('docs', app, document); 32 | 33 | await app.listen(PORT); 34 | console.log( 35 | '======================================================================', 36 | ); 37 | console.log(`Server Berjalan DI : http://localhost:${PORT}/`); 38 | console.log(`Swagger Berjalan DI : http://localhost:${PORT}/${SWAGGER_PATH}`); 39 | console.log( 40 | '======================================================================', 41 | ); 42 | } 43 | bootstrap(); 44 | -------------------------------------------------------------------------------- /src/movie/movie.service.ts: -------------------------------------------------------------------------------- 1 | import { Injectable } from '@nestjs/common'; 2 | import { CreateMovieDto, UpdateMovieDto } from './dto'; 3 | import { PrismaService } from 'src/prisma/prisma.service'; 4 | import { EventEmitter2 } from '@nestjs/event-emitter'; 5 | 6 | @Injectable() 7 | export class MovieService { 8 | constructor( 9 | private readonly prisma: PrismaService, 10 | private readonly eventEmitter: EventEmitter2, 11 | ) {} 12 | 13 | async deleteOneById(id: string) { 14 | return await this.prisma.movies.delete({ 15 | where: { 16 | id, 17 | }, 18 | }); 19 | } 20 | 21 | async create(dto: CreateMovieDto, userId: string) { 22 | const movies = await this.prisma.movies.create({ 23 | data: { 24 | title: dto.title, 25 | description: dto.description, 26 | year: dto.year, 27 | posted_by: userId, 28 | }, 29 | }); 30 | 31 | this.eventEmitter.emitAsync('movie:created', movies); 32 | return movies; 33 | } 34 | 35 | async updateOneById(id: string, dto: UpdateMovieDto, userId: string) { 36 | console.log({ id, dto, userId }); 37 | return await this.prisma.movies.update({ 38 | where: { 39 | id, 40 | }, 41 | data: { 42 | title: dto.title, 43 | description: dto.description, 44 | year: dto.year, 45 | updated_by: userId, 46 | }, 47 | }); 48 | } 49 | 50 | async findOneById(id: string) { 51 | return await this.prisma.movies.findUnique({ 52 | where: { 53 | id, 54 | }, 55 | }); 56 | } 57 | 58 | async getAll() { 59 | return await this.prisma.movies.findMany({ 60 | include: { 61 | user: { 62 | select: { 63 | id: true, 64 | username: true, 65 | }, 66 | }, 67 | }, 68 | }); 69 | } 70 | } 71 | -------------------------------------------------------------------------------- /src/auth/auth.service.ts: -------------------------------------------------------------------------------- 1 | import { ForbiddenException, Injectable } from '@nestjs/common'; 2 | import { PrismaService } from 'src/prisma/prisma.service'; 3 | import { UserLoginDto } from './dto/auth.dto'; 4 | import { Prisma } from '@prisma/client'; 5 | import * as bcrypt from 'bcrypt'; 6 | import { ConfigService } from '@nestjs/config'; 7 | import { JwtService } from '@nestjs/jwt'; 8 | 9 | @Injectable() 10 | export class AuthService { 11 | constructor( 12 | private readonly prisma: PrismaService, 13 | private readonly config: ConfigService, 14 | private readonly jwt: JwtService, 15 | ) {} 16 | async userLogin(dto: UserLoginDto) { 17 | const user = await this.prisma.users.findFirst({ 18 | where: { 19 | username: dto.username, 20 | }, 21 | }); 22 | 23 | try { 24 | if (!user) throw new ForbiddenException('Account Not Found!'); 25 | const pwMatches = await bcrypt.compare(dto.password, user.password); 26 | if (!pwMatches) throw new ForbiddenException('Account Not Found!'); 27 | delete user.password; 28 | const token = await this.signToken(user.id, user.username); 29 | return { 30 | statusCode: 200, 31 | message: 'Log In!', 32 | data: user, 33 | token, 34 | }; 35 | } catch (error) { 36 | throw error; 37 | } 38 | } 39 | 40 | getAge(dateString) { 41 | var today = new Date(); 42 | var birthDate = new Date(dateString); 43 | var age = today.getFullYear() - birthDate.getFullYear(); 44 | var m = today.getMonth() - birthDate.getMonth(); 45 | if (m < 0 || (m === 0 && today.getDate() < birthDate.getDate())) { 46 | age--; 47 | } 48 | return age; 49 | } 50 | 51 | async signToken(id: string, email: string): Promise { 52 | const payload = { sub: id, email }; 53 | return this.jwt.signAsync(payload, { 54 | expiresIn: '1d', 55 | secret: this.config.get('SECRET_TOKEN'), 56 | }); 57 | } 58 | } 59 | -------------------------------------------------------------------------------- /src/user/user.service.ts: -------------------------------------------------------------------------------- 1 | import { BadRequestException, Inject, Injectable } from '@nestjs/common'; 2 | import { CreateUserDto, UpdateUserDto } from './dto'; 3 | import { PrismaService } from 'src/prisma/prisma.service'; 4 | import * as bcrypt from 'bcrypt'; 5 | 6 | @Injectable() 7 | export class UserService { 8 | constructor(private readonly prisma: PrismaService) {} 9 | 10 | async update(id: string, dto: UpdateUserDto) { 11 | return await this.prisma.users.update({ 12 | where: { 13 | id, 14 | }, 15 | data: { 16 | name: dto.name, 17 | username: dto.username, 18 | }, 19 | select: { 20 | id: true, 21 | name: true, 22 | username: true, 23 | }, 24 | }); 25 | } 26 | 27 | async create(dto: CreateUserDto) { 28 | dto.password = await bcrypt.hash(dto.password, 10); 29 | const username = dto.username; 30 | const isUsernameExist = await this.prisma.users.findFirst({ 31 | where: { 32 | username, 33 | }, 34 | select: { 35 | username: true, 36 | }, 37 | }); 38 | if (isUsernameExist) 39 | throw new BadRequestException('Username Already Registered !'); 40 | 41 | return this.prisma.users.create({ 42 | data: { 43 | username, 44 | name: dto.name, 45 | password: dto.password, 46 | }, 47 | select: { 48 | id: true, 49 | username: true, 50 | created_at: true, 51 | }, 52 | }); 53 | } 54 | 55 | async getAll() { 56 | return await this.prisma.users.findMany({ 57 | orderBy: { 58 | created_at: 'desc', 59 | }, 60 | select: { 61 | id: true, 62 | name: true, 63 | created_at: true, 64 | updated_at: true, 65 | }, 66 | }); 67 | } 68 | 69 | async findOneById(id: string) { 70 | return await this.prisma.users.findUnique({ 71 | where: { 72 | id, 73 | }, 74 | select: { 75 | id: true, 76 | name: true, 77 | created_at: true, 78 | updated_at: true, 79 | }, 80 | }); 81 | } 82 | } 83 | -------------------------------------------------------------------------------- /package.json: -------------------------------------------------------------------------------- 1 | { 2 | "name": "uas-se", 3 | "version": "0.0.1", 4 | "description": "", 5 | "author": "", 6 | "private": true, 7 | "license": "UNLICENSED", 8 | "scripts": { 9 | "db:dev:down": "docker compose down", 10 | "db:dev:up": "docker compose up -d", 11 | "db:migrate": "npx prisma db push", 12 | "db:restart": "npm run db:dev:down && sleep 1 && npm run db:dev:up && sleep 2 && npm run db:migrate", 13 | "build": "nest build", 14 | "format": "prettier --write \"src/**/*.ts\" \"test/**/*.ts\"", 15 | "start": "nest start", 16 | "start:dev": "nest start --watch", 17 | "start:debug": "nest start --debug --watch", 18 | "start:prod": "node dist/main", 19 | "lint": "eslint \"{src,apps,libs,test}/**/*.ts\" --fix", 20 | "test": "jest", 21 | "test:watch": "jest --watch", 22 | "test:cov": "jest --coverage", 23 | "test:debug": "node --inspect-brk -r tsconfig-paths/register -r ts-node/register node_modules/.bin/jest --runInBand", 24 | "test:e2e": "jest --config ./test/jest-e2e.json" 25 | }, 26 | "dependencies": { 27 | "@nestjs/common": "^10.0.0", 28 | "@nestjs/config": "^3.1.1", 29 | "@nestjs/core": "^10.0.0", 30 | "@nestjs/event-emitter": "^2.0.3", 31 | "@nestjs/jwt": "^10.2.0", 32 | "@nestjs/passport": "^10.0.3", 33 | "@nestjs/platform-express": "^10.0.0", 34 | "@nestjs/swagger": "^7.1.16", 35 | "@prisma/client": "^5.6.0", 36 | "bcrypt": "^5.1.1", 37 | "class-transformer": "^0.5.1", 38 | "class-validator": "^0.14.0", 39 | "passport": "^0.7.0", 40 | "passport-jwt": "^4.0.1", 41 | "reflect-metadata": "^0.1.13", 42 | "rxjs": "^7.8.1" 43 | }, 44 | "devDependencies": { 45 | "@nestjs/cli": "^10.0.0", 46 | "@nestjs/schematics": "^10.0.0", 47 | "@nestjs/testing": "^10.0.0", 48 | "@types/bcrypt": "^5.0.2", 49 | "@types/express": "^4.17.17", 50 | "@types/jest": "^29.5.2", 51 | "@types/node": "^20.3.1", 52 | "@types/passport-jwt": "^3.0.13", 53 | "@types/supertest": "^2.0.12", 54 | "@typescript-eslint/eslint-plugin": "^6.0.0", 55 | "@typescript-eslint/parser": "^6.0.0", 56 | "eslint": "^8.42.0", 57 | "eslint-config-prettier": "^9.0.0", 58 | "eslint-plugin-prettier": "^5.0.0", 59 | "jest": "^29.5.0", 60 | "prettier": "^3.0.0", 61 | "prisma": "^5.6.0", 62 | "source-map-support": "^0.5.21", 63 | "supertest": "^6.3.3", 64 | "ts-jest": "^29.1.0", 65 | "ts-loader": "^9.4.3", 66 | "ts-node": "^10.9.1", 67 | "tsconfig-paths": "^4.2.0", 68 | "typescript": "^5.1.3" 69 | }, 70 | "jest": { 71 | "moduleFileExtensions": [ 72 | "js", 73 | "json", 74 | "ts" 75 | ], 76 | "rootDir": "src", 77 | "testRegex": ".*\\.spec\\.ts$", 78 | "transform": { 79 | "^.+\\.(t|j)s$": "ts-jest" 80 | }, 81 | "collectCoverageFrom": [ 82 | "**/*.(t|j)s" 83 | ], 84 | "coverageDirectory": "../coverage", 85 | "testEnvironment": "node" 86 | } 87 | } 88 | -------------------------------------------------------------------------------- /README.md: -------------------------------------------------------------------------------- 1 |

2 | Nest Logo 3 |

4 | 5 | [circleci-image]: https://img.shields.io/circleci/build/github/nestjs/nest/master?token=abc123def456 6 | [circleci-url]: https://circleci.com/gh/nestjs/nest 7 | 8 |

A progressive Node.js framework for building efficient and scalable server-side applications.

9 |

10 | NPM Version 11 | Package License 12 | NPM Downloads 13 | CircleCI 14 | Coverage 15 | Discord 16 | Backers on Open Collective 17 | Sponsors on Open Collective 18 | 19 | Support us 20 | 21 |

22 | 24 | 25 | ## Description 26 | 27 | [Nest](https://github.com/nestjs/nest) framework TypeScript starter repository. 28 | 29 | ## Installation 30 | 31 | ```bash 32 | $ yarn install 33 | ``` 34 | 35 | ## Running the app 36 | 37 | ```bash 38 | # development 39 | $ yarn run start 40 | 41 | # watch mode 42 | $ yarn run start:dev 43 | 44 | # production mode 45 | $ yarn run start:prod 46 | ``` 47 | 48 | ## Test 49 | 50 | ```bash 51 | # unit tests 52 | $ yarn run test 53 | 54 | # e2e tests 55 | $ yarn run test:e2e 56 | 57 | # test coverage 58 | $ yarn run test:cov 59 | ``` 60 | 61 | ## Support 62 | 63 | Nest is an MIT-licensed open source project. It can grow thanks to the sponsors and support by the amazing backers. If you'd like to join them, please [read more here](https://docs.nestjs.com/support). 64 | 65 | ## Stay in touch 66 | 67 | - Author - [Kamil Myśliwiec](https://kamilmysliwiec.com) 68 | - Website - [https://nestjs.com](https://nestjs.com/) 69 | - Twitter - [@nestframework](https://twitter.com/nestframework) 70 | 71 | ## License 72 | 73 | Nest is [MIT licensed](LICENSE). 74 | --------------------------------------------------------------------------------