import { IBaseModel , Model , Service } from '@fastify-resty/core' ; import PostEntity from './post.entity' ; @ Service ( ) export default class PostService { @ Model ( PostEntity ) postModel : IBaseModel < PostEntity > ; async getRandomPost ( ) { const postsTotal = await this . postModel . total ( ) ; const posts = await this . postModel . find ( ) ; return posts [ this . getRandomNumber ( 0 , postsTotal - 1 ) ] ; } getRandomNumber ( min , max ) { return Math . random ( ) * ( max - min ) + min ; } }