Skip to content

Commit

Permalink
crud client
Browse files Browse the repository at this point in the history
  • Loading branch information
mairisb committed Jul 13, 2024
1 parent 835eb96 commit fd43b7c
Show file tree
Hide file tree
Showing 11 changed files with 85 additions and 34 deletions.
11 changes: 9 additions & 2 deletions packages/api/src/features/card/card.controller.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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';
Expand All @@ -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();
Expand Down
8 changes: 6 additions & 2 deletions packages/api/src/features/card/card.service.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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);
Expand Down
6 changes: 3 additions & 3 deletions packages/api/src/features/card/card.service.type.ts
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
import { CardDto } from '@tspark/common';
import { Card } from './card.entity';

export interface ICardService {
getAll(): Promise<Card[]>;
save(cardDto: CardDto): Promise<Card>;
getById(id: number): Promise<CardDto | null>;
getAll(): Promise<CardDto[]>;
save(cardDto: CardDto): Promise<CardDto>;
}
File renamed without changes.
52 changes: 52 additions & 0 deletions packages/client/src/core/api-client/crud.client.ts
Original file line number Diff line number Diff line change
@@ -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<T> implements IClient<T> {
abstract readonly RESOURCE_NAME: string;

async get(id: number): Promise<T | null> {
try {
const res = await apiClient.get(`${this.RESOURCE_NAME}/${id}`);
return res.data;
} catch (err) {
console.error(err);
return null;
}
}

async getAll(): Promise<T[]> {
try {
const res = await apiClient.get(this.RESOURCE_NAME);
return res.data;
} catch (err) {
console.error(err);
return [];
}
}

async create(item: T): Promise<void> {
try {
await apiClient.post(this.RESOURCE_NAME, item);
} catch (err) {
console.error(err);
}
}

async update(item: T): Promise<void> {
try {
await apiClient.put(this.RESOURCE_NAME, item);
} catch (err) {
console.error(err);
}
}

async delete(id: number): Promise<void> {
try {
await apiClient.delete(`${this.RESOURCE_NAME}/${id}`);
} catch (err) {
console.error(err);
}
}
}
7 changes: 7 additions & 0 deletions packages/client/src/core/api-client/crud.client.type.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
export interface ICrudClient<T> {
get(id: number): Promise<T | null>;
getAll(): Promise<T[]>;
create(card: T): Promise<void>;
update(card: T): Promise<void>;
delete(id: number): Promise<void>;
}
2 changes: 1 addition & 1 deletion packages/client/src/features/auth/auth.client.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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<UserDto> =>
Expand Down
23 changes: 3 additions & 20 deletions packages/client/src/features/card/card.client.ts
Original file line number Diff line number Diff line change
@@ -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<CardDto[]> {
try {
const res = await apiClient.get('card');
return res.data;
} catch (err) {
console.error(err);
return [];
}
}

async create(card: CardDto): Promise<void> {
try {
const res = await apiClient.post('card', card);
return res.data;
} catch (err) {
console.error(err);
}
}
export class CardClient extends CrudClient<CardDto> implements ICardClient {
RESOURCE_NAME = 'card';
}
6 changes: 2 additions & 4 deletions packages/client/src/features/card/card.client.type.ts
Original file line number Diff line number Diff line change
@@ -1,6 +1,4 @@
import { CardDto } from '@tspark/common';
import { ICrudClient } from '../../core/api-client/crud.client.type';

export interface ICardClient {
getAll(): Promise<CardDto[]>;
create(card: CardDto): Promise<void>;
}
export interface ICardClient extends ICrudClient<CardDto> {}
2 changes: 1 addition & 1 deletion packages/client/src/features/game/game.client.ts
Original file line number Diff line number Diff line change
@@ -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<GameDto[]> {
Expand Down
2 changes: 1 addition & 1 deletion packages/client/src/features/user/user.client.ts
Original file line number Diff line number Diff line change
@@ -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<UserDto[]> => {
return apiClient
Expand Down

0 comments on commit fd43b7c

Please sign in to comment.