2019-04-07 07:50:36 -05:00
|
|
|
import { EntityRepository, Repository } from 'typeorm';
|
|
|
|
import { NoteFavorite } from '../entities/note-favorite';
|
|
|
|
import { Notes } from '..';
|
2019-04-12 11:43:22 -05:00
|
|
|
import { ensure } from '../../prelude/ensure';
|
2019-04-07 07:50:36 -05:00
|
|
|
|
|
|
|
@EntityRepository(NoteFavorite)
|
|
|
|
export class NoteFavoriteRepository extends Repository<NoteFavorite> {
|
|
|
|
public async pack(
|
|
|
|
src: NoteFavorite['id'] | NoteFavorite,
|
|
|
|
me?: any
|
|
|
|
) {
|
2019-04-12 11:43:22 -05:00
|
|
|
const favorite = typeof src === 'object' ? src : await this.findOne(src).then(ensure);
|
2019-04-07 07:50:36 -05:00
|
|
|
|
|
|
|
return {
|
|
|
|
id: favorite.id,
|
2019-05-19 06:48:10 -05:00
|
|
|
createdAt: favorite.createdAt,
|
|
|
|
noteId: favorite.noteId,
|
2019-04-07 07:50:36 -05:00
|
|
|
note: await Notes.pack(favorite.note || favorite.noteId, me),
|
|
|
|
};
|
|
|
|
}
|
2019-04-24 23:27:07 -05:00
|
|
|
|
|
|
|
public packMany(
|
|
|
|
favorites: any[],
|
|
|
|
me: any
|
|
|
|
) {
|
|
|
|
return Promise.all(favorites.map(x => this.pack(x, me)));
|
|
|
|
}
|
2019-04-07 07:50:36 -05:00
|
|
|
}
|
2019-05-20 08:01:32 -05:00
|
|
|
|
|
|
|
export const packedNoteFavoriteSchema = {
|
2019-06-27 04:04:09 -05:00
|
|
|
type: 'object' as const,
|
|
|
|
optional: false as const, nullable: false as const,
|
2019-05-20 08:01:32 -05:00
|
|
|
properties: {
|
|
|
|
id: {
|
2019-06-27 04:04:09 -05:00
|
|
|
type: 'string' as const,
|
|
|
|
optional: false as const, nullable: false as const,
|
2019-05-20 08:01:32 -05:00
|
|
|
format: 'id',
|
|
|
|
description: 'The unique identifier for this favorite.',
|
|
|
|
example: 'xxxxxxxxxx',
|
|
|
|
},
|
|
|
|
createdAt: {
|
2019-06-27 04:04:09 -05:00
|
|
|
type: 'string' as const,
|
|
|
|
optional: false as const, nullable: false as const,
|
2019-05-20 08:01:32 -05:00
|
|
|
format: 'date-time',
|
|
|
|
description: 'The date that the favorite was created.'
|
|
|
|
},
|
|
|
|
note: {
|
2019-06-27 04:04:09 -05:00
|
|
|
type: 'object' as const,
|
|
|
|
optional: false as const, nullable: false as const,
|
2019-05-20 08:01:32 -05:00
|
|
|
ref: 'Note',
|
|
|
|
},
|
|
|
|
noteId: {
|
2019-06-27 04:04:09 -05:00
|
|
|
type: 'string' as const,
|
|
|
|
optional: false as const, nullable: false as const,
|
2019-05-20 08:01:32 -05:00
|
|
|
format: 'id',
|
|
|
|
},
|
|
|
|
},
|
|
|
|
};
|