commit current backend

此提交包含在:
2021-08-01 23:50:59 +08:00
父節點 7880218a0b
當前提交 449ac13557
共有 10 個檔案被更改,包括 366 行新增0 行删除

查看文件

@@ -0,0 +1,5 @@
require('dotenv').config()
let appConfig = {
port: parseInt(process.env.SL_PORT as any)
}
export {appConfig}

查看文件

@@ -0,0 +1,27 @@
import {IExercise, ITextbook, IToc} from "./types"
export function fillIToc(single: any) {
return <IToc>{
section: single.section,
count: single.count,
pageBegin: single.pageBegin,
pageEnd: single.pageEnd
}
}
export function fillITextbook(single: any) {
return <ITextbook>{
title: single.title,
quantity: single.quantity
}
}
export function fillIExercise(single: any) {
return <IExercise>{
exercise: single.exercise,
section: single.section,
part: single.part,
page: single.page,
html: single.html
}
}

查看文件

@@ -0,0 +1,36 @@
import {validationResult} from "express-validator"
import {IResultJson} from "./types"
import express from "express"
import Database from "better-sqlite3"
import path from "path"
let db = Database(path.join(__dirname, '../slader.db'), {verbose: message => console.log('SQLITE3: ' + message)})
export {db}
export const resultJson = {
success(data: any) {
return <IResultJson>{
status: true,
data: data
}
},
error(data: any) {
return <IResultJson>{
status: false,
data: data
}
}
}
export function hasValidationErrors(req: express.Request, res: express.Response) {
let errors = validationResult(req)
if (!errors.isEmpty()) {
res.json(resultJson.error(errors.array()))
return true
}
return false
}
export function getTimestampInSeconds() {
return Math.floor(Date.now() / 1000)
}

查看文件

@@ -0,0 +1,13 @@
import express from 'express'
import cors from 'cors'
import {textbookRouter} from "./routers/textbook-router"
import {appConfig} from "./config"
let app = express()
app.use(cors())
app.use('/textbook', textbookRouter)
app.listen(appConfig.port, () => {
console.log('Server has started at port ' + appConfig.port)
})

查看文件

@@ -0,0 +1,65 @@
import 'reflect-metadata'
import {Service} from "typedi"
import {db} from "../includes"
import {fillIExercise, fillITextbook, fillIToc} from "../entity-fill"
import {text} from "express"
@Service()
export class TextbookModel {
existTextbook(textbook: string) {
let raw = db.prepare('select rowid from solutions where textbook=? limit 1').get(textbook)
return !!raw?.rowid
}
existSection(textbook: string, section: string) {
let raw = db.prepare('select rowid from solutions where textbook=? and section=? limit 1').get(textbook, section)
return !!raw?.rowid
}
existExercise(textbook: string, section: string, exercise: string) {
let raw = db.prepare('select rowid from solutions ' +
'where textbook=? and section=? and exercise=? limit 1').get(textbook, section, exercise)
return !!raw?.rowid
}
getTextbooks() {
let raw = db.prepare('SELECT textbook as title,count(rowid) as quantity from solutions GROUP BY textbook').all()
return raw.map(value => fillITextbook(value))
}
findExercisesByTextbookSection(textbook: string, section: string) {
let raw = db.prepare('SELECT exercise,section,part,page,html ' +
'from solutions ' +
'where textbook=? ' +
'and section=? order by exercise asc').all(textbook, section)
return raw.map(value => fillIExercise(value))
}
findExercise(textbook: string, section: string, exercise: string) {
let raw = db.prepare('SELECT exercise,section,part,page,html ' +
'from solutions ' +
'where textbook=? ' +
'and section=? and exercise=?').get(textbook, section, exercise)
return fillIExercise(raw)
}
findExercisesByTextbookPage(textbook: string, page: number) {
let raw = db.prepare('SELECT exercise,section,part,page,html ' +
'from solutions ' +
'where textbook=? ' +
'and page=? order by exercise asc').all(textbook, page)
return raw.map(value => fillIExercise(value))
}
findTocByTextbook(textbook: string) {
let raw = db.prepare('SELECT section, ' +
'count(rowid) as count, ' +
'min(page) as pageBegin, ' +
'max(page) as pageEnd ' +
'FROM "solutions" ' +
'where textbook=? ' +
'group by section ' +
'order by pageBegin asc').all(textbook)
return raw.map(value => fillIToc(value))
}
}

查看文件

@@ -0,0 +1,53 @@
import 'reflect-metadata'
import express from "express"
import {param, query} from "express-validator"
import {Container} from "typedi"
import {TextbookModel} from "../models/textbook-model"
import expressAsyncHandler from "express-async-handler"
import {TextbookService} from "../services/textbook-service"
import {hasValidationErrors} from "../includes"
let textbookRouter = express.Router()
let textbookModel = Container.get(TextbookModel)
let textbookService = Container.get(TextbookService)
textbookRouter.get('/',
expressAsyncHandler(async (req: express.Request, res: express.Response) => {
res.json(await textbookService.listTextbooks())
})
)
textbookRouter.get('/:textbook/:section?/:exercise?',
param('textbook').notEmpty().bail().custom(input => {
if (!textbookModel.existTextbook(input)) {
throw new Error('textbook not exist')
}
return true
}),
query('page').optional().isInt({min: 1}),
param('section').optional().notEmpty().bail().custom((input, {req}) => {
if (!textbookModel.existSection(req.params!.textbook, input)) {
throw new Error('section not exist')
}
return true
}),
param('exercise').optional().notEmpty().bail().custom((input, {req}) => {
if (!textbookModel.existExercise(req.params!.textbook, req.params!.section, input)) {
throw new Error('exercise not exist')
}
return true
}),
expressAsyncHandler(async (req: express.Request, res: express.Response) => {
if (hasValidationErrors(req, res)) return
if (req.query.page) {
res.json(await textbookService.getPage(req.params.textbook, Number(req.query.page)))
} else if (req.params.exercise) {
res.json(await textbookService.getExercise(req.params.textbook, req.params.section, req.params.exercise))
} else if (req.params.section) {
res.json(await textbookService.getSection(req.params.textbook, req.params.section))
} else {
res.json(await textbookService.getTextbook(req.params.textbook))
}
})
)
export {textbookRouter}

查看文件

@@ -0,0 +1,30 @@
import 'reflect-metadata'
import {Inject, Service} from "typedi"
import {TextbookModel} from "../models/textbook-model"
import {resultJson} from "../includes"
@Service()
export class TextbookService {
@Inject()
textbookModel!: TextbookModel
async getTextbook(textbook: string) {
return resultJson.success(this.textbookModel.findTocByTextbook(textbook))
}
async getSection(textbook: string, section: string) {
return resultJson.success(this.textbookModel.findExercisesByTextbookSection(textbook, section))
}
async getExercise(textbook: string, section: string, exercise: string) {
return resultJson.success(this.textbookModel.findExercise(textbook, section, exercise))
}
async getPage(textbook: string, page: number) {
return resultJson.success(this.textbookModel.findExercisesByTextbookPage(textbook, page))
}
async listTextbooks() {
return resultJson.success(this.textbookModel.getTextbooks())
}
}

查看文件

@@ -0,0 +1,24 @@
export interface IResultJson {
status: boolean,
data: any
}
export interface IToc {
section: string,
count: number,
pageBegin: number,
pageEnd: number
}
export interface ITextbook {
title: string,
quantity: number
}
export interface IExercise {
exercise: string,
section: string,
part: string,
page: number,
html: string
}