From fd43b7c017aa298133fab37673fa4d64dbebe791 Mon Sep 17 00:00:00 2001 From: Mairis Berzins Date: Sat, 13 Jul 2024 22:42:36 +0300 Subject: [PATCH] crud client --- .../api/src/features/card/card.controller.ts | 11 +++- .../api/src/features/card/card.service.ts | 8 ++- .../src/features/card/card.service.type.ts | 6 +-- .../core/{api => api-client}/api.client.ts | 0 .../client/src/core/api-client/crud.client.ts | 52 +++++++++++++++++++ .../src/core/api-client/crud.client.type.ts | 7 +++ .../client/src/features/auth/auth.client.ts | 2 +- .../client/src/features/card/card.client.ts | 23 ++------ .../src/features/card/card.client.type.ts | 6 +-- .../client/src/features/game/game.client.ts | 2 +- .../client/src/features/user/user.client.ts | 2 +- 11 files changed, 85 insertions(+), 34 deletions(-) rename packages/client/src/core/{api => api-client}/api.client.ts (100%) create mode 100644 packages/client/src/core/api-client/crud.client.ts create mode 100644 packages/client/src/core/api-client/crud.client.type.ts diff --git a/packages/api/src/features/card/card.controller.ts b/packages/api/src/features/card/card.controller.ts index a8b2e48..1e21fdc 100644 --- a/packages/api/src/features/card/card.controller.ts +++ b/packages/api/src/features/card/card.controller.ts @@ -4,6 +4,7 @@ import { controller, httpGet, httpPost, + requestParam, } from 'inversify-express-utils'; import { Middleware, Services } from '../../core/inversify.identifiers'; import { ICardService } from './card.service.type'; @@ -14,14 +15,20 @@ export class CardController extends BaseHttpController { super(); } + @httpGet('/:id') + async index(@requestParam('id') id: number) { + const cards = await this.cardService.getById(id); + return this.json(cards); + } + @httpGet('/') - public async getAll() { + async list() { const cards = await this.cardService.getAll(); return this.json(cards); } @httpPost('/') - public async create() { + async create() { try { await this.cardService.save(this.httpContext.request.body); return this.ok(); diff --git a/packages/api/src/features/card/card.service.ts b/packages/api/src/features/card/card.service.ts index ca2c70d..779b64d 100644 --- a/packages/api/src/features/card/card.service.ts +++ b/packages/api/src/features/card/card.service.ts @@ -6,11 +6,15 @@ import { ICardService } from './card.service.type'; @injectable() export class CardService implements ICardService { - public getAll() { + getById(id: number) { + return cardRepository.findOneBy({ id }); + } + + getAll() { return cardRepository.find(); } - public save(cardDto: CardDto) { + save(cardDto: CardDto) { const card = new Card(); card.name = cardDto.name; return cardRepository.save(card); diff --git a/packages/api/src/features/card/card.service.type.ts b/packages/api/src/features/card/card.service.type.ts index 207b01e..632cac4 100644 --- a/packages/api/src/features/card/card.service.type.ts +++ b/packages/api/src/features/card/card.service.type.ts @@ -1,7 +1,7 @@ import { CardDto } from '@tspark/common'; -import { Card } from './card.entity'; export interface ICardService { - getAll(): Promise; - save(cardDto: CardDto): Promise; + getById(id: number): Promise; + getAll(): Promise; + save(cardDto: CardDto): Promise; } diff --git a/packages/client/src/core/api/api.client.ts b/packages/client/src/core/api-client/api.client.ts similarity index 100% rename from packages/client/src/core/api/api.client.ts rename to packages/client/src/core/api-client/api.client.ts diff --git a/packages/client/src/core/api-client/crud.client.ts b/packages/client/src/core/api-client/crud.client.ts new file mode 100644 index 0000000..431b1ab --- /dev/null +++ b/packages/client/src/core/api-client/crud.client.ts @@ -0,0 +1,52 @@ +import { decorate, injectable } from 'inversify'; +import { apiClient } from './api.client'; +import { ICrudClient as IClient } from './crud.client.type'; + +@injectable() +export abstract class CrudClient implements IClient { + abstract readonly RESOURCE_NAME: string; + + async get(id: number): Promise { + try { + const res = await apiClient.get(`${this.RESOURCE_NAME}/${id}`); + return res.data; + } catch (err) { + console.error(err); + return null; + } + } + + async getAll(): Promise { + try { + const res = await apiClient.get(this.RESOURCE_NAME); + return res.data; + } catch (err) { + console.error(err); + return []; + } + } + + async create(item: T): Promise { + try { + await apiClient.post(this.RESOURCE_NAME, item); + } catch (err) { + console.error(err); + } + } + + async update(item: T): Promise { + try { + await apiClient.put(this.RESOURCE_NAME, item); + } catch (err) { + console.error(err); + } + } + + async delete(id: number): Promise { + try { + await apiClient.delete(`${this.RESOURCE_NAME}/${id}`); + } catch (err) { + console.error(err); + } + } +} diff --git a/packages/client/src/core/api-client/crud.client.type.ts b/packages/client/src/core/api-client/crud.client.type.ts new file mode 100644 index 0000000..94f44be --- /dev/null +++ b/packages/client/src/core/api-client/crud.client.type.ts @@ -0,0 +1,7 @@ +export interface ICrudClient { + get(id: number): Promise; + getAll(): Promise; + create(card: T): Promise; + update(card: T): Promise; + delete(id: number): Promise; +} diff --git a/packages/client/src/features/auth/auth.client.ts b/packages/client/src/features/auth/auth.client.ts index d405c38..8825655 100644 --- a/packages/client/src/features/auth/auth.client.ts +++ b/packages/client/src/features/auth/auth.client.ts @@ -4,7 +4,7 @@ import { RegisterRequest, UserDto, } from '@tspark/common'; -import { apiClient } from '../../core/api/api.client'; +import { apiClient } from '../../core/api-client/api.client'; // TODO: handle register failure const register = (req: RegisterRequest): Promise => diff --git a/packages/client/src/features/card/card.client.ts b/packages/client/src/features/card/card.client.ts index 4d7a31a..3847292 100644 --- a/packages/client/src/features/card/card.client.ts +++ b/packages/client/src/features/card/card.client.ts @@ -1,26 +1,9 @@ import { CardDto } from '@tspark/common'; import { injectable } from 'inversify'; -import { apiClient } from '../../core/api/api.client'; +import { CrudClient } from '../../core/api-client/crud.client'; import { ICardClient } from './card.client.type'; @injectable() -export class CardClient implements ICardClient { - async getAll(): Promise { - try { - const res = await apiClient.get('card'); - return res.data; - } catch (err) { - console.error(err); - return []; - } - } - - async create(card: CardDto): Promise { - try { - const res = await apiClient.post('card', card); - return res.data; - } catch (err) { - console.error(err); - } - } +export class CardClient extends CrudClient implements ICardClient { + RESOURCE_NAME = 'card'; } diff --git a/packages/client/src/features/card/card.client.type.ts b/packages/client/src/features/card/card.client.type.ts index 8b27359..384700b 100644 --- a/packages/client/src/features/card/card.client.type.ts +++ b/packages/client/src/features/card/card.client.type.ts @@ -1,6 +1,4 @@ import { CardDto } from '@tspark/common'; +import { ICrudClient } from '../../core/api-client/crud.client.type'; -export interface ICardClient { - getAll(): Promise; - create(card: CardDto): Promise; -} +export interface ICardClient extends ICrudClient {} diff --git a/packages/client/src/features/game/game.client.ts b/packages/client/src/features/game/game.client.ts index a39f35a..d926c5e 100644 --- a/packages/client/src/features/game/game.client.ts +++ b/packages/client/src/features/game/game.client.ts @@ -1,5 +1,5 @@ import { GameDto } from '@tspark/common'; -import { apiClient } from '../../core/api/api.client'; +import { apiClient } from '../../core/api-client/api.client'; export class GameClient { static getGames(): Promise { diff --git a/packages/client/src/features/user/user.client.ts b/packages/client/src/features/user/user.client.ts index c579e4a..e5ca663 100644 --- a/packages/client/src/features/user/user.client.ts +++ b/packages/client/src/features/user/user.client.ts @@ -1,5 +1,5 @@ import { UserDto } from '@tspark/common'; -import { apiClient } from '../../core/api/api.client'; +import { apiClient } from '../../core/api-client/api.client'; const getAll = (): Promise => { return apiClient