Skip to content

Commit

Permalink
Create NoteList function
Browse files Browse the repository at this point in the history
  • Loading branch information
HyTekCoop committed Oct 11, 2023
1 parent cc90ee2 commit a7d7d84
Show file tree
Hide file tree
Showing 8 changed files with 158 additions and 3 deletions.
9 changes: 7 additions & 2 deletions src/domain/entities/note.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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
*/
Expand All @@ -28,9 +33,9 @@ export interface Note {
content: JSON;

/**
* Note creator
* Note creator id
*/
creatorId: number;
creatorId: NoteCreatorId;
}


Expand Down
3 changes: 3 additions & 0 deletions src/domain/entities/noteList.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
import type { Note } from '@domain/entities/note';

export type NoteList = Note[];
9 changes: 9 additions & 0 deletions src/domain/index.ts
Original file line number Diff line number Diff line change
@@ -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';
Expand All @@ -15,6 +16,11 @@ export interface DomainServices {
*/
noteService: NoteService,

/**
* Note List service instance
*/
noteListService: NoteListService,

/**
* Auth service instance
*/
Expand All @@ -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,
Expand All @@ -55,6 +63,7 @@ export function init(repositories: Repositories, appConfig: AppConfig): DomainSe

return {
noteService,
noteListService,
userService,
authService,
aiService,
Expand Down
29 changes: 29 additions & 0 deletions src/domain/service/noteList.ts
Original file line number Diff line number Diff line change
@@ -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<NoteList | null> } note
*/
public async getNoteListByCreatorId(id: NoteCreatorId): Promise<NoteList | null> {
return await this.repository.getNoteListByCreatorId(id);
}
}
8 changes: 8 additions & 0 deletions src/presentation/http/http-server.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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';
Expand Down Expand Up @@ -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,
Expand Down
73 changes: 73 additions & 0 deletions src/presentation/http/router/noteList.ts
Original file line number Diff line number Diff line change
@@ -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<NoteListRouterOptions> = (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;
12 changes: 12 additions & 0 deletions src/repository/note.repository.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down Expand Up @@ -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<NoteList | null> } note
*/
public async getNoteListByCreatorId(id: NoteCreatorId): Promise<NoteList | null> {
return await this.storage.getNoteListByCreatorId(id);
}

/**
* Gets note settings by id
*
Expand Down
18 changes: 17 additions & 1 deletion src/repository/storage/postgres/orm/sequelize/note.ts
Original file line number Diff line number Diff line change
@@ -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';
Expand Down Expand Up @@ -244,6 +245,21 @@ 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,
},
});
return noteList;
}

/**
* Get note settings
*
Expand Down

0 comments on commit a7d7d84

Please sign in to comment.