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: added cover to note list #263

Merged
merged 4 commits into from
Jul 2, 2024
Merged
Show file tree
Hide file tree
Changes from all 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
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: 'DZnvqi63.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
2 changes: 1 addition & 1 deletion src/repository/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,7 @@ import EditorToolsRepository from '@repository/editorTools.repository.js';
import TeamRepository from '@repository/team.repository.js';
import TeamStorage from '@repository/storage/team.storage.js';
import NoteRelationsRepository from '@repository/noteRelations.repository.js';
import { S3Storage } from './storage/s3/index.js';
import { S3Storage } from '@repository/storage/s3/index.js';
import FileStorage from './storage/file.storage.js';
import FileRepository from './file.repository.js';
import ObjectStorageRepository from './object.repository.js';
Expand Down
2 changes: 1 addition & 1 deletion src/repository/object.repository.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
import type { S3Storage } from './storage/s3/index.js';
import type { S3Storage } from '@repository/storage/s3/index.js';
import type { Buffer } from 'buffer';

/**
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 type { 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
Loading