Closed B-Dmitriy closed 9 months ago
import { Repository } from 'typeorm';
import { InjectRepository } from '@nestjs/typeorm';
import { Injectable, NotFoundException } from '@nestjs/common';
import { TransactionDto } from './dto/transaction.dto';
import { Transaction } from './entities/transaction.entity';
import { CreateTransactionDto } from './dto/create-transaction.dto';
import { UpdateTransactionDto } from './dto/update-transaction.dto';
import { GetTransactionsQueryDto } from './dto/get-transaction-query.dto';
import { Counterparty } from '../counterparties/entities/counterparty.entity';
@Injectable()
export class TransactionsService {
constructor(
@InjectRepository(Counterparty)
private counterpartyRepository: Repository<Counterparty>,
@InjectRepository(Transaction)
private transactionsRepository: Repository<Transaction>,
) {}
async create(
createTransactionDto: CreateTransactionDto,
): Promise<TransactionDto> {
const counterpartyId = createTransactionDto.counterpartyId;
const counterparty = await this.counterpartyRepository.findOneBy({
id: counterpartyId,
});
if (!counterparty) {
throw new NotFoundException({
statusCode: 404,
message: `counterparty with id ${counterpartyId} not found`,
});
}
return await this.transactionsRepository.save(createTransactionDto);
}
// TODO: Common return type for tables get-counterparties-response.dto.ts
async findAll(queryParams: GetTransactionsQueryDto) {
const transactions = await this.transactionsRepository.query(`
select t.id,t.amount,t.currency,
c.name as counterparty,
c.description as counterpartyDescription,
t.createdAt,t.comment
from "transaction" AS t
LEFT JOIN counterparty AS c
ON t.counterpartyId = c.id
LIMIT ${queryParams.limit}
OFFSET ${(+queryParams.page - 1) * +queryParams.limit};
`);
const total = await this.transactionsRepository.count();
return {
items: transactions,
page: +queryParams.page,
limit: +queryParams.limit,
total,
};
}
async findOne(id: number) {
const transaction = await this.transactionsRepository.findOneBy({ id });
if (!transaction) throw new NotFoundException();
return transaction;
}
async update(id: number, updateTransactionDto: UpdateTransactionDto) {
const counterpartyId = updateTransactionDto.counterpartyId;
const counterparty = await this.counterpartyRepository.findOneBy({
id: counterpartyId,
});
if (!counterparty) {
throw new NotFoundException({
statusCode: 404,
message: `counterparty with id ${counterpartyId} not found`,
});
}
const updateResult = await this.transactionsRepository.update(
id,
updateTransactionDto,
);
if (updateResult.affected === 0) throw new NotFoundException();
return await this.transactionsRepository.findOneBy({ id });
}
async remove(id: number) {
const deletedResult = await this.transactionsRepository.delete(id);
if (deletedResult.affected === 0) throw new NotFoundException();
return;
}
}
Closed in pr #6
Π Π°Π·ΡΠ°Π±ΠΎΡΠ°ΡΡ ΡΠ΅ΡΠ²ΠΈΡ ΡΡΠ°Π½Π·Π°ΠΊΡΠΈΠΈ
ΠΠ° Π΄Π°Π½Π½ΡΠΉ ΠΌΠΎΠΌΠ΅Π½Ρ Π½ΡΠΆΠ½Ρ ΡΠ»Π΅Π΄ΡΡΡΠΈΠ΅ ΠΊΠΎΠ»ΠΎΠ½ΠΊΠΈ: 1) id 2) comment 3) amount - ΡΡΠΌΠΌΠ°, ΠΊΠΎΠ»-Π²ΠΎ Π΄Π΅Π½Π΅Π³ 4) currency - Π²Π°Π»ΡΡΠ° 5) in/out - Π²Ρ ΠΎΠ΄ΡΡΠΈΠΉ/ΠΈΡΡ ΠΎΠ΄ΡΡΠΈΠΉ (Π΄Π»Ρ ΡΠΈΠ»ΡΡΠ°ΡΠΈΠΈ) 5) counterpartyId - ΡΡΡΠ»ΠΊΠ° Π½Π° ΠΊΠΎΠ½ΡΡΠ°Π³Π΅Π½ΡΠ° 6) createdAt
ΠΠ΅ΠΎΠ±Ρ ΠΎΠ΄ΠΈΠΌΠΎ ΡΠ΅Π°Π»ΠΈΠ·ΠΎΠ²Π°ΡΡ Π² ΠΏΡΠΎΡΠΎΡΠΈΠΏΠ΅ (v0)