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] 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 *