diff --git a/.gitignore b/.gitignore index b3ab1ae..984bbd5 100644 --- a/.gitignore +++ b/.gitignore @@ -3,4 +3,5 @@ node_modules/ build/ tmp/ -temp/ \ No newline at end of file +temp/ +./build \ No newline at end of file diff --git a/package.json b/package.json index a28c770..808d098 100644 --- a/package.json +++ b/package.json @@ -17,6 +17,7 @@ }, "scripts": { "start": "ts-node src/index.ts", - "typeorm": "typeorm-ts-node-commonjs" + "typeorm": "typeorm-ts-node-commonjs", + "serve": "npm i && npx tsc && node ./build/index.js" } } \ No newline at end of file diff --git a/src/controller/HotTopController.ts b/src/controller/HotTopController.ts new file mode 100644 index 0000000..f843c88 --- /dev/null +++ b/src/controller/HotTopController.ts @@ -0,0 +1,30 @@ +import { AppDataSource } from "../data-source"; +import { Times } from "../entity/Times"; +import { NextFunction, Request, Response } from "express" + +export class HotTopController{ + private TimesRepository = AppDataSource.getRepository(Times) + async all(request: Request, response: Response, next: NextFunction){ + const nowDate = new Date() + const yestDay = new Date() + yestDay.setDate(yestDay.getDate()-1) + // console.log(nowDate.toLocaleString()) + let num:number + if(request.params.num) num = parseInt(request.params.num) + // console.log(id) + if(isNaN(num) || num>50) num = 50 + // return "OK" + return this.TimesRepository + .createQueryBuilder("time") + .leftJoinAndSelect("time.fromWeb", "fromWeb") + .where("time.lastTime BETWEEN :beginTime AND :endTime", + { + beginTime:yestDay, + endTime:nowDate + } + ) + .addOrderBy('time.times',"DESC") + .limit(num) + .getMany() + } +} \ No newline at end of file diff --git a/src/controller/UserController.ts b/src/controller/UserController.ts index 339a75b..d9e5741 100644 --- a/src/controller/UserController.ts +++ b/src/controller/UserController.ts @@ -1,6 +1,7 @@ +/* import { AppDataSource } from "../data-source" import { NextFunction, Request, Response } from "express" -import { User } from "../entity/User" + export class UserController { @@ -50,4 +51,5 @@ export class UserController { return "user has been removed" } -} \ No newline at end of file +} +*/ \ No newline at end of file diff --git a/src/data-source.ts b/src/data-source.ts index 506fb2d..64bf0d7 100644 --- a/src/data-source.ts +++ b/src/data-source.ts @@ -1,17 +1,35 @@ import "reflect-metadata" import { DataSource } from "typeorm" -import { User } from "./entity/User" +import { Search } from "./entity/Search" +import { Hot } from "./entity/Hot" +import { Web } from "./entity/Web" +import { Times } from "./entity/Times" +import { readFileSync } from "fs" +import path = require("path") +// import { main } from "." export const AppDataSource = new DataSource({ type: "mysql", host: "localhost", port: 3306, - username: "test", - password: "test", - database: "test", + username: "root", + password: "ZengtudorRXR2008", + // password: "Zengtudor", + database: "hot", synchronize: true, logging: false, - entities: [User], + entities: [Search,Hot,Web,Times], migrations: [], subscribers: [], }) + + +// export const AppDataSource = new DataSource({ +// type: "sqlite", +// database: "./hot.sqlite", +// synchronize: true, +// logging: false, +// entities: [Search,Hot,Web,Times], +// migrations: [], +// subscribers: [], +// }) diff --git a/src/entity/Hot.ts b/src/entity/Hot.ts new file mode 100644 index 0000000..2a86365 --- /dev/null +++ b/src/entity/Hot.ts @@ -0,0 +1,15 @@ +import { Column, Entity, ManyToOne, PrimaryGeneratedColumn } from "typeorm"; +import { Search } from "./Search"; +import { Times } from "./Times"; + +@Entity() +export class Hot{ + @PrimaryGeneratedColumn() + id:number + @Column() + word:string + @ManyToOne(()=>Search,(search)=>search.Hots) + fromSearch:Search + @ManyToOne((type)=>Times,(time)=>time.Hots) + fromTimes +} \ No newline at end of file diff --git a/src/entity/Search.ts b/src/entity/Search.ts new file mode 100644 index 0000000..fb8c2f6 --- /dev/null +++ b/src/entity/Search.ts @@ -0,0 +1,15 @@ +import { Column, Entity, ManyToOne, OneToMany, PrimaryGeneratedColumn } from "typeorm"; +import { Hot } from "./Hot"; +import { Web } from "./Web"; + +@Entity() +export class Search{ + @PrimaryGeneratedColumn() + id:number + @OneToMany((type)=>Hot,(hot)=>hot.fromSearch) + Hots:Hot[] + @ManyToOne((type)=>Web,(web)=>web.Searches) + fromWeb + @Column() + date:Date +} \ No newline at end of file diff --git a/src/entity/Times.ts b/src/entity/Times.ts new file mode 100644 index 0000000..40e0bd5 --- /dev/null +++ b/src/entity/Times.ts @@ -0,0 +1,19 @@ +import { Column, Entity, ManyToOne, OneToMany, OneToOne, PrimaryGeneratedColumn } from "typeorm"; +import { Hot } from "./Hot"; +import { Web } from "./Web"; + +@Entity() +export class Times{ + @PrimaryGeneratedColumn() + id:number + @OneToMany((type)=>Hot,(hot)=>hot.fromTimes) + Hots:Hot[] + @Column() + times:number + @Column() + word:string + @ManyToOne(()=>Web,(web)=>web.times) + fromWeb:Web + @Column() + lastTime:Date +} \ No newline at end of file diff --git a/src/entity/User.ts b/src/entity/User.ts deleted file mode 100644 index d26ad0a..0000000 --- a/src/entity/User.ts +++ /dev/null @@ -1,18 +0,0 @@ -import { Entity, PrimaryGeneratedColumn, Column } from "typeorm" - -@Entity() -export class User { - - @PrimaryGeneratedColumn() - id: number - - @Column() - firstName: string - - @Column() - lastName: string - - @Column() - age: number - -} diff --git a/src/entity/Web.ts b/src/entity/Web.ts new file mode 100644 index 0000000..b06d143 --- /dev/null +++ b/src/entity/Web.ts @@ -0,0 +1,19 @@ +import { Column, Entity, OneToMany, PrimaryGeneratedColumn } from "typeorm"; +import { Search } from "./Search"; +import { Times } from "./Times"; + +@Entity() +export class Web{ + @PrimaryGeneratedColumn() + id:number + @OneToMany((type)=>Search,(search)=>search.fromWeb) + Searches:Search[] + @Column() + name:string + @Column() + fromUrl:string + @Column() + searchUrl:string + @OneToMany(()=>Times,(times)=>times.fromWeb) + times:Times[] +} \ No newline at end of file diff --git a/src/index.ts b/src/index.ts index df4cdf1..0771786 100644 --- a/src/index.ts +++ b/src/index.ts @@ -3,7 +3,6 @@ import * as bodyParser from "body-parser" import { Request, Response } from "express" import { AppDataSource } from "./data-source" import { Routes } from "./routes" -import { User } from "./entity/User" AppDataSource.initialize().then(async () => { @@ -28,25 +27,25 @@ AppDataSource.initialize().then(async () => { // ... // start express server - app.listen(3000) + app.listen(3501) // insert new users for test - await AppDataSource.manager.save( - AppDataSource.manager.create(User, { - firstName: "Timber", - lastName: "Saw", - age: 27 - }) - ) + // await AppDataSource.manager.save( + // AppDataSource.manager.create(User, { + // firstName: "Timber", + // lastName: "Saw", + // age: 27 + // }) + // ) - await AppDataSource.manager.save( - AppDataSource.manager.create(User, { - firstName: "Phantom", - lastName: "Assassin", - age: 24 - }) - ) + // await AppDataSource.manager.save( + // AppDataSource.manager.create(User, { + // firstName: "Phantom", + // lastName: "Assassin", + // age: 24 + // }) + // ) - console.log("Express server has started on port 3000. Open http://localhost:3000/users to see results") + console.log("Express server has started on port 3501. Open http://localhost:3501/ to see results") }).catch(error => console.log(error)) diff --git a/src/routes.ts b/src/routes.ts index 7a69e36..38a87a3 100644 --- a/src/routes.ts +++ b/src/routes.ts @@ -1,23 +1,33 @@ -import { UserController } from "./controller/UserController" +import { HotTopController } from "./controller/HotTopController"; -export const Routes = [{ - method: "get", - route: "/users", - controller: UserController, - action: "all" -}, { - method: "get", - route: "/users/:id", - controller: UserController, - action: "one" -}, { - method: "post", - route: "/users", - controller: UserController, - action: "save" -}, { - method: "delete", - route: "/users/:id", - controller: UserController, - action: "remove" -}] \ No newline at end of file + + +export const Routes = [ + { + method: "get", + route: "/hot/top/:num", + controller: HotTopController, + action: "all" + }, + // { + // method: "get", + // route: "/users", + // controller: UserController, + // action: "all" + // }, { + // method: "get", + // route: "/users/:id", + // controller: UserController, + // action: "one" + // }, { + // method: "post", + // route: "/users", + // controller: UserController, + // action: "save" + // }, { + // method: "delete", + // route: "/users/:id", + // controller: UserController, + // action: "remove" + // } +] \ No newline at end of file