Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

feat: function GET NoteList #62

Merged
merged 18 commits into from
Oct 21, 2023
Merged
Show file tree
Hide file tree
Changes from 2 commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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;
slaveeks marked this conversation as resolved.
Show resolved Hide resolved

/**
* 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[];
neSpecc marked this conversation as resolved.
Show resolved Hide resolved
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';
slaveeks marked this conversation as resolved.
Show resolved Hide resolved
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
neSpecc marked this conversation as resolved.
Show resolved Hide resolved
*
* @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',
HyTekCoop marked this conversation as resolved.
Show resolved Hide resolved
noteListService: domainServices.noteListService,
middlewares: middlewares,
});

await this.server.register(OauthRouter, {
prefix: '/oauth',
userService: domainServices.userService,
Expand Down
64 changes: 64 additions & 0 deletions src/presentation/http/router/noteList.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,64 @@
import type { FastifyPluginCallback } from 'fastify';
import type NoteListService from '@domain/service/noteList';
HyTekCoop marked this conversation as resolved.
Show resolved Hide resolved
import type { Middlewares } from '@presentation/http/middlewares/index.js';
import type { ErrorResponse } from '@presentation/http/types/HttpResponse';
HyTekCoop marked this conversation as resolved.
Show resolved Hide resolved
import { StatusCodes } from 'http-status-codes';
import type { NoteList } from '@domain/entities/noteList';
HyTekCoop marked this conversation as resolved.
Show resolved Hide resolved

/**
* Interface for the noteList router.
*/
interface NoteListRouterOptions {
/**
* Note list service instance
*/
noteListService: NoteListService,

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


/**
* Note list 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 list by userId
*/
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 note list does not exist
*/
if (!noteList) {
const response: ErrorResponse = {
code: StatusCodes.NOT_FOUND,
message: 'Note not found Test Answer',
HyTekCoop marked this conversation as resolved.
Show resolved Hide resolved
};

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';
HyTekCoop marked this conversation as resolved.
Show resolved Hide resolved

/**
* 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';
HyTekCoop marked this conversation as resolved.
Show resolved Hide resolved
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> {
HyTekCoop marked this conversation as resolved.
Show resolved Hide resolved
const noteList = await this.model.findAll({
where: {
creatorId,
},
});
return noteList;
}

/**
* Get note settings
*
Expand Down