support limit

This commit is contained in:
2022-11-23 18:08:04 +08:00
parent bfe739e442
commit f4cb36dbee
5 changed files with 108 additions and 28 deletions

View File

@@ -13,6 +13,7 @@ type Timetable struct {
ID int64 `json:"id"`
Name string `json:"name"`
Status bool `json:"status"`
Limit int64 `json:"limit"`
Created time.Time `json:"created"`
}
@@ -51,6 +52,7 @@ var (
UpdateTakeCount *sql.Stmt
CheckTableStatus *sql.Stmt
CheckLimit *sql.Stmt
)
func init() {
@@ -60,7 +62,7 @@ func init() {
log.Fatal(err)
}
if len(os.Args) > 1{
if len(os.Args) > 1 {
if os.Args[1] == "install" {
install()
os.Exit(0)
@@ -68,7 +70,7 @@ func init() {
}
GetAllTimetables, err = DB.Prepare(`
select id, name, status
select id, name, status, "limit"
from timetables
order by status desc, created desc
@@ -77,8 +79,8 @@ order by status desc, created desc
log.Fatal(err)
}
CreateNewTimetable, err = DB.Prepare(`
insert into timetables (name)
values ($1)
insert into timetables (name, "limit")
values ($1, $2)
returning id
`)
if err != nil {
@@ -93,7 +95,8 @@ where id = $1
}
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
case sub.username when $2 then true else false end as success,
t2."limit"
from timeslots t
join timetables t2 on t.ttid = t2.id
left outer join (
@@ -137,8 +140,8 @@ where tsid = $1 and username = $2
}
UpdateTimetableStatus, err = DB.Prepare(`
update timetables
set status = $1
where id = $2
set status = $1, "limit" = $2
where id = $3
`)
if err != nil {
log.Fatal(err)
@@ -171,4 +174,15 @@ select status from timetables where id = $1
if err != nil {
log.Fatal(err)
}
CheckLimit, err = DB.Prepare(`
select count(t1.username) < t3."limit" as c
from takes t1
join timeslots t2 on t1.tsid = t2.id
join timetables t3 on t2.ttid = t3.id
where t1.username = $1 and t3.id = $2
group by t1.username, t3."limit"
`)
if err != nil {
log.Fatal(err)
}
}