-
Notifications
You must be signed in to change notification settings - Fork 1
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
feat: add notes module with a simple crud
Signed-off-by: iverly <github@iverly.net>
- Loading branch information
Showing
12 changed files
with
478 additions
and
0 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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,18 @@ | ||
{ | ||
"extends": ["../../.eslintrc.json"], | ||
"ignorePatterns": ["!**/*"], | ||
"overrides": [ | ||
{ | ||
"files": ["*.ts", "*.tsx", "*.js", "*.jsx"], | ||
"rules": {} | ||
}, | ||
{ | ||
"files": ["*.ts", "*.tsx"], | ||
"rules": {} | ||
}, | ||
{ | ||
"files": ["*.js", "*.jsx"], | ||
"rules": {} | ||
} | ||
] | ||
} |
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,11 @@ | ||
/* eslint-disable */ | ||
export default { | ||
displayName: 'notes', | ||
preset: '../../jest.preset.js', | ||
testEnvironment: 'node', | ||
transform: { | ||
'^.+\\.[tj]s$': ['ts-jest', { tsconfig: '<rootDir>/tsconfig.spec.json' }], | ||
}, | ||
moduleFileExtensions: ['ts', 'js', 'html'], | ||
coverageDirectory: '../../coverage/packages/notes', | ||
}; |
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,23 @@ | ||
{ | ||
"name": "notes", | ||
"$schema": "../../node_modules/nx/schemas/project-schema.json", | ||
"sourceRoot": "packages/notes/src", | ||
"projectType": "library", | ||
"targets": { | ||
"lint": { | ||
"executor": "@nx/eslint:lint", | ||
"outputs": ["{options.outputFile}"], | ||
"options": { | ||
"lintFilePatterns": ["packages/notes/**/*.ts"] | ||
} | ||
}, | ||
"test": { | ||
"executor": "@nx/jest:jest", | ||
"outputs": ["{workspaceRoot}/coverage/{projectRoot}"], | ||
"options": { | ||
"jestConfig": "packages/notes/jest.config.ts" | ||
} | ||
} | ||
}, | ||
"tags": [] | ||
} |
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 @@ | ||
export * from './lib/notes.module'; |
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,159 @@ | ||
import { | ||
Body, | ||
Controller, | ||
NotFoundException, | ||
Param, | ||
Req, | ||
RequestMethod, | ||
} from '@nestjs/common'; | ||
import { ApiTags } from '@nestjs/swagger'; | ||
import { ApiRouteAuthenticated } from '@nx-next-nest-prisma-ory-template/utils'; | ||
import { NotesService } from './notes.service'; | ||
import { FastifyRequest } from 'fastify'; | ||
import { | ||
CreateNoteDto, | ||
Note, | ||
NoteDto, | ||
UpdateNoteDto, | ||
} from '@nx-next-nest-prisma-ory-template/types'; | ||
|
||
@ApiTags('Notes') | ||
@Controller('notes') | ||
export class NotesController { | ||
constructor(private notesService: NotesService) {} | ||
|
||
@ApiRouteAuthenticated({ | ||
method: RequestMethod.POST, | ||
operation: { | ||
summary: 'Create a note', | ||
description: 'Create a new note with the given name and type', | ||
}, | ||
response: { | ||
status: 201, | ||
type: NoteDto, | ||
}, | ||
}) | ||
create( | ||
@Body() body: CreateNoteDto, | ||
@Req() request: FastifyRequest | ||
): Promise<NoteDto> { | ||
return this.notesService.create(body, request.user.sub); | ||
} | ||
|
||
@ApiRouteAuthenticated({ | ||
method: RequestMethod.GET, | ||
operation: { | ||
summary: 'List all notes', | ||
description: 'Returns the list of all notes (limited to 1000)', | ||
}, | ||
response: { | ||
status: 200, | ||
schema: { | ||
type: 'array', | ||
items: { | ||
type: 'object', | ||
properties: { | ||
id: { | ||
type: 'string', | ||
description: 'The note id', | ||
example: 'b1774d2f-05f7-4ea4-b427-0d808bdca583', | ||
}, | ||
title: { | ||
type: 'string', | ||
description: 'The note title', | ||
example: 'My note', | ||
}, | ||
body: { | ||
type: 'string', | ||
description: 'The note body', | ||
example: 'This is the body of my note', | ||
}, | ||
createdAt: { | ||
type: 'string', | ||
description: 'The note creation date', | ||
example: '2021-03-11T20:43:06.000Z', | ||
}, | ||
}, | ||
}, | ||
}, | ||
}, | ||
}) | ||
async list(@Req() request: FastifyRequest): Promise<Note[]> { | ||
const notes = await this.notesService.list(request.user.sub); | ||
|
||
if (!notes) { | ||
throw new NotFoundException(); | ||
} | ||
|
||
return notes; | ||
} | ||
|
||
@ApiRouteAuthenticated({ | ||
method: RequestMethod.GET, | ||
path: ':noteId', | ||
operation: { | ||
summary: 'Get a specific note', | ||
description: 'Returns the note with the given id', | ||
}, | ||
response: { | ||
status: 200, | ||
type: NoteDto, | ||
}, | ||
}) | ||
async get(@Param('noteId') noteId: string): Promise<NoteDto> { | ||
const note = await this.notesService.get(noteId); | ||
|
||
if (!note) { | ||
throw new NotFoundException(); | ||
} | ||
|
||
return note; | ||
} | ||
|
||
@ApiRouteAuthenticated({ | ||
method: RequestMethod.PATCH, | ||
path: ':noteId', | ||
operation: { | ||
summary: 'Update a specific note', | ||
description: 'Updates the note with the given id', | ||
}, | ||
response: { | ||
status: 200, | ||
type: NoteDto, | ||
}, | ||
}) | ||
async patch( | ||
@Body() body: UpdateNoteDto, | ||
@Param('noteId') noteId: string | ||
): Promise<NoteDto> { | ||
const note = await this.notesService.patch(noteId, body); | ||
|
||
if (!note) { | ||
throw new NotFoundException(); | ||
} | ||
|
||
return note; | ||
} | ||
|
||
@ApiRouteAuthenticated({ | ||
method: RequestMethod.DELETE, | ||
path: ':noteId', | ||
operation: { | ||
summary: 'Delete a specific note', | ||
description: 'Deletes the note with the given id', | ||
}, | ||
response: { | ||
status: 200, | ||
type: NoteDto, | ||
}, | ||
}) | ||
async delete(@Param('noteId') noteId: string): Promise<NoteDto> { | ||
const note = await this.notesService.delete(noteId); | ||
|
||
if (!note) { | ||
throw new NotFoundException(); | ||
} | ||
|
||
return note; | ||
} | ||
} |
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,13 @@ | ||
import { Module } from '@nestjs/common'; | ||
import { AuthModule } from '@nx-next-nest-prisma-ory-template/auth'; | ||
import { DatabaseModule } from '@nx-next-nest-prisma-ory-template/database'; | ||
import { NotesController } from './notes.controller'; | ||
import { NotesService } from './notes.service'; | ||
|
||
@Module({ | ||
imports: [AuthModule, DatabaseModule], | ||
controllers: [NotesController], | ||
providers: [NotesService], | ||
exports: [], | ||
}) | ||
export class NotesModule {} |
Oops, something went wrong.