diff --git a/prisma/migrations/20240607134613_/migration.sql b/prisma/migrations/20240607134613_/migration.sql new file mode 100644 index 0000000..0d2823f --- /dev/null +++ b/prisma/migrations/20240607134613_/migration.sql @@ -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; diff --git a/prisma/schema.prisma b/prisma/schema.prisma index 4448e2a..6bc640a 100644 --- a/prisma/schema.prisma +++ b/prisma/schema.prisma @@ -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 { diff --git a/src/crypto/crypto.controller.ts b/src/crypto/crypto.controller.ts index 46ba85d..8415d9b 100644 --- a/src/crypto/crypto.controller.ts +++ b/src/crypto/crypto.controller.ts @@ -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( diff --git a/src/crypto/crypto.service.ts b/src/crypto/crypto.service.ts index 304b637..293b6f6 100644 --- a/src/crypto/crypto.service.ts +++ b/src/crypto/crypto.service.ts @@ -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); diff --git a/src/trade/trade.service.ts b/src/trade/trade.service.ts index eb79761..fd94a8e 100644 --- a/src/trade/trade.service.ts +++ b/src/trade/trade.service.ts @@ -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: {