added crypto history

This commit is contained in:
Kevsl 2024-06-07 15:55:20 +02:00
parent 73fd9bf8a1
commit f4abd98d8c
5 changed files with 57 additions and 2 deletions

View File

@ -0,0 +1,13 @@
-- CreateTable
CREATE TABLE "CryptoHistory" (
"id" TEXT NOT NULL,
"id_crypto" TEXT NOT NULL,
"value" DOUBLE PRECISION NOT NULL,
"created_at" TIMESTAMP(3) NOT NULL DEFAULT CURRENT_TIMESTAMP,
"updated_at" TIMESTAMP(3) NOT NULL DEFAULT CURRENT_TIMESTAMP,
CONSTRAINT "CryptoHistory_pkey" PRIMARY KEY ("id")
);
-- AddForeignKey
ALTER TABLE "CryptoHistory" ADD CONSTRAINT "CryptoHistory_id_crypto_fkey" FOREIGN KEY ("id_crypto") REFERENCES "Crypto"("id") ON DELETE RESTRICT ON UPDATE CASCADE;

View File

@ -19,8 +19,17 @@ model Crypto {
UserHasCrypto UserHasCrypto[]
Trade Trade[]
Offer Offer[]
CryptoHistory CryptoHistory[]
}
model CryptoHistory {
id String @id @default(uuid())
id_crypto String
value Float
created_at DateTime @default(now())
updated_at DateTime @updatedAt @default(now())
Crypto Crypto @relation(fields: [id_crypto], references: [id])
}
model Offer {

View File

@ -33,6 +33,11 @@ export class CryptoController {
return this.promoService.searchCryptos(user.id, cryptoName);
}
@Get('/history/:id')
CryptoHistory(@GetUser() user: User, @Param('id') cryptoId: string) {
return this.promoService.getCryptoHistory(user.id, cryptoId);
}
@HttpCode(HttpStatus.CREATED)
@Post('/create')
createPromoCode(

View File

@ -32,6 +32,23 @@ export class CryptoService {
});
}
async getCryptoHistory(userId: string, cryptoId: string) {
await checkUserHasAccount(userId);
if (cryptoId) {
return this.prisma.crypto.findMany({
where: {
id: cryptoId,
},
orderBy: {
created_at: 'desc',
},
});
} else {
throw new ForbiddenException('Crypto UUID required');
}
}
async createCrypto(userId: string, dto: CryptoDto) {
await checkuserIsAdmin(userId);
@ -103,6 +120,13 @@ export class CryptoService {
}
}
const newCryptoValue = crypto.value * 1.1;
await this.prisma.cryptoHistory.create({
data: {
id_crypto: crypto.id,
value: newCryptoValue,
},
});
return this.prisma.crypto.update({
where: {
id: dto.id_crypto,
@ -111,8 +135,6 @@ export class CryptoService {
value: newCryptoValue,
},
});
return crypto;
}
async editCryptoById(userId: string, cryptoId: string, dto: CryptoDto) {
await checkuserIsAdmin(userId);

View File

@ -117,6 +117,12 @@ export class TradeService {
}
const newValue = crypto.value * 1.1;
await this.prisma.cryptoHistory.create({
data: {
id_crypto: crypto.id,
value: newValue,
},
});
await this.prisma.crypto.update({
where: {