@@ -44,6 +44,11 @@ export class RecentlyController {
|
|||||||
return await this.recentlyService.getOffset({ before, after, size })
|
return await this.recentlyService.getOffset({ before, after, size })
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@Get('/:id')
|
||||||
|
async getOne(@Param() { id }: MongoIdDto) {
|
||||||
|
return await this.recentlyService.getOne(id)
|
||||||
|
}
|
||||||
|
|
||||||
@Post('/')
|
@Post('/')
|
||||||
@HTTPDecorators.Idempotence()
|
@HTTPDecorators.Idempotence()
|
||||||
@Auth()
|
@Auth()
|
||||||
|
|||||||
@@ -77,6 +77,45 @@ export class RecentlyService {
|
|||||||
return result
|
return result
|
||||||
}
|
}
|
||||||
|
|
||||||
|
async getOne(id: string) {
|
||||||
|
const result = (await this.model.aggregate([
|
||||||
|
{
|
||||||
|
$lookup: {
|
||||||
|
from: 'comments',
|
||||||
|
as: 'comment',
|
||||||
|
foreignField: 'ref',
|
||||||
|
localField: '_id',
|
||||||
|
},
|
||||||
|
},
|
||||||
|
|
||||||
|
{
|
||||||
|
$addFields: {
|
||||||
|
comments: {
|
||||||
|
$size: '$comment',
|
||||||
|
},
|
||||||
|
},
|
||||||
|
},
|
||||||
|
{
|
||||||
|
$project: {
|
||||||
|
comment: 0,
|
||||||
|
},
|
||||||
|
},
|
||||||
|
{
|
||||||
|
$sort: {
|
||||||
|
created: -1,
|
||||||
|
},
|
||||||
|
},
|
||||||
|
{
|
||||||
|
$match: {
|
||||||
|
_id: new ObjectId(id),
|
||||||
|
},
|
||||||
|
},
|
||||||
|
])) as RecentlyModel[]
|
||||||
|
|
||||||
|
await this.populateRef(result)
|
||||||
|
|
||||||
|
return result[0] || null
|
||||||
|
}
|
||||||
async populateRef(result: RecentlyModel[], omit = ['text']) {
|
async populateRef(result: RecentlyModel[], omit = ['text']) {
|
||||||
const refMap: Record<
|
const refMap: Record<
|
||||||
Exclude<CollectionRefTypes, CollectionRefTypes.Recently>,
|
Exclude<CollectionRefTypes, CollectionRefTypes.Recently>,
|
||||||
@@ -149,8 +188,8 @@ export class RecentlyService {
|
|||||||
},
|
},
|
||||||
}
|
}
|
||||||
: before
|
: before
|
||||||
? { _id: { $lt: new ObjectId(before) } }
|
? { _id: { $lt: new ObjectId(before) } }
|
||||||
: {},
|
: {},
|
||||||
},
|
},
|
||||||
|
|
||||||
{
|
{
|
||||||
|
|||||||
@@ -15,7 +15,19 @@ describe('test recently client, /recently', () => {
|
|||||||
},
|
},
|
||||||
],
|
],
|
||||||
})
|
})
|
||||||
const data = await client.recently.getList('616182162657089e483aac5c')
|
const data = await client.recently.getList({
|
||||||
|
before: '616182162657089e483aac5c',
|
||||||
|
})
|
||||||
|
expect(data).toEqual(mocked)
|
||||||
|
})
|
||||||
|
|
||||||
|
test('GET /6608f877e345af4659011d28', async () => {
|
||||||
|
const mocked = mockResponse('/recently/6608f877e345af4659011d28', {
|
||||||
|
id: '6608f877e345af4659011d28',
|
||||||
|
content: 'x',
|
||||||
|
created: '2021-10-05T13:53:15.891Z',
|
||||||
|
})
|
||||||
|
const data = await client.recently.getById('6608f877e345af4659011d28')
|
||||||
expect(data).toEqual(mocked)
|
expect(data).toEqual(mocked)
|
||||||
})
|
})
|
||||||
|
|
||||||
|
|||||||
@@ -50,11 +50,15 @@ export class RecentlyController<ResponseWrapper> implements IController {
|
|||||||
}>()
|
}>()
|
||||||
}
|
}
|
||||||
|
|
||||||
getList(
|
getList({
|
||||||
before?: string | undefined,
|
before,
|
||||||
after?: string | undefined,
|
after,
|
||||||
size?: number | number,
|
size,
|
||||||
) {
|
}: {
|
||||||
|
before?: string | undefined
|
||||||
|
after?: string | undefined
|
||||||
|
size?: number | number
|
||||||
|
} = {}) {
|
||||||
return this.proxy.get<{ data: RecentlyModel[] & { comments: number } }>({
|
return this.proxy.get<{ data: RecentlyModel[] & { comments: number } }>({
|
||||||
params: {
|
params: {
|
||||||
before,
|
before,
|
||||||
@@ -64,6 +68,10 @@ export class RecentlyController<ResponseWrapper> implements IController {
|
|||||||
})
|
})
|
||||||
}
|
}
|
||||||
|
|
||||||
|
getById(id: string) {
|
||||||
|
return this.proxy(id).get<RecentlyModel & { comments: number }>()
|
||||||
|
}
|
||||||
|
|
||||||
/** 表态:点赞,点踩 */
|
/** 表态:点赞,点踩 */
|
||||||
attitude(id: string, attitude: RecentlyAttitudeEnum) {
|
attitude(id: string, attitude: RecentlyAttitudeEnum) {
|
||||||
return this.proxy.attitude(id).get<{ code: RecentlyAttitudeResultEnum }>({
|
return this.proxy.attitude(id).get<{ code: RecentlyAttitudeResultEnum }>({
|
||||||
|
|||||||
Reference in New Issue
Block a user