Skip to content

Commit 67a5676

Browse files
committed
add price resource
1 parent ddf5edb commit 67a5676

File tree

9 files changed

+141
-1
lines changed

9 files changed

+141
-1
lines changed

src/app.module.ts

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -3,7 +3,7 @@ import { ConfigModule, ConfigService } from '@nestjs/config';
33

44
import { TypeOrmModule } from '@nestjs/typeorm';
55
import configuration from './config/configuration';
6-
6+
import { PriceModule } from './price/price.module';
77
@Module({
88
imports: [
99
ConfigModule.forRoot({
@@ -24,6 +24,7 @@ import configuration from './config/configuration';
2424
}),
2525
inject: [ConfigService],
2626
}),
27+
PriceModule,
2728
],
2829
})
2930
export class AppModule {}

src/price/dto/create-price.dto.ts

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
export class CreatePriceDto {}

src/price/dto/update-price.dto.ts

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,4 @@
1+
import { PartialType } from '@nestjs/swagger';
2+
import { CreatePriceDto } from './create-price.dto';
3+
4+
export class UpdatePriceDto extends PartialType(CreatePriceDto) {}

src/price/entities/price.entity.ts

Lines changed: 21 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,21 @@
1+
import {
2+
Entity,
3+
Column,
4+
PrimaryGeneratedColumn,
5+
CreateDateColumn,
6+
} from 'typeorm';
7+
8+
@Entity()
9+
export class Price {
10+
@PrimaryGeneratedColumn()
11+
id: number;
12+
13+
@Column()
14+
chain: string;
15+
16+
@Column('decimal', { precision: 10, scale: 2 })
17+
price: number;
18+
19+
@CreateDateColumn()
20+
timestamp: Date;
21+
}

src/price/price.controller.spec.ts

Lines changed: 26 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,26 @@
1+
import { Test, TestingModule } from '@nestjs/testing';
2+
import { PriceController } from './price.controller';
3+
import { PriceService } from './price.service';
4+
5+
describe('PriceController', () => {
6+
let controller: PriceController;
7+
8+
beforeEach(async () => {
9+
const module: TestingModule = await Test.createTestingModule({
10+
controllers: [PriceController],
11+
providers: [
12+
PriceService,
13+
{
14+
provide: 'PriceRepository',
15+
useValue: {}, // Mock implementation of PriceRepository
16+
},
17+
],
18+
}).compile();
19+
20+
controller = module.get<PriceController>(PriceController);
21+
});
22+
23+
it('should be defined', () => {
24+
expect(controller).toBeDefined();
25+
});
26+
});

src/price/price.controller.ts

Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,15 @@
1+
import { Controller, Get } from '@nestjs/common';
2+
import { PriceService } from './price.service';
3+
import { ApiTags, ApiOperation } from '@nestjs/swagger';
4+
5+
@ApiTags('prices')
6+
@Controller('prices')
7+
export class PriceController {
8+
constructor(private readonly priceService: PriceService) {}
9+
10+
@Get('last-24-hours')
11+
@ApiOperation({ summary: 'Get prices for the last 24 hours' })
12+
async getPricesLast24Hours() {
13+
return this.priceService.getPricesLast24Hours();
14+
}
15+
}

src/price/price.module.ts

Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,13 @@
1+
import { Module } from '@nestjs/common';
2+
import { TypeOrmModule } from '@nestjs/typeorm';
3+
import { PriceService } from './price.service';
4+
import { PriceController } from './price.controller';
5+
import { Price } from './entities/price.entity';
6+
7+
@Module({
8+
imports: [TypeOrmModule.forFeature([Price])],
9+
providers: [PriceService],
10+
controllers: [PriceController],
11+
exports: [PriceService],
12+
})
13+
export class PriceModule {}

src/price/price.service.spec.ts

Lines changed: 18 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,18 @@
1+
import { Test, TestingModule } from '@nestjs/testing';
2+
import { PriceService } from './price.service';
3+
4+
describe('PriceService', () => {
5+
let service: PriceService;
6+
7+
beforeEach(async () => {
8+
const module: TestingModule = await Test.createTestingModule({
9+
providers: [PriceService],
10+
}).compile();
11+
12+
service = module.get<PriceService>(PriceService);
13+
});
14+
15+
it('should be defined', () => {
16+
expect(service).toBeDefined();
17+
});
18+
});

src/price/price.service.ts

Lines changed: 41 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,41 @@
1+
import { Injectable } from '@nestjs/common';
2+
import { InjectRepository } from '@nestjs/typeorm';
3+
import { MoreThanOrEqual, Repository } from 'typeorm';
4+
import { Price } from './entities/price.entity';
5+
import { ConfigService } from '@nestjs/config';
6+
7+
@Injectable()
8+
export class PriceService {
9+
constructor(
10+
@InjectRepository(Price)
11+
private priceRepository: Repository<Price>,
12+
private configService: ConfigService,
13+
) {}
14+
15+
async savePrices() {
16+
//TODO
17+
// Implement Moralis API call to get prices
18+
// Save prices to the database
19+
}
20+
21+
async getPricesLastHour(chain: string): Promise<Price[]> {
22+
const oneHourAgo = new Date(Date.now() - 60 * 60 * 1000);
23+
return this.priceRepository.find({
24+
where: {
25+
chain,
26+
timestamp: MoreThanOrEqual(oneHourAgo),
27+
},
28+
order: { timestamp: 'DESC' },
29+
});
30+
}
31+
32+
async getPricesLast24Hours(): Promise<Price[]> {
33+
const oneDayAgo = new Date(Date.now() - 24 * 60 * 60 * 1000);
34+
return this.priceRepository.find({
35+
where: {
36+
timestamp: MoreThanOrEqual(oneDayAgo),
37+
},
38+
order: { timestamp: 'DESC' },
39+
});
40+
}
41+
}

0 commit comments

Comments
 (0)