第一次提交
This commit is contained in:
13
src/bids/bids.module.ts
Normal file
13
src/bids/bids.module.ts
Normal file
@@ -0,0 +1,13 @@
|
||||
import { Module } from '@nestjs/common';
|
||||
import { TypeOrmModule } from '@nestjs/typeorm';
|
||||
import { BidItem } from './entities/bid-item.entity';
|
||||
import { BidsService } from './services/bid.service';
|
||||
import { BidsController } from './controllers/bid.controller';
|
||||
|
||||
@Module({
|
||||
imports: [TypeOrmModule.forFeature([BidItem])],
|
||||
providers: [BidsService],
|
||||
controllers: [BidsController],
|
||||
exports: [BidsService],
|
||||
})
|
||||
export class BidsModule {}
|
||||
17
src/bids/controllers/bid.controller.ts
Normal file
17
src/bids/controllers/bid.controller.ts
Normal file
@@ -0,0 +1,17 @@
|
||||
import { Controller, Get, Query } from '@nestjs/common';
|
||||
import { BidsService } from '../services/bid.service';
|
||||
|
||||
@Controller('api/bids')
|
||||
export class BidsController {
|
||||
constructor(private readonly bidsService: BidsService) {}
|
||||
|
||||
@Get()
|
||||
findAll(@Query() query: any) {
|
||||
return this.bidsService.findAll(query);
|
||||
}
|
||||
|
||||
@Get('high-priority')
|
||||
getHighPriority() {
|
||||
return this.bidsService.getHighPriorityCorrected();
|
||||
}
|
||||
}
|
||||
28
src/bids/entities/bid-item.entity.ts
Normal file
28
src/bids/entities/bid-item.entity.ts
Normal file
@@ -0,0 +1,28 @@
|
||||
import { Entity, PrimaryGeneratedColumn, Column, CreateDateColumn, UpdateDateColumn } from 'typeorm';
|
||||
|
||||
@Entity('bid_items')
|
||||
export class BidItem {
|
||||
@PrimaryGeneratedColumn('uuid')
|
||||
id: string;
|
||||
|
||||
@Column()
|
||||
title: string;
|
||||
|
||||
@Column()
|
||||
url: string;
|
||||
|
||||
@Column({ type: 'datetime' })
|
||||
publishDate: Date;
|
||||
|
||||
@Column()
|
||||
source: string;
|
||||
|
||||
@Column({ default: false })
|
||||
isRead: boolean;
|
||||
|
||||
@CreateDateColumn()
|
||||
createdAt: Date;
|
||||
|
||||
@UpdateDateColumn()
|
||||
updatedAt: Date;
|
||||
}
|
||||
68
src/bids/services/bid.service.ts
Normal file
68
src/bids/services/bid.service.ts
Normal file
@@ -0,0 +1,68 @@
|
||||
import { Injectable } from '@nestjs/common';
|
||||
import { InjectRepository } from '@nestjs/typeorm';
|
||||
import { Repository, LessThan } from 'typeorm';
|
||||
import { BidItem } from '../entities/bid-item.entity';
|
||||
|
||||
@Injectable()
|
||||
export class BidsService {
|
||||
constructor(
|
||||
@InjectRepository(BidItem)
|
||||
private bidRepository: Repository<BidItem>,
|
||||
) {}
|
||||
|
||||
async findAll(query?: any) {
|
||||
const { page = 1, limit = 10, source, keyword } = query || {};
|
||||
const qb = this.bidRepository.createQueryBuilder('bid');
|
||||
|
||||
if (source) {
|
||||
qb.andWhere('bid.source = :source', { source });
|
||||
}
|
||||
|
||||
if (keyword) {
|
||||
qb.andWhere('bid.title LIKE :keyword', { keyword: `%${keyword}%` });
|
||||
}
|
||||
|
||||
qb.orderBy('bid.publishDate', 'DESC')
|
||||
.skip((page - 1) * limit)
|
||||
.take(limit);
|
||||
|
||||
const [items, total] = await qb.getManyAndCount();
|
||||
return { items, total };
|
||||
}
|
||||
|
||||
getHighPriority() {
|
||||
return this.bidRepository.find({
|
||||
where: { priority: LessThan(0) }, // This is just a placeholder logic, priority should be > 0
|
||||
order: { priority: 'DESC', publishDate: 'DESC' },
|
||||
take: 10,
|
||||
});
|
||||
}
|
||||
|
||||
// Update logic for priority
|
||||
async getHighPriorityCorrected() {
|
||||
return this.bidRepository.createQueryBuilder('bid')
|
||||
.where('bid.priority > 0')
|
||||
.orderBy('bid.priority', 'DESC')
|
||||
.addOrderBy('bid.publishDate', 'DESC')
|
||||
.limit(10)
|
||||
.getMany();
|
||||
}
|
||||
|
||||
async createOrUpdate(data: Partial<BidItem>) {
|
||||
// Use URL or a hash of URL to check for duplicates
|
||||
let item = await this.bidRepository.findOne({ where: { url: data.url } });
|
||||
if (item) {
|
||||
Object.assign(item, data);
|
||||
return this.bidRepository.save(item);
|
||||
}
|
||||
return this.bidRepository.save(data);
|
||||
}
|
||||
|
||||
async cleanOldData() {
|
||||
const thirtyDaysAgo = new Date();
|
||||
thirtyDaysAgo.setDate(thirtyDaysAgo.getDate() - 30);
|
||||
return this.bidRepository.delete({
|
||||
createdAt: LessThan(thirtyDaysAgo),
|
||||
});
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user