From a7d7d84a9f6b73b1983779bda00d6be7268eaf45 Mon Sep 17 00:00:00 2001 From: Alexander Chernyaev <265195@niuitmo.ru> Date: Wed, 11 Oct 2023 21:33:33 +0300 Subject: [PATCH 01/14] Create NoteList function --- src/domain/entities/note.ts | 9 ++- src/domain/entities/noteList.ts | 3 + src/domain/index.ts | 9 +++ src/domain/service/noteList.ts | 29 ++++++++ src/presentation/http/http-server.ts | 8 ++ src/presentation/http/router/noteList.ts | 73 +++++++++++++++++++ src/repository/note.repository.ts | 12 +++ .../storage/postgres/orm/sequelize/note.ts | 18 ++++- 8 files changed, 158 insertions(+), 3 deletions(-) create mode 100644 src/domain/entities/noteList.ts create mode 100644 src/domain/service/noteList.ts create mode 100644 src/presentation/http/router/noteList.ts diff --git a/src/domain/entities/note.ts b/src/domain/entities/note.ts index c00d3e35..d2994df3 100644 --- a/src/domain/entities/note.ts +++ b/src/domain/entities/note.ts @@ -8,6 +8,11 @@ export type NoteInternalId = number; */ export type NotePublicId = string; +/** + * Id from DataBase. Used to query Note by User (creator) id + */ +export type NoteCreatorId = number; + /** * Note entity */ @@ -28,9 +33,9 @@ export interface Note { content: JSON; /** - * Note creator + * Note creator id */ - creatorId: number; + creatorId: NoteCreatorId; } diff --git a/src/domain/entities/noteList.ts b/src/domain/entities/noteList.ts new file mode 100644 index 00000000..6bd26e50 --- /dev/null +++ b/src/domain/entities/noteList.ts @@ -0,0 +1,3 @@ +import type { Note } from '@domain/entities/note'; + +export type NoteList = Note[]; diff --git a/src/domain/index.ts b/src/domain/index.ts index 5ff4ce69..b4479c8f 100644 --- a/src/domain/index.ts +++ b/src/domain/index.ts @@ -1,4 +1,5 @@ import NoteService from '@domain/service/note.js'; +import NoteListService from '@domain/service/noteList.js'; import type { Repositories } from '@repository/index.js'; import AuthService from '@domain/service/auth.js'; import type { AppConfig } from '@infrastructure/config/index.js'; @@ -15,6 +16,11 @@ export interface DomainServices { */ noteService: NoteService, + /** + * Note List service instance + */ + noteListService: NoteListService, + /** * Auth service instance */ @@ -41,6 +47,8 @@ export interface DomainServices { export function init(repositories: Repositories, appConfig: AppConfig): DomainServices { const noteService = new NoteService(repositories.noteRepository); + const noteListService = new NoteListService(repositories.noteRepository); + const authService = new AuthService( appConfig.auth.accessSecret, appConfig.auth.accessExpiresIn, @@ -55,6 +63,7 @@ export function init(repositories: Repositories, appConfig: AppConfig): DomainSe return { noteService, + noteListService, userService, authService, aiService, diff --git a/src/domain/service/noteList.ts b/src/domain/service/noteList.ts new file mode 100644 index 00000000..6810bcee --- /dev/null +++ b/src/domain/service/noteList.ts @@ -0,0 +1,29 @@ +import type NoteRepository from '@repository/note.repository'; +import type { NoteCreatorId } from '@domain/entities/note'; +import type { NoteList } from '@domain/entities/noteList'; + +export default class NoteListService { + /** + * Note repository + */ + public repository: NoteRepository; + + /** + * Note service constructor + * + * @param repository - note repository + */ + constructor(repository: NoteRepository) { + this.repository = repository; + } + + /** + * Gets note list by creator id + * + * @param id - note creator id + * @returns { Promise } note + */ + public async getNoteListByCreatorId(id: NoteCreatorId): Promise { + return await this.repository.getNoteListByCreatorId(id); + } +} diff --git a/src/presentation/http/http-server.ts b/src/presentation/http/http-server.ts index f9bce3c7..ebb86fb7 100644 --- a/src/presentation/http/http-server.ts +++ b/src/presentation/http/http-server.ts @@ -3,6 +3,7 @@ import type { HttpApiConfig } from '@infrastructure/config/index.js'; import fastify from 'fastify'; import type API from '@presentation/api.interface.js'; import NoteRouter from '@presentation/http/router/note.js'; +import NoteListRouter from '@presentation/http/router/noteList.js'; import type { DomainServices } from '@domain/index.js'; import cors from '@fastify/cors'; import fastifyOauth2 from '@fastify/oauth2'; @@ -94,6 +95,13 @@ export default class HttpServer implements API { noteService: domainServices.noteService, middlewares: middlewares, }); + + await this.server.register(NoteListRouter, { + prefix: '/noteList', + noteListService: domainServices.noteListService, + middlewares: middlewares, + }); + await this.server.register(OauthRouter, { prefix: '/oauth', userService: domainServices.userService, diff --git a/src/presentation/http/router/noteList.ts b/src/presentation/http/router/noteList.ts new file mode 100644 index 00000000..a9f95d1f --- /dev/null +++ b/src/presentation/http/router/noteList.ts @@ -0,0 +1,73 @@ +import type { FastifyPluginCallback } from 'fastify'; +import type NoteListService from '@domain/service/noteList'; +import type { Middlewares } from '@presentation/http/middlewares/index.js'; +import type { NoteCreatorId } from '@domain/entities/note'; +import type { ErrorResponse } from '@presentation/http/types/HttpResponse'; +import { StatusCodes } from 'http-status-codes'; +import type { NoteList } from '@domain/entities/noteList'; + + +interface GetNoteListById { + /** + * Note id + */ + id: NoteCreatorId; +}; + +/** + * Interface for the noteList router. + */ +interface NoteListRouterOptions { + /** + * Note service instance + */ + noteListService: NoteListService, + + /** + * Middlewares + */ + middlewares: Middlewares, +}; + + +/** + * Note router plugin + * + * @param fastify - fastify instance + * @param opts - empty options + * @param done - callback + */ +const NoteListRouter: FastifyPluginCallback = (fastify, opts, done) => { + + const noteListService = opts.noteListService; + + /** + * Get note by id + */ + fastify.get<{ Params: GetNoteListById }>('/', { preHandler: [opts.middlewares.authRequired, opts.middlewares.withUser] }, async (request, reply) => { + const userId = request.ctx.auth.id; + + const noteList = await noteListService.getNoteListByCreatorId(userId); + + /** + * Check if noteList does not exist + */ + if (!noteList) { + const response: ErrorResponse = { + code: StatusCodes.NOT_FOUND, + message: 'Note not found Test Answer', + }; + + return reply.send(response); + } + + const response: {data:NoteList} = { + data: noteList, + }; + + return reply.send(response); + }); + + done(); +}; +export default NoteListRouter; diff --git a/src/repository/note.repository.ts b/src/repository/note.repository.ts index bfc04832..43d502b9 100644 --- a/src/repository/note.repository.ts +++ b/src/repository/note.repository.ts @@ -2,6 +2,8 @@ import type { Note, NoteCreationAttributes, NotePublicId } from '@domain/entitie import type NotesSettings from '@domain/entities/notesSettings.js'; import type NoteStorage from '@repository/storage/note.storage.js'; import type { NotesSettingsCreationAttributes } from '@domain/entities/notesSettings.js'; +import type { NoteCreatorId } from '@domain/entities/note.js'; +import type { NoteList } from '@domain/entities/noteList'; /** * Repository allows accessing data from business-logic (domain) level @@ -41,6 +43,16 @@ export default class NoteRepository { return await this.storage.getNoteById(id); } + /** + * Gets note list by creator id + * + * @param id - note creator id + * @returns { Promise } note + */ + public async getNoteListByCreatorId(id: NoteCreatorId): Promise { + return await this.storage.getNoteListByCreatorId(id); + } + /** * Gets note settings by id * diff --git a/src/repository/storage/postgres/orm/sequelize/note.ts b/src/repository/storage/postgres/orm/sequelize/note.ts index f52380a3..822f1996 100644 --- a/src/repository/storage/postgres/orm/sequelize/note.ts +++ b/src/repository/storage/postgres/orm/sequelize/note.ts @@ -1,7 +1,8 @@ import type { Sequelize, InferAttributes, InferCreationAttributes, CreationOptional } from 'sequelize'; import { Model, DataTypes } from 'sequelize'; import type Orm from '@repository/storage/postgres/orm/sequelize/index.js'; -import type { Note, NoteInternalId, NotePublicId } from '@domain/entities/note.js'; +import type { Note, NoteInternalId, NotePublicId, NoteCreatorId } from '@domain/entities/note.js'; +import type {NoteList} from '@domain/entities/noteList'; import type { NoteCreationAttributes } from '@domain/entities/note.js'; import { NotesSettingsModel } from '@repository/storage/postgres/orm/sequelize/notesSettings.js'; import type NotesSettings from '@domain/entities/notesSettings.js'; @@ -244,6 +245,21 @@ export default class NoteSequelizeStorage { return note; }; + /** + * Gets note list by creator id + * + * @param creatorId - note creator id + * @returns { Promise } note + */ + public async getNoteListByCreatorId(creatorId: NoteCreatorId): Promise { + const noteList = await this.model.findAll({ + where: { + creatorId, + }, + }); + return noteList; + } + /** * Get note settings * From 33d084511462abdfbabf71d07bb53248fff3b8f3 Mon Sep 17 00:00:00 2001 From: Alexander Chernyaev <265195@niuitmo.ru> Date: Thu, 19 Oct 2023 10:45:23 +0300 Subject: [PATCH 02/14] Update noteList.ts Delete nterface GetNoteListById in notelist router --- src/presentation/http/router/noteList.ts | 19 +++++-------------- 1 file changed, 5 insertions(+), 14 deletions(-) diff --git a/src/presentation/http/router/noteList.ts b/src/presentation/http/router/noteList.ts index a9f95d1f..2bc926cd 100644 --- a/src/presentation/http/router/noteList.ts +++ b/src/presentation/http/router/noteList.ts @@ -1,25 +1,16 @@ import type { FastifyPluginCallback } from 'fastify'; import type NoteListService from '@domain/service/noteList'; import type { Middlewares } from '@presentation/http/middlewares/index.js'; -import type { NoteCreatorId } from '@domain/entities/note'; import type { ErrorResponse } from '@presentation/http/types/HttpResponse'; import { StatusCodes } from 'http-status-codes'; import type { NoteList } from '@domain/entities/noteList'; - -interface GetNoteListById { - /** - * Note id - */ - id: NoteCreatorId; -}; - /** * Interface for the noteList router. */ interface NoteListRouterOptions { /** - * Note service instance + * Note list service instance */ noteListService: NoteListService, @@ -31,7 +22,7 @@ interface NoteListRouterOptions { /** - * Note router plugin + * Note list router plugin * * @param fastify - fastify instance * @param opts - empty options @@ -42,15 +33,15 @@ const NoteListRouter: FastifyPluginCallback = (fastify, o const noteListService = opts.noteListService; /** - * Get note by id + * Get note list by userId */ - fastify.get<{ Params: GetNoteListById }>('/', { preHandler: [opts.middlewares.authRequired, opts.middlewares.withUser] }, async (request, reply) => { + fastify.get('/', { preHandler: [opts.middlewares.authRequired, opts.middlewares.withUser] }, async (request, reply) => { const userId = request.ctx.auth.id; const noteList = await noteListService.getNoteListByCreatorId(userId); /** - * Check if noteList does not exist + * Check if note list does not exist */ if (!noteList) { const response: ErrorResponse = { From bf9f803d3067866626d77021f711d5d1e2f9c8c2 Mon Sep 17 00:00:00 2001 From: Alexander Chernyaev <265195@niuitmo.ru> Date: Thu, 19 Oct 2023 10:48:20 +0300 Subject: [PATCH 03/14] Update note.ts --- src/domain/entities/note.ts | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/src/domain/entities/note.ts b/src/domain/entities/note.ts index 30a3541c..bc64b2af 100644 --- a/src/domain/entities/note.ts +++ b/src/domain/entities/note.ts @@ -9,7 +9,7 @@ export type NoteInternalId = number; export type NotePublicId = string; /** - * Id from DataBase. Used to query Note by User (creator) id + * Id from DataBase. Used to query Note by Creator id */ export type NoteCreatorId = number; From f1c3eb533a225e1e2ebb82428e6c387f7be2eb82 Mon Sep 17 00:00:00 2001 From: Alexander Chernyaev <265195@niuitmo.ru> Date: Sat, 21 Oct 2023 10:42:39 +0300 Subject: [PATCH 04/14] Review changes --- src/domain/entities/noteList.ts | 5 ++++- src/presentation/http/http-server.ts | 2 +- src/presentation/http/router/noteList.ts | 11 +++-------- src/repository/note.repository.ts | 2 +- src/repository/storage/postgres/orm/sequelize/note.ts | 7 ++++++- 5 files changed, 15 insertions(+), 12 deletions(-) diff --git a/src/domain/entities/noteList.ts b/src/domain/entities/noteList.ts index 6bd26e50..7228379f 100644 --- a/src/domain/entities/noteList.ts +++ b/src/domain/entities/noteList.ts @@ -1,3 +1,6 @@ -import type { Note } from '@domain/entities/note'; +import type { Note } from '@domain/entities/note.js'; export type NoteList = Note[]; +// export type NoteList = { +// items: Note[]; +// }; diff --git a/src/presentation/http/http-server.ts b/src/presentation/http/http-server.ts index ebb86fb7..2d09fe99 100644 --- a/src/presentation/http/http-server.ts +++ b/src/presentation/http/http-server.ts @@ -97,7 +97,7 @@ export default class HttpServer implements API { }); await this.server.register(NoteListRouter, { - prefix: '/noteList', + prefix: '/notes', noteListService: domainServices.noteListService, middlewares: middlewares, }); diff --git a/src/presentation/http/router/noteList.ts b/src/presentation/http/router/noteList.ts index 2bc926cd..4f7035fd 100644 --- a/src/presentation/http/router/noteList.ts +++ b/src/presentation/http/router/noteList.ts @@ -1,9 +1,8 @@ import type { FastifyPluginCallback } from 'fastify'; import type NoteListService from '@domain/service/noteList'; import type { Middlewares } from '@presentation/http/middlewares/index.js'; -import type { ErrorResponse } from '@presentation/http/types/HttpResponse'; +import type { ErrorResponse } from '@presentation/http/types/HttpResponse.js'; import { StatusCodes } from 'http-status-codes'; -import type { NoteList } from '@domain/entities/noteList'; /** * Interface for the noteList router. @@ -46,17 +45,13 @@ const NoteListRouter: FastifyPluginCallback = (fastify, o if (!noteList) { const response: ErrorResponse = { code: StatusCodes.NOT_FOUND, - message: 'Note not found Test Answer', + message: 'Note list not found', }; return reply.send(response); } - const response: {data:NoteList} = { - data: noteList, - }; - - return reply.send(response); + return reply.send(noteList); }); done(); diff --git a/src/repository/note.repository.ts b/src/repository/note.repository.ts index 20cc1b48..fccca00e 100644 --- a/src/repository/note.repository.ts +++ b/src/repository/note.repository.ts @@ -3,7 +3,7 @@ import type NotesSettings from '@domain/entities/notesSettings.js'; import type NoteStorage from '@repository/storage/note.storage.js'; import type { NotesSettingsCreationAttributes } from '@domain/entities/notesSettings.js'; import type { NoteCreatorId } from '@domain/entities/note.js'; -import type { NoteList } from '@domain/entities/noteList'; +import type { NoteList } from '@domain/entities/noteList.js'; /** * Repository allows accessing data from business-logic (domain) level diff --git a/src/repository/storage/postgres/orm/sequelize/note.ts b/src/repository/storage/postgres/orm/sequelize/note.ts index 9ad2ea00..8115da7b 100644 --- a/src/repository/storage/postgres/orm/sequelize/note.ts +++ b/src/repository/storage/postgres/orm/sequelize/note.ts @@ -2,7 +2,7 @@ import type { Sequelize, InferAttributes, InferCreationAttributes, CreationOptio import { Model, DataTypes } from 'sequelize'; import type Orm from '@repository/storage/postgres/orm/sequelize/index.js'; import type { Note, NoteInternalId, NotePublicId, NoteCreatorId } from '@domain/entities/note.js'; -import type {NoteList} from '@domain/entities/noteList'; +import type { NoteList } from '@domain/entities/noteList.js'; import type { NoteCreationAttributes } from '@domain/entities/note.js'; import { NotesSettingsModel } from '@repository/storage/postgres/orm/sequelize/notesSettings.js'; import type NotesSettings from '@domain/entities/notesSettings.js'; @@ -293,6 +293,11 @@ export default class NoteSequelizeStorage { creatorId, }, }); + + // if (noteList.length === 0) { + // return null; + // }; + return noteList; } From c55485450dd8706fc351edd74a41502e73ac7eac Mon Sep 17 00:00:00 2001 From: Alexander Chernyaev <265195@niuitmo.ru> Date: Sat, 21 Oct 2023 12:11:36 +0300 Subject: [PATCH 05/14] Update note.ts --- src/repository/storage/postgres/orm/sequelize/note.ts | 6 +++--- 1 file changed, 3 insertions(+), 3 deletions(-) diff --git a/src/repository/storage/postgres/orm/sequelize/note.ts b/src/repository/storage/postgres/orm/sequelize/note.ts index 8115da7b..a6a525a6 100644 --- a/src/repository/storage/postgres/orm/sequelize/note.ts +++ b/src/repository/storage/postgres/orm/sequelize/note.ts @@ -294,9 +294,9 @@ export default class NoteSequelizeStorage { }, }); - // if (noteList.length === 0) { - // return null; - // }; + if (noteList.length === 0) { + return null; + }; return noteList; } From 6e5fa6d4fd6e5813a3eb5542e6bd3fa43ccae59d Mon Sep 17 00:00:00 2001 From: Alexander Chernyaev <265195@niuitmo.ru> Date: Sat, 21 Oct 2023 12:40:09 +0300 Subject: [PATCH 06/14] Update noteList.ts --- src/presentation/http/router/noteList.ts | 6 +++--- 1 file changed, 3 insertions(+), 3 deletions(-) diff --git a/src/presentation/http/router/noteList.ts b/src/presentation/http/router/noteList.ts index 4f7035fd..ad13c31a 100644 --- a/src/presentation/http/router/noteList.ts +++ b/src/presentation/http/router/noteList.ts @@ -1,5 +1,5 @@ import type { FastifyPluginCallback } from 'fastify'; -import type NoteListService from '@domain/service/noteList'; +import type NoteListService from '@domain/service/noteList.js'; import type { Middlewares } from '@presentation/http/middlewares/index.js'; import type { ErrorResponse } from '@presentation/http/types/HttpResponse.js'; import { StatusCodes } from 'http-status-codes'; @@ -35,9 +35,9 @@ const NoteListRouter: FastifyPluginCallback = (fastify, o * Get note list by userId */ fastify.get('/', { preHandler: [opts.middlewares.authRequired, opts.middlewares.withUser] }, async (request, reply) => { - const userId = request.ctx.auth.id; + const creatorId = request.ctx.auth.id; - const noteList = await noteListService.getNoteListByCreatorId(userId); + const noteList = await noteListService.getNoteListByCreatorId(creatorId); /** * Check if note list does not exist From c262aa3f36b8caf4ab1fc4450e45374cefc5827a Mon Sep 17 00:00:00 2001 From: Alexander Chernyaev <265195@niuitmo.ru> Date: Sat, 21 Oct 2023 13:37:43 +0300 Subject: [PATCH 07/14] Corrected code after conflicts --- src/presentation/http/http-api.ts | 6 +++ src/presentation/http/router/noteList.ts | 21 ++++----- src/repository/note.repository.ts | 47 +++---------------- .../storage/postgres/orm/sequelize/note.ts | 22 ++++++++- 4 files changed, 43 insertions(+), 53 deletions(-) diff --git a/src/presentation/http/http-api.ts b/src/presentation/http/http-api.ts index 8485f53c..781dbb41 100644 --- a/src/presentation/http/http-api.ts +++ b/src/presentation/http/http-api.ts @@ -22,6 +22,7 @@ import { NoteSchema } from './schema/Note.js'; import Policies from './policies/index.js'; import type { RequestParams, Response } from '@presentation/api.interface.js'; import NoteSettingsRouter from './router/noteSettings.js'; +import NoteListRouter from '@presentation/http/router/noteList.js'; const appServerLogger = getLogger('appServer'); @@ -169,6 +170,11 @@ export default class HttpApi implements Api { noteSettingsService: domainServices.noteSettingsService, }); + await this.server?.register(NoteListRouter, { + prefix: '/notes', + noteListService: domainServices.noteListService, + }); + await this.server?.register(NoteSettingsRouter, { prefix: '/note-settings', noteSettingsService: domainServices.noteSettingsService, diff --git a/src/presentation/http/router/noteList.ts b/src/presentation/http/router/noteList.ts index ad13c31a..9f44ddab 100644 --- a/src/presentation/http/router/noteList.ts +++ b/src/presentation/http/router/noteList.ts @@ -1,6 +1,5 @@ import type { FastifyPluginCallback } from 'fastify'; import type NoteListService from '@domain/service/noteList.js'; -import type { Middlewares } from '@presentation/http/middlewares/index.js'; import type { ErrorResponse } from '@presentation/http/types/HttpResponse.js'; import { StatusCodes } from 'http-status-codes'; @@ -13,12 +12,7 @@ interface NoteListRouterOptions { */ noteListService: NoteListService, - /** - * Middlewares - */ - middlewares: Middlewares, -}; - +} /** * Note list router plugin @@ -34,10 +28,15 @@ const NoteListRouter: FastifyPluginCallback = (fastify, o /** * Get note list by userId */ - fastify.get('/', { preHandler: [opts.middlewares.authRequired, opts.middlewares.withUser] }, async (request, reply) => { - const creatorId = request.ctx.auth.id; - - const noteList = await noteListService.getNoteListByCreatorId(creatorId); + fastify.get('/', { + config: { + policy: [ + 'authRequired', + ], + }, + }, async (request, reply) => { + const { userId } = request; + const noteList = await noteListService.getNoteListByCreatorId(userId as number); /** * Check if note list does not exist diff --git a/src/repository/note.repository.ts b/src/repository/note.repository.ts index 89467373..5011f24f 100644 --- a/src/repository/note.repository.ts +++ b/src/repository/note.repository.ts @@ -1,7 +1,5 @@ import type { Note, NoteCreationAttributes, NotePublicId } from '@domain/entities/note.js'; -import type NotesSettings from '@domain/entities/notesSettings.js'; import type NoteStorage from '@repository/storage/note.storage.js'; -import type { NotesSettingsCreationAttributes } from '@domain/entities/notesSettings.js'; import type { NoteCreatorId } from '@domain/entities/note.js'; import type { NoteList } from '@domain/entities/noteList.js'; @@ -40,7 +38,7 @@ export default class NoteRepository { * @param content - new content * @returns Note on success, null on failure */ - public async updateNoteContentByPublicId(publicId: NotePublicId, content: Note['content'] ): Promise { + public async updateNoteContentByPublicId(publicId: NotePublicId, content: Note['content']): Promise { return await this.storage.updateNoteContentByPublicId(publicId, content); } @@ -75,45 +73,12 @@ export default class NoteRepository { } /** - * Get note settings by note id + * Gets note list by creator id * - * @param id - note public id - * @returns { Promise } found note settings + * @param id - note creator id + * @returns { Promise } note */ - public async getNoteSettingsByPublicId(id: NotePublicId): Promise { - /** - * @todo get internal id by public id and resolve note settings by the internal id - */ - return await this.storage.getNoteSettingsByPublicId(id); - } - - /** - * Get note settings by note id - * - * @param id - note id - * @returns { Promise } found note settings - */ - public async getNoteSettingsByNoteId(id: Note['id']): Promise { - return await this.storage.getNoteSettingsByNoteId(id); - } - - /** - * Add note settings - * - * @param settings - note settings - */ - public async addNoteSettings(settings: NotesSettingsCreationAttributes): Promise { - return await this.storage.insertNoteSettings(settings); - } - - /** - * Patch note settings - * - * @param data - note settings new values - * @param id - note settings id - * @returns { Promise } patched note settings - */ - public async patchNoteSettings(data: Partial, id: NotesSettings['id']): Promise { - return await this.storage.patchNoteSettings(data, id); + public async getNoteListByCreatorId(id: NoteCreatorId): Promise { + return await this.storage.getNoteListByCreatorId(id); } } diff --git a/src/repository/storage/postgres/orm/sequelize/note.ts b/src/repository/storage/postgres/orm/sequelize/note.ts index 02f79f3f..2a95429b 100644 --- a/src/repository/storage/postgres/orm/sequelize/note.ts +++ b/src/repository/storage/postgres/orm/sequelize/note.ts @@ -2,7 +2,8 @@ import type { Sequelize, InferAttributes, InferCreationAttributes, CreationOptio import { Model, DataTypes } from 'sequelize'; import type { ModelStatic } from 'sequelize'; import type Orm from '@repository/storage/postgres/orm/sequelize/index.js'; -import type { Note, NoteInternalId, NotePublicId } from '@domain/entities/note.js'; +import type { Note, NoteInternalId, NotePublicId, NoteCreatorId } from '@domain/entities/note.js'; +import type { NoteList} from '@domain/entities/noteList.js'; import type { NoteCreationAttributes } from '@domain/entities/note.js'; import type { NoteSettingsModel } from '@repository/storage/postgres/orm/sequelize/noteSettings.js'; import { UserModel } from '@repository/storage/postgres/orm/sequelize/user.js'; @@ -187,6 +188,25 @@ export default class NoteSequelizeStorage { return note; } + /** + * Gets note list by creator id + * + * @param creatorId - note creator id + * @returns { Promise } note + */ + public async getNoteListByCreatorId(creatorId: NoteCreatorId): Promise { + const noteList = await this.model.findAll({ + where: { + creatorId, + }, + }); + + if (noteList.length === 0) { + return null; + }; + + return noteList; + } /** * Gets note by id * From e11aead398ef8866dd29de78238e81b30eb67c40 Mon Sep 17 00:00:00 2001 From: Alexander Chernyaev <265195@niuitmo.ru> Date: Sat, 21 Oct 2023 15:36:01 +0300 Subject: [PATCH 08/14] Review changes --- src/domain/entities/note.ts | 9 ++---- src/domain/entities/noteList.ts | 7 ++--- src/domain/service/noteList.ts | 3 +- src/repository/note.repository.ts | 3 +- .../storage/postgres/orm/sequelize/note.ts | 31 +++++++++++++------ 5 files changed, 30 insertions(+), 23 deletions(-) diff --git a/src/domain/entities/note.ts b/src/domain/entities/note.ts index bc64b2af..91b887ce 100644 --- a/src/domain/entities/note.ts +++ b/src/domain/entities/note.ts @@ -1,3 +1,5 @@ +import type User from '@domain/entities/user.js'; + /** * Note internal id. Used to query Note by internal API */ @@ -8,11 +10,6 @@ export type NoteInternalId = number; */ export type NotePublicId = string; -/** - * Id from DataBase. Used to query Note by Creator id - */ -export type NoteCreatorId = number; - /** * Note entity */ @@ -35,7 +32,7 @@ export interface Note { /** * Note creator id */ - creatorId: NoteCreatorId; + creatorId: User['id']; /** * When note was created diff --git a/src/domain/entities/noteList.ts b/src/domain/entities/noteList.ts index 7228379f..17dacf6a 100644 --- a/src/domain/entities/noteList.ts +++ b/src/domain/entities/noteList.ts @@ -1,6 +1,5 @@ import type { Note } from '@domain/entities/note.js'; -export type NoteList = Note[]; -// export type NoteList = { -// items: Note[]; -// }; +export type NoteList = { + items: Note[]; +}; diff --git a/src/domain/service/noteList.ts b/src/domain/service/noteList.ts index 6810bcee..5ab929d1 100644 --- a/src/domain/service/noteList.ts +++ b/src/domain/service/noteList.ts @@ -1,5 +1,4 @@ import type NoteRepository from '@repository/note.repository'; -import type { NoteCreatorId } from '@domain/entities/note'; import type { NoteList } from '@domain/entities/noteList'; export default class NoteListService { @@ -23,7 +22,7 @@ export default class NoteListService { * @param id - note creator id * @returns { Promise } note */ - public async getNoteListByCreatorId(id: NoteCreatorId): Promise { + public async getNoteListByCreatorId(id: number): Promise { return await this.repository.getNoteListByCreatorId(id); } } diff --git a/src/repository/note.repository.ts b/src/repository/note.repository.ts index 5011f24f..4d6b04a7 100644 --- a/src/repository/note.repository.ts +++ b/src/repository/note.repository.ts @@ -1,6 +1,5 @@ import type { Note, NoteCreationAttributes, NotePublicId } from '@domain/entities/note.js'; import type NoteStorage from '@repository/storage/note.storage.js'; -import type { NoteCreatorId } from '@domain/entities/note.js'; import type { NoteList } from '@domain/entities/noteList.js'; /** @@ -78,7 +77,7 @@ export default class NoteRepository { * @param id - note creator id * @returns { Promise } note */ - public async getNoteListByCreatorId(id: NoteCreatorId): Promise { + public async getNoteListByCreatorId(id: number): Promise { return await this.storage.getNoteListByCreatorId(id); } } diff --git a/src/repository/storage/postgres/orm/sequelize/note.ts b/src/repository/storage/postgres/orm/sequelize/note.ts index 2a95429b..e13f018e 100644 --- a/src/repository/storage/postgres/orm/sequelize/note.ts +++ b/src/repository/storage/postgres/orm/sequelize/note.ts @@ -1,10 +1,8 @@ -import type { Sequelize, InferAttributes, InferCreationAttributes, CreationOptional } from 'sequelize'; -import { Model, DataTypes } from 'sequelize'; -import type { ModelStatic } from 'sequelize'; +import type { CreationOptional, InferAttributes, InferCreationAttributes, ModelStatic, Sequelize } from 'sequelize'; +import { DataTypes, Model } from 'sequelize'; import type Orm from '@repository/storage/postgres/orm/sequelize/index.js'; -import type { Note, NoteInternalId, NotePublicId, NoteCreatorId } from '@domain/entities/note.js'; -import type { NoteList} from '@domain/entities/noteList.js'; -import type { NoteCreationAttributes } from '@domain/entities/note.js'; +import type { Note, NoteCreationAttributes, NoteInternalId, NotePublicId } from '@domain/entities/note.js'; +import type { NoteList } from '@domain/entities/noteList.js'; import type { NoteSettingsModel } from '@repository/storage/postgres/orm/sequelize/noteSettings.js'; import { UserModel } from '@repository/storage/postgres/orm/sequelize/user.js'; @@ -194,8 +192,8 @@ export default class NoteSequelizeStorage { * @param creatorId - note creator id * @returns { Promise } note */ - public async getNoteListByCreatorId(creatorId: NoteCreatorId): Promise { - const noteList = await this.model.findAll({ + public async getNoteListByCreatorId(creatorId: number): Promise { + const noteList = await this.model.findAll({ where: { creatorId, }, @@ -204,8 +202,23 @@ export default class NoteSequelizeStorage { if (noteList.length === 0) { return null; }; + /** + * Converting a NoteModel array to a NoteList object + */ + const noteListItems: Note[] = noteList.map((noteModel) => { + return { + id: noteModel.id, + publicId: noteModel.publicId, + content: noteModel.content, + creatorId: noteModel.creatorId, + createdAt: noteModel.createdAt, + updatedAt: noteModel.updatedAt, + }; + }); - return noteList; + return { + items: noteListItems, + }; } /** * Gets note by id From b914666f91216141486ffcce333190b35d53db19 Mon Sep 17 00:00:00 2001 From: Alexander Chernyaev <265195@niuitmo.ru> Date: Sat, 21 Oct 2023 16:06:39 +0300 Subject: [PATCH 09/14] Update note.ts --- .../storage/postgres/orm/sequelize/note.ts | 17 ++--------------- 1 file changed, 2 insertions(+), 15 deletions(-) diff --git a/src/repository/storage/postgres/orm/sequelize/note.ts b/src/repository/storage/postgres/orm/sequelize/note.ts index e13f018e..ce4f8da0 100644 --- a/src/repository/storage/postgres/orm/sequelize/note.ts +++ b/src/repository/storage/postgres/orm/sequelize/note.ts @@ -201,23 +201,10 @@ export default class NoteSequelizeStorage { if (noteList.length === 0) { return null; - }; - /** - * Converting a NoteModel array to a NoteList object - */ - const noteListItems: Note[] = noteList.map((noteModel) => { - return { - id: noteModel.id, - publicId: noteModel.publicId, - content: noteModel.content, - creatorId: noteModel.creatorId, - createdAt: noteModel.createdAt, - updatedAt: noteModel.updatedAt, - }; - }); + } return { - items: noteListItems, + items: noteList, }; } /** From 59ed895b0254db140516feb5d5937d2b2e33e132 Mon Sep 17 00:00:00 2001 From: Alexander Chernyaev <265195@niuitmo.ru> Date: Sat, 21 Oct 2023 17:15:04 +0300 Subject: [PATCH 10/14] Review changes --- src/domain/entities/noteList.ts | 3 +++ src/domain/service/noteList.ts | 6 +++--- src/presentation/http/router/noteList.ts | 12 ------------ src/repository/note.repository.ts | 4 ++-- .../storage/postgres/orm/sequelize/note.ts | 8 ++------ 5 files changed, 10 insertions(+), 23 deletions(-) diff --git a/src/domain/entities/noteList.ts b/src/domain/entities/noteList.ts index 17dacf6a..61d3da76 100644 --- a/src/domain/entities/noteList.ts +++ b/src/domain/entities/noteList.ts @@ -1,5 +1,8 @@ import type { Note } from '@domain/entities/note.js'; +/** + * Note list entity + */ export type NoteList = { items: Note[]; }; diff --git a/src/domain/service/noteList.ts b/src/domain/service/noteList.ts index 5ab929d1..d806abd0 100644 --- a/src/domain/service/noteList.ts +++ b/src/domain/service/noteList.ts @@ -17,12 +17,12 @@ export default class NoteListService { } /** - * Gets note list by creator id + * Returns note list by creator id * * @param id - note creator id - * @returns { Promise } note + * @returns { Promise } note */ - public async getNoteListByCreatorId(id: number): Promise { + public async getNoteListByCreatorId(id: number): Promise { return await this.repository.getNoteListByCreatorId(id); } } diff --git a/src/presentation/http/router/noteList.ts b/src/presentation/http/router/noteList.ts index 9f44ddab..6752ba97 100644 --- a/src/presentation/http/router/noteList.ts +++ b/src/presentation/http/router/noteList.ts @@ -38,18 +38,6 @@ const NoteListRouter: FastifyPluginCallback = (fastify, o const { userId } = request; const noteList = await noteListService.getNoteListByCreatorId(userId as number); - /** - * Check if note list does not exist - */ - if (!noteList) { - const response: ErrorResponse = { - code: StatusCodes.NOT_FOUND, - message: 'Note list not found', - }; - - return reply.send(response); - } - return reply.send(noteList); }); diff --git a/src/repository/note.repository.ts b/src/repository/note.repository.ts index 81965061..3e407413 100644 --- a/src/repository/note.repository.ts +++ b/src/repository/note.repository.ts @@ -74,9 +74,9 @@ export default class NoteRepository { * Gets note list by creator id * * @param id - note creator id - * @returns { Promise } note + * @returns { Promise } note */ - public async getNoteListByCreatorId(id: number): Promise { + public async getNoteListByCreatorId(id: number): Promise { return await this.storage.getNoteListByCreatorId(id); } } diff --git a/src/repository/storage/postgres/orm/sequelize/note.ts b/src/repository/storage/postgres/orm/sequelize/note.ts index 0bedc055..98ca577b 100644 --- a/src/repository/storage/postgres/orm/sequelize/note.ts +++ b/src/repository/storage/postgres/orm/sequelize/note.ts @@ -190,19 +190,15 @@ export default class NoteSequelizeStorage { * Gets note list by creator id * * @param creatorId - note creator id - * @returns { Promise } note + * @returns { Promise } note */ - public async getNoteListByCreatorId(creatorId: number): Promise { + public async getNoteListByCreatorId(creatorId: number): Promise { const noteList = await this.model.findAll({ where: { creatorId, }, }); - if (noteList.length === 0) { - return null; - } - return { items: noteList, }; From b454e83f4fc3e74c709e5391ba8cdf49ad503713 Mon Sep 17 00:00:00 2001 From: Alexander Chernyaev <265195@niuitmo.ru> Date: Sat, 21 Oct 2023 17:18:54 +0300 Subject: [PATCH 11/14] Update noteList.ts --- src/presentation/http/router/noteList.ts | 2 -- 1 file changed, 2 deletions(-) diff --git a/src/presentation/http/router/noteList.ts b/src/presentation/http/router/noteList.ts index 6752ba97..76595d57 100644 --- a/src/presentation/http/router/noteList.ts +++ b/src/presentation/http/router/noteList.ts @@ -1,7 +1,5 @@ import type { FastifyPluginCallback } from 'fastify'; import type NoteListService from '@domain/service/noteList.js'; -import type { ErrorResponse } from '@presentation/http/types/HttpResponse.js'; -import { StatusCodes } from 'http-status-codes'; /** * Interface for the noteList router. From 704f18da679059d8ecec5c18e9cf89d720ffa535 Mon Sep 17 00:00:00 2001 From: Alexander Chernyaev <265195@niuitmo.ru> Date: Sat, 21 Oct 2023 17:20:32 +0300 Subject: [PATCH 12/14] Update noteList.ts --- src/domain/service/noteList.ts | 3 +++ 1 file changed, 3 insertions(+) diff --git a/src/domain/service/noteList.ts b/src/domain/service/noteList.ts index d806abd0..989403ac 100644 --- a/src/domain/service/noteList.ts +++ b/src/domain/service/noteList.ts @@ -1,6 +1,9 @@ import type NoteRepository from '@repository/note.repository'; import type { NoteList } from '@domain/entities/noteList'; +/** + * Note list service + */ export default class NoteListService { /** * Note repository From c8b69efe78c38e8c4106ff04ba2547700d38cae6 Mon Sep 17 00:00:00 2001 From: Alexander Chernyaev <265195@niuitmo.ru> Date: Sat, 21 Oct 2023 19:06:36 +0300 Subject: [PATCH 13/14] =?UTF-8?q?=D0=A1hanging=20type=20at=20service=20lev?= =?UTF-8?q?el?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- src/domain/service/noteList.ts | 4 +++- src/repository/note.repository.ts | 2 +- src/repository/storage/postgres/orm/sequelize/note.ts | 6 ++---- 3 files changed, 6 insertions(+), 6 deletions(-) diff --git a/src/domain/service/noteList.ts b/src/domain/service/noteList.ts index 989403ac..4503610d 100644 --- a/src/domain/service/noteList.ts +++ b/src/domain/service/noteList.ts @@ -26,6 +26,8 @@ export default class NoteListService { * @returns { Promise } note */ public async getNoteListByCreatorId(id: number): Promise { - return await this.repository.getNoteListByCreatorId(id); + return { + items: await this.repository.getNoteListByCreatorId(id), + }; } } diff --git a/src/repository/note.repository.ts b/src/repository/note.repository.ts index 3e407413..f47f1bab 100644 --- a/src/repository/note.repository.ts +++ b/src/repository/note.repository.ts @@ -76,7 +76,7 @@ export default class NoteRepository { * @param id - note creator id * @returns { Promise } note */ - public async getNoteListByCreatorId(id: number): Promise { + public async getNoteListByCreatorId(id: number): Promise { return await this.storage.getNoteListByCreatorId(id); } } diff --git a/src/repository/storage/postgres/orm/sequelize/note.ts b/src/repository/storage/postgres/orm/sequelize/note.ts index 98ca577b..8c6b16bb 100644 --- a/src/repository/storage/postgres/orm/sequelize/note.ts +++ b/src/repository/storage/postgres/orm/sequelize/note.ts @@ -192,16 +192,14 @@ export default class NoteSequelizeStorage { * @param creatorId - note creator id * @returns { Promise } note */ - public async getNoteListByCreatorId(creatorId: number): Promise { + public async getNoteListByCreatorId(creatorId: number): Promise { const noteList = await this.model.findAll({ where: { creatorId, }, }); - return { - items: noteList, - }; + return noteList; } /** * Gets note by id From bc9782972baaa4f3ce84f011ec5b464bede4c18c Mon Sep 17 00:00:00 2001 From: Alexander Chernyaev <265195@niuitmo.ru> Date: Sat, 21 Oct 2023 19:19:53 +0300 Subject: [PATCH 14/14] Improved description of note list entities --- src/domain/entities/noteList.ts | 3 ++- 1 file changed, 2 insertions(+), 1 deletion(-) diff --git a/src/domain/entities/noteList.ts b/src/domain/entities/noteList.ts index 61d3da76..25753407 100644 --- a/src/domain/entities/noteList.ts +++ b/src/domain/entities/noteList.ts @@ -1,7 +1,8 @@ import type { Note } from '@domain/entities/note.js'; /** - * Note list entity + * Note list entity. + * An object with the "items" property containing a list of all existing notes created by the user */ export type NoteList = { items: Note[];