Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

feat: [GET] get deck row by id #43

Merged
merged 1 commit into from
Jul 15, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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