增加了获取所有网站的方式

This commit is contained in:
ZtRXR 2023-09-02 01:25:53 +08:00
parent 4b1a0b0078
commit e33430e08c
4 changed files with 38 additions and 8 deletions

View File

@ -0,0 +1,11 @@
import { Request, Response, NextFunction } from "express";
import { AppDataSource } from "../data-source";
import { Web } from "../entity/Web";
export class GetWebsController{
async all(request: Request, response: Response, next: NextFunction) {
AppDataSource.getRepository(Web).find().then((data)=>{
return response.json(data.map(d=>d.name))
})
}
}

View File

@ -1,13 +1,23 @@
import { AppDataSource } from "../data-source"; import { AppDataSource } from "../data-source";
import { Times } from "../entity/Times"; import { Times } from "../entity/Times";
import { NextFunction, Request, Response } from "express" import { NextFunction, Request, Response } from "express"
import { Web } from "../entity/Web";
export class HotTopController { export class HotTopController {
private TimesRepository = AppDataSource.getRepository(Times) private TimesRepository = AppDataSource.getRepository(Times)
async all(request: Request, response: Response, next: NextFunction) { async all(request: Request, response: Response, next: NextFunction) {
const name = request.params.name
const searchWeb = await AppDataSource.getRepository(Web).findOne({
where:{
name:name
}
})
if(searchWeb == null){
response.status(404)
return "error"
}
const nowDate = new Date() const nowDate = new Date()
const yestDay = new Date() const yestDay = new Date()
let hours = 1 let hours = 1
let rawHours = parseInt(request.query.hours as unknown as string) let rawHours = parseInt(request.query.hours as unknown as string)
if (request.query.hours !=undefined && !isNaN(rawHours) && rawHours <= 24 && rawHours > 0) { if (request.query.hours !=undefined && !isNaN(rawHours) && rawHours <= 24 && rawHours > 0) {
@ -34,16 +44,19 @@ export class HotTopController {
// console.log(limit,hours) // console.log(limit,hours)
// return "OK" // return "OK"
return this.TimesRepository return this.TimesRepository
.createQueryBuilder("time") .createQueryBuilder("times")
.leftJoinAndSelect("time.fromWeb", "fromWeb") .leftJoinAndSelect("times.fromWeb", "fromWeb")
.where("time.lastTime BETWEEN :beginTime AND :endTime", .where("times.lastTime BETWEEN :beginTime AND :endTime",
{ {
beginTime: yestDay, beginTime: yestDay,
endTime: nowDate endTime: nowDate
} }
) )
.addOrderBy('time.lastTime', "DESC") .andWhere("times.fromWeb = :searchWeb",{
.addOrderBy('time.times', "DESC") searchWeb:searchWeb.id
})
.addOrderBy('times.lastTime', "DESC")
.addOrderBy('times.times', "DESC")
.limit(limit) .limit(limit)
// .getSql() // .getSql()
.getMany() .getMany()

View File

@ -16,7 +16,6 @@ AppDataSource.initialize().then(async () => {
const result = (new (route.controller as any))[route.action](req, res, next) const result = (new (route.controller as any))[route.action](req, res, next)
if (result instanceof Promise) { if (result instanceof Promise) {
result.then(result => result !== null && result !== undefined ? res.send(result) : undefined) result.then(result => result !== null && result !== undefined ? res.send(result) : undefined)
} else if (result !== null && result !== undefined) { } else if (result !== null && result !== undefined) {
res.json(result) res.json(result)
} }

View File

@ -1,3 +1,4 @@
import { GetWebsController } from "./controller/GetWebsController";
import { HotTopController } from "./controller/HotTopController"; import { HotTopController } from "./controller/HotTopController";
@ -5,10 +6,16 @@ import { HotTopController } from "./controller/HotTopController";
export const Routes = [ export const Routes = [
{ {
method: "get", method: "get",
route: "/hot/top/all", route: "/hot/top/:name",
controller: HotTopController, controller: HotTopController,
action: "all" action: "all"
}, },
{
method: "get",
route: "/hot/webs",
controller: GetWebsController,
action: "all"
},
// { // {
// method: "get", // method: "get",
// route: "/users", // route: "/users",