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

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 { Times } from "../entity/Times";
import { NextFunction, Request, Response } from "express"
import { Web } from "../entity/Web";
export class HotTopController {
private TimesRepository = AppDataSource.getRepository(Times)
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 yestDay = new Date()
let hours = 1
let rawHours = parseInt(request.query.hours as unknown as string)
if (request.query.hours !=undefined && !isNaN(rawHours) && rawHours <= 24 && rawHours > 0) {
@ -34,16 +44,19 @@ export class HotTopController {
// console.log(limit,hours)
// return "OK"
return this.TimesRepository
.createQueryBuilder("time")
.leftJoinAndSelect("time.fromWeb", "fromWeb")
.where("time.lastTime BETWEEN :beginTime AND :endTime",
.createQueryBuilder("times")
.leftJoinAndSelect("times.fromWeb", "fromWeb")
.where("times.lastTime BETWEEN :beginTime AND :endTime",
{
beginTime: yestDay,
endTime: nowDate
}
)
.addOrderBy('time.lastTime', "DESC")
.addOrderBy('time.times', "DESC")
.andWhere("times.fromWeb = :searchWeb",{
searchWeb:searchWeb.id
})
.addOrderBy('times.lastTime', "DESC")
.addOrderBy('times.times', "DESC")
.limit(limit)
// .getSql()
.getMany()

View File

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

View File

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