Skip to content

Commit

Permalink
refactor: rename notesSettings to noteSettings
Browse files Browse the repository at this point in the history
  • Loading branch information
GeekaN2 committed Oct 20, 2023
1 parent d69ee60 commit fee85b9
Show file tree
Hide file tree
Showing 9 changed files with 57 additions and 59 deletions.
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
/**
* Notes settings entity
*/
export default interface NotesSettings {
export default interface NoteSettings {
/**
* Just unique property identifier
*/
Expand All @@ -26,8 +26,8 @@ export default interface NotesSettings {
/**
* Notes settings creation attributes, omitting id, because it's generated by database
*/
type NotesSettingsCreationAttributes = Omit<NotesSettings, 'id'>;
type NoteSettingsCreationAttributes = Omit<NoteSettings, 'id'>;

export type {
NotesSettingsCreationAttributes
NoteSettingsCreationAttributes
};
18 changes: 9 additions & 9 deletions src/domain/service/noteSettings.ts
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
import type { Note, NotePublicId } from '@domain/entities/note.js';
import type NotesSettings from '@domain/entities/notesSettings.js';
import type NoteSettings from '@domain/entities/noteSettings.js';
import type NoteSettingsRepository from '@repository/noteSettings.repository.js';

/**
Expand All @@ -24,9 +24,9 @@ export default class NoteSettingsService {
* Gets note settings by public id
*
* @param id - note public id
* @returns { Promise<NotesSettings | null> } note settings
* @returns { Promise<NoteSettings | null> } note settings
*/
public async getNoteSettingsByPublicId(id: NotePublicId): Promise<NotesSettings> {
public async getNoteSettingsByPublicId(id: NotePublicId): Promise<NoteSettings> {
/**
* @todo get internal id by public id and resolve note settings by the internal id
*/
Expand All @@ -37,9 +37,9 @@ export default class NoteSettingsService {
* Gets note settings by note id
*
* @param id - note id
* @returns { Promise<NotesSettings | null> } note
* @returns { Promise<NoteSettings | null> } note
*/
public async getNoteSettingsByNoteId(id: Note['id']): Promise<NotesSettings | null> {
public async getNoteSettingsByNoteId(id: Note['id']): Promise<NoteSettings | null> {
return await this.repository.getNoteSettingsByNoteId(id);
}

Expand All @@ -48,9 +48,9 @@ export default class NoteSettingsService {
*
* @param noteId - note id
* @param enabled - is note enabled
* @returns { Promise<NotesSettings> } note settings
* @returns { Promise<NoteSettings> } note settings
*/
public async addNoteSettings(noteId: Note['id'], enabled: boolean = true): Promise<NotesSettings> {
public async addNoteSettings(noteId: Note['id'], enabled: boolean = true): Promise<NoteSettings> {
return await this.repository.addNoteSettings({
noteId: noteId,
enabled: enabled,
Expand All @@ -62,9 +62,9 @@ export default class NoteSettingsService {
*
* @param data - note settings data with new values
* @param noteId - note public id
* @returns { Promise<NotesSettings> } updated note settings
* @returns { Promise<NoteSettings> } updated note settings
*/
public async patchNoteSettingsByPublicId(data: Partial<NotesSettings>, noteId: NotePublicId): Promise<NotesSettings | null> {
public async patchNoteSettingsByPublicId(data: Partial<NoteSettings>, noteId: NotePublicId): Promise<NoteSettings | null> {
const noteSettings = await this.repository.getNoteSettingsByPublicId(noteId);

return await this.repository.patchNoteSettingsByPublicId(data, noteSettings.id);
Expand Down
1 change: 0 additions & 1 deletion src/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -24,7 +24,6 @@ const start = async (): Promise<void> => {

logger.info('Application launched successfully');
} catch (err) {
console.log('err', err);
logger.fatal('Failed to start application ' + err);
process.exit(1);
}
Expand Down
8 changes: 4 additions & 4 deletions src/presentation/http/router/noteSettings.ts
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
import type { FastifyPluginCallback } from 'fastify';
import type NoteSettingsService from '@domain/service/noteSettings.js';
import type { NotePublicId } from '@domain/entities/note.js';
import type NotesSettings from '@domain/entities/notesSettings.js';
import type NoteSettings from '@domain/entities/noteSettings.js';
import type { Middlewares } from '@presentation/http/middlewares/index.js';
import notEmpty from '@infrastructure/utils/notEmpty.js';

Expand Down Expand Up @@ -50,7 +50,7 @@ const NoteSettingsRouter: FastifyPluginCallback<NoteSettingsRouterOptions> = (fa
*/
fastify.get<{
Params: GetNoteSettingsByNodeIdOptions,
Reply: NotesSettings
Reply: NoteSettings
}>('/:id', async (request, reply) => {
const params = request.params;
/**
Expand All @@ -74,9 +74,9 @@ const NoteSettingsRouter: FastifyPluginCallback<NoteSettingsRouterOptions> = (fa
* Patch noteSettings by note public id
*/
fastify.patch<{
Body: Partial<NotesSettings>,
Body: Partial<NoteSettings>,
Params: GetNoteSettingsByNodeIdOptions,
Reply: NotesSettings,
Reply: NoteSettings,
}>('/:id', { preHandler: [opts.middlewares.authRequired, opts.middlewares.withUser] }, async (request, reply) => {
const noteId = request.params.id;

Expand Down
22 changes: 11 additions & 11 deletions src/repository/noteSettings.repository.ts
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
import type { Note, NotePublicId } from '@domain/entities/note.js';
import type NotesSettings from '@domain/entities/notesSettings.js';
import type NoteSettings from '@domain/entities/noteSettings.js';
import type NoteSettingsStorage from '@repository/storage/noteSettings.storage.js';
import type { NotesSettingsCreationAttributes } from '@domain/entities/notesSettings.js';
import type { NoteSettingsCreationAttributes } from '@domain/entities/noteSettings.js';

/**
* Repository allows accessing data from business-logic (domain) level
Expand All @@ -25,19 +25,19 @@ export default class NoteSettingsRepository {
* Gets note settings by id
*
* @param id - note id
* @returns { Promise<NotesSettings | null> } - found note
* @returns { Promise<NoteSettings | null> } - found note
*/
public async getNoteSettingsById(id: NotesSettings['id']): Promise<NotesSettings | null> {
public async getNoteSettingsById(id: NoteSettings['id']): Promise<NoteSettings | null> {
return await this.storage.getNoteSettingsById(id);
}

/**
* Get note settings by note id
*
* @param id - note public id
* @returns { Promise<NotesSettings | null> } found note settings
* @returns { Promise<NoteSettings | null> } found note settings
*/
public async getNoteSettingsByPublicId(id: NotePublicId): Promise<NotesSettings> {
public async getNoteSettingsByPublicId(id: NotePublicId): Promise<NoteSettings> {
/**
* @todo get internal id by public id and resolve note settings by the internal id
*/
Expand All @@ -48,9 +48,9 @@ export default class NoteSettingsRepository {
* Get note settings by note id
*
* @param id - note id
* @returns { Promise<NotesSettings | null> } found note settings
* @returns { Promise<NoteSettings | null> } found note settings
*/
public async getNoteSettingsByNoteId(id: Note['id']): Promise<NotesSettings> {
public async getNoteSettingsByNoteId(id: Note['id']): Promise<NoteSettings> {
return await this.storage.getNoteSettingsByNoteId(id);
}

Expand All @@ -59,7 +59,7 @@ export default class NoteSettingsRepository {
*
* @param settings - note settings
*/
public async addNoteSettings(settings: NotesSettingsCreationAttributes): Promise<NotesSettings> {
public async addNoteSettings(settings: NoteSettingsCreationAttributes): Promise<NoteSettings> {
return await this.storage.insertNoteSettings(settings);
}

Expand All @@ -68,9 +68,9 @@ export default class NoteSettingsRepository {
*
* @param data - note settings new values
* @param id - note settings id
* @returns { Promise<NotesSettings> } patched note settings
* @returns { Promise<NoteSettings> } patched note settings
*/
public async patchNoteSettingsByPublicId(data: Partial<NotesSettings>, id: NotesSettings['id']): Promise<NotesSettings | null> {
public async patchNoteSettingsByPublicId(data: Partial<NoteSettings>, id: NoteSettings['id']): Promise<NoteSettings | null> {
return await this.storage.patchNoteSettingsByPublicId(data, id);
}
}
2 changes: 1 addition & 1 deletion src/repository/storage/noteSettings.storage.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
import NoteSettingsSequelizeStorage from './postgres/orm/sequelize/notesSettings.js';
import NoteSettingsSequelizeStorage from './postgres/orm/sequelize/noteSettings.js';
/**
* Current note storage
*/
Expand Down
8 changes: 4 additions & 4 deletions src/repository/storage/postgres/orm/sequelize/note.ts
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@ 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 { NoteCreationAttributes } from '@domain/entities/note.js';
import type { NotesSettingsModel } from '@repository/storage/postgres/orm/sequelize/notesSettings.js';
import type { NoteSettingsModel } from '@repository/storage/postgres/orm/sequelize/noteSettings.js';
import { UserModel } from '@repository/storage/postgres/orm/sequelize/user.js';

/* eslint-disable @typescript-eslint/naming-convention */
Expand Down Expand Up @@ -56,7 +56,7 @@ export default class NoteSequelizeStorage {
/**
* Notes settings model in database
*/
public settingsModel: typeof NotesSettingsModel;
public settingsModel: typeof NoteSettingsModel;

/**
* Database instance
Expand All @@ -74,7 +74,7 @@ export default class NoteSequelizeStorage {
* @param ormInstance - ORM instance
* @param settingsModel - note customization parameters
*/
constructor({ connection }: Orm, settingsModel: typeof NotesSettingsModel) {
constructor({ connection }: Orm, settingsModel: typeof NoteSettingsModel) {
this.database = connection;

/**
Expand Down Expand Up @@ -112,7 +112,7 @@ export default class NoteSequelizeStorage {
*/
this.settingsModel = settingsModel;

/** NoteModel and NotesSettingsModel are connected as ONE-TO-ONE */
/** NoteModel and NoteSettingsModel are connected as ONE-TO-ONE */
this.model.hasOne(this.settingsModel, {
foreignKey: 'note_id',
as: this.settingsModel.tableName,
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -3,34 +3,34 @@ import { Model, DataTypes } from 'sequelize';
import type Orm from '@repository/storage/postgres/orm/sequelize/index.js';
import type { NotePublicId } from '@domain/entities/note.js';
import { NoteModel } from '@repository/storage/postgres/orm/sequelize/note.js';
import type NotesSettings from '@domain/entities/notesSettings.js';
import type { NotesSettingsCreationAttributes } from '@domain/entities/notesSettings.js';
import type NoteSettings from '@domain/entities/noteSettings.js';
import type { NoteSettingsCreationAttributes } from '@domain/entities/noteSettings.js';

/* eslint-disable @typescript-eslint/naming-convention */

/**
* Class representing a notes settings model in database
*/
export class NotesSettingsModel extends Model<InferAttributes<NotesSettingsModel>, InferCreationAttributes<NotesSettingsModel>> {
export class NoteSettingsModel extends Model<InferAttributes<NoteSettingsModel>, InferCreationAttributes<NoteSettingsModel>> {
/**
* Note Settings id
*/
public declare id: CreationOptional<NotesSettings['id']>;
public declare id: CreationOptional<NoteSettings['id']>;

/**
* Note ID
*/
public declare note_id: NotesSettings['noteId'];
public declare note_id: NoteSettings['noteId'];

/**
* Custom hostname
*/
public declare custom_hostname: CreationOptional<NotesSettings['customHostname']>;
public declare custom_hostname: CreationOptional<NoteSettings['customHostname']>;

/**
* Is note public
*/
public declare enabled: CreationOptional<NotesSettings['enabled']>;
public declare enabled: CreationOptional<NoteSettings['enabled']>;
}

/**
Expand All @@ -40,7 +40,7 @@ export default class NoteSettingsSequelizeStorage {
/**
* Notes settings model in database
*/
public model: typeof NotesSettingsModel;
public model: typeof NoteSettingsModel;

/**
* Database instance
Expand All @@ -50,7 +50,7 @@ export default class NoteSettingsSequelizeStorage {
/**
* Settings table name
*/
private readonly tableName = 'notes_settings';
private readonly tableName = 'note_settings';

/**
* Constructor for note storage
Expand All @@ -60,11 +60,10 @@ export default class NoteSettingsSequelizeStorage {
constructor({ connection }: Orm) {
this.database = connection;

console.log("GIVE ME THE ONNECTION", this.database)
/**
* Initiate note settings model
*/
this.model = NotesSettingsModel.init({
this.model = NoteSettingsModel.init({
id: {
type: DataTypes.INTEGER,
autoIncrement: true,
Expand Down Expand Up @@ -99,9 +98,9 @@ export default class NoteSettingsSequelizeStorage {
* Gets note settings by id
*
* @param id - note id
* @returns { Promise<NotesSettings | null> } found note
* @returns { Promise<NoteSettings | null> } found note
*/
public async getNoteSettingsById(id: NotesSettings['id']): Promise<NotesSettings | null> {
public async getNoteSettingsById(id: NoteSettings['id']): Promise<NoteSettings | null> {
const noteSettings = await this.model.findOne({
where: {
id,
Expand All @@ -127,9 +126,9 @@ export default class NoteSettingsSequelizeStorage {
* Get note settings
*
* @param noteId - note id
* @returns { Promise<NotesSettings | null> } - note settings
* @returns { Promise<NoteSettings | null> } - note settings
*/
public async getNoteSettingsByNoteId(noteId: NotesSettings['noteId']): Promise<NotesSettings> {
public async getNoteSettingsByNoteId(noteId: NoteSettings['noteId']): Promise<NoteSettings> {
const settings = await this.model.findOne({
where: {
note_id: noteId,
Expand All @@ -155,12 +154,12 @@ export default class NoteSettingsSequelizeStorage {
* Get note settings
*
* @param id - note internal id
* @returns { Promise<NotesSettings | null> } - note settings
* @returns { Promise<NoteSettings | null> } - note settings
*
* @deprecated
* @todo resolve note setting by internal id
*/
public async getNoteSettingsByPublicId(id: NotePublicId): Promise<NotesSettings> {
public async getNoteSettingsByPublicId(id: NotePublicId): Promise<NoteSettings> {
const settings = await this.model.findOne({
where: {
id: id,
Expand All @@ -186,14 +185,14 @@ export default class NoteSettingsSequelizeStorage {
* Insert note settings
*
* @param options - note settings options
* @returns { Promise<NotesSettings> } - inserted note settings
* @returns { Promise<NoteSettings> } - inserted note settings
*/
public async insertNoteSettings({
noteId,
customHostname,
enabled,
}: NotesSettingsCreationAttributes
): Promise<NotesSettings> {
}: NoteSettingsCreationAttributes
): Promise<NoteSettings> {
const settings = await this.model.create({
note_id: noteId,
custom_hostname: customHostname,
Expand All @@ -214,7 +213,7 @@ export default class NoteSettingsSequelizeStorage {
* @param data - note settings new data
* @param id - note settings id
*/
public async patchNoteSettingsByPublicId(data: Partial<NotesSettings>, id: NotesSettings['id']): Promise<NotesSettings | null> {
public async patchNoteSettingsByPublicId(data: Partial<NoteSettings>, id: NoteSettings['id']): Promise<NoteSettings | null> {
const settingsToUpdate = await this.model.findByPk(id);

/**
Expand Down
10 changes: 5 additions & 5 deletions src/tests/utils/insert-data.ts
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
import type SequelizeOrm from '@repository/storage/postgres/orm/index.js';
import users from '../test-data/users.json';
import notes from '../test-data/notes.json';
import notesSettings from '../test-data/notes-settings.json';
import noteSettings from '../test-data/notes-settings.json';

/**
* Fills in the database with users data
Expand Down Expand Up @@ -30,9 +30,9 @@ async function insertNotes(db: SequelizeOrm): Promise<void> {
*
* @param db - SequelizeOrm instance
*/
async function insertNotesSettings(db: SequelizeOrm): Promise<void> {
for (const noteSettings of notesSettings) {
await db.connection.query(`INSERT INTO public.notes_settings (id, "note_id", "custom_hostname", "enabled") VALUES (${noteSettings.id}, '${noteSettings.note_id}', '${noteSettings.custom_hostname}', ${noteSettings.enabled})`);
async function insertNoteSettings(db: SequelizeOrm): Promise<void> {
for (const noteSetting of noteSettings) {
await db.connection.query(`INSERT INTO public.notes_settings (id, "note_id", "custom_hostname", "enabled") VALUES (${noteSetting.id}, '${noteSetting.note_id}', '${noteSetting.custom_hostname}', ${noteSetting.enabled})`);
}
}

Expand All @@ -45,6 +45,6 @@ async function insertNotesSettings(db: SequelizeOrm): Promise<void> {
export async function insertData(db: SequelizeOrm): Promise<void> {
await insertUsers(db);
await insertNotes(db);
await insertNotesSettings(db);
await insertNoteSettings(db);
}

0 comments on commit fee85b9

Please sign in to comment.