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 13 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
6 changes: 4 additions & 2 deletions src/domain/entities/note.ts
Original file line number Diff line number Diff line change
@@ -1,3 +1,5 @@
import type User from '@domain/entities/user.js';

/**
* Note internal id. Used to query Note by internal API
*/
Expand Down Expand Up @@ -28,9 +30,9 @@ export interface Note {
content: JSON;

/**
* Note creator
* Note creator id
*/
creatorId: number;
creatorId: User['id'];

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

export type NoteList = {
neSpecc marked this conversation as resolved.
Show resolved Hide resolved
items: 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 NoteSettingsService from './service/noteSettings.js';
import type { Repositories } from '@repository/index.js';
import AuthService from '@domain/service/auth.js';
Expand All @@ -16,6 +17,11 @@ export interface DomainServices {
*/
noteService: NoteService,

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

/**
* Note settings service instance
*/
Expand Down Expand Up @@ -48,6 +54,8 @@ export function init(repositories: Repositories, appConfig: AppConfig): DomainSe
const noteService = new NoteService(repositories.noteRepository);
const noteSettingsService = new NoteSettingsService(repositories.noteSettingsRepository);

const noteListService = new NoteListService(repositories.noteRepository);

const authService = new AuthService(
appConfig.auth.accessSecret,
appConfig.auth.accessExpiresIn,
Expand All @@ -62,6 +70,7 @@ export function init(repositories: Repositories, appConfig: AppConfig): DomainSe

return {
noteService,
noteListService,
noteSettingsService,
userService,
authService,
Expand Down
28 changes: 28 additions & 0 deletions src/domain/service/noteList.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,28 @@
import type NoteRepository from '@repository/note.repository';
slaveeks marked this conversation as resolved.
Show resolved Hide resolved
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: number): Promise<NoteList | null> {
return await this.repository.getNoteListByCreatorId(id);
}
}
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 @@ -191,6 +192,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
58 changes: 58 additions & 0 deletions src/presentation/http/router/noteList.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,58 @@
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.
*/
interface NoteListRouterOptions {
/**
* Note list service instance
*/
noteListService: NoteListService,

}

/**
* 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('/', {
config: {
policy: [
'authRequired',
],
},
}, async (request, reply) => {
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',
};
neSpecc marked this conversation as resolved.
Show resolved Hide resolved

return reply.send(response);
}

return reply.send(noteList);
});

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
@@ -1,5 +1,6 @@
import type { Note, NoteCreationAttributes, NoteInternalId, NotePublicId } from '@domain/entities/note.js';
import type NoteStorage from '@repository/storage/note.storage.js';
import type { NoteList } from '@domain/entities/noteList.js';

/**
* Repository allows accessing data from business-logic (domain) level
Expand Down Expand Up @@ -34,6 +35,7 @@ export default class NoteRepository {
*
* @param id - note internal id
* @param content - new content
* @returns Note on success, null on failure
*/
public async updateNoteContentById(id: NoteInternalId, content: Note['content'] ): Promise<Note | null> {
return await this.storage.updateNoteContentById(id, content);
Expand Down Expand Up @@ -67,4 +69,14 @@ export default class NoteRepository {
public async getNoteByPublicId(publicId: NotePublicId): Promise<Note | null> {
return await this.storage.getNoteByPublicId(publicId);
}

/**
* Gets note list by creator id
*
* @param id - note creator id
* @returns { Promise<NoteList | null> } note
*/
public async getNoteListByCreatorId(id: number): Promise<NoteList | null> {
return await this.storage.getNoteListByCreatorId(id);
}
}
30 changes: 25 additions & 5 deletions src/repository/storage/postgres/orm/sequelize/note.ts
Original file line number Diff line number Diff line change
@@ -1,9 +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 } from '@domain/entities/note.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';

Expand Down Expand Up @@ -187,6 +186,27 @@ 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: number): Promise<NoteList | null> {
const noteList = await this.model.findAll({
where: {
creatorId,
},
});

if (noteList.length === 0) {
slaveeks marked this conversation as resolved.
Show resolved Hide resolved
return null;
}

return {
slaveeks marked this conversation as resolved.
Show resolved Hide resolved
items: noteList,
};
}
/**
* Gets note by id
*
Expand Down