Skip to content

Commit

Permalink
Corrected code after conflicts
Browse files Browse the repository at this point in the history
  • Loading branch information
HyTekCoop committed Oct 21, 2023
1 parent 5e9779b commit c262aa3
Show file tree
Hide file tree
Showing 4 changed files with 43 additions and 53 deletions.
6 changes: 6 additions & 0 deletions src/presentation/http/http-api.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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');
Expand Down Expand Up @@ -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,
Expand Down
21 changes: 10 additions & 11 deletions src/presentation/http/router/noteList.ts
Original file line number Diff line number Diff line change
@@ -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';

Expand All @@ -13,12 +12,7 @@ interface NoteListRouterOptions {
*/
noteListService: NoteListService,

/**
* Middlewares
*/
middlewares: Middlewares,
};

}

/**
* Note list router plugin
Expand All @@ -34,10 +28,15 @@ const NoteListRouter: FastifyPluginCallback<NoteListRouterOptions> = (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
Expand Down
47 changes: 6 additions & 41 deletions src/repository/note.repository.ts
Original file line number Diff line number Diff line change
@@ -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';

Expand Down Expand Up @@ -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<Note | null> {
public async updateNoteContentByPublicId(publicId: NotePublicId, content: Note['content']): Promise<Note | null> {
return await this.storage.updateNoteContentByPublicId(publicId, content);
}

Expand Down Expand Up @@ -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<NotesSettings | null> } found note settings
* @param id - note creator id
* @returns { Promise<NoteList | null> } note
*/
public async getNoteSettingsByPublicId(id: NotePublicId): Promise<NotesSettings> {
/**
* @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<NotesSettings | null> } found note settings
*/
public async getNoteSettingsByNoteId(id: Note['id']): Promise<NotesSettings> {
return await this.storage.getNoteSettingsByNoteId(id);
}

/**
* Add note settings
*
* @param settings - note settings
*/
public async addNoteSettings(settings: NotesSettingsCreationAttributes): Promise<NotesSettings> {
return await this.storage.insertNoteSettings(settings);
}

/**
* Patch note settings
*
* @param data - note settings new values
* @param id - note settings id
* @returns { Promise<NotesSettings> } patched note settings
*/
public async patchNoteSettings(data: Partial<NotesSettings>, id: NotesSettings['id']): Promise<NotesSettings | null> {
return await this.storage.patchNoteSettings(data, id);
public async getNoteListByCreatorId(id: NoteCreatorId): Promise<NoteList | null> {
return await this.storage.getNoteListByCreatorId(id);
}
}
22 changes: 21 additions & 1 deletion src/repository/storage/postgres/orm/sequelize/note.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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';
Expand Down Expand Up @@ -187,6 +188,25 @@ export default class NoteSequelizeStorage {
return note;
}

/**
* Gets note list by creator id
*
* @param creatorId - note creator id
* @returns { Promise<NoteList | null> } note
*/
public async getNoteListByCreatorId(creatorId: NoteCreatorId): Promise<NoteList | null> {
const noteList = await this.model.findAll({
where: {
creatorId,
},
});

if (noteList.length === 0) {
return null;
};

return noteList;
}
/**
* Gets note by id
*
Expand Down

0 comments on commit c262aa3

Please sign in to comment.