Skip to content

Commit

Permalink
feat: get deck row api using by param id
Browse files Browse the repository at this point in the history
  • Loading branch information
JonghunAn authored Jul 15, 2023
1 parent 732ed47 commit ef1fa90
Show file tree
Hide file tree
Showing 3 changed files with 32 additions and 22 deletions.
13 changes: 7 additions & 6 deletions packages/service-backend/src/modules/deck/DeckController.ts
Original file line number Diff line number Diff line change
Expand Up @@ -8,14 +8,15 @@ export class DeckController {
constructor(private readonly deckService: DeckService) {}

@Post()
async createDeck(@Body() createDeckDto: CreateDeckDto) {
const result = await this.deckService.create(createDeckDto.name, createDeckDto.userId);
return {deck_id: result};
async createDeck(@Body() createDeckDto: CreateDeckDto): Promise<any> {
const result = await this.deckService.create(createDeckDto.name, createDeckDto.userId);
return { result: { deck_id: result } };
}

@Get(':id')
async findDeck(@Param('id') id: string) {
return await this.deckService.findDeck(id);
@Get('/:id')
async findDeck(@Param('id') id: string): Promise<any> {
const result = await this.deckService.findDeck(id);
return { result: result };
}

@Patch(':id')
Expand Down
39 changes: 24 additions & 15 deletions packages/service-backend/src/modules/deck/DeckRepository.ts
Original file line number Diff line number Diff line change
@@ -1,27 +1,36 @@
import {Injectable, InternalServerErrorException} from '@nestjs/common';
import {InjectModel} from "../../core/database";
import { SoftDeleteModel } from 'soft-delete-plugin-mongoose'
import {DeckDocument} from "../../core/database/Deck";
import { Injectable, InternalServerErrorException, NotFoundException } from '@nestjs/common';
import { InjectModel } from '../../core/database';
import { SoftDeleteModel } from 'soft-delete-plugin-mongoose';
import { DeckDocument } from '../../core/database/Deck';

@Injectable()
export class DeckRepository {
constructor(@InjectModel.Deck private readonly deckModel: SoftDeleteModel<DeckDocument>) {
}
constructor(@InjectModel.Deck private readonly deckModel: SoftDeleteModel<DeckDocument>) {}

async create(name: string, userId:string): Promise<string> {
try{
async create(name: string, userId: string): Promise<string> {
try {
const deck = await this.deckModel.create({
name,
userId,
})
return deck.id;
}catch(error){
throw new InternalServerErrorException(`error: ${error}`);
}
});
return deck.id;
} catch (error) {
throw new InternalServerErrorException(`error: ${error}`);
}
}

async findOne(id: string) {
return `This action returns a #${id} deck`;
async findOne(id: string): Promise<any> {
try {
const deck = await this.deckModel.findOne({ id });

if (!deck) {
throw new NotFoundException(`Deck with id ${id} not found`);
}

return deck;
} catch (error) {
throw new InternalServerErrorException(`error: ${error}`);
}
}

async update(id: string) {
Expand Down
2 changes: 1 addition & 1 deletion packages/service-backend/src/modules/deck/DeckService.ts
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,7 @@ export class DeckService {
return await this.deckRepository.create(name, userId);
}

async findDeck(id: string) {
async findDeck(id: string): Promise<any> {
return await this.deckRepository.findOne(id);
}

Expand Down

0 comments on commit ef1fa90

Please sign in to comment.