💥
This commit is contained in:
@@ -1,3 +0,0 @@
|
|||||||
{
|
|
||||||
"extends": "next/core-web-vitals"
|
|
||||||
}
|
|
||||||
6
Caddyfile
Normal file
6
Caddyfile
Normal file
@@ -0,0 +1,6 @@
|
|||||||
|
:8082 {
|
||||||
|
route {
|
||||||
|
reverse_proxy /api/* localhost:8081
|
||||||
|
reverse_proxy localhost:3000
|
||||||
|
}
|
||||||
|
}
|
||||||
34
README.md
34
README.md
@@ -1,34 +0,0 @@
|
|||||||
This is a [Next.js](https://nextjs.org/) project bootstrapped with [`create-next-app`](https://github.com/vercel/next.js/tree/canary/packages/create-next-app).
|
|
||||||
|
|
||||||
## Getting Started
|
|
||||||
|
|
||||||
First, run the development server:
|
|
||||||
|
|
||||||
```bash
|
|
||||||
npm run dev
|
|
||||||
# or
|
|
||||||
yarn dev
|
|
||||||
```
|
|
||||||
|
|
||||||
Open [http://localhost:3000](http://localhost:3000) with your browser to see the result.
|
|
||||||
|
|
||||||
You can start editing the page by modifying `pages/index.js`. The page auto-updates as you edit the file.
|
|
||||||
|
|
||||||
[API routes](https://nextjs.org/docs/api-routes/introduction) can be accessed on [http://localhost:3000/api/hello](http://localhost:3000/api/hello). This endpoint can be edited in `pages/api/hello.js`.
|
|
||||||
|
|
||||||
The `pages/api` directory is mapped to `/api/*`. Files in this directory are treated as [API routes](https://nextjs.org/docs/api-routes/introduction) instead of React pages.
|
|
||||||
|
|
||||||
## Learn More
|
|
||||||
|
|
||||||
To learn more about Next.js, take a look at the following resources:
|
|
||||||
|
|
||||||
- [Next.js Documentation](https://nextjs.org/docs) - learn about Next.js features and API.
|
|
||||||
- [Learn Next.js](https://nextjs.org/learn) - an interactive Next.js tutorial.
|
|
||||||
|
|
||||||
You can check out [the Next.js GitHub repository](https://github.com/vercel/next.js/) - your feedback and contributions are welcome!
|
|
||||||
|
|
||||||
## Deploy on Vercel
|
|
||||||
|
|
||||||
The easiest way to deploy your Next.js app is to use the [Vercel Platform](https://vercel.com/new?utm_medium=default-template&filter=next.js&utm_source=create-next-app&utm_campaign=create-next-app-readme) from the creators of Next.js.
|
|
||||||
|
|
||||||
Check out our [Next.js deployment documentation](https://nextjs.org/docs/deployment) for more details.
|
|
||||||
173
db/db.go
Normal file
173
db/db.go
Normal file
@@ -0,0 +1,173 @@
|
|||||||
|
package db
|
||||||
|
|
||||||
|
import (
|
||||||
|
"database/sql"
|
||||||
|
"log"
|
||||||
|
"os"
|
||||||
|
"time"
|
||||||
|
|
||||||
|
_ "github.com/lib/pq"
|
||||||
|
)
|
||||||
|
|
||||||
|
type Timetable struct {
|
||||||
|
ID int64 `json:"id"`
|
||||||
|
Name string `json:"name"`
|
||||||
|
Status bool `json:"status"`
|
||||||
|
Created time.Time `json:"created"`
|
||||||
|
}
|
||||||
|
|
||||||
|
type TimeSlot struct {
|
||||||
|
ID int64 `json:"id"`
|
||||||
|
TTID int64 `json:"ttid"`
|
||||||
|
Name string `json:"name"`
|
||||||
|
Time string `json:"time"`
|
||||||
|
Capacity int64 `json:"capacity"`
|
||||||
|
Take int64 `json:"take"`
|
||||||
|
Created time.Time `json:"Created"`
|
||||||
|
Success bool `json:"success"`
|
||||||
|
}
|
||||||
|
|
||||||
|
type Take struct {
|
||||||
|
Username string `json:"username"`
|
||||||
|
TTID int64 `json:"ttid"`
|
||||||
|
Created time.Time `json:"created"`
|
||||||
|
}
|
||||||
|
|
||||||
|
var (
|
||||||
|
DB *sql.DB
|
||||||
|
GetAllTimetables *sql.Stmt
|
||||||
|
CreateNewTimetable *sql.Stmt
|
||||||
|
DeleteTimetable *sql.Stmt
|
||||||
|
UpdateTimetableStatus *sql.Stmt
|
||||||
|
|
||||||
|
GetTimeSlotsByTimetable *sql.Stmt
|
||||||
|
CreateNewTimeslot *sql.Stmt
|
||||||
|
DeleteTimeslot *sql.Stmt
|
||||||
|
|
||||||
|
GetTakesByTimeslot *sql.Stmt
|
||||||
|
DeleteTake *sql.Stmt
|
||||||
|
UserTakeTimeslot *sql.Stmt
|
||||||
|
UserUntakeTimeslot *sql.Stmt
|
||||||
|
UpdateTakeCount *sql.Stmt
|
||||||
|
|
||||||
|
CheckTableStatus *sql.Stmt
|
||||||
|
)
|
||||||
|
|
||||||
|
func init() {
|
||||||
|
var err error
|
||||||
|
DB, err = sql.Open("postgres", "postgres://itsc@localhost:5432/itsc?sslmode=disable")
|
||||||
|
if err != nil {
|
||||||
|
log.Fatal(err)
|
||||||
|
}
|
||||||
|
|
||||||
|
if len(os.Args) > 1{
|
||||||
|
if os.Args[1] == "install" {
|
||||||
|
install()
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
GetAllTimetables, err = DB.Prepare(`
|
||||||
|
select id, name, status
|
||||||
|
from timetables
|
||||||
|
order by status desc, created desc
|
||||||
|
|
||||||
|
`)
|
||||||
|
if err != nil {
|
||||||
|
log.Fatal(err)
|
||||||
|
}
|
||||||
|
CreateNewTimetable, err = DB.Prepare(`
|
||||||
|
insert into timetables (name)
|
||||||
|
values ($1)
|
||||||
|
returning id
|
||||||
|
`)
|
||||||
|
if err != nil {
|
||||||
|
log.Fatal(err)
|
||||||
|
}
|
||||||
|
DeleteTimetable, err = DB.Prepare(`
|
||||||
|
delete from timetables
|
||||||
|
where id = $1
|
||||||
|
`)
|
||||||
|
if err != nil {
|
||||||
|
log.Fatal(err)
|
||||||
|
}
|
||||||
|
GetTimeSlotsByTimetable, err = DB.Prepare(`
|
||||||
|
select t.id, t."name" ,t."time" ,t.take, t.capacity, t2."name", t2.status,
|
||||||
|
case sub.username when $2 then true else false end as success
|
||||||
|
from timeslots t
|
||||||
|
join timetables t2 on t.ttid = t2.id
|
||||||
|
left outer join (
|
||||||
|
select username, tsid from takes t3 where username = $2
|
||||||
|
) sub on t.id = sub.tsid
|
||||||
|
where t2.id = $1
|
||||||
|
order by t."name", t."time"
|
||||||
|
`)
|
||||||
|
if err != nil {
|
||||||
|
log.Fatal(err)
|
||||||
|
}
|
||||||
|
CreateNewTimeslot, err = DB.Prepare(`
|
||||||
|
insert into timeslots (ttid, name, time, capacity)
|
||||||
|
values ($1, $2, $3, $4)
|
||||||
|
returning id
|
||||||
|
`)
|
||||||
|
if err != nil {
|
||||||
|
log.Fatal(err)
|
||||||
|
}
|
||||||
|
DeleteTimeslot, err = DB.Prepare(`
|
||||||
|
delete from timeslots
|
||||||
|
where id = $1
|
||||||
|
`)
|
||||||
|
if err != nil {
|
||||||
|
log.Fatal(err)
|
||||||
|
}
|
||||||
|
GetTakesByTimeslot, err = DB.Prepare(`
|
||||||
|
select username, created
|
||||||
|
from takes
|
||||||
|
where tsid = $1
|
||||||
|
`)
|
||||||
|
if err != nil {
|
||||||
|
log.Fatal(err)
|
||||||
|
}
|
||||||
|
DeleteTake, err = DB.Prepare(`
|
||||||
|
delete from takes
|
||||||
|
where tsid = $1 and username = $2
|
||||||
|
`)
|
||||||
|
if err != nil {
|
||||||
|
log.Fatal(err)
|
||||||
|
}
|
||||||
|
UpdateTimetableStatus, err = DB.Prepare(`
|
||||||
|
update timetables
|
||||||
|
set status = $1
|
||||||
|
where id = $2
|
||||||
|
`)
|
||||||
|
if err != nil {
|
||||||
|
log.Fatal(err)
|
||||||
|
}
|
||||||
|
UserTakeTimeslot, err = DB.Prepare(`
|
||||||
|
insert into takes (username, tsid)
|
||||||
|
values ($1, $2)
|
||||||
|
`)
|
||||||
|
if err != nil {
|
||||||
|
log.Fatal(err)
|
||||||
|
}
|
||||||
|
UserUntakeTimeslot, err = DB.Prepare(`
|
||||||
|
delete from takes
|
||||||
|
where username = $1 and tsid = $2
|
||||||
|
`)
|
||||||
|
if err != nil {
|
||||||
|
log.Fatal(err)
|
||||||
|
}
|
||||||
|
UpdateTakeCount, err = DB.Prepare(`
|
||||||
|
update timeslots
|
||||||
|
set take = take + $1
|
||||||
|
where id = $2
|
||||||
|
`)
|
||||||
|
if err != nil {
|
||||||
|
log.Fatal(err)
|
||||||
|
}
|
||||||
|
CheckTableStatus, err = DB.Prepare(`
|
||||||
|
select status from timetables where id = $1
|
||||||
|
`)
|
||||||
|
if err != nil {
|
||||||
|
log.Fatal(err)
|
||||||
|
}
|
||||||
|
}
|
||||||
66
db/install.go
Normal file
66
db/install.go
Normal file
@@ -0,0 +1,66 @@
|
|||||||
|
package db
|
||||||
|
|
||||||
|
import "log"
|
||||||
|
|
||||||
|
func install() {
|
||||||
|
var err error
|
||||||
|
log.Println("Installing tables")
|
||||||
|
tx, err := DB.Begin()
|
||||||
|
if err != nil{
|
||||||
|
log.Fatal(err)
|
||||||
|
tx.Rollback()
|
||||||
|
}
|
||||||
|
|
||||||
|
_, err = tx.Exec(`
|
||||||
|
CREATE TABLE timetables (
|
||||||
|
id serial primary key,
|
||||||
|
"name" text NOT NULL,
|
||||||
|
status bool NOT NULL DEFAULT false,
|
||||||
|
created timestamp NOT NULL DEFAULT now()
|
||||||
|
);
|
||||||
|
`)
|
||||||
|
if err != nil{
|
||||||
|
log.Fatal(err)
|
||||||
|
tx.Rollback()
|
||||||
|
}
|
||||||
|
|
||||||
|
_, err = tx.Exec(`
|
||||||
|
CREATE TABLE timeslots (
|
||||||
|
id serial primary key,
|
||||||
|
ttid integer not null references timetables(id),
|
||||||
|
"name" text NOT NULL,
|
||||||
|
"time" text NOT NULL,
|
||||||
|
capacity integer NOT NULL DEFAULT 1,
|
||||||
|
take integer NOT NULL DEFAULT 0 check (take <= capacity),
|
||||||
|
created timestamp NOT NULL DEFAULT now()
|
||||||
|
);
|
||||||
|
`)
|
||||||
|
|
||||||
|
|
||||||
|
if err != nil{
|
||||||
|
log.Fatal(err)
|
||||||
|
tx.Rollback()
|
||||||
|
}
|
||||||
|
|
||||||
|
_, err = tx.Exec(`
|
||||||
|
CREATE TABLE takes (
|
||||||
|
username text NOT NULL,
|
||||||
|
tsid integer NOT null references timeslots(id),
|
||||||
|
created timestamp not null default now(),
|
||||||
|
PRIMARY KEY (username, tsid)
|
||||||
|
);
|
||||||
|
`)
|
||||||
|
|
||||||
|
if err != nil{
|
||||||
|
log.Fatal(err)
|
||||||
|
tx.Rollback()
|
||||||
|
}
|
||||||
|
|
||||||
|
err = tx.Commit()
|
||||||
|
if err != nil{
|
||||||
|
log.Fatal(err)
|
||||||
|
tx.Rollback()
|
||||||
|
}
|
||||||
|
|
||||||
|
log.Println("Successfully installed all tables")
|
||||||
|
}
|
||||||
30
go.mod
Normal file
30
go.mod
Normal file
@@ -0,0 +1,30 @@
|
|||||||
|
module itsc
|
||||||
|
|
||||||
|
go 1.19
|
||||||
|
|
||||||
|
require (
|
||||||
|
github.com/gin-gonic/gin v1.8.1
|
||||||
|
github.com/gorilla/websocket v1.5.0
|
||||||
|
github.com/lib/pq v1.10.7
|
||||||
|
)
|
||||||
|
|
||||||
|
require (
|
||||||
|
github.com/gin-contrib/sse v0.1.0 // indirect
|
||||||
|
github.com/go-playground/locales v0.14.0 // indirect
|
||||||
|
github.com/go-playground/universal-translator v0.18.0 // indirect
|
||||||
|
github.com/go-playground/validator/v10 v10.10.0 // indirect
|
||||||
|
github.com/goccy/go-json v0.9.7 // indirect
|
||||||
|
github.com/json-iterator/go v1.1.12 // indirect
|
||||||
|
github.com/leodido/go-urn v1.2.1 // indirect
|
||||||
|
github.com/mattn/go-isatty v0.0.14 // indirect
|
||||||
|
github.com/modern-go/concurrent v0.0.0-20180228061459-e0a39a4cb421 // indirect
|
||||||
|
github.com/modern-go/reflect2 v1.0.2 // indirect
|
||||||
|
github.com/pelletier/go-toml/v2 v2.0.1 // indirect
|
||||||
|
github.com/ugorji/go/codec v1.2.7 // indirect
|
||||||
|
golang.org/x/crypto v0.0.0-20210711020723-a769d52b0f97 // indirect
|
||||||
|
golang.org/x/net v0.0.0-20210226172049-e18ecbb05110 // indirect
|
||||||
|
golang.org/x/sys v0.0.0-20210806184541-e5e7981a1069 // indirect
|
||||||
|
golang.org/x/text v0.3.6 // indirect
|
||||||
|
google.golang.org/protobuf v1.28.0 // indirect
|
||||||
|
gopkg.in/yaml.v2 v2.4.0 // indirect
|
||||||
|
)
|
||||||
90
go.sum
Normal file
90
go.sum
Normal file
@@ -0,0 +1,90 @@
|
|||||||
|
github.com/creack/pty v1.1.9/go.mod h1:oKZEueFk5CKHvIhNR5MUki03XCEU+Q6VDXinZuGJ33E=
|
||||||
|
github.com/davecgh/go-spew v1.1.0/go.mod h1:J7Y8YcW2NihsgmVo/mv3lAwl/skON4iLHjSsI+c5H38=
|
||||||
|
github.com/davecgh/go-spew v1.1.1 h1:vj9j/u1bqnvCEfJOwUhtlOARqs3+rkHYY13jYWTU97c=
|
||||||
|
github.com/davecgh/go-spew v1.1.1/go.mod h1:J7Y8YcW2NihsgmVo/mv3lAwl/skON4iLHjSsI+c5H38=
|
||||||
|
github.com/gin-contrib/sse v0.1.0 h1:Y/yl/+YNO8GZSjAhjMsSuLt29uWRFHdHYUb5lYOV9qE=
|
||||||
|
github.com/gin-contrib/sse v0.1.0/go.mod h1:RHrZQHXnP2xjPF+u1gW/2HnVO7nvIa9PG3Gm+fLHvGI=
|
||||||
|
github.com/gin-gonic/gin v1.8.1 h1:4+fr/el88TOO3ewCmQr8cx/CtZ/umlIRIs5M4NTNjf8=
|
||||||
|
github.com/gin-gonic/gin v1.8.1/go.mod h1:ji8BvRH1azfM+SYow9zQ6SZMvR8qOMZHmsCuWR9tTTk=
|
||||||
|
github.com/go-playground/assert/v2 v2.0.1 h1:MsBgLAaY856+nPRTKrp3/OZK38U/wa0CcBYNjji3q3A=
|
||||||
|
github.com/go-playground/assert/v2 v2.0.1/go.mod h1:VDjEfimB/XKnb+ZQfWdccd7VUvScMdVu0Titje2rxJ4=
|
||||||
|
github.com/go-playground/locales v0.14.0 h1:u50s323jtVGugKlcYeyzC0etD1HifMjqmJqb8WugfUU=
|
||||||
|
github.com/go-playground/locales v0.14.0/go.mod h1:sawfccIbzZTqEDETgFXqTho0QybSa7l++s0DH+LDiLs=
|
||||||
|
github.com/go-playground/universal-translator v0.18.0 h1:82dyy6p4OuJq4/CByFNOn/jYrnRPArHwAcmLoJZxyho=
|
||||||
|
github.com/go-playground/universal-translator v0.18.0/go.mod h1:UvRDBj+xPUEGrFYl+lu/H90nyDXpg0fqeB/AQUGNTVA=
|
||||||
|
github.com/go-playground/validator/v10 v10.10.0 h1:I7mrTYv78z8k8VXa/qJlOlEXn/nBh+BF8dHX5nt/dr0=
|
||||||
|
github.com/go-playground/validator/v10 v10.10.0/go.mod h1:74x4gJWsvQexRdW8Pn3dXSGrTK4nAUsbPlLADvpJkos=
|
||||||
|
github.com/goccy/go-json v0.9.7 h1:IcB+Aqpx/iMHu5Yooh7jEzJk1JZ7Pjtmys2ukPr7EeM=
|
||||||
|
github.com/goccy/go-json v0.9.7/go.mod h1:6MelG93GURQebXPDq3khkgXZkazVtN9CRI+MGFi0w8I=
|
||||||
|
github.com/golang/protobuf v1.5.0/go.mod h1:FsONVRAS9T7sI+LIUmWTfcYkHO4aIWwzhcaSAoJOfIk=
|
||||||
|
github.com/google/go-cmp v0.5.5 h1:Khx7svrCpmxxtHBq5j2mp/xVjsi8hQMfNLvJFAlrGgU=
|
||||||
|
github.com/google/go-cmp v0.5.5/go.mod h1:v8dTdLbMG2kIc/vJvl+f65V22dbkXbowE6jgT/gNBxE=
|
||||||
|
github.com/google/gofuzz v1.0.0/go.mod h1:dBl0BpW6vV/+mYPU4Po3pmUjxk6FQPldtuIdl/M65Eg=
|
||||||
|
github.com/gorilla/websocket v1.5.0 h1:PPwGk2jz7EePpoHN/+ClbZu8SPxiqlu12wZP/3sWmnc=
|
||||||
|
github.com/gorilla/websocket v1.5.0/go.mod h1:YR8l580nyteQvAITg2hZ9XVh4b55+EU/adAjf1fMHhE=
|
||||||
|
github.com/json-iterator/go v1.1.12 h1:PV8peI4a0ysnczrg+LtxykD8LfKY9ML6u2jnxaEnrnM=
|
||||||
|
github.com/json-iterator/go v1.1.12/go.mod h1:e30LSqwooZae/UwlEbR2852Gd8hjQvJoHmT4TnhNGBo=
|
||||||
|
github.com/kr/pretty v0.1.0/go.mod h1:dAy3ld7l9f0ibDNOQOHHMYYIIbhfbHSm3C4ZsoJORNo=
|
||||||
|
github.com/kr/pretty v0.2.1/go.mod h1:ipq/a2n7PKx3OHsz4KJII5eveXtPO4qwEXGdVfWzfnI=
|
||||||
|
github.com/kr/pretty v0.3.0 h1:WgNl7dwNpEZ6jJ9k1snq4pZsg7DOEN8hP9Xw0Tsjwk0=
|
||||||
|
github.com/kr/pretty v0.3.0/go.mod h1:640gp4NfQd8pI5XOwp5fnNeVWj67G7CFk/SaSQn7NBk=
|
||||||
|
github.com/kr/pty v1.1.1/go.mod h1:pFQYn66WHrOpPYNljwOMqo10TkYh1fy3cYio2l3bCsQ=
|
||||||
|
github.com/kr/text v0.1.0/go.mod h1:4Jbv+DJW3UT/LiOwJeYQe1efqtUx/iVham/4vfdArNI=
|
||||||
|
github.com/kr/text v0.2.0 h1:5Nx0Ya0ZqY2ygV366QzturHI13Jq95ApcVaJBhpS+AY=
|
||||||
|
github.com/kr/text v0.2.0/go.mod h1:eLer722TekiGuMkidMxC/pM04lWEeraHUUmBw8l2grE=
|
||||||
|
github.com/leodido/go-urn v1.2.1 h1:BqpAaACuzVSgi/VLzGZIobT2z4v53pjosyNd9Yv6n/w=
|
||||||
|
github.com/leodido/go-urn v1.2.1/go.mod h1:zt4jvISO2HfUBqxjfIshjdMTYS56ZS/qv49ictyFfxY=
|
||||||
|
github.com/lib/pq v1.10.7 h1:p7ZhMD+KsSRozJr34udlUrhboJwWAgCg34+/ZZNvZZw=
|
||||||
|
github.com/lib/pq v1.10.7/go.mod h1:AlVN5x4E4T544tWzH6hKfbfQvm3HdbOxrmggDNAPY9o=
|
||||||
|
github.com/mattn/go-isatty v0.0.14 h1:yVuAays6BHfxijgZPzw+3Zlu5yQgKGP2/hcQbHb7S9Y=
|
||||||
|
github.com/mattn/go-isatty v0.0.14/go.mod h1:7GGIvUiUoEMVVmxf/4nioHXj79iQHKdU27kJ6hsGG94=
|
||||||
|
github.com/modern-go/concurrent v0.0.0-20180228061459-e0a39a4cb421 h1:ZqeYNhU3OHLH3mGKHDcjJRFFRrJa6eAM5H+CtDdOsPc=
|
||||||
|
github.com/modern-go/concurrent v0.0.0-20180228061459-e0a39a4cb421/go.mod h1:6dJC0mAP4ikYIbvyc7fijjWJddQyLn8Ig3JB5CqoB9Q=
|
||||||
|
github.com/modern-go/reflect2 v1.0.2 h1:xBagoLtFs94CBntxluKeaWgTMpvLxC4ur3nMaC9Gz0M=
|
||||||
|
github.com/modern-go/reflect2 v1.0.2/go.mod h1:yWuevngMOJpCy52FWWMvUC8ws7m/LJsjYzDa0/r8luk=
|
||||||
|
github.com/pelletier/go-toml/v2 v2.0.1 h1:8e3L2cCQzLFi2CR4g7vGFuFxX7Jl1kKX8gW+iV0GUKU=
|
||||||
|
github.com/pelletier/go-toml/v2 v2.0.1/go.mod h1:r9LEWfGN8R5k0VXJ+0BkIe7MYkRdwZOjgMj2KwnJFUo=
|
||||||
|
github.com/pkg/diff v0.0.0-20210226163009-20ebb0f2a09e/go.mod h1:pJLUxLENpZxwdsKMEsNbx1VGcRFpLqf3715MtcvvzbA=
|
||||||
|
github.com/pmezard/go-difflib v1.0.0 h1:4DBwDE0NGyQoBHbLQYPwSUPoCMWR5BEzIk/f1lZbAQM=
|
||||||
|
github.com/pmezard/go-difflib v1.0.0/go.mod h1:iKH77koFhYxTK1pcRnkKkqfTogsbg7gZNVY4sRDYZ/4=
|
||||||
|
github.com/rogpeppe/go-internal v1.6.1/go.mod h1:xXDCJY+GAPziupqXw64V24skbSoqbTEfhy4qGm1nDQc=
|
||||||
|
github.com/rogpeppe/go-internal v1.8.0 h1:FCbCCtXNOY3UtUuHUYaghJg4y7Fd14rXifAYUAtL9R8=
|
||||||
|
github.com/rogpeppe/go-internal v1.8.0/go.mod h1:WmiCO8CzOY8rg0OYDC4/i/2WRWAB6poM+XZ2dLUbcbE=
|
||||||
|
github.com/stretchr/objx v0.1.0/go.mod h1:HFkY916IF+rwdDfMAkV7OtwuqBVzrE8GR6GFx+wExME=
|
||||||
|
github.com/stretchr/testify v1.3.0/go.mod h1:M5WIy9Dh21IEIfnGCwXGc5bZfKNJtfHm1UVUgZn+9EI=
|
||||||
|
github.com/stretchr/testify v1.6.1/go.mod h1:6Fq8oRcR53rry900zMqJjRRixrwX3KX962/h/Wwjteg=
|
||||||
|
github.com/stretchr/testify v1.7.0/go.mod h1:6Fq8oRcR53rry900zMqJjRRixrwX3KX962/h/Wwjteg=
|
||||||
|
github.com/stretchr/testify v1.7.1 h1:5TQK59W5E3v0r2duFAb7P95B6hEeOyEnHRa8MjYSMTY=
|
||||||
|
github.com/stretchr/testify v1.7.1/go.mod h1:6Fq8oRcR53rry900zMqJjRRixrwX3KX962/h/Wwjteg=
|
||||||
|
github.com/ugorji/go v1.2.7/go.mod h1:nF9osbDWLy6bDVv/Rtoh6QgnvNDpmCalQV5urGCCS6M=
|
||||||
|
github.com/ugorji/go/codec v1.2.7 h1:YPXUKf7fYbp/y8xloBqZOw2qaVggbfwMlI8WM3wZUJ0=
|
||||||
|
github.com/ugorji/go/codec v1.2.7/go.mod h1:WGN1fab3R1fzQlVQTkfxVtIBhWDRqOviHU95kRgeqEY=
|
||||||
|
golang.org/x/crypto v0.0.0-20210711020723-a769d52b0f97 h1:/UOmuWzQfxxo9UtlXMwuQU8CMgg1eZXqTRwkSQJWKOI=
|
||||||
|
golang.org/x/crypto v0.0.0-20210711020723-a769d52b0f97/go.mod h1:GvvjBRRGRdwPK5ydBHafDWAxML/pGHZbMvKqRZ5+Abc=
|
||||||
|
golang.org/x/net v0.0.0-20210226172049-e18ecbb05110 h1:qWPm9rbaAMKs8Bq/9LRpbMqxWRVUAQwMI9fVrssnTfw=
|
||||||
|
golang.org/x/net v0.0.0-20210226172049-e18ecbb05110/go.mod h1:m0MpNAwzfU5UDzcl9v0D8zg8gWTRqZa9RBIspLL5mdg=
|
||||||
|
golang.org/x/sys v0.0.0-20201119102817-f84b799fce68/go.mod h1:h1NjWce9XRLGQEsW7wpKNCjG9DtNlClVuFLEZdDNbEs=
|
||||||
|
golang.org/x/sys v0.0.0-20210615035016-665e8c7367d1/go.mod h1:oPkhp1MJrh7nUepCBck5+mAzfO9JrbApNNgaTdGDITg=
|
||||||
|
golang.org/x/sys v0.0.0-20210630005230-0f9fa26af87c/go.mod h1:oPkhp1MJrh7nUepCBck5+mAzfO9JrbApNNgaTdGDITg=
|
||||||
|
golang.org/x/sys v0.0.0-20210806184541-e5e7981a1069 h1:siQdpVirKtzPhKl3lZWozZraCFObP8S1v6PRp0bLrtU=
|
||||||
|
golang.org/x/sys v0.0.0-20210806184541-e5e7981a1069/go.mod h1:oPkhp1MJrh7nUepCBck5+mAzfO9JrbApNNgaTdGDITg=
|
||||||
|
golang.org/x/term v0.0.0-20201126162022-7de9c90e9dd1/go.mod h1:bj7SfCRtBDWHUb9snDiAeCFNEtKQo2Wmx5Cou7ajbmo=
|
||||||
|
golang.org/x/text v0.3.3/go.mod h1:5Zoc/QRtKVWzQhOtBMvqHzDpF6irO9z98xDceosuGiQ=
|
||||||
|
golang.org/x/text v0.3.6 h1:aRYxNxv6iGQlyVaZmk6ZgYEDa+Jg18DxebPSrd6bg1M=
|
||||||
|
golang.org/x/text v0.3.6/go.mod h1:5Zoc/QRtKVWzQhOtBMvqHzDpF6irO9z98xDceosuGiQ=
|
||||||
|
golang.org/x/tools v0.0.0-20180917221912-90fa682c2a6e/go.mod h1:n7NCudcB/nEzxVGmLbDWY5pfWTLqBcC2KZ6jyYvM4mQ=
|
||||||
|
golang.org/x/xerrors v0.0.0-20191204190536-9bdfabe68543 h1:E7g+9GITq07hpfrRu66IVDexMakfv52eLZ2CXBWiKr4=
|
||||||
|
golang.org/x/xerrors v0.0.0-20191204190536-9bdfabe68543/go.mod h1:I/5z698sn9Ka8TeJc9MKroUUfqBBauWjQqLJ2OPfmY0=
|
||||||
|
google.golang.org/protobuf v1.26.0-rc.1/go.mod h1:jlhhOSvTdKEhbULTjvd4ARK9grFBp09yW+WbY/TyQbw=
|
||||||
|
google.golang.org/protobuf v1.28.0 h1:w43yiav+6bVFTBQFZX0r7ipe9JQ1QsbMgHwbBziscLw=
|
||||||
|
google.golang.org/protobuf v1.28.0/go.mod h1:HV8QOd/L58Z+nl8r43ehVNZIU/HEI6OcFqwMG9pJV4I=
|
||||||
|
gopkg.in/check.v1 v0.0.0-20161208181325-20d25e280405/go.mod h1:Co6ibVJAznAaIkqp8huTwlJQCZ016jof/cbN4VW5Yz0=
|
||||||
|
gopkg.in/check.v1 v1.0.0-20180628173108-788fd7840127/go.mod h1:Co6ibVJAznAaIkqp8huTwlJQCZ016jof/cbN4VW5Yz0=
|
||||||
|
gopkg.in/check.v1 v1.0.0-20201130134442-10cb98267c6c h1:Hei/4ADfdWqJk1ZMxUNpqntNwaWcugrBjAiHlqqRiVk=
|
||||||
|
gopkg.in/check.v1 v1.0.0-20201130134442-10cb98267c6c/go.mod h1:JHkPIbrfpd72SG/EVd6muEfDQjcINNoR0C8j2r3qZ4Q=
|
||||||
|
gopkg.in/errgo.v2 v2.1.0/go.mod h1:hNsd1EY+bozCKY1Ytp96fpM3vjJbqLJn88ws8XvfDNI=
|
||||||
|
gopkg.in/yaml.v2 v2.4.0 h1:D8xgwECY7CYvx+Y2n4sBz93Jn9JRvxdiyyo8CTfuKaY=
|
||||||
|
gopkg.in/yaml.v2 v2.4.0/go.mod h1:RDklbk79AGWmwhnvt/jBztapEOGDOx6ZbXqjP6csGnQ=
|
||||||
|
gopkg.in/yaml.v3 v3.0.0-20200313102051-9f266ea9e77c/go.mod h1:K4uyk7z7BCEPqu6E+C64Yfv1cQ7kz7rIZviUmN+EgEM=
|
||||||
|
gopkg.in/yaml.v3 v3.0.0-20210107192922-496545a6307b h1:h8qDotaEPuJATrMmW04NCwg7v22aHH28wwpauUhK9Oo=
|
||||||
|
gopkg.in/yaml.v3 v3.0.0-20210107192922-496545a6307b/go.mod h1:K4uyk7z7BCEPqu6E+C64Yfv1cQ7kz7rIZviUmN+EgEM=
|
||||||
103
libs/db.js
103
libs/db.js
@@ -1,103 +0,0 @@
|
|||||||
import Database from "better-sqlite3";
|
|
||||||
|
|
||||||
const db = new Database("db.sqlite");
|
|
||||||
|
|
||||||
// init DB
|
|
||||||
db.prepare(
|
|
||||||
`CREATE TABLE IF NOT EXISTS time_ranges (
|
|
||||||
id INTEGER PRIMARY KEY,
|
|
||||||
name TEXT NOT NULL DEFAULT '',
|
|
||||||
range TEXT NOT NULL DEFAULT '',
|
|
||||||
username TEXT NOT NULL DEFAULT ''
|
|
||||||
)`
|
|
||||||
).run();
|
|
||||||
db.prepare(
|
|
||||||
`CREATE TABLE IF NOT EXISTS configs (
|
|
||||||
name TEXT PRIMARY KEY,
|
|
||||||
value TEXT NOT NULL
|
|
||||||
)`
|
|
||||||
).run();
|
|
||||||
db.prepare(
|
|
||||||
`INSERT OR IGNORE INTO configs (name, value) VALUES ('limit', '1')`
|
|
||||||
).run();
|
|
||||||
db.prepare(
|
|
||||||
`INSERT OR IGNORE INTO configs (name, value) VALUES ('token', 'woshimima')`
|
|
||||||
).run();
|
|
||||||
db.prepare(
|
|
||||||
`INSERT OR IGNORE INTO configs (name, value) VALUES ('started', 'false')`
|
|
||||||
).run();
|
|
||||||
|
|
||||||
// prepare statements
|
|
||||||
const insertTimeRange = db.prepare(
|
|
||||||
`INSERT INTO time_ranges (name, range) VALUES (?, ?)`
|
|
||||||
);
|
|
||||||
const getTimeRanges = db.prepare(`SELECT * FROM time_ranges ORDER BY range, name`);
|
|
||||||
const deleteTimeRange = db.prepare(`DELETE FROM time_ranges WHERE id = ?`);
|
|
||||||
const update = db.prepare(
|
|
||||||
`UPDATE time_ranges SET name = ?, range = ?, username = ? WHERE id = ?`
|
|
||||||
);
|
|
||||||
const updateUsername = db.prepare(
|
|
||||||
`UPDATE time_ranges SET username = ? WHERE id = ?`
|
|
||||||
);
|
|
||||||
|
|
||||||
const countUser = db.prepare(
|
|
||||||
`SELECT COUNT(*) as count FROM time_ranges WHERE username = ?`
|
|
||||||
);
|
|
||||||
const getUsername = db.prepare(`SELECT username FROM time_ranges WHERE id = ?`);
|
|
||||||
const updateUsernameWithLimit = db.transaction((username, id, limit) => {
|
|
||||||
const count = countUser.get(username).count;
|
|
||||||
const existingUsername = getUsername.get(id).username;
|
|
||||||
if (!getStarted()) {
|
|
||||||
throw new Error("还没到开始时间喔");
|
|
||||||
}
|
|
||||||
if (existingUsername !== "") {
|
|
||||||
throw new Error("这个时间段已经有人了喔");
|
|
||||||
}
|
|
||||||
if (count >= limit) {
|
|
||||||
throw new Error("达到数量上限啦");
|
|
||||||
}
|
|
||||||
updateUsername.run(username, id);
|
|
||||||
});
|
|
||||||
|
|
||||||
const getConfigStmt = db.prepare(`SELECT value FROM configs WHERE name = ?`);
|
|
||||||
const setConfigStmt = db.prepare(`UPDATE configs SET value = ? WHERE name = ?`);
|
|
||||||
const getLimit = () => {
|
|
||||||
const limit = getConfigStmt.get("limit").value;
|
|
||||||
return parseInt(limit);
|
|
||||||
};
|
|
||||||
const setLimit = (limit) => {
|
|
||||||
setConfigStmt.run(limit, "limit");
|
|
||||||
};
|
|
||||||
const getToken = () => {
|
|
||||||
const token = getConfigStmt.get("token").value;
|
|
||||||
return token;
|
|
||||||
};
|
|
||||||
|
|
||||||
const authenticate = (token) => {
|
|
||||||
const tokenFromDB = getToken();
|
|
||||||
return token === tokenFromDB;
|
|
||||||
};
|
|
||||||
|
|
||||||
const getStarted = () => {
|
|
||||||
const started = getConfigStmt.get("started").value;
|
|
||||||
return started === "true";
|
|
||||||
};
|
|
||||||
|
|
||||||
const setStarted = (started) => {
|
|
||||||
const started_string = started ? "true" : "false";
|
|
||||||
setConfigStmt.run(started_string, "started");
|
|
||||||
};
|
|
||||||
|
|
||||||
export {
|
|
||||||
insertTimeRange,
|
|
||||||
getTimeRanges,
|
|
||||||
deleteTimeRange,
|
|
||||||
update,
|
|
||||||
updateUsername,
|
|
||||||
updateUsernameWithLimit,
|
|
||||||
getLimit,
|
|
||||||
setLimit,
|
|
||||||
authenticate,
|
|
||||||
getStarted,
|
|
||||||
setStarted,
|
|
||||||
};
|
|
||||||
@@ -1,21 +0,0 @@
|
|||||||
const stats = {
|
|
||||||
apiqps: 0,
|
|
||||||
lastAccess: 0,
|
|
||||||
};
|
|
||||||
|
|
||||||
const addAPIQPS = () => {
|
|
||||||
stats.apiqps++;
|
|
||||||
clearStats();
|
|
||||||
};
|
|
||||||
|
|
||||||
const clearStats = () => {
|
|
||||||
const now = Date.now();
|
|
||||||
if (stats.lastAccess + 1000 < now) {
|
|
||||||
const now_formated = new Date(now).toISOString();
|
|
||||||
console.log(`${now_formated} - APIQPS: ${stats.apiqps}`);
|
|
||||||
stats.apiqps = parseInt(stats.apiqps / 3);
|
|
||||||
stats.lastAccess = now;
|
|
||||||
}
|
|
||||||
};
|
|
||||||
|
|
||||||
export { stats, addAPIQPS };
|
|
||||||
366
main.go
Normal file
366
main.go
Normal file
@@ -0,0 +1,366 @@
|
|||||||
|
package main
|
||||||
|
|
||||||
|
import (
|
||||||
|
"errors"
|
||||||
|
"log"
|
||||||
|
"net/http"
|
||||||
|
"strconv"
|
||||||
|
|
||||||
|
"itsc/db"
|
||||||
|
|
||||||
|
"github.com/gin-gonic/gin"
|
||||||
|
"github.com/gin-gonic/gin/binding"
|
||||||
|
"github.com/gorilla/websocket"
|
||||||
|
)
|
||||||
|
|
||||||
|
var TOKEN = "woshimima"
|
||||||
|
|
||||||
|
func ok(c *gin.Context) {
|
||||||
|
c.JSON(200, gin.H{
|
||||||
|
"status": "OK",
|
||||||
|
})
|
||||||
|
}
|
||||||
|
|
||||||
|
func auth(c *gin.Context) {
|
||||||
|
type Request struct {
|
||||||
|
Token string `json:"token"`
|
||||||
|
}
|
||||||
|
req := &Request{}
|
||||||
|
err := c.ShouldBindBodyWith(req, binding.JSON)
|
||||||
|
if err != nil {
|
||||||
|
c.AbortWithError(400, errors.New("解析Token错误"))
|
||||||
|
return
|
||||||
|
}
|
||||||
|
if req.Token != TOKEN {
|
||||||
|
c.AbortWithError(403, errors.New("Token错误"))
|
||||||
|
return
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
var wsUpgrader = websocket.Upgrader{
|
||||||
|
CheckOrigin: func(r *http.Request) bool {
|
||||||
|
return true
|
||||||
|
},
|
||||||
|
}
|
||||||
|
|
||||||
|
func main() {
|
||||||
|
r := gin.Default()
|
||||||
|
|
||||||
|
r.Use(func(c *gin.Context) {
|
||||||
|
c.Next()
|
||||||
|
if len(c.Errors) > 0 {
|
||||||
|
c.JSON(-1, gin.H{
|
||||||
|
"errors": c.Errors.Errors(),
|
||||||
|
"note": "General error handler abort",
|
||||||
|
})
|
||||||
|
}
|
||||||
|
})
|
||||||
|
|
||||||
|
api := r.Group("/api")
|
||||||
|
|
||||||
|
api.GET("/timetables", func(c *gin.Context) {
|
||||||
|
timetables := make([]*db.Timetable, 0)
|
||||||
|
rows, err := db.GetAllTimetables.Query()
|
||||||
|
if err != nil {
|
||||||
|
c.AbortWithError(400, err)
|
||||||
|
return
|
||||||
|
}
|
||||||
|
for rows.Next() {
|
||||||
|
s := &db.Timetable{}
|
||||||
|
rows.Scan(&s.ID, &s.Name, &s.Status)
|
||||||
|
timetables = append(timetables, s)
|
||||||
|
}
|
||||||
|
c.JSON(http.StatusOK, gin.H{
|
||||||
|
"timetables": timetables,
|
||||||
|
})
|
||||||
|
})
|
||||||
|
|
||||||
|
api.POST("/timetables", auth, func(c *gin.Context) {
|
||||||
|
type Request struct {
|
||||||
|
Name string `json:"newTimeTableName"`
|
||||||
|
}
|
||||||
|
req := &Request{}
|
||||||
|
err := c.ShouldBindBodyWith(req, binding.JSON)
|
||||||
|
if err != nil {
|
||||||
|
c.AbortWithError(400, err)
|
||||||
|
return
|
||||||
|
}
|
||||||
|
row := db.CreateNewTimetable.QueryRow(req.Name)
|
||||||
|
var id int64
|
||||||
|
err = row.Scan(&id)
|
||||||
|
if err != nil {
|
||||||
|
c.AbortWithError(400, err)
|
||||||
|
return
|
||||||
|
}
|
||||||
|
c.JSON(200, gin.H{
|
||||||
|
"id": id,
|
||||||
|
})
|
||||||
|
})
|
||||||
|
|
||||||
|
api.DELETE("/timetables/:id", auth, func(c *gin.Context) {
|
||||||
|
timetableID, err := strconv.ParseInt(c.Param("id"), 10, 64)
|
||||||
|
if err != nil {
|
||||||
|
c.AbortWithError(400, err)
|
||||||
|
return
|
||||||
|
}
|
||||||
|
_, err = db.DeleteTimetable.Exec(timetableID)
|
||||||
|
if err != nil {
|
||||||
|
c.AbortWithError(400, err)
|
||||||
|
return
|
||||||
|
}
|
||||||
|
ok(c)
|
||||||
|
})
|
||||||
|
|
||||||
|
api.GET("/timetables/:id/username/:username", func(c *gin.Context) {
|
||||||
|
timetableID, err := strconv.ParseInt(c.Param("id"), 10, 64)
|
||||||
|
if err != nil {
|
||||||
|
c.AbortWithError(400, err)
|
||||||
|
return
|
||||||
|
}
|
||||||
|
rows, err := db.GetTimeSlotsByTimetable.Query(timetableID, c.Param("username"))
|
||||||
|
if err != nil {
|
||||||
|
c.AbortWithError(400, err)
|
||||||
|
return
|
||||||
|
}
|
||||||
|
timeslots := make([]*db.TimeSlot, 0)
|
||||||
|
var timetableName string
|
||||||
|
var timetableStatus bool
|
||||||
|
for rows.Next() {
|
||||||
|
s := &db.TimeSlot{}
|
||||||
|
err = rows.Scan(&s.ID, &s.Name, &s.Time, &s.Take, &s.Capacity, &timetableName, &timetableStatus, &s.Success)
|
||||||
|
if err != nil {
|
||||||
|
c.AbortWithError(400, err)
|
||||||
|
return
|
||||||
|
}
|
||||||
|
timeslots = append(timeslots, s)
|
||||||
|
}
|
||||||
|
c.JSON(200, gin.H{
|
||||||
|
"timeslots": timeslots,
|
||||||
|
"timetableName": timetableName,
|
||||||
|
"timetableStatus": timetableStatus,
|
||||||
|
})
|
||||||
|
})
|
||||||
|
|
||||||
|
api.POST("/timetables/:id", auth, func(c *gin.Context) {
|
||||||
|
timetableID, err := strconv.ParseInt(c.Param("id"), 10, 64)
|
||||||
|
if err != nil {
|
||||||
|
c.AbortWithError(400, err)
|
||||||
|
return
|
||||||
|
}
|
||||||
|
req := &db.TimeSlot{}
|
||||||
|
err = c.ShouldBindBodyWith(req, binding.JSON)
|
||||||
|
if err != nil {
|
||||||
|
c.AbortWithError(400, err)
|
||||||
|
return
|
||||||
|
}
|
||||||
|
row := db.CreateNewTimeslot.QueryRow(timetableID, req.Name, req.Time, req.Capacity)
|
||||||
|
var newID int64
|
||||||
|
err = row.Scan(&newID)
|
||||||
|
if err != nil {
|
||||||
|
c.AbortWithError(400, err)
|
||||||
|
return
|
||||||
|
}
|
||||||
|
ok(c)
|
||||||
|
})
|
||||||
|
|
||||||
|
api.DELETE("/timetables/:id/:tsid", auth, func(c *gin.Context) {
|
||||||
|
timeslotID, err := strconv.ParseInt(c.Param("tsid"), 10, 64)
|
||||||
|
if err != nil {
|
||||||
|
c.AbortWithError(400, err)
|
||||||
|
return
|
||||||
|
}
|
||||||
|
_, err = db.DeleteTimeslot.Exec(timeslotID)
|
||||||
|
if err != nil {
|
||||||
|
c.AbortWithError(400, err)
|
||||||
|
return
|
||||||
|
}
|
||||||
|
ok(c)
|
||||||
|
})
|
||||||
|
|
||||||
|
api.GET("/timetables/:id/:tsid", func(c *gin.Context) {
|
||||||
|
timeslotID, err := strconv.ParseInt(c.Param("tsid"), 10, 64)
|
||||||
|
if err != nil {
|
||||||
|
c.AbortWithError(400, err)
|
||||||
|
return
|
||||||
|
}
|
||||||
|
takes := make([]*db.Take, 0)
|
||||||
|
rows, err := db.GetTakesByTimeslot.Query(timeslotID)
|
||||||
|
if err != nil {
|
||||||
|
c.AbortWithError(400, err)
|
||||||
|
return
|
||||||
|
}
|
||||||
|
for rows.Next() {
|
||||||
|
s := &db.Take{}
|
||||||
|
err = rows.Scan(&s.Username, &s.Created)
|
||||||
|
if err != nil {
|
||||||
|
c.AbortWithError(400, err)
|
||||||
|
return
|
||||||
|
}
|
||||||
|
takes = append(takes, s)
|
||||||
|
}
|
||||||
|
c.JSON(200, gin.H{
|
||||||
|
"takes": takes,
|
||||||
|
})
|
||||||
|
})
|
||||||
|
|
||||||
|
api.DELETE("/timetables/:id/:tsid/:tkname", auth, func(c *gin.Context) {
|
||||||
|
timeslotID, err := strconv.ParseInt(c.Param("tsid"), 10, 64)
|
||||||
|
if err != nil {
|
||||||
|
c.AbortWithError(400, err)
|
||||||
|
return
|
||||||
|
}
|
||||||
|
tkname := c.Param("tkname")
|
||||||
|
tx, err := db.DB.Begin()
|
||||||
|
if err != nil {
|
||||||
|
c.AbortWithError(400, err)
|
||||||
|
tx.Rollback()
|
||||||
|
return
|
||||||
|
}
|
||||||
|
deleteTakeStmt := tx.Stmt(db.DeleteTake)
|
||||||
|
_, err = deleteTakeStmt.Exec(timeslotID, tkname)
|
||||||
|
if err != nil {
|
||||||
|
c.AbortWithError(400, err)
|
||||||
|
tx.Rollback()
|
||||||
|
return
|
||||||
|
}
|
||||||
|
UpdateTakeCountStmt := tx.Stmt(db.UpdateTakeCount)
|
||||||
|
_, err = UpdateTakeCountStmt.Exec(-1, timeslotID)
|
||||||
|
if err != nil {
|
||||||
|
c.AbortWithError(400, err)
|
||||||
|
tx.Rollback()
|
||||||
|
return
|
||||||
|
}
|
||||||
|
err = tx.Commit()
|
||||||
|
if err != nil {
|
||||||
|
c.AbortWithError(400, err)
|
||||||
|
tx.Rollback()
|
||||||
|
return
|
||||||
|
}
|
||||||
|
ok(c)
|
||||||
|
})
|
||||||
|
|
||||||
|
api.PUT("/timetables/:id", auth, func(c *gin.Context) {
|
||||||
|
timetableID, err := strconv.ParseInt(c.Param("id"), 10, 64)
|
||||||
|
if err != nil {
|
||||||
|
c.AbortWithError(400, err)
|
||||||
|
return
|
||||||
|
}
|
||||||
|
req := &db.Timetable{}
|
||||||
|
err = c.ShouldBindBodyWith(req, binding.JSON)
|
||||||
|
if err != nil {
|
||||||
|
c.AbortWithError(400, err)
|
||||||
|
return
|
||||||
|
}
|
||||||
|
_, err = db.UpdateTimetableStatus.Exec(req.Status, timetableID)
|
||||||
|
if err != nil {
|
||||||
|
c.AbortWithError(400, err)
|
||||||
|
return
|
||||||
|
}
|
||||||
|
ok(c)
|
||||||
|
})
|
||||||
|
|
||||||
|
api.PUT("/timetables/:id/:tsid", func(c *gin.Context) {
|
||||||
|
timetableID, err := strconv.ParseInt(c.Param("id"), 10, 64)
|
||||||
|
if err != nil {
|
||||||
|
c.AbortWithError(400, err)
|
||||||
|
return
|
||||||
|
}
|
||||||
|
timeslotID, err := strconv.ParseInt(c.Param("tsid"), 10, 64)
|
||||||
|
if err != nil {
|
||||||
|
c.AbortWithError(400, err)
|
||||||
|
return
|
||||||
|
}
|
||||||
|
req := &db.Take{}
|
||||||
|
err = c.ShouldBindBodyWith(req, binding.JSON)
|
||||||
|
if err != nil {
|
||||||
|
c.AbortWithError(400, err)
|
||||||
|
return
|
||||||
|
}
|
||||||
|
if err != nil {
|
||||||
|
c.AbortWithError(400, err)
|
||||||
|
return
|
||||||
|
}
|
||||||
|
tx, err := db.DB.Begin()
|
||||||
|
if err != nil {
|
||||||
|
c.AbortWithError(400, err)
|
||||||
|
tx.Rollback()
|
||||||
|
return
|
||||||
|
}
|
||||||
|
updateCountStmt := tx.Stmt(db.UpdateTakeCount)
|
||||||
|
if req.Username[0] == '!' {
|
||||||
|
untakeStmt := tx.Stmt(db.UserUntakeTimeslot)
|
||||||
|
username := req.Username[1:len(req.Username)]
|
||||||
|
_, err = untakeStmt.Exec(username, timeslotID)
|
||||||
|
if err != nil {
|
||||||
|
c.AbortWithError(400, err)
|
||||||
|
tx.Rollback()
|
||||||
|
return
|
||||||
|
}
|
||||||
|
_, err = updateCountStmt.Exec(-1, timeslotID)
|
||||||
|
if err != nil {
|
||||||
|
c.AbortWithError(400, err)
|
||||||
|
tx.Rollback()
|
||||||
|
return
|
||||||
|
}
|
||||||
|
} else {
|
||||||
|
checkStatusStmt := tx.Stmt(db.CheckTableStatus)
|
||||||
|
var status bool
|
||||||
|
row := checkStatusStmt.QueryRow(timetableID)
|
||||||
|
err = row.Scan(&status)
|
||||||
|
if err != nil {
|
||||||
|
c.AbortWithError(400, err)
|
||||||
|
tx.Rollback()
|
||||||
|
return
|
||||||
|
}
|
||||||
|
if !status {
|
||||||
|
c.AbortWithError(403, errors.New("此表暂未开始招募"))
|
||||||
|
tx.Rollback()
|
||||||
|
return
|
||||||
|
}
|
||||||
|
takeStmt := tx.Stmt(db.UserTakeTimeslot)
|
||||||
|
_, err = takeStmt.Exec(req.Username, timeslotID)
|
||||||
|
if err != nil {
|
||||||
|
c.AbortWithError(400, err)
|
||||||
|
tx.Rollback()
|
||||||
|
return
|
||||||
|
}
|
||||||
|
_, err = updateCountStmt.Exec(1, timeslotID)
|
||||||
|
if err != nil {
|
||||||
|
c.AbortWithError(400, err)
|
||||||
|
tx.Rollback()
|
||||||
|
return
|
||||||
|
}
|
||||||
|
}
|
||||||
|
err = tx.Commit()
|
||||||
|
if err != nil {
|
||||||
|
c.AbortWithError(400, err)
|
||||||
|
return
|
||||||
|
}
|
||||||
|
ok(c)
|
||||||
|
})
|
||||||
|
|
||||||
|
api.GET("/ws", func(c *gin.Context) {
|
||||||
|
ws, err := wsUpgrader.Upgrade(c.Writer, c.Request, nil)
|
||||||
|
if err != nil {
|
||||||
|
c.AbortWithError(401, err)
|
||||||
|
return
|
||||||
|
}
|
||||||
|
defer ws.Close()
|
||||||
|
for {
|
||||||
|
mt, message, err := ws.ReadMessage()
|
||||||
|
if err != nil {
|
||||||
|
break
|
||||||
|
}
|
||||||
|
if string(message) == "ping" {
|
||||||
|
message = []byte("pong")
|
||||||
|
}
|
||||||
|
err = ws.WriteMessage(mt, message)
|
||||||
|
if err != nil {
|
||||||
|
break
|
||||||
|
}
|
||||||
|
}
|
||||||
|
})
|
||||||
|
|
||||||
|
log.Println("Started")
|
||||||
|
r.Run(":8081")
|
||||||
|
}
|
||||||
@@ -1,7 +0,0 @@
|
|||||||
/** @type {import('next').NextConfig} */
|
|
||||||
const nextConfig = {
|
|
||||||
reactStrictMode: true,
|
|
||||||
// basePath: '/itsc',
|
|
||||||
}
|
|
||||||
|
|
||||||
module.exports = nextConfig
|
|
||||||
7370
package-lock.json
generated
7370
package-lock.json
generated
File diff suppressed because it is too large
Load Diff
25
package.json
25
package.json
@@ -1,25 +0,0 @@
|
|||||||
{
|
|
||||||
"name": "itsc",
|
|
||||||
"version": "0.1.0",
|
|
||||||
"private": true,
|
|
||||||
"scripts": {
|
|
||||||
"dev": "next dev",
|
|
||||||
"build": "next build",
|
|
||||||
"start": "next start",
|
|
||||||
"lint": "next lint"
|
|
||||||
},
|
|
||||||
"dependencies": {
|
|
||||||
"@emotion/react": "^11.8.2",
|
|
||||||
"@emotion/styled": "^11.8.1",
|
|
||||||
"@mui/icons-material": "^5.5.1",
|
|
||||||
"@mui/material": "^5.5.3",
|
|
||||||
"better-sqlite3": "^7.5.0",
|
|
||||||
"next": "12.1.2",
|
|
||||||
"react": "^17.0.2",
|
|
||||||
"react-dom": "^17.0.2"
|
|
||||||
},
|
|
||||||
"devDependencies": {
|
|
||||||
"eslint": "8.12.0",
|
|
||||||
"eslint-config-next": "12.1.2"
|
|
||||||
}
|
|
||||||
}
|
|
||||||
@@ -1,63 +0,0 @@
|
|||||||
import Head from "next/head";
|
|
||||||
import Link from "next/link";
|
|
||||||
import { useState } from "react";
|
|
||||||
import {
|
|
||||||
Button,
|
|
||||||
Stack,
|
|
||||||
Box,
|
|
||||||
CssBaseline,
|
|
||||||
AppBar,
|
|
||||||
Toolbar,
|
|
||||||
Typography,
|
|
||||||
} from "@mui/material";
|
|
||||||
import "../styles/globals.css";
|
|
||||||
|
|
||||||
function MyApp({ Component, pageProps }) {
|
|
||||||
const [username, setUsername] = useState("");
|
|
||||||
pageProps = { ...pageProps, username, setUsername };
|
|
||||||
return (
|
|
||||||
<>
|
|
||||||
<Head>
|
|
||||||
<title>ITSC Tool</title>
|
|
||||||
<meta
|
|
||||||
name="viewport"
|
|
||||||
content="minimum-scale=1, initial-scale=1, width=device-width"
|
|
||||||
/>
|
|
||||||
<CssBaseline />
|
|
||||||
</Head>
|
|
||||||
<AppBar
|
|
||||||
position="static"
|
|
||||||
sx={{
|
|
||||||
mb: 3,
|
|
||||||
}}
|
|
||||||
>
|
|
||||||
<Toolbar
|
|
||||||
sx={{
|
|
||||||
flex: 1,
|
|
||||||
justifyContent: "space-between",
|
|
||||||
alignItems: "center",
|
|
||||||
}}
|
|
||||||
>
|
|
||||||
<Typography variant="h5">
|
|
||||||
<Link href="/">抢 班</Link>
|
|
||||||
</Typography>
|
|
||||||
{username && (
|
|
||||||
<Button
|
|
||||||
variant="contained"
|
|
||||||
color="secondary"
|
|
||||||
onClick={() => {
|
|
||||||
//localStorage.removeItem("username");
|
|
||||||
setUsername("");
|
|
||||||
}}
|
|
||||||
>
|
|
||||||
{username} (点击登出)
|
|
||||||
</Button>
|
|
||||||
)}
|
|
||||||
</Toolbar>
|
|
||||||
</AppBar>
|
|
||||||
<Component {...pageProps} />
|
|
||||||
</>
|
|
||||||
);
|
|
||||||
}
|
|
||||||
|
|
||||||
export default MyApp;
|
|
||||||
@@ -1,5 +0,0 @@
|
|||||||
// Next.js API route support: https://nextjs.org/docs/api-routes/introduction
|
|
||||||
|
|
||||||
export default function handler(req, res) {
|
|
||||||
res.status(200).json({ name: 'John Doe' })
|
|
||||||
}
|
|
||||||
@@ -1,7 +0,0 @@
|
|||||||
import { stats, addAPIQPS } from "../../libs/stats";
|
|
||||||
|
|
||||||
export default function handler(req, res) {
|
|
||||||
addAPIQPS();
|
|
||||||
res.setHeader("Cache-Control", "no-cache no-store must-revalidate");
|
|
||||||
res.status(200).json(stats);
|
|
||||||
}
|
|
||||||
@@ -1,37 +0,0 @@
|
|||||||
import { authenticate, setLimit, getLimit } from "../../../libs/db";
|
|
||||||
import { addAPIQPS } from "../../../libs/stats";
|
|
||||||
|
|
||||||
export default function handler(req, res) {
|
|
||||||
addAPIQPS();
|
|
||||||
// put method
|
|
||||||
if (req.method === "PUT") {
|
|
||||||
const { token, limit } = req.body;
|
|
||||||
|
|
||||||
// authenticate
|
|
||||||
if (!authenticate(token)) {
|
|
||||||
res.status(401).json({
|
|
||||||
error: `token ${token} 验证失败`,
|
|
||||||
});
|
|
||||||
return;
|
|
||||||
}
|
|
||||||
|
|
||||||
// check type is integer
|
|
||||||
const limitInt = parseInt(limit);
|
|
||||||
if (!limitInt) {
|
|
||||||
res.status(400).json({
|
|
||||||
error: `数量限制必须是整数,但是传入了 ${limit}`,
|
|
||||||
});
|
|
||||||
return;
|
|
||||||
}
|
|
||||||
|
|
||||||
setLimit(limitInt);
|
|
||||||
res.status(200).send({ success: true });
|
|
||||||
} else if (req.method === "GET") {
|
|
||||||
res.setHeader("Cache-Control", "no-cache no-store must-revalidate");
|
|
||||||
res.status(200).json({
|
|
||||||
limit: getLimit(),
|
|
||||||
});
|
|
||||||
} else {
|
|
||||||
res.status(405).send({ error: "方法" + req.method + "不被允许" });
|
|
||||||
}
|
|
||||||
}
|
|
||||||
@@ -1,31 +0,0 @@
|
|||||||
import { authenticate, getTimeRanges, insertTimeRange } from "../../../libs/db";
|
|
||||||
|
|
||||||
import { addAPIQPS } from "../../../libs/stats";
|
|
||||||
|
|
||||||
export default function handler(req, res) {
|
|
||||||
addAPIQPS();
|
|
||||||
// get method
|
|
||||||
if (req.method === "GET") {
|
|
||||||
res.setHeader("Cache-Control", "no-cache no-store must-revalidate");
|
|
||||||
res.status(200).json(getTimeRanges.all());
|
|
||||||
return;
|
|
||||||
} else if (req.method === "POST") {
|
|
||||||
// authenticate
|
|
||||||
const { token } = req.body;
|
|
||||||
if (!authenticate(token)) {
|
|
||||||
res.status(401).json({
|
|
||||||
error: `token ${token} 验证失败`,
|
|
||||||
});
|
|
||||||
return;
|
|
||||||
}
|
|
||||||
// jsonfiy
|
|
||||||
const { name, range } = req.body;
|
|
||||||
insertTimeRange.run(name, range);
|
|
||||||
res.status(200).json({
|
|
||||||
success: true,
|
|
||||||
});
|
|
||||||
return;
|
|
||||||
} else {
|
|
||||||
res.status(405).send({ error: "方法" + req.method + "不被允许" });
|
|
||||||
}
|
|
||||||
}
|
|
||||||
@@ -1,78 +0,0 @@
|
|||||||
import {
|
|
||||||
deleteTimeRange,
|
|
||||||
getLimit,
|
|
||||||
authenticate,
|
|
||||||
update,
|
|
||||||
updateUsername,
|
|
||||||
updateUsernameWithLimit,
|
|
||||||
} from "../../../../libs/db";
|
|
||||||
|
|
||||||
import { addAPIQPS } from "../../../../libs/stats";
|
|
||||||
|
|
||||||
export default function handler(req, res) {
|
|
||||||
addAPIQPS();
|
|
||||||
// check if id is valid
|
|
||||||
const { id } = req.query;
|
|
||||||
if (id === undefined) {
|
|
||||||
res.status(400).json({
|
|
||||||
error: `缺少参数 id`,
|
|
||||||
});
|
|
||||||
return;
|
|
||||||
}
|
|
||||||
|
|
||||||
// delete method
|
|
||||||
if (req.method === "DELETE") {
|
|
||||||
// authenticate
|
|
||||||
const { token } = req.body;
|
|
||||||
if (!authenticate(token)) {
|
|
||||||
console.log("[DELETE] Authentication failed");
|
|
||||||
res.status(401).json({
|
|
||||||
error: `token ${token} 验证失败`,
|
|
||||||
});
|
|
||||||
return;
|
|
||||||
}
|
|
||||||
deleteTimeRange.run(id);
|
|
||||||
|
|
||||||
// update username
|
|
||||||
} else if (req.method === "PUT") {
|
|
||||||
const { username } = req.body;
|
|
||||||
|
|
||||||
// admin update username
|
|
||||||
const { token } = req.body;
|
|
||||||
if (authenticate(token)) {
|
|
||||||
const { name, username, id, range } = req.body;
|
|
||||||
const result = update.run(name, range, username, id);
|
|
||||||
res.status(200).json({
|
|
||||||
success: true,
|
|
||||||
});
|
|
||||||
return;
|
|
||||||
}
|
|
||||||
|
|
||||||
// check if id is valid
|
|
||||||
// check if username is valid
|
|
||||||
if (username === undefined) {
|
|
||||||
res.status(400).json({
|
|
||||||
error: `缺少参数 username`,
|
|
||||||
});
|
|
||||||
return;
|
|
||||||
}
|
|
||||||
|
|
||||||
try {
|
|
||||||
const limit = getLimit();
|
|
||||||
updateUsernameWithLimit(username, id, limit);
|
|
||||||
} catch (err) {
|
|
||||||
res.status(400).json({
|
|
||||||
error: err.message,
|
|
||||||
});
|
|
||||||
return;
|
|
||||||
}
|
|
||||||
|
|
||||||
// not allow
|
|
||||||
} else {
|
|
||||||
res.status(405).send({ error: "方法" + req.method + "不被允许" });
|
|
||||||
}
|
|
||||||
|
|
||||||
res.status(200).json({
|
|
||||||
success: true,
|
|
||||||
});
|
|
||||||
}
|
|
||||||
@@ -1,29 +0,0 @@
|
|||||||
import { authenticate, getStarted, setStarted } from "../../../libs/db";
|
|
||||||
import { addAPIQPS } from "../../../libs/stats";
|
|
||||||
|
|
||||||
export default function handler(req, res) {
|
|
||||||
addAPIQPS();
|
|
||||||
// get method
|
|
||||||
if (req.method === "GET") {
|
|
||||||
res.setHeader("Cache-Control", "no-cache no-store must-revalidate");
|
|
||||||
res.status(200).json(getStarted());
|
|
||||||
return;
|
|
||||||
} else if (req.method === "PUT") {
|
|
||||||
// authenticate
|
|
||||||
const { token } = req.body;
|
|
||||||
if (!authenticate(token)) {
|
|
||||||
res.status(401).json({
|
|
||||||
error: `token ${token} 验证失败`,
|
|
||||||
});
|
|
||||||
return;
|
|
||||||
}
|
|
||||||
// set method
|
|
||||||
const { started } = req.body;
|
|
||||||
setStarted(started);
|
|
||||||
res.status(200).json({
|
|
||||||
success: true,
|
|
||||||
});
|
|
||||||
} else {
|
|
||||||
res.status(405).send({ error: "方法" + req.method + "不被允许" });
|
|
||||||
}
|
|
||||||
}
|
|
||||||
@@ -1,82 +0,0 @@
|
|||||||
import { useState, useEffect } from "react";
|
|
||||||
import { useRouter } from "next/router";
|
|
||||||
import Link from "next/link";
|
|
||||||
import {
|
|
||||||
Alert,
|
|
||||||
Button,
|
|
||||||
TextField,
|
|
||||||
Stack,
|
|
||||||
InputField,
|
|
||||||
Box,
|
|
||||||
Snackbar,
|
|
||||||
Container,
|
|
||||||
} from "@mui/material";
|
|
||||||
|
|
||||||
export default function Index(props) {
|
|
||||||
const [username, setUsername] = useState("");
|
|
||||||
const [snackbarOpen, setSnackbarOpen] = useState(false);
|
|
||||||
|
|
||||||
// get username from localStorage
|
|
||||||
useEffect(() => {
|
|
||||||
const localUsername = localStorage.getItem("username");
|
|
||||||
if (localUsername) {
|
|
||||||
setUsername(localUsername);
|
|
||||||
}
|
|
||||||
}, []);
|
|
||||||
|
|
||||||
const router = useRouter();
|
|
||||||
|
|
||||||
const login = () => {
|
|
||||||
if (!username) {
|
|
||||||
setSnackbarOpen(true);
|
|
||||||
return;
|
|
||||||
}
|
|
||||||
// set local storage
|
|
||||||
localStorage.setItem("username", username);
|
|
||||||
props.setUsername(username);
|
|
||||||
};
|
|
||||||
|
|
||||||
useEffect(() => {
|
|
||||||
if (props.username) {
|
|
||||||
router.push("/time");
|
|
||||||
}
|
|
||||||
}, [props.username]);
|
|
||||||
|
|
||||||
return (
|
|
||||||
<Container>
|
|
||||||
<Stack direction="row" spacing={2}>
|
|
||||||
<TextField
|
|
||||||
label="您的大名?"
|
|
||||||
value={username}
|
|
||||||
onChange={(e) => setUsername(e.target.value)}
|
|
||||||
onKeyUp={(e) => {
|
|
||||||
if (e.key === "Enter") {
|
|
||||||
login();
|
|
||||||
}
|
|
||||||
}}
|
|
||||||
/>
|
|
||||||
<Link href="/time" passHref>
|
|
||||||
<Button
|
|
||||||
variant="contained"
|
|
||||||
color="primary"
|
|
||||||
onClick={(e) => {
|
|
||||||
e.preventDefault();
|
|
||||||
login();
|
|
||||||
}}
|
|
||||||
>
|
|
||||||
登入
|
|
||||||
</Button>
|
|
||||||
</Link>
|
|
||||||
</Stack>
|
|
||||||
<Snackbar
|
|
||||||
open={snackbarOpen}
|
|
||||||
autoHideDuration={1000}
|
|
||||||
onClose={() => setSnackbarOpen(false)}
|
|
||||||
>
|
|
||||||
<Alert variant="filled" severity="error">
|
|
||||||
请输入您的大名
|
|
||||||
</Alert>
|
|
||||||
</Snackbar>
|
|
||||||
</Container>
|
|
||||||
);
|
|
||||||
}
|
|
||||||
474
pages/time.js
474
pages/time.js
@@ -1,474 +0,0 @@
|
|||||||
import {
|
|
||||||
Container,
|
|
||||||
Box,
|
|
||||||
Alert,
|
|
||||||
Snackbar,
|
|
||||||
Button,
|
|
||||||
FormGroup,
|
|
||||||
FormControlLabel,
|
|
||||||
Checkbox,
|
|
||||||
Stack,
|
|
||||||
Dialog,
|
|
||||||
DialogTitle,
|
|
||||||
DialogContent,
|
|
||||||
DialogActions,
|
|
||||||
Typography,
|
|
||||||
TextField,
|
|
||||||
TableContainer,
|
|
||||||
Table,
|
|
||||||
TableHead,
|
|
||||||
TableRow,
|
|
||||||
TableCell,
|
|
||||||
TableBody,
|
|
||||||
} from "@mui/material";
|
|
||||||
import { useEffect, useState } from "react";
|
|
||||||
import { useRouter } from "next/router";
|
|
||||||
import config from "../next.config";
|
|
||||||
|
|
||||||
const prefix = config.basePath ? config.basePath : "";
|
|
||||||
|
|
||||||
export default function Time(props) {
|
|
||||||
const [ranges, setRanges] = useState([]);
|
|
||||||
const [range, setRange] = useState("");
|
|
||||||
const [newName, setNewName] = useState("");
|
|
||||||
const [snackbarError, setSnackbarError] = useState(false);
|
|
||||||
const [snackbarErrorMessage, setSnackbarErrorMessage] = useState("");
|
|
||||||
const [snackbarSuccess, setSnackbarSuccess] = useState(false);
|
|
||||||
const [limit, setLimit] = useState(1);
|
|
||||||
const [inputedLimit, setInputedLimit] = useState(1);
|
|
||||||
const [token, setToken] = useState("");
|
|
||||||
const [stats, setStats] = useState({});
|
|
||||||
|
|
||||||
const [onlyShowAvaliable, setOnlyShowAvaliable] = useState(false);
|
|
||||||
|
|
||||||
const [modifyTime, setModifyTime] = useState({});
|
|
||||||
|
|
||||||
const router = useRouter();
|
|
||||||
|
|
||||||
const modifyRange = () => {
|
|
||||||
fetch(`${prefix}/api/time/ranges/${modifyTime.id}`, {
|
|
||||||
method: "PUT",
|
|
||||||
headers: {
|
|
||||||
"Content-Type": "application/json",
|
|
||||||
},
|
|
||||||
body: JSON.stringify({ token, ...modifyTime }),
|
|
||||||
})
|
|
||||||
.then((res) => res.json())
|
|
||||||
.then((res) => {
|
|
||||||
if (res.error) {
|
|
||||||
setSnackbarError(true);
|
|
||||||
setSnackbarErrorMessage(res.error);
|
|
||||||
} else {
|
|
||||||
setModifyTime({});
|
|
||||||
setSnackbarSuccess(true);
|
|
||||||
refreshRanges();
|
|
||||||
}
|
|
||||||
});
|
|
||||||
};
|
|
||||||
|
|
||||||
const getStats = () => {
|
|
||||||
fetch(`${prefix}/api/stats`, {
|
|
||||||
method: "GET",
|
|
||||||
})
|
|
||||||
.then((res) => res.json())
|
|
||||||
.then((res) => {
|
|
||||||
setStats(res);
|
|
||||||
});
|
|
||||||
};
|
|
||||||
|
|
||||||
const setStarted = (started) => {
|
|
||||||
fetch(`${prefix}/api/time/started`, {
|
|
||||||
method: "PUT",
|
|
||||||
headers: {
|
|
||||||
"Content-Type": "application/json",
|
|
||||||
},
|
|
||||||
body: JSON.stringify({ token, started }),
|
|
||||||
})
|
|
||||||
.then((res) => res.json())
|
|
||||||
.then((res) => {
|
|
||||||
if (res.error) {
|
|
||||||
setSnackbarError(true);
|
|
||||||
setSnackbarErrorMessage(res.error);
|
|
||||||
} else {
|
|
||||||
setSnackbarSuccess(true);
|
|
||||||
}
|
|
||||||
});
|
|
||||||
};
|
|
||||||
|
|
||||||
const isAdmin = () => {
|
|
||||||
if (props.username === "admin") {
|
|
||||||
return true;
|
|
||||||
} else {
|
|
||||||
return false;
|
|
||||||
}
|
|
||||||
};
|
|
||||||
|
|
||||||
const getLimit = () => {
|
|
||||||
fetch(`${prefix}/api/time/limit`, {
|
|
||||||
method: "GET",
|
|
||||||
headers: {
|
|
||||||
"Content-Type": "application/json",
|
|
||||||
},
|
|
||||||
})
|
|
||||||
.then((res) => res.json())
|
|
||||||
.then((res) => {
|
|
||||||
setLimit(res.limit);
|
|
||||||
});
|
|
||||||
};
|
|
||||||
|
|
||||||
const addRange = () => {
|
|
||||||
fetch(`${prefix}/api/time/ranges`, {
|
|
||||||
method: "POST",
|
|
||||||
headers: {
|
|
||||||
"Content-Type": "application/json",
|
|
||||||
},
|
|
||||||
body: JSON.stringify({
|
|
||||||
name: newName,
|
|
||||||
range,
|
|
||||||
token,
|
|
||||||
}),
|
|
||||||
}).then((res) =>
|
|
||||||
res.json().then((res) => {
|
|
||||||
if (res.error) {
|
|
||||||
setSnackbarError(true);
|
|
||||||
setSnackbarErrorMessage(res.error);
|
|
||||||
} else {
|
|
||||||
setSnackbarSuccess(true);
|
|
||||||
refreshRanges();
|
|
||||||
}
|
|
||||||
})
|
|
||||||
);
|
|
||||||
};
|
|
||||||
|
|
||||||
const refreshRanges = () => {
|
|
||||||
fetch(`${prefix}/api/time/ranges`)
|
|
||||||
.then((res) => res.json())
|
|
||||||
.then((res) => {
|
|
||||||
if (res.error) {
|
|
||||||
setSnackbarError(true);
|
|
||||||
setSnackbarErrorMessage(res.error);
|
|
||||||
} else {
|
|
||||||
setRanges(res);
|
|
||||||
}
|
|
||||||
});
|
|
||||||
};
|
|
||||||
|
|
||||||
const deleteRange = (id) => {
|
|
||||||
fetch(`${prefix}/api/time/ranges/${id}`, {
|
|
||||||
method: "DELETE",
|
|
||||||
headers: {
|
|
||||||
"Content-Type": "application/json",
|
|
||||||
},
|
|
||||||
body: JSON.stringify({ token }),
|
|
||||||
})
|
|
||||||
.then((res) => res.json())
|
|
||||||
.then((res) => {
|
|
||||||
if (res.error) {
|
|
||||||
setSnackbarError(true);
|
|
||||||
setSnackbarErrorMessage(res.error);
|
|
||||||
} else {
|
|
||||||
setSnackbarSuccess(true);
|
|
||||||
refreshRanges();
|
|
||||||
}
|
|
||||||
});
|
|
||||||
};
|
|
||||||
|
|
||||||
const updateUsername = (id, username) => {
|
|
||||||
fetch(`${prefix}/api/time/ranges/${id}`, {
|
|
||||||
method: "PUT",
|
|
||||||
headers: {
|
|
||||||
"Content-Type": "application/json",
|
|
||||||
},
|
|
||||||
body: JSON.stringify({ username }),
|
|
||||||
})
|
|
||||||
.then((res) => res.json())
|
|
||||||
.then((res) => {
|
|
||||||
if (res.error) {
|
|
||||||
setSnackbarError(true);
|
|
||||||
setSnackbarErrorMessage(res.error);
|
|
||||||
refreshRanges();
|
|
||||||
} else {
|
|
||||||
setSnackbarSuccess(true);
|
|
||||||
refreshRanges();
|
|
||||||
}
|
|
||||||
});
|
|
||||||
};
|
|
||||||
|
|
||||||
const updateLimit = (limit) => {
|
|
||||||
fetch(`${prefix}/api/time/limit`, {
|
|
||||||
method: "PUT",
|
|
||||||
headers: {
|
|
||||||
"Content-Type": "application/json",
|
|
||||||
},
|
|
||||||
body: JSON.stringify({ limit, token }),
|
|
||||||
})
|
|
||||||
.then((res) => res.json())
|
|
||||||
.then((res) => {
|
|
||||||
if (res.error) {
|
|
||||||
setSnackbarError(true);
|
|
||||||
setSnackbarErrorMessage(res.error);
|
|
||||||
} else {
|
|
||||||
setSnackbarSuccess(true);
|
|
||||||
refreshRanges();
|
|
||||||
}
|
|
||||||
});
|
|
||||||
};
|
|
||||||
|
|
||||||
useEffect(() => {
|
|
||||||
if (!props.username) {
|
|
||||||
router.push("/");
|
|
||||||
}
|
|
||||||
});
|
|
||||||
|
|
||||||
useEffect(() => {
|
|
||||||
refreshRanges();
|
|
||||||
getLimit();
|
|
||||||
getStats();
|
|
||||||
const interval = setInterval(() => {
|
|
||||||
getLimit();
|
|
||||||
getStats();
|
|
||||||
if (!onlyShowAvaliable) {
|
|
||||||
refreshRanges();
|
|
||||||
}
|
|
||||||
}, 1000);
|
|
||||||
return () => clearInterval(interval);
|
|
||||||
}, [onlyShowAvaliable]);
|
|
||||||
|
|
||||||
/*
|
|
||||||
useEffect(() => {
|
|
||||||
refreshRanges();
|
|
||||||
}, []);
|
|
||||||
*/
|
|
||||||
|
|
||||||
return (
|
|
||||||
<Container>
|
|
||||||
{isAdmin() && (
|
|
||||||
<Box>
|
|
||||||
<Box
|
|
||||||
sx={{
|
|
||||||
my: 2,
|
|
||||||
}}
|
|
||||||
>
|
|
||||||
<TextField
|
|
||||||
label="Token"
|
|
||||||
value={token}
|
|
||||||
onChange={(e) => setToken(e.target.value)}
|
|
||||||
/>
|
|
||||||
<Box
|
|
||||||
sx={{
|
|
||||||
my: 2,
|
|
||||||
}}
|
|
||||||
>
|
|
||||||
<TextField
|
|
||||||
label="名称"
|
|
||||||
value={newName}
|
|
||||||
onChange={(e) => setNewName(e.target.value)}
|
|
||||||
/>
|
|
||||||
<TextField
|
|
||||||
label="时间段"
|
|
||||||
value={range}
|
|
||||||
onChange={(e) => setRange(e.target.value)}
|
|
||||||
placeholder="2022-01-01 00:00:00"
|
|
||||||
/>
|
|
||||||
</Box>
|
|
||||||
<Button
|
|
||||||
variant="contained"
|
|
||||||
color="primary"
|
|
||||||
onClick={() => addRange()}
|
|
||||||
>
|
|
||||||
添加
|
|
||||||
</Button>
|
|
||||||
<Box
|
|
||||||
sx={{
|
|
||||||
my: 2,
|
|
||||||
}}
|
|
||||||
>
|
|
||||||
<TextField
|
|
||||||
label="每人数量上限"
|
|
||||||
value={inputedLimit}
|
|
||||||
onChange={(e) => setInputedLimit(e.target.value)}
|
|
||||||
/>
|
|
||||||
<Button
|
|
||||||
variant="contained"
|
|
||||||
color="primary"
|
|
||||||
onClick={() => {
|
|
||||||
updateLimit(inputedLimit);
|
|
||||||
}}
|
|
||||||
>
|
|
||||||
修改上限
|
|
||||||
</Button>
|
|
||||||
<Button
|
|
||||||
variant="contained"
|
|
||||||
color="primary"
|
|
||||||
onClick={() => setStarted(true)}
|
|
||||||
>
|
|
||||||
开始
|
|
||||||
</Button>
|
|
||||||
<Button
|
|
||||||
variant="contained"
|
|
||||||
color="primary"
|
|
||||||
onClick={() => setStarted(false)}
|
|
||||||
>
|
|
||||||
停止
|
|
||||||
</Button>
|
|
||||||
</Box>
|
|
||||||
</Box>
|
|
||||||
</Box>
|
|
||||||
)}
|
|
||||||
<Box
|
|
||||||
sx={{
|
|
||||||
display: "flex",
|
|
||||||
flexDirection: "row",
|
|
||||||
alignItems: "center",
|
|
||||||
justifyContent: "space-between",
|
|
||||||
}}
|
|
||||||
>
|
|
||||||
<Button
|
|
||||||
sx={{
|
|
||||||
userSelect: "none",
|
|
||||||
}}
|
|
||||||
variant="contained"
|
|
||||||
color="primary"
|
|
||||||
onClick={() => refreshRanges()}
|
|
||||||
>
|
|
||||||
Refresh
|
|
||||||
</Button>
|
|
||||||
<FormGroup>
|
|
||||||
<FormControlLabel
|
|
||||||
control={
|
|
||||||
<Checkbox
|
|
||||||
checked={onlyShowAvaliable}
|
|
||||||
onChange={(e) => setOnlyShowAvaliable(e.target.checked)}
|
|
||||||
/>
|
|
||||||
}
|
|
||||||
label="仅显示空余"
|
|
||||||
/>
|
|
||||||
</FormGroup>
|
|
||||||
<Typography>
|
|
||||||
当前每人数量上限: {limit}
|
|
||||||
<br />
|
|
||||||
服务器负载 (QPS): {stats.apiqps}
|
|
||||||
</Typography>
|
|
||||||
</Box>
|
|
||||||
<TableContainer>
|
|
||||||
<Table>
|
|
||||||
<TableHead>
|
|
||||||
<TableRow>
|
|
||||||
<TableCell>名称</TableCell>
|
|
||||||
<TableCell>时间段</TableCell>
|
|
||||||
<TableCell>操作</TableCell>
|
|
||||||
</TableRow>
|
|
||||||
</TableHead>
|
|
||||||
<TableBody>
|
|
||||||
{ranges.map((range) => {
|
|
||||||
if (onlyShowAvaliable && range.username !== "") {
|
|
||||||
return;
|
|
||||||
}
|
|
||||||
return (
|
|
||||||
<TableRow key={range.id}>
|
|
||||||
<TableCell>{range.name}</TableCell>
|
|
||||||
<TableCell>{range.range}</TableCell>
|
|
||||||
<TableCell>
|
|
||||||
<Button
|
|
||||||
sx={{
|
|
||||||
userSelect: "none",
|
|
||||||
}}
|
|
||||||
disabled={range.username !== ""}
|
|
||||||
variant="contained"
|
|
||||||
color="primary"
|
|
||||||
onClick={() => updateUsername(range.id, props.username)}
|
|
||||||
>
|
|
||||||
{range.username ? range.username : "抢!"}
|
|
||||||
</Button>
|
|
||||||
{isAdmin() && (
|
|
||||||
<>
|
|
||||||
<Button
|
|
||||||
variant="contained"
|
|
||||||
color="secondary"
|
|
||||||
onClick={() => setModifyTime(range)}
|
|
||||||
>
|
|
||||||
修改
|
|
||||||
</Button>
|
|
||||||
<Button
|
|
||||||
variant="contained"
|
|
||||||
color="secondary"
|
|
||||||
onClick={() => deleteRange(range.id)}
|
|
||||||
>
|
|
||||||
删除
|
|
||||||
</Button>
|
|
||||||
</>
|
|
||||||
)}
|
|
||||||
</TableCell>
|
|
||||||
</TableRow>
|
|
||||||
);
|
|
||||||
})}
|
|
||||||
</TableBody>
|
|
||||||
</Table>
|
|
||||||
</TableContainer>
|
|
||||||
<Snackbar
|
|
||||||
open={snackbarError}
|
|
||||||
autoHideDuration={1000}
|
|
||||||
onClose={() => setSnackbarError(false)}
|
|
||||||
>
|
|
||||||
<Alert
|
|
||||||
variant="filled"
|
|
||||||
onClose={() => setSnackbarError(false)}
|
|
||||||
severity="error"
|
|
||||||
>
|
|
||||||
{snackbarErrorMessage}
|
|
||||||
</Alert>
|
|
||||||
</Snackbar>
|
|
||||||
<Snackbar
|
|
||||||
open={snackbarSuccess}
|
|
||||||
autoHideDuration={1000}
|
|
||||||
onClose={() => setSnackbarSuccess(false)}
|
|
||||||
>
|
|
||||||
<Alert
|
|
||||||
variant="filled"
|
|
||||||
onClose={() => setSnackbarSuccess(false)}
|
|
||||||
severity="success"
|
|
||||||
>
|
|
||||||
操作成功!
|
|
||||||
</Alert>
|
|
||||||
</Snackbar>
|
|
||||||
<Dialog open={modifyTime.id} onClose={() => setModifyTime({})}>
|
|
||||||
<DialogTitle>修改时间段 {modifyTime.id}</DialogTitle>
|
|
||||||
<DialogContent>
|
|
||||||
<Stack
|
|
||||||
sx={{
|
|
||||||
mt: 1,
|
|
||||||
}}
|
|
||||||
spacing={2}
|
|
||||||
>
|
|
||||||
<TextField
|
|
||||||
label="名称"
|
|
||||||
value={modifyTime.name}
|
|
||||||
onChange={(e) =>
|
|
||||||
setModifyTime({ ...modifyTime, name: e.target.value })
|
|
||||||
}
|
|
||||||
/>
|
|
||||||
<TextField
|
|
||||||
label="时间段"
|
|
||||||
value={modifyTime.range}
|
|
||||||
onChange={(e) =>
|
|
||||||
setModifyTime({ ...modifyTime, range: e.target.value })
|
|
||||||
}
|
|
||||||
placeholder="2022-01-01 00:00:00"
|
|
||||||
/>
|
|
||||||
<TextField
|
|
||||||
label="姓名"
|
|
||||||
value={modifyTime.username}
|
|
||||||
onChange={(e) =>
|
|
||||||
setModifyTime({ ...modifyTime, username: e.target.value })
|
|
||||||
}
|
|
||||||
/>
|
|
||||||
</Stack>
|
|
||||||
</DialogContent>
|
|
||||||
<DialogActions>
|
|
||||||
<Button onClick={() => setModifyTime({})}>取消</Button>
|
|
||||||
<Button onClick={modifyRange}>确定</Button>
|
|
||||||
</DialogActions>
|
|
||||||
</Dialog>
|
|
||||||
</Container>
|
|
||||||
);
|
|
||||||
}
|
|
||||||
Binary file not shown.
|
Before Width: | Height: | Size: 25 KiB |
@@ -1,4 +0,0 @@
|
|||||||
<svg width="283" height="64" viewBox="0 0 283 64" fill="none"
|
|
||||||
xmlns="http://www.w3.org/2000/svg">
|
|
||||||
<path d="M141.04 16c-11.04 0-19 7.2-19 18s8.96 18 20 18c6.67 0 12.55-2.64 16.19-7.09l-7.65-4.42c-2.02 2.21-5.09 3.5-8.54 3.5-4.79 0-8.86-2.5-10.37-6.5h28.02c.22-1.12.35-2.28.35-3.5 0-10.79-7.96-17.99-19-17.99zm-9.46 14.5c1.25-3.99 4.67-6.5 9.45-6.5 4.79 0 8.21 2.51 9.45 6.5h-18.9zM248.72 16c-11.04 0-19 7.2-19 18s8.96 18 20 18c6.67 0 12.55-2.64 16.19-7.09l-7.65-4.42c-2.02 2.21-5.09 3.5-8.54 3.5-4.79 0-8.86-2.5-10.37-6.5h28.02c.22-1.12.35-2.28.35-3.5 0-10.79-7.96-17.99-19-17.99zm-9.45 14.5c1.25-3.99 4.67-6.5 9.45-6.5 4.79 0 8.21 2.51 9.45 6.5h-18.9zM200.24 34c0 6 3.92 10 10 10 4.12 0 7.21-1.87 8.8-4.92l7.68 4.43c-3.18 5.3-9.14 8.49-16.48 8.49-11.05 0-19-7.2-19-18s7.96-18 19-18c7.34 0 13.29 3.19 16.48 8.49l-7.68 4.43c-1.59-3.05-4.68-4.92-8.8-4.92-6.07 0-10 4-10 10zm82.48-29v46h-9V5h9zM36.95 0L73.9 64H0L36.95 0zm92.38 5l-27.71 48L73.91 5H84.3l17.32 30 17.32-30h10.39zm58.91 12v9.69c-1-.29-2.06-.49-3.2-.49-5.81 0-10 4-10 10V51h-9V17h9v9.2c0-5.08 5.91-9.2 13.2-9.2z" fill="#000"/>
|
|
||||||
</svg>
|
|
||||||
|
Before Width: | Height: | Size: 1.1 KiB |
@@ -1,116 +0,0 @@
|
|||||||
.container {
|
|
||||||
padding: 0 2rem;
|
|
||||||
}
|
|
||||||
|
|
||||||
.main {
|
|
||||||
min-height: 100vh;
|
|
||||||
padding: 4rem 0;
|
|
||||||
flex: 1;
|
|
||||||
display: flex;
|
|
||||||
flex-direction: column;
|
|
||||||
justify-content: center;
|
|
||||||
align-items: center;
|
|
||||||
}
|
|
||||||
|
|
||||||
.footer {
|
|
||||||
display: flex;
|
|
||||||
flex: 1;
|
|
||||||
padding: 2rem 0;
|
|
||||||
border-top: 1px solid #eaeaea;
|
|
||||||
justify-content: center;
|
|
||||||
align-items: center;
|
|
||||||
}
|
|
||||||
|
|
||||||
.footer a {
|
|
||||||
display: flex;
|
|
||||||
justify-content: center;
|
|
||||||
align-items: center;
|
|
||||||
flex-grow: 1;
|
|
||||||
}
|
|
||||||
|
|
||||||
.title a {
|
|
||||||
color: #0070f3;
|
|
||||||
text-decoration: none;
|
|
||||||
}
|
|
||||||
|
|
||||||
.title a:hover,
|
|
||||||
.title a:focus,
|
|
||||||
.title a:active {
|
|
||||||
text-decoration: underline;
|
|
||||||
}
|
|
||||||
|
|
||||||
.title {
|
|
||||||
margin: 0;
|
|
||||||
line-height: 1.15;
|
|
||||||
font-size: 4rem;
|
|
||||||
}
|
|
||||||
|
|
||||||
.title,
|
|
||||||
.description {
|
|
||||||
text-align: center;
|
|
||||||
}
|
|
||||||
|
|
||||||
.description {
|
|
||||||
margin: 4rem 0;
|
|
||||||
line-height: 1.5;
|
|
||||||
font-size: 1.5rem;
|
|
||||||
}
|
|
||||||
|
|
||||||
.code {
|
|
||||||
background: #fafafa;
|
|
||||||
border-radius: 5px;
|
|
||||||
padding: 0.75rem;
|
|
||||||
font-size: 1.1rem;
|
|
||||||
font-family: Menlo, Monaco, Lucida Console, Liberation Mono, DejaVu Sans Mono,
|
|
||||||
Bitstream Vera Sans Mono, Courier New, monospace;
|
|
||||||
}
|
|
||||||
|
|
||||||
.grid {
|
|
||||||
display: flex;
|
|
||||||
align-items: center;
|
|
||||||
justify-content: center;
|
|
||||||
flex-wrap: wrap;
|
|
||||||
max-width: 800px;
|
|
||||||
}
|
|
||||||
|
|
||||||
.card {
|
|
||||||
margin: 1rem;
|
|
||||||
padding: 1.5rem;
|
|
||||||
text-align: left;
|
|
||||||
color: inherit;
|
|
||||||
text-decoration: none;
|
|
||||||
border: 1px solid #eaeaea;
|
|
||||||
border-radius: 10px;
|
|
||||||
transition: color 0.15s ease, border-color 0.15s ease;
|
|
||||||
max-width: 300px;
|
|
||||||
}
|
|
||||||
|
|
||||||
.card:hover,
|
|
||||||
.card:focus,
|
|
||||||
.card:active {
|
|
||||||
color: #0070f3;
|
|
||||||
border-color: #0070f3;
|
|
||||||
}
|
|
||||||
|
|
||||||
.card h2 {
|
|
||||||
margin: 0 0 1rem 0;
|
|
||||||
font-size: 1.5rem;
|
|
||||||
}
|
|
||||||
|
|
||||||
.card p {
|
|
||||||
margin: 0;
|
|
||||||
font-size: 1.25rem;
|
|
||||||
line-height: 1.5;
|
|
||||||
}
|
|
||||||
|
|
||||||
.logo {
|
|
||||||
height: 1em;
|
|
||||||
margin-left: 0.5rem;
|
|
||||||
}
|
|
||||||
|
|
||||||
@media (max-width: 600px) {
|
|
||||||
.grid {
|
|
||||||
width: 100%;
|
|
||||||
flex-direction: column;
|
|
||||||
}
|
|
||||||
}
|
|
||||||
@@ -1,16 +0,0 @@
|
|||||||
html,
|
|
||||||
body {
|
|
||||||
padding: 0;
|
|
||||||
margin: 0;
|
|
||||||
font-family: -apple-system, BlinkMacSystemFont, Segoe UI, Roboto, Oxygen,
|
|
||||||
Ubuntu, Cantarell, Fira Sans, Droid Sans, Helvetica Neue, sans-serif;
|
|
||||||
}
|
|
||||||
|
|
||||||
a {
|
|
||||||
color: inherit;
|
|
||||||
text-decoration: none;
|
|
||||||
}
|
|
||||||
|
|
||||||
* {
|
|
||||||
box-sizing: border-box;
|
|
||||||
}
|
|
||||||
19
.gitignore → web/.gitignore
vendored
19
.gitignore → web/.gitignore
vendored
@@ -8,27 +8,16 @@
|
|||||||
# testing
|
# testing
|
||||||
/coverage
|
/coverage
|
||||||
|
|
||||||
# next.js
|
|
||||||
/.next/
|
|
||||||
/out/
|
|
||||||
|
|
||||||
# production
|
# production
|
||||||
/build
|
/build
|
||||||
|
|
||||||
# misc
|
# misc
|
||||||
.DS_Store
|
.DS_Store
|
||||||
*.pem
|
.env.local
|
||||||
|
.env.development.local
|
||||||
|
.env.test.local
|
||||||
|
.env.production.local
|
||||||
|
|
||||||
# debug
|
|
||||||
npm-debug.log*
|
npm-debug.log*
|
||||||
yarn-debug.log*
|
yarn-debug.log*
|
||||||
yarn-error.log*
|
yarn-error.log*
|
||||||
.pnpm-debug.log*
|
|
||||||
|
|
||||||
# local env files
|
|
||||||
.env*.local
|
|
||||||
|
|
||||||
# vercel
|
|
||||||
.vercel
|
|
||||||
|
|
||||||
/db.sqlite
|
|
||||||
70
web/README.md
Normal file
70
web/README.md
Normal file
@@ -0,0 +1,70 @@
|
|||||||
|
# Getting Started with Create React App
|
||||||
|
|
||||||
|
This project was bootstrapped with [Create React App](https://github.com/facebook/create-react-app).
|
||||||
|
|
||||||
|
## Available Scripts
|
||||||
|
|
||||||
|
In the project directory, you can run:
|
||||||
|
|
||||||
|
### `npm start`
|
||||||
|
|
||||||
|
Runs the app in the development mode.\
|
||||||
|
Open [http://localhost:3000](http://localhost:3000) to view it in your browser.
|
||||||
|
|
||||||
|
The page will reload when you make changes.\
|
||||||
|
You may also see any lint errors in the console.
|
||||||
|
|
||||||
|
### `npm test`
|
||||||
|
|
||||||
|
Launches the test runner in the interactive watch mode.\
|
||||||
|
See the section about [running tests](https://facebook.github.io/create-react-app/docs/running-tests) for more information.
|
||||||
|
|
||||||
|
### `npm run build`
|
||||||
|
|
||||||
|
Builds the app for production to the `build` folder.\
|
||||||
|
It correctly bundles React in production mode and optimizes the build for the best performance.
|
||||||
|
|
||||||
|
The build is minified and the filenames include the hashes.\
|
||||||
|
Your app is ready to be deployed!
|
||||||
|
|
||||||
|
See the section about [deployment](https://facebook.github.io/create-react-app/docs/deployment) for more information.
|
||||||
|
|
||||||
|
### `npm run eject`
|
||||||
|
|
||||||
|
**Note: this is a one-way operation. Once you `eject`, you can't go back!**
|
||||||
|
|
||||||
|
If you aren't satisfied with the build tool and configuration choices, you can `eject` at any time. This command will remove the single build dependency from your project.
|
||||||
|
|
||||||
|
Instead, it will copy all the configuration files and the transitive dependencies (webpack, Babel, ESLint, etc) right into your project so you have full control over them. All of the commands except `eject` will still work, but they will point to the copied scripts so you can tweak them. At this point you're on your own.
|
||||||
|
|
||||||
|
You don't have to ever use `eject`. The curated feature set is suitable for small and middle deployments, and you shouldn't feel obligated to use this feature. However we understand that this tool wouldn't be useful if you couldn't customize it when you are ready for it.
|
||||||
|
|
||||||
|
## Learn More
|
||||||
|
|
||||||
|
You can learn more in the [Create React App documentation](https://facebook.github.io/create-react-app/docs/getting-started).
|
||||||
|
|
||||||
|
To learn React, check out the [React documentation](https://reactjs.org/).
|
||||||
|
|
||||||
|
### Code Splitting
|
||||||
|
|
||||||
|
This section has moved here: [https://facebook.github.io/create-react-app/docs/code-splitting](https://facebook.github.io/create-react-app/docs/code-splitting)
|
||||||
|
|
||||||
|
### Analyzing the Bundle Size
|
||||||
|
|
||||||
|
This section has moved here: [https://facebook.github.io/create-react-app/docs/analyzing-the-bundle-size](https://facebook.github.io/create-react-app/docs/analyzing-the-bundle-size)
|
||||||
|
|
||||||
|
### Making a Progressive Web App
|
||||||
|
|
||||||
|
This section has moved here: [https://facebook.github.io/create-react-app/docs/making-a-progressive-web-app](https://facebook.github.io/create-react-app/docs/making-a-progressive-web-app)
|
||||||
|
|
||||||
|
### Advanced Configuration
|
||||||
|
|
||||||
|
This section has moved here: [https://facebook.github.io/create-react-app/docs/advanced-configuration](https://facebook.github.io/create-react-app/docs/advanced-configuration)
|
||||||
|
|
||||||
|
### Deployment
|
||||||
|
|
||||||
|
This section has moved here: [https://facebook.github.io/create-react-app/docs/deployment](https://facebook.github.io/create-react-app/docs/deployment)
|
||||||
|
|
||||||
|
### `npm run build` fails to minify
|
||||||
|
|
||||||
|
This section has moved here: [https://facebook.github.io/create-react-app/docs/troubleshooting#npm-run-build-fails-to-minify](https://facebook.github.io/create-react-app/docs/troubleshooting#npm-run-build-fails-to-minify)
|
||||||
28848
web/package-lock.json
generated
Normal file
28848
web/package-lock.json
generated
Normal file
File diff suppressed because it is too large
Load Diff
43
web/package.json
Normal file
43
web/package.json
Normal file
@@ -0,0 +1,43 @@
|
|||||||
|
{
|
||||||
|
"name": "web",
|
||||||
|
"version": "0.1.0",
|
||||||
|
"private": true,
|
||||||
|
"dependencies": {
|
||||||
|
"@emotion/react": "^11.10.5",
|
||||||
|
"@emotion/styled": "^11.10.5",
|
||||||
|
"@mui/icons-material": "^5.10.14",
|
||||||
|
"@mui/material": "^5.10.14",
|
||||||
|
"@testing-library/jest-dom": "^5.16.5",
|
||||||
|
"@testing-library/react": "^13.4.0",
|
||||||
|
"@testing-library/user-event": "^13.5.0",
|
||||||
|
"react": "^18.2.0",
|
||||||
|
"react-dom": "^18.2.0",
|
||||||
|
"react-router-dom": "^6.4.3",
|
||||||
|
"react-scripts": "5.0.1",
|
||||||
|
"web-vitals": "^2.1.4"
|
||||||
|
},
|
||||||
|
"scripts": {
|
||||||
|
"start": "react-scripts start",
|
||||||
|
"build": "react-scripts build",
|
||||||
|
"test": "react-scripts test",
|
||||||
|
"eject": "react-scripts eject"
|
||||||
|
},
|
||||||
|
"eslintConfig": {
|
||||||
|
"extends": [
|
||||||
|
"react-app",
|
||||||
|
"react-app/jest"
|
||||||
|
]
|
||||||
|
},
|
||||||
|
"browserslist": {
|
||||||
|
"production": [
|
||||||
|
">0.2%",
|
||||||
|
"not dead",
|
||||||
|
"not op_mini all"
|
||||||
|
],
|
||||||
|
"development": [
|
||||||
|
"last 1 chrome version",
|
||||||
|
"last 1 firefox version",
|
||||||
|
"last 1 safari version"
|
||||||
|
]
|
||||||
|
}
|
||||||
|
}
|
||||||
BIN
web/public/favicon.ico
Normal file
BIN
web/public/favicon.ico
Normal file
Binary file not shown.
|
After Width: | Height: | Size: 3.8 KiB |
43
web/public/index.html
Normal file
43
web/public/index.html
Normal file
@@ -0,0 +1,43 @@
|
|||||||
|
<!DOCTYPE html>
|
||||||
|
<html lang="en">
|
||||||
|
<head>
|
||||||
|
<meta charset="utf-8" />
|
||||||
|
<link rel="icon" href="%PUBLIC_URL%/favicon.ico" />
|
||||||
|
<meta name="viewport" content="width=device-width, initial-scale=1" />
|
||||||
|
<meta name="theme-color" content="#000000" />
|
||||||
|
<meta
|
||||||
|
name="description"
|
||||||
|
content="Web site created using create-react-app"
|
||||||
|
/>
|
||||||
|
<link rel="apple-touch-icon" href="%PUBLIC_URL%/logo192.png" />
|
||||||
|
<!--
|
||||||
|
manifest.json provides metadata used when your web app is installed on a
|
||||||
|
user's mobile device or desktop. See https://developers.google.com/web/fundamentals/web-app-manifest/
|
||||||
|
-->
|
||||||
|
<link rel="manifest" href="%PUBLIC_URL%/manifest.json" />
|
||||||
|
<!--
|
||||||
|
Notice the use of %PUBLIC_URL% in the tags above.
|
||||||
|
It will be replaced with the URL of the `public` folder during the build.
|
||||||
|
Only files inside the `public` folder can be referenced from the HTML.
|
||||||
|
|
||||||
|
Unlike "/favicon.ico" or "favicon.ico", "%PUBLIC_URL%/favicon.ico" will
|
||||||
|
work correctly both with client-side routing and a non-root public URL.
|
||||||
|
Learn how to configure a non-root public URL by running `npm run build`.
|
||||||
|
-->
|
||||||
|
<title>React App</title>
|
||||||
|
</head>
|
||||||
|
<body>
|
||||||
|
<noscript>You need to enable JavaScript to run this app.</noscript>
|
||||||
|
<div id="root"></div>
|
||||||
|
<!--
|
||||||
|
This HTML file is a template.
|
||||||
|
If you open it directly in the browser, you will see an empty page.
|
||||||
|
|
||||||
|
You can add webfonts, meta tags, or analytics to this file.
|
||||||
|
The build step will place the bundled scripts into the <body> tag.
|
||||||
|
|
||||||
|
To begin the development, run `npm start` or `yarn start`.
|
||||||
|
To create a production bundle, use `npm run build` or `yarn build`.
|
||||||
|
-->
|
||||||
|
</body>
|
||||||
|
</html>
|
||||||
BIN
web/public/logo192.png
Normal file
BIN
web/public/logo192.png
Normal file
Binary file not shown.
|
After Width: | Height: | Size: 5.2 KiB |
BIN
web/public/logo512.png
Normal file
BIN
web/public/logo512.png
Normal file
Binary file not shown.
|
After Width: | Height: | Size: 9.4 KiB |
25
web/public/manifest.json
Normal file
25
web/public/manifest.json
Normal file
@@ -0,0 +1,25 @@
|
|||||||
|
{
|
||||||
|
"short_name": "React App",
|
||||||
|
"name": "Create React App Sample",
|
||||||
|
"icons": [
|
||||||
|
{
|
||||||
|
"src": "favicon.ico",
|
||||||
|
"sizes": "64x64 32x32 24x24 16x16",
|
||||||
|
"type": "image/x-icon"
|
||||||
|
},
|
||||||
|
{
|
||||||
|
"src": "logo192.png",
|
||||||
|
"type": "image/png",
|
||||||
|
"sizes": "192x192"
|
||||||
|
},
|
||||||
|
{
|
||||||
|
"src": "logo512.png",
|
||||||
|
"type": "image/png",
|
||||||
|
"sizes": "512x512"
|
||||||
|
}
|
||||||
|
],
|
||||||
|
"start_url": ".",
|
||||||
|
"display": "standalone",
|
||||||
|
"theme_color": "#000000",
|
||||||
|
"background_color": "#ffffff"
|
||||||
|
}
|
||||||
3
web/public/robots.txt
Normal file
3
web/public/robots.txt
Normal file
@@ -0,0 +1,3 @@
|
|||||||
|
# https://www.robotstxt.org/robotstxt.html
|
||||||
|
User-agent: *
|
||||||
|
Disallow:
|
||||||
38
web/src/App.css
Normal file
38
web/src/App.css
Normal file
@@ -0,0 +1,38 @@
|
|||||||
|
.App {
|
||||||
|
text-align: center;
|
||||||
|
}
|
||||||
|
|
||||||
|
.App-logo {
|
||||||
|
height: 40vmin;
|
||||||
|
pointer-events: none;
|
||||||
|
}
|
||||||
|
|
||||||
|
@media (prefers-reduced-motion: no-preference) {
|
||||||
|
.App-logo {
|
||||||
|
animation: App-logo-spin infinite 20s linear;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
.App-header {
|
||||||
|
background-color: #282c34;
|
||||||
|
min-height: 100vh;
|
||||||
|
display: flex;
|
||||||
|
flex-direction: column;
|
||||||
|
align-items: center;
|
||||||
|
justify-content: center;
|
||||||
|
font-size: calc(10px + 2vmin);
|
||||||
|
color: white;
|
||||||
|
}
|
||||||
|
|
||||||
|
.App-link {
|
||||||
|
color: #61dafb;
|
||||||
|
}
|
||||||
|
|
||||||
|
@keyframes App-logo-spin {
|
||||||
|
from {
|
||||||
|
transform: rotate(0deg);
|
||||||
|
}
|
||||||
|
to {
|
||||||
|
transform: rotate(360deg);
|
||||||
|
}
|
||||||
|
}
|
||||||
27
web/src/App.js
Normal file
27
web/src/App.js
Normal file
@@ -0,0 +1,27 @@
|
|||||||
|
import * as React from "react";
|
||||||
|
import Login, { UsernameContext } from "./components/Login";
|
||||||
|
import Home from "./components/Home";
|
||||||
|
import Timetable from "./components/Timetable";
|
||||||
|
import TimeSlot from './components/TimeSlot';
|
||||||
|
import { createBrowserRouter, RouterProvider } from "react-router-dom";
|
||||||
|
|
||||||
|
const router = createBrowserRouter([
|
||||||
|
{ path: "/", element: <Home /> },
|
||||||
|
{ path: "/login", element: <Login /> },
|
||||||
|
{ path: "/timetable/:timetableID", element: <Timetable /> },
|
||||||
|
{ path: "/timetable/:timetableID/:timeslotID", element: <TimeSlot /> },
|
||||||
|
]);
|
||||||
|
|
||||||
|
function App() {
|
||||||
|
const [username, setUsername] = React.useState("");
|
||||||
|
const [token, setToken] = React.useState("");
|
||||||
|
return (
|
||||||
|
<UsernameContext.Provider
|
||||||
|
value={{ username, setUsername, token, setToken }}
|
||||||
|
>
|
||||||
|
<RouterProvider router={router} />
|
||||||
|
</UsernameContext.Provider>
|
||||||
|
);
|
||||||
|
}
|
||||||
|
|
||||||
|
export default App;
|
||||||
8
web/src/App.test.js
Normal file
8
web/src/App.test.js
Normal file
@@ -0,0 +1,8 @@
|
|||||||
|
import { render, screen } from '@testing-library/react';
|
||||||
|
import App from './App';
|
||||||
|
|
||||||
|
test('renders learn react link', () => {
|
||||||
|
render(<App />);
|
||||||
|
const linkElement = screen.getByText(/learn react/i);
|
||||||
|
expect(linkElement).toBeInTheDocument();
|
||||||
|
});
|
||||||
109
web/src/components/Home.js
Normal file
109
web/src/components/Home.js
Normal file
@@ -0,0 +1,109 @@
|
|||||||
|
import * as React from "react";
|
||||||
|
import Layout from "./Layout";
|
||||||
|
import {
|
||||||
|
Box,
|
||||||
|
Chip,
|
||||||
|
Button,
|
||||||
|
List,
|
||||||
|
ListItem,
|
||||||
|
ListItemButton,
|
||||||
|
ListItemText,
|
||||||
|
IconButton,
|
||||||
|
TextField,
|
||||||
|
Typography,
|
||||||
|
} from "@mui/material";
|
||||||
|
import { Delete } from "@mui/icons-material";
|
||||||
|
import { UsernameContext } from "./Login";
|
||||||
|
import { useNavigate } from "react-router-dom";
|
||||||
|
import { get, post, del } from "../fetches";
|
||||||
|
|
||||||
|
const Home = () => {
|
||||||
|
const { username, token } = React.useContext(UsernameContext);
|
||||||
|
|
||||||
|
const [timetables, setTimetables] = React.useState([]);
|
||||||
|
const [newTimetableName, setNewTimetableName] = React.useState("");
|
||||||
|
const navigator = useNavigate();
|
||||||
|
|
||||||
|
const fetchData = async () => {
|
||||||
|
const data = await get("/timetables");
|
||||||
|
setTimetables(data.timetables);
|
||||||
|
};
|
||||||
|
|
||||||
|
React.useEffect(() => {
|
||||||
|
fetchData();
|
||||||
|
}, []);
|
||||||
|
|
||||||
|
return (
|
||||||
|
<Layout>
|
||||||
|
{username === "admin" && (
|
||||||
|
<Box
|
||||||
|
sx={{
|
||||||
|
display: "flex",
|
||||||
|
}}
|
||||||
|
>
|
||||||
|
<TextField
|
||||||
|
value={newTimetableName}
|
||||||
|
onChange={(event) => {
|
||||||
|
setNewTimetableName(event.target.value);
|
||||||
|
}}
|
||||||
|
placeholder="班表名"
|
||||||
|
/>
|
||||||
|
<Button
|
||||||
|
variant="contained"
|
||||||
|
color="secondary"
|
||||||
|
onClick={async () => {
|
||||||
|
await post("/timetables", {
|
||||||
|
token,
|
||||||
|
newTimetableName,
|
||||||
|
});
|
||||||
|
await fetchData();
|
||||||
|
}}
|
||||||
|
>
|
||||||
|
添加新班表
|
||||||
|
</Button>
|
||||||
|
</Box>
|
||||||
|
)}
|
||||||
|
|
||||||
|
<Typography>选择班表:</Typography>
|
||||||
|
|
||||||
|
<List>
|
||||||
|
{timetables.map(({ id, name, status }) => (
|
||||||
|
<ListItem key={id}>
|
||||||
|
<ListItemButton
|
||||||
|
onClick={() => {
|
||||||
|
navigator(`/timetable/${id}`);
|
||||||
|
}}
|
||||||
|
>
|
||||||
|
<ListItemText
|
||||||
|
sx={{ display: "flex", justifyContent: "space-between" }}
|
||||||
|
>
|
||||||
|
{name}
|
||||||
|
<Chip
|
||||||
|
label={status ? "招募中" : "未开放"}
|
||||||
|
sx={{ mx: 5 }}
|
||||||
|
color={status ? "success" : "default"}
|
||||||
|
/>
|
||||||
|
</ListItemText>
|
||||||
|
</ListItemButton>
|
||||||
|
{username === "admin" && (
|
||||||
|
<IconButton
|
||||||
|
onClick={async () => {
|
||||||
|
console.log(
|
||||||
|
await del(`/timetables/${id}`, {
|
||||||
|
token,
|
||||||
|
})
|
||||||
|
);
|
||||||
|
await fetchData();
|
||||||
|
}}
|
||||||
|
>
|
||||||
|
<Delete />
|
||||||
|
</IconButton>
|
||||||
|
)}
|
||||||
|
</ListItem>
|
||||||
|
))}
|
||||||
|
</List>
|
||||||
|
</Layout>
|
||||||
|
);
|
||||||
|
};
|
||||||
|
|
||||||
|
export default Home;
|
||||||
70
web/src/components/Layout.js
Normal file
70
web/src/components/Layout.js
Normal file
@@ -0,0 +1,70 @@
|
|||||||
|
import * as React from "react";
|
||||||
|
import { UsernameContext } from "./Login";
|
||||||
|
import { useLocation, useNavigate } from "react-router-dom";
|
||||||
|
import {
|
||||||
|
CssBaseline,
|
||||||
|
Container,
|
||||||
|
Toolbar,
|
||||||
|
AppBar,
|
||||||
|
Typography,
|
||||||
|
Button,
|
||||||
|
Paper,
|
||||||
|
} from "@mui/material";
|
||||||
|
|
||||||
|
const LogoutButton = ({ username, setUsername }) => {
|
||||||
|
const text = username ? `${username} (点击登出)` : "登陆";
|
||||||
|
const navigator = useNavigate();
|
||||||
|
return (
|
||||||
|
<Button
|
||||||
|
variant="contained"
|
||||||
|
color="secondary"
|
||||||
|
onClick={() => {
|
||||||
|
if (username) {
|
||||||
|
setUsername("");
|
||||||
|
}
|
||||||
|
navigator("/login");
|
||||||
|
}}
|
||||||
|
>
|
||||||
|
{text}
|
||||||
|
</Button>
|
||||||
|
);
|
||||||
|
};
|
||||||
|
|
||||||
|
const Layout = ({ children }) => {
|
||||||
|
const locat = useLocation();
|
||||||
|
const navigator = useNavigate();
|
||||||
|
const { username, setUsername } = React.useContext(UsernameContext);
|
||||||
|
React.useEffect(() => {
|
||||||
|
if (!username) {
|
||||||
|
const { pathname } = locat;
|
||||||
|
if (pathname !== "/login") {
|
||||||
|
navigator(`/login?redir=${pathname}`);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}, []);
|
||||||
|
return (
|
||||||
|
<>
|
||||||
|
<header>
|
||||||
|
<CssBaseline />
|
||||||
|
<AppBar position="relative">
|
||||||
|
<Toolbar>
|
||||||
|
<Typography sx={{ flexGrow: 1 }} variant="h6">
|
||||||
|
ITSC
|
||||||
|
</Typography>
|
||||||
|
<LogoutButton username={username} setUsername={setUsername} />
|
||||||
|
</Toolbar>
|
||||||
|
</AppBar>
|
||||||
|
</header>
|
||||||
|
|
||||||
|
<main>
|
||||||
|
<Container maxWidth="sm" sx={{ mt: 3 }}>
|
||||||
|
<Paper sx={{ p: 2 }}>{children}</Paper>
|
||||||
|
</Container>
|
||||||
|
</main>
|
||||||
|
|
||||||
|
<footer></footer>
|
||||||
|
</>
|
||||||
|
);
|
||||||
|
};
|
||||||
|
|
||||||
|
export default Layout;
|
||||||
74
web/src/components/Login.js
Normal file
74
web/src/components/Login.js
Normal file
@@ -0,0 +1,74 @@
|
|||||||
|
import * as React from "react";
|
||||||
|
import { TextField, Typography, Button, Snackbar, Stack } from "@mui/material";
|
||||||
|
import Layout from "./Layout";
|
||||||
|
import { useNavigate, useSearchParams } from "react-router-dom";
|
||||||
|
|
||||||
|
const Login = () => {
|
||||||
|
const [inputUsername, setInputUsername] = React.useState("");
|
||||||
|
const [inputToken, setInputToken] = React.useState("");
|
||||||
|
const [errorMessage, setErrorMessage] = React.useState("");
|
||||||
|
const { setUsername, setToken } = React.useContext(UsernameContext);
|
||||||
|
const navigator = useNavigate();
|
||||||
|
const [search, _] = useSearchParams();
|
||||||
|
|
||||||
|
React.useEffect(() => {
|
||||||
|
if (typeof Storage === "undefined") {
|
||||||
|
return;
|
||||||
|
}
|
||||||
|
const storageUsername = localStorage.getItem("itsc-username") || "";
|
||||||
|
setInputUsername(storageUsername);
|
||||||
|
}, []);
|
||||||
|
|
||||||
|
const handleLogin = () => {
|
||||||
|
if (!inputUsername) {
|
||||||
|
setErrorMessage("请输入您的大名!");
|
||||||
|
return;
|
||||||
|
}
|
||||||
|
setUsername(inputUsername);
|
||||||
|
setToken(inputToken);
|
||||||
|
localStorage.setItem("itsc-username", inputUsername);
|
||||||
|
const next = search.get("redir") || "/";
|
||||||
|
console.log("next", next);
|
||||||
|
navigator(next);
|
||||||
|
};
|
||||||
|
|
||||||
|
return (
|
||||||
|
<Layout>
|
||||||
|
<Stack spacing={2}>
|
||||||
|
<Typography>您的大名:</Typography>
|
||||||
|
<TextField
|
||||||
|
value={inputUsername}
|
||||||
|
onChange={(event) => {
|
||||||
|
setInputUsername(event.target.value);
|
||||||
|
}}
|
||||||
|
/>
|
||||||
|
{inputUsername === "admin" && (
|
||||||
|
<TextField
|
||||||
|
value={inputToken}
|
||||||
|
onChange={(event) => {
|
||||||
|
setInputToken(event.target.value);
|
||||||
|
}}
|
||||||
|
placeholder="token"
|
||||||
|
/>
|
||||||
|
)}
|
||||||
|
<Button variant="contained" onClick={handleLogin}>
|
||||||
|
登入
|
||||||
|
</Button>
|
||||||
|
<Snackbar
|
||||||
|
open={!!errorMessage}
|
||||||
|
message={errorMessage}
|
||||||
|
autoHideDuration={2000}
|
||||||
|
onClose={() => {
|
||||||
|
setErrorMessage("");
|
||||||
|
}}
|
||||||
|
/>
|
||||||
|
</Stack>
|
||||||
|
</Layout>
|
||||||
|
);
|
||||||
|
};
|
||||||
|
|
||||||
|
const UsernameContext = React.createContext("error undefined value");
|
||||||
|
|
||||||
|
export default Login;
|
||||||
|
|
||||||
|
export { UsernameContext };
|
||||||
96
web/src/components/TimeSlot.js
Normal file
96
web/src/components/TimeSlot.js
Normal file
@@ -0,0 +1,96 @@
|
|||||||
|
import * as React from "react";
|
||||||
|
import Layout from "./Layout";
|
||||||
|
import {
|
||||||
|
Box,
|
||||||
|
Button,
|
||||||
|
Typography,
|
||||||
|
List,
|
||||||
|
ListItem,
|
||||||
|
IconButton,
|
||||||
|
ListItemButton,
|
||||||
|
ListItemText,
|
||||||
|
} from "@mui/material";
|
||||||
|
import { Delete } from "@mui/icons-material";
|
||||||
|
import { useNavigate, useParams } from "react-router-dom";
|
||||||
|
import { get, del } from "../fetches";
|
||||||
|
import { UsernameContext } from "./Login";
|
||||||
|
|
||||||
|
const TimeSlot = () => {
|
||||||
|
const navigator = useNavigate();
|
||||||
|
const params = useParams();
|
||||||
|
const { timetableID, timeslotID } = params;
|
||||||
|
const { username, token } = React.useContext(UsernameContext);
|
||||||
|
const [takes, setTakes] = React.useState([]);
|
||||||
|
const deleteThis = async () => {
|
||||||
|
await del(`/timetables/${timetableID}/${timeslotID}`, {
|
||||||
|
token,
|
||||||
|
});
|
||||||
|
navigator("./../");
|
||||||
|
};
|
||||||
|
const refresh = async () => {
|
||||||
|
const data = await get(`/timetables/${timetableID}/${timeslotID}`);
|
||||||
|
setTakes(data.takes);
|
||||||
|
};
|
||||||
|
React.useEffect(() => {
|
||||||
|
refresh();
|
||||||
|
}, []);
|
||||||
|
const deleteTake = async (takeusername) => {
|
||||||
|
await del(`/timetables/${timetableID}/${timeslotID}/${takeusername}`, {
|
||||||
|
token,
|
||||||
|
});
|
||||||
|
await refresh();
|
||||||
|
};
|
||||||
|
return (
|
||||||
|
<Layout>
|
||||||
|
<Typography variant="h5">报名列表</Typography>
|
||||||
|
<List>
|
||||||
|
{takes.map((take) => {
|
||||||
|
const created = new Date(take.created);
|
||||||
|
const date = `${created.getFullYear()}年${created.getMonth()}月${created.getDay()}日 ${created.getHours()}:${created.getMinutes()}:${created.getSeconds()}`;
|
||||||
|
return (
|
||||||
|
<ListItem key={take.username}>
|
||||||
|
<ListItemButton>
|
||||||
|
<ListItemText>
|
||||||
|
<Typography
|
||||||
|
sx={{
|
||||||
|
display: "flex",
|
||||||
|
justifyContent: "space-between",
|
||||||
|
flexGrow: 1,
|
||||||
|
}}
|
||||||
|
>
|
||||||
|
<span>{take.username}</span>
|
||||||
|
<span>{date}</span>
|
||||||
|
</Typography>
|
||||||
|
</ListItemText>
|
||||||
|
</ListItemButton>
|
||||||
|
{username === "admin" && (
|
||||||
|
<IconButton
|
||||||
|
onClick={() => {
|
||||||
|
deleteTake(take.username);
|
||||||
|
}}
|
||||||
|
>
|
||||||
|
<Delete />
|
||||||
|
</IconButton>
|
||||||
|
)}
|
||||||
|
</ListItem>
|
||||||
|
);
|
||||||
|
})}
|
||||||
|
</List>
|
||||||
|
<Box sx={{ display: "flex", justifyContent: "space-between" }}>
|
||||||
|
<Button
|
||||||
|
variant="contained"
|
||||||
|
onClick={() => {
|
||||||
|
navigator("./../");
|
||||||
|
}}
|
||||||
|
>
|
||||||
|
返回
|
||||||
|
</Button>
|
||||||
|
<Button variant="contained" color="warning" onClick={deleteThis}>
|
||||||
|
删除
|
||||||
|
</Button>
|
||||||
|
</Box>
|
||||||
|
</Layout>
|
||||||
|
);
|
||||||
|
};
|
||||||
|
|
||||||
|
export default TimeSlot;
|
||||||
210
web/src/components/Timetable.js
Normal file
210
web/src/components/Timetable.js
Normal file
@@ -0,0 +1,210 @@
|
|||||||
|
import * as React from "react";
|
||||||
|
import {
|
||||||
|
Stack,
|
||||||
|
TextField,
|
||||||
|
LinearProgress,
|
||||||
|
Typography,
|
||||||
|
Button,
|
||||||
|
Table,
|
||||||
|
TableBody,
|
||||||
|
TableCell,
|
||||||
|
TableHead,
|
||||||
|
TableRow,
|
||||||
|
TableContainer,
|
||||||
|
} from "@mui/material";
|
||||||
|
import Layout from "./Layout";
|
||||||
|
import { CheckCircle } from "@mui/icons-material";
|
||||||
|
import { UsernameContext } from "./Login";
|
||||||
|
import { useNavigate, useParams } from "react-router-dom";
|
||||||
|
import { get, post, put } from "../fetches";
|
||||||
|
|
||||||
|
const AdminComponent = ({ refresh, timetableStatus }) => {
|
||||||
|
const { username, token } = React.useContext(UsernameContext);
|
||||||
|
const [newName, setNewName] = React.useState("");
|
||||||
|
const [newTime, setNewTime] = React.useState("");
|
||||||
|
const [newCapacity, setNewCapacity] = React.useState(1);
|
||||||
|
const params = useParams();
|
||||||
|
let { timetableID } = params;
|
||||||
|
const handleNewTimeSlot = async () => {
|
||||||
|
await post(`/timetables/${timetableID}`, {
|
||||||
|
token,
|
||||||
|
name: newName,
|
||||||
|
time: newTime,
|
||||||
|
capacity: newCapacity,
|
||||||
|
});
|
||||||
|
await refresh();
|
||||||
|
};
|
||||||
|
const handleOpen = async () => {
|
||||||
|
await put(`/timetables/${timetableID}`, {
|
||||||
|
token,
|
||||||
|
status: true,
|
||||||
|
});
|
||||||
|
await refresh();
|
||||||
|
};
|
||||||
|
const handleClose = async () => {
|
||||||
|
await put(`/timetables/${timetableID}`, {
|
||||||
|
token,
|
||||||
|
status: false,
|
||||||
|
});
|
||||||
|
await refresh();
|
||||||
|
};
|
||||||
|
return (
|
||||||
|
username === "admin" && (
|
||||||
|
<Stack spacing={1}>
|
||||||
|
<TextField
|
||||||
|
value={newName}
|
||||||
|
placeholder="类型"
|
||||||
|
onChange={(event) => {
|
||||||
|
setNewName(event.target.value);
|
||||||
|
}}
|
||||||
|
/>
|
||||||
|
<TextField
|
||||||
|
value={newTime}
|
||||||
|
placeholder="时间"
|
||||||
|
onChange={(event) => {
|
||||||
|
setNewTime(event.target.value);
|
||||||
|
}}
|
||||||
|
/>
|
||||||
|
<TextField
|
||||||
|
value={newCapacity}
|
||||||
|
placeholder="招募数量"
|
||||||
|
onChange={(event) => {
|
||||||
|
let number = parseInt(event.target.value);
|
||||||
|
if (number <= 0) {
|
||||||
|
number = 1;
|
||||||
|
}
|
||||||
|
setNewCapacity(number);
|
||||||
|
}}
|
||||||
|
type="number"
|
||||||
|
/>
|
||||||
|
<Button
|
||||||
|
variant="contained"
|
||||||
|
color="secondary"
|
||||||
|
onClick={handleNewTimeSlot}
|
||||||
|
>
|
||||||
|
添加
|
||||||
|
</Button>
|
||||||
|
{timetableStatus && (
|
||||||
|
<Button variant="contained" color="warning" onClick={handleClose}>
|
||||||
|
暂停报名
|
||||||
|
</Button>
|
||||||
|
)}
|
||||||
|
{!timetableStatus && (
|
||||||
|
<Button variant="contained" color="success" onClick={handleOpen}>
|
||||||
|
开放报名
|
||||||
|
</Button>
|
||||||
|
)}
|
||||||
|
</Stack>
|
||||||
|
)
|
||||||
|
);
|
||||||
|
};
|
||||||
|
|
||||||
|
const Timetable = () => {
|
||||||
|
const [timeslots, setTimeslots] = React.useState([]);
|
||||||
|
const [timetableName, setTimetableName] = React.useState("");
|
||||||
|
const [timetableStatus, setTimetableStatus] = React.useState(true);
|
||||||
|
const { username } = React.useContext(UsernameContext);
|
||||||
|
const navigator = useNavigate();
|
||||||
|
const params = useParams();
|
||||||
|
let { timetableID } = params;
|
||||||
|
const refresh = async () => {
|
||||||
|
if (!username) {
|
||||||
|
return;
|
||||||
|
}
|
||||||
|
const data = await get(`/timetables/${timetableID}/username/${username}`);
|
||||||
|
setTimeslots(data.timeslots);
|
||||||
|
setTimetableName(data.timetableName);
|
||||||
|
setTimetableStatus(data.timetableStatus);
|
||||||
|
};
|
||||||
|
const handleTakeSlot = async (slotID, status) => {
|
||||||
|
if (username === "admin") {
|
||||||
|
navigator(`./${slotID}`);
|
||||||
|
return;
|
||||||
|
} else {
|
||||||
|
// TODO
|
||||||
|
await put(`/timetables/${timetableID}/${slotID}`, {
|
||||||
|
username: status ? username : `!${username}`,
|
||||||
|
});
|
||||||
|
await refresh();
|
||||||
|
}
|
||||||
|
};
|
||||||
|
React.useEffect(() => {
|
||||||
|
refresh()
|
||||||
|
const interval = setInterval(() => {
|
||||||
|
refresh();
|
||||||
|
}, 1000);
|
||||||
|
return () => {
|
||||||
|
clearInterval(interval);
|
||||||
|
};
|
||||||
|
}, []);
|
||||||
|
return (
|
||||||
|
<Layout>
|
||||||
|
<Button
|
||||||
|
variant="contained"
|
||||||
|
onClick={() => {
|
||||||
|
navigator("/");
|
||||||
|
}}
|
||||||
|
sx={{ my: 2 }}
|
||||||
|
>
|
||||||
|
返回
|
||||||
|
</Button>
|
||||||
|
<Typography variant="h4">{timetableName}</Typography>
|
||||||
|
|
||||||
|
<AdminComponent refresh={refresh} timetableStatus={timetableStatus} />
|
||||||
|
|
||||||
|
<Typography>列表实时更新中</Typography>
|
||||||
|
<LinearProgress />
|
||||||
|
<TableContainer>
|
||||||
|
<Table>
|
||||||
|
<TableHead>
|
||||||
|
<TableRow>
|
||||||
|
<TableCell>类型</TableCell>
|
||||||
|
<TableCell>时间</TableCell>
|
||||||
|
<TableCell>已报名/招募</TableCell>
|
||||||
|
</TableRow>
|
||||||
|
</TableHead>
|
||||||
|
<TableBody>
|
||||||
|
{timeslots.map((timeslot) => {
|
||||||
|
return (
|
||||||
|
<TableRow key={timeslot.id}>
|
||||||
|
<TableCell>{timeslot.name}</TableCell>
|
||||||
|
<TableCell>{timeslot.time}</TableCell>
|
||||||
|
<TableCell>
|
||||||
|
{!timeslot.success ? (
|
||||||
|
<Button
|
||||||
|
disabled={
|
||||||
|
(timeslot.take >= timeslot.capacity ||
|
||||||
|
timetableStatus === false) &&
|
||||||
|
username !== "admin"
|
||||||
|
}
|
||||||
|
variant="contained"
|
||||||
|
onClick={() => {
|
||||||
|
handleTakeSlot(timeslot.id, true);
|
||||||
|
}}
|
||||||
|
>
|
||||||
|
{timeslot.take} / {timeslot.capacity}
|
||||||
|
</Button>
|
||||||
|
) : (
|
||||||
|
<Button
|
||||||
|
variant="contained"
|
||||||
|
color="warning"
|
||||||
|
onClick={() => {
|
||||||
|
handleTakeSlot(timeslot.id, false);
|
||||||
|
}}
|
||||||
|
>
|
||||||
|
撤销
|
||||||
|
</Button>
|
||||||
|
)}
|
||||||
|
{timeslot.success && <CheckCircle color="success" />}
|
||||||
|
</TableCell>
|
||||||
|
</TableRow>
|
||||||
|
);
|
||||||
|
})}
|
||||||
|
</TableBody>
|
||||||
|
</Table>
|
||||||
|
</TableContainer>
|
||||||
|
</Layout>
|
||||||
|
);
|
||||||
|
};
|
||||||
|
|
||||||
|
export default Timetable;
|
||||||
37
web/src/fetches/index.js
Normal file
37
web/src/fetches/index.js
Normal file
@@ -0,0 +1,37 @@
|
|||||||
|
const API_ENDPOINT = "/api";
|
||||||
|
|
||||||
|
const get = async (url) => {
|
||||||
|
const resp = await fetch(`${API_ENDPOINT}${url}`);
|
||||||
|
const json = await resp.json();
|
||||||
|
return json;
|
||||||
|
};
|
||||||
|
|
||||||
|
const post = async (url, data) => {
|
||||||
|
return await _post(url, data, "POST");
|
||||||
|
};
|
||||||
|
|
||||||
|
const del = async (url, data) => {
|
||||||
|
return await _post(url, data, "DELETE");
|
||||||
|
};
|
||||||
|
|
||||||
|
const put = async (url, data) => {
|
||||||
|
return await _post(url, data, "PUT");
|
||||||
|
};
|
||||||
|
|
||||||
|
const _post = async (url, data, method) => {
|
||||||
|
const resp = await fetch(`${API_ENDPOINT}${url}`, {
|
||||||
|
method,
|
||||||
|
headers: {
|
||||||
|
Accept: "application/json",
|
||||||
|
"Content-Type": "application/json",
|
||||||
|
},
|
||||||
|
body: JSON.stringify(data),
|
||||||
|
});
|
||||||
|
const json = await resp.json();
|
||||||
|
if (json.errors) {
|
||||||
|
alert(json.errors);
|
||||||
|
}
|
||||||
|
return json;
|
||||||
|
};
|
||||||
|
|
||||||
|
export { get, post, del, put };
|
||||||
13
web/src/index.css
Normal file
13
web/src/index.css
Normal file
@@ -0,0 +1,13 @@
|
|||||||
|
body {
|
||||||
|
margin: 0;
|
||||||
|
font-family: -apple-system, BlinkMacSystemFont, 'Segoe UI', 'Roboto', 'Oxygen',
|
||||||
|
'Ubuntu', 'Cantarell', 'Fira Sans', 'Droid Sans', 'Helvetica Neue',
|
||||||
|
sans-serif;
|
||||||
|
-webkit-font-smoothing: antialiased;
|
||||||
|
-moz-osx-font-smoothing: grayscale;
|
||||||
|
}
|
||||||
|
|
||||||
|
code {
|
||||||
|
font-family: source-code-pro, Menlo, Monaco, Consolas, 'Courier New',
|
||||||
|
monospace;
|
||||||
|
}
|
||||||
16
web/src/index.js
Normal file
16
web/src/index.js
Normal file
@@ -0,0 +1,16 @@
|
|||||||
|
import React from "react";
|
||||||
|
import ReactDOM from "react-dom/client";
|
||||||
|
import App from "./App";
|
||||||
|
import reportWebVitals from "./reportWebVitals";
|
||||||
|
|
||||||
|
const root = ReactDOM.createRoot(document.getElementById("root"));
|
||||||
|
root.render(
|
||||||
|
<React.StrictMode>
|
||||||
|
<App />
|
||||||
|
</React.StrictMode>
|
||||||
|
);
|
||||||
|
|
||||||
|
// If you want to start measuring performance in your app, pass a function
|
||||||
|
// to log results (for example: reportWebVitals(console.log))
|
||||||
|
// or send to an analytics endpoint. Learn more: https://bit.ly/CRA-vitals
|
||||||
|
reportWebVitals();
|
||||||
1
web/src/logo.svg
Normal file
1
web/src/logo.svg
Normal file
@@ -0,0 +1 @@
|
|||||||
|
<svg xmlns="http://www.w3.org/2000/svg" viewBox="0 0 841.9 595.3"><g fill="#61DAFB"><path d="M666.3 296.5c0-32.5-40.7-63.3-103.1-82.4 14.4-63.6 8-114.2-20.2-130.4-6.5-3.8-14.1-5.6-22.4-5.6v22.3c4.6 0 8.3.9 11.4 2.6 13.6 7.8 19.5 37.5 14.9 75.7-1.1 9.4-2.9 19.3-5.1 29.4-19.6-4.8-41-8.5-63.5-10.9-13.5-18.5-27.5-35.3-41.6-50 32.6-30.3 63.2-46.9 84-46.9V78c-27.5 0-63.5 19.6-99.9 53.6-36.4-33.8-72.4-53.2-99.9-53.2v22.3c20.7 0 51.4 16.5 84 46.6-14 14.7-28 31.4-41.3 49.9-22.6 2.4-44 6.1-63.6 11-2.3-10-4-19.7-5.2-29-4.7-38.2 1.1-67.9 14.6-75.8 3-1.8 6.9-2.6 11.5-2.6V78.5c-8.4 0-16 1.8-22.6 5.6-28.1 16.2-34.4 66.7-19.9 130.1-62.2 19.2-102.7 49.9-102.7 82.3 0 32.5 40.7 63.3 103.1 82.4-14.4 63.6-8 114.2 20.2 130.4 6.5 3.8 14.1 5.6 22.5 5.6 27.5 0 63.5-19.6 99.9-53.6 36.4 33.8 72.4 53.2 99.9 53.2 8.4 0 16-1.8 22.6-5.6 28.1-16.2 34.4-66.7 19.9-130.1 62-19.1 102.5-49.9 102.5-82.3zm-130.2-66.7c-3.7 12.9-8.3 26.2-13.5 39.5-4.1-8-8.4-16-13.1-24-4.6-8-9.5-15.8-14.4-23.4 14.2 2.1 27.9 4.7 41 7.9zm-45.8 106.5c-7.8 13.5-15.8 26.3-24.1 38.2-14.9 1.3-30 2-45.2 2-15.1 0-30.2-.7-45-1.9-8.3-11.9-16.4-24.6-24.2-38-7.6-13.1-14.5-26.4-20.8-39.8 6.2-13.4 13.2-26.8 20.7-39.9 7.8-13.5 15.8-26.3 24.1-38.2 14.9-1.3 30-2 45.2-2 15.1 0 30.2.7 45 1.9 8.3 11.9 16.4 24.6 24.2 38 7.6 13.1 14.5 26.4 20.8 39.8-6.3 13.4-13.2 26.8-20.7 39.9zm32.3-13c5.4 13.4 10 26.8 13.8 39.8-13.1 3.2-26.9 5.9-41.2 8 4.9-7.7 9.8-15.6 14.4-23.7 4.6-8 8.9-16.1 13-24.1zM421.2 430c-9.3-9.6-18.6-20.3-27.8-32 9 .4 18.2.7 27.5.7 9.4 0 18.7-.2 27.8-.7-9 11.7-18.3 22.4-27.5 32zm-74.4-58.9c-14.2-2.1-27.9-4.7-41-7.9 3.7-12.9 8.3-26.2 13.5-39.5 4.1 8 8.4 16 13.1 24 4.7 8 9.5 15.8 14.4 23.4zM420.7 163c9.3 9.6 18.6 20.3 27.8 32-9-.4-18.2-.7-27.5-.7-9.4 0-18.7.2-27.8.7 9-11.7 18.3-22.4 27.5-32zm-74 58.9c-4.9 7.7-9.8 15.6-14.4 23.7-4.6 8-8.9 16-13 24-5.4-13.4-10-26.8-13.8-39.8 13.1-3.1 26.9-5.8 41.2-7.9zm-90.5 125.2c-35.4-15.1-58.3-34.9-58.3-50.6 0-15.7 22.9-35.6 58.3-50.6 8.6-3.7 18-7 27.7-10.1 5.7 19.6 13.2 40 22.5 60.9-9.2 20.8-16.6 41.1-22.2 60.6-9.9-3.1-19.3-6.5-28-10.2zM310 490c-13.6-7.8-19.5-37.5-14.9-75.7 1.1-9.4 2.9-19.3 5.1-29.4 19.6 4.8 41 8.5 63.5 10.9 13.5 18.5 27.5 35.3 41.6 50-32.6 30.3-63.2 46.9-84 46.9-4.5-.1-8.3-1-11.3-2.7zm237.2-76.2c4.7 38.2-1.1 67.9-14.6 75.8-3 1.8-6.9 2.6-11.5 2.6-20.7 0-51.4-16.5-84-46.6 14-14.7 28-31.4 41.3-49.9 22.6-2.4 44-6.1 63.6-11 2.3 10.1 4.1 19.8 5.2 29.1zm38.5-66.7c-8.6 3.7-18 7-27.7 10.1-5.7-19.6-13.2-40-22.5-60.9 9.2-20.8 16.6-41.1 22.2-60.6 9.9 3.1 19.3 6.5 28.1 10.2 35.4 15.1 58.3 34.9 58.3 50.6-.1 15.7-23 35.6-58.4 50.6zM320.8 78.4z"/><circle cx="420.9" cy="296.5" r="45.7"/><path d="M520.5 78.1z"/></g></svg>
|
||||||
|
After Width: | Height: | Size: 2.6 KiB |
13
web/src/reportWebVitals.js
Normal file
13
web/src/reportWebVitals.js
Normal file
@@ -0,0 +1,13 @@
|
|||||||
|
const reportWebVitals = onPerfEntry => {
|
||||||
|
if (onPerfEntry && onPerfEntry instanceof Function) {
|
||||||
|
import('web-vitals').then(({ getCLS, getFID, getFCP, getLCP, getTTFB }) => {
|
||||||
|
getCLS(onPerfEntry);
|
||||||
|
getFID(onPerfEntry);
|
||||||
|
getFCP(onPerfEntry);
|
||||||
|
getLCP(onPerfEntry);
|
||||||
|
getTTFB(onPerfEntry);
|
||||||
|
});
|
||||||
|
}
|
||||||
|
};
|
||||||
|
|
||||||
|
export default reportWebVitals;
|
||||||
5
web/src/setupTests.js
Normal file
5
web/src/setupTests.js
Normal file
@@ -0,0 +1,5 @@
|
|||||||
|
// jest-dom adds custom jest matchers for asserting on DOM nodes.
|
||||||
|
// allows you to do things like:
|
||||||
|
// expect(element).toHaveTextContent(/react/i)
|
||||||
|
// learn more: https://github.com/testing-library/jest-dom
|
||||||
|
import '@testing-library/jest-dom';
|
||||||
Reference in New Issue
Block a user