2019-04-07 15:50:36 +03:00
|
|
|
import { EntityRepository, Repository } from 'typeorm';
|
2021-08-19 12:33:41 +03:00
|
|
|
import { NoteFavorite } from '../entities/note-favorite.js';
|
|
|
|
import { Notes } from '../index.js';
|
|
|
|
import { User } from '../entities/user.js';
|
2019-04-07 15:50:36 +03:00
|
|
|
|
|
|
|
@EntityRepository(NoteFavorite)
|
|
|
|
export class NoteFavoriteRepository extends Repository<NoteFavorite> {
|
|
|
|
public async pack(
|
|
|
|
src: NoteFavorite['id'] | NoteFavorite,
|
2021-03-24 04:05:37 +02:00
|
|
|
me?: { id: User['id'] } | null | undefined
|
2019-04-07 15:50:36 +03:00
|
|
|
) {
|
2021-02-13 08:33:38 +02:00
|
|
|
const favorite = typeof src === 'object' ? src : await this.findOneOrFail(src);
|
2019-04-07 15:50:36 +03:00
|
|
|
|
|
|
|
return {
|
|
|
|
id: favorite.id,
|
2019-05-19 14:48:10 +03:00
|
|
|
createdAt: favorite.createdAt,
|
|
|
|
noteId: favorite.noteId,
|
2019-04-07 15:50:36 +03:00
|
|
|
note: await Notes.pack(favorite.note || favorite.noteId, me),
|
|
|
|
};
|
|
|
|
}
|
2019-04-25 07:27:07 +03:00
|
|
|
|
|
|
|
public packMany(
|
|
|
|
favorites: any[],
|
2021-03-24 04:05:37 +02:00
|
|
|
me: { id: User['id'] }
|
2019-04-25 07:27:07 +03:00
|
|
|
) {
|
|
|
|
return Promise.all(favorites.map(x => this.pack(x, me)));
|
|
|
|
}
|
2019-04-07 15:50:36 +03:00
|
|
|
}
|
2019-05-20 16:01:32 +03:00
|
|
|
|
|
|
|
export const packedNoteFavoriteSchema = {
|
2019-06-27 12:04:09 +03:00
|
|
|
type: 'object' as const,
|
|
|
|
optional: false as const, nullable: false as const,
|
2019-05-20 16:01:32 +03:00
|
|
|
properties: {
|
|
|
|
id: {
|
2019-06-27 12:04:09 +03:00
|
|
|
type: 'string' as const,
|
|
|
|
optional: false as const, nullable: false as const,
|
2019-05-20 16:01:32 +03:00
|
|
|
format: 'id',
|
|
|
|
example: 'xxxxxxxxxx',
|
|
|
|
},
|
|
|
|
createdAt: {
|
2019-06-27 12:04:09 +03:00
|
|
|
type: 'string' as const,
|
|
|
|
optional: false as const, nullable: false as const,
|
2019-05-20 16:01:32 +03:00
|
|
|
format: 'date-time',
|
|
|
|
},
|
|
|
|
note: {
|
2019-06-27 12:04:09 +03:00
|
|
|
type: 'object' as const,
|
|
|
|
optional: false as const, nullable: false as const,
|
2019-05-20 16:01:32 +03:00
|
|
|
ref: 'Note',
|
|
|
|
},
|
|
|
|
noteId: {
|
2019-06-27 12:04:09 +03:00
|
|
|
type: 'string' as const,
|
|
|
|
optional: false as const, nullable: false as const,
|
2019-05-20 16:01:32 +03:00
|
|
|
format: 'id',
|
|
|
|
},
|
|
|
|
},
|
|
|
|
};
|