Skip to content

Commit

Permalink
feat: added cover to note list
Browse files Browse the repository at this point in the history
  • Loading branch information
slaveeks committed Jul 2, 2024
1 parent 4ce9724 commit e64785e
Show file tree
Hide file tree
Showing 6 changed files with 51 additions and 3 deletions.
1 change: 1 addition & 0 deletions .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@ app-config.local.yaml
.idea
dist
database
s3

# Logs
logs
Expand Down
5 changes: 5 additions & 0 deletions src/domain/entities/note.ts
Original file line number Diff line number Diff line change
Expand Up @@ -67,6 +67,11 @@ export interface Note {
*/
updatedAt: string;

/**
* Note image id
*/
cover?: string | null;

/**
* All tools used in certain note
*/
Expand Down
3 changes: 2 additions & 1 deletion src/domain/entities/notePublic.ts
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
import type { Note } from '@domain/entities/note.js';

type NotePublicProperties = 'content' | 'createdAt' | 'updatedAt' | 'creatorId';
type NotePublicProperties = 'content' | 'createdAt' | 'updatedAt' | 'creatorId' | 'cover';

export interface NotePublic extends Pick<Note, NotePublicProperties> {
/**
Expand All @@ -20,6 +20,7 @@ export function definePublicNote(note: Note): NotePublic {
createdAt: note.createdAt,
updatedAt: note.updatedAt,
creatorId: note.creatorId,
cover: note.cover,
};

return notePublic;
Expand Down
6 changes: 6 additions & 0 deletions src/presentation/http/router/noteList.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -109,6 +109,12 @@ describe('GET /notes?page', () => {
creatorId: creator.id,
});

await global.db.insertNoteSetting({
noteId: note.id,
cover: 'image.png',
isPublic: false
})

if (notesVisited) {
await global.db.insertNoteVisit({
userId: randomGuy.id,
Expand Down
1 change: 1 addition & 0 deletions src/presentation/http/schema/Note.ts
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,7 @@ export const NoteSchema = {
maxLength: 10,
minLength: 10,
},
cover: { type: 'string' },
content: {
type: 'object',
properties: {
Expand Down
38 changes: 36 additions & 2 deletions src/repository/storage/postgres/orm/sequelize/note.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
import type { CreationOptional, InferAttributes, InferCreationAttributes, ModelStatic, Sequelize } from 'sequelize';
import { CreationOptional, InferAttributes, InferCreationAttributes, ModelStatic, NonAttribute, Sequelize } from 'sequelize';
import { DataTypes, Model } from 'sequelize';
import type Orm from '@repository/storage/postgres/orm/sequelize/index.js';
import type { Note, NoteCreationAttributes, NoteInternalId, NotePublicId } from '@domain/entities/note.js';
Expand Down Expand Up @@ -43,7 +43,15 @@ export class NoteModel extends Model<InferAttributes<NoteModel>, InferCreationAt
*/
public declare updatedAt: CreationOptional<Note['updatedAt']>;

/**
* Editor tools, which note contains
*/
public declare tools: Note['tools'];

/**
* Joined note settings model
*/
public declare noteSettings?: NonAttribute<NoteSettingsModel>;
}

/**
Expand Down Expand Up @@ -225,6 +233,10 @@ export default class NoteSequelizeStorage {
throw new DomainError('NoteVisit model should be defined');
}

if (!this.settingsModel) {
throw new Error('Note settings model not initialized');
}

const reply = await this.model.findAll({
offset: offset,
limit: limit,
Expand All @@ -243,10 +255,32 @@ export default class NoteSequelizeStorage {
model: this.visitsModel,
as: 'noteVisits',
duplicating: false,
}, {
model: this.settingsModel!,
as: 'noteSettings',
attributes: ['cover'],
duplicating: false,
}],
});

return reply;
/**
* Convert note model data to Note entity with cover property
*/
return reply.map((note) => {
return {
id: note.id,
/**
* noteSettings is required to be, because we make join
*/
cover: note.noteSettings!.cover,
content: note.content,
updatedAt: note.updatedAt,
createdAt: note.createdAt,
publicId: note.publicId,
creatorId: note.creatorId,
tools: note.tools,
}
});
}

/**
Expand Down

0 comments on commit e64785e

Please sign in to comment.