Skip to content

Commit

Permalink
feat: add index and update code
Browse files Browse the repository at this point in the history
  • Loading branch information
Fedorrychkov committed Jul 28, 2024
1 parent 534ed4d commit 81e8040
Show file tree
Hide file tree
Showing 4 changed files with 81 additions and 26 deletions.
17 changes: 16 additions & 1 deletion firestore.indexes.json
Original file line number Diff line number Diff line change
@@ -1,4 +1,19 @@
{
"indexes": [],
"indexes": [
{
"collectionGroup": "example",
"queryScope": "COLLECTION",
"fields": [
{
"fieldPath": "isPublished",
"order": "ASCENDING"
},
{
"fieldPath": "createdAt",
"order": "DESCENDING"
}
]
}
],
"fieldOverrides": []
}
74 changes: 56 additions & 18 deletions pnpm-lock.yaml

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

10 changes: 5 additions & 5 deletions src/modules/example/controllers/example.controller.ts
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
import { Body, Controller, Get, NotFoundException, Param, Patch, Post, Query } from '@nestjs/common'
import { Body, Controller, Get, NotFoundException, Param, ParseBoolPipe, Patch, Post, Query } from '@nestjs/common'

import { ExampleFilter, ExampleRequestBody } from '../dto'
import { ExampleRequestBody } from '../dto'
import { ExampleDocument } from '../entities'
import { ExampleService } from '../services'

Expand All @@ -9,8 +9,8 @@ export class ExampleController {
constructor(private readonly exampleService: ExampleService) {}

@Get('/')
async getList(@Query() query?: ExampleFilter): Promise<ExampleDocument[]> {
const response = await this.exampleService.getList(query)
async getList(@Query('isPublished', ParseBoolPipe) isPublished?: boolean): Promise<ExampleDocument[]> {
const response = await this.exampleService.getList({ isPublished })

if (!response?.length) {
throw new NotFoundException('Examples are not exist')
Expand Down Expand Up @@ -40,7 +40,7 @@ export class ExampleController {
return this.exampleService.update(id, body)
}

@Patch('/:id')
@Patch('/publish/:id')
async togglePublish(@Param('id') id: string): Promise<ExampleDocument> {
return this.exampleService.togglePublish(id)
}
Expand Down
6 changes: 4 additions & 2 deletions src/modules/example/repositories/example.repository.ts
Original file line number Diff line number Diff line change
Expand Up @@ -48,7 +48,9 @@ export class ExampleRepository {

async find(filter: ExampleFilter): Promise<ExampleDocument[]> {
const list: ExampleDocument[] = []
const query = this.findGenerator(filter)
let query = this.findGenerator(filter)

query = query.orderBy('createdAt', 'desc')

const snapshot = await query.get()

Expand All @@ -58,8 +60,8 @@ export class ExampleRepository {
}

async create(payload: Pick<ExampleDocument, 'title'> & Partial<ExampleDocument>) {
const document = await this.collection.doc(payload.id)
const validPayload = this.getValidProperties(payload)
const document = await this.collection.doc(validPayload.id)
await document.set(validPayload)

return validPayload
Expand Down

0 comments on commit 81e8040

Please sign in to comment.