Skip to content

Commit 2c780f0

Browse files
committed
add question process
1 parent e332c8a commit 2c780f0

12 files changed

+298
-114
lines changed

app/card.go

Lines changed: 59 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,59 @@
1+
package app
2+
3+
import (
4+
"net/http"
5+
"time"
6+
7+
"github.com/haozibi/leetcode-badge/internal/card"
8+
"github.com/haozibi/leetcode-badge/internal/leetcodecn"
9+
"github.com/pkg/errors"
10+
)
11+
12+
func (a *APP) getCard(badgeType BadgeType, name string, r *http.Request) ([]byte, error) {
13+
14+
var f func(string, *http.Request) ([]byte, error)
15+
switch badgeType {
16+
case BadgeTypeQuestionProcessCard:
17+
f = a.getQuestionProcess
18+
default:
19+
return nil, errors.Errorf("not found card function")
20+
}
21+
22+
query := r.URL.Query().Encode()
23+
key := badgeType.String() + "_" + name + query
24+
25+
body, err := a.cache.GetByteBody(key)
26+
if err == nil && len(body) != 0 {
27+
return body, nil
28+
}
29+
fn := func() (interface{}, error) {
30+
body, err := f(name, r)
31+
if err != nil {
32+
return nil, err
33+
}
34+
35+
err = a.cache.SaveByteBody(key, body, 5*time.Minute)
36+
return body, err
37+
}
38+
39+
result, err, _ := a.group.Do(key, fn)
40+
if err != nil {
41+
return nil, err
42+
}
43+
44+
return result.([]byte), nil
45+
}
46+
47+
func (a *APP) getQuestionProcess(name string, r *http.Request) ([]byte, error) {
48+
data, err := leetcodecn.GetUserQuestionProgress(name)
49+
if err != nil {
50+
return nil, err
51+
52+
}
53+
if data == nil {
54+
return nil, ErrUserNotSupport
55+
}
56+
57+
body, err := card.Build(data)
58+
return body, err
59+
}

app/chart.go

Lines changed: 0 additions & 26 deletions
Original file line numberDiff line numberDiff line change
@@ -8,7 +8,6 @@ import (
88
"time"
99

1010
"github.com/pkg/errors"
11-
"github.com/rs/zerolog/log"
1211

1312
"github.com/haozibi/leetcode-badge/internal/chart"
1413
"github.com/haozibi/leetcode-badge/internal/heatmap"
@@ -121,31 +120,6 @@ func (a *APP) getHistoryList(name string, isCN bool, start, end time.Time) ([]st
121120
return result.([]storage.HistoryRecord), nil
122121
}
123122

124-
// SubCal SubmissionCalendar
125-
func (a *APP) SubCal(_ BadgeType, name string, isCN bool, w http.ResponseWriter, r *http.Request) {
126-
if !isCN {
127-
w.WriteHeader(http.StatusNotFound)
128-
w.Write([]byte("404 not found"))
129-
return
130-
}
131-
132-
body, err := a.getSubCal(name, r)
133-
if err != nil {
134-
if err == ErrUserNotSupport {
135-
a.write(w, statics.SVGNotFound())
136-
} else {
137-
log.Err(err).
138-
Str("Name", name).
139-
Bool("IsCN", isCN).
140-
Msg("get subcal error")
141-
w.WriteHeader(http.StatusInternalServerError)
142-
}
143-
return
144-
}
145-
146-
a.write(w, body)
147-
}
148-
149123
func (a *APP) getSubCal(name string, r *http.Request) ([]byte, error) {
150124
var (
151125
query = r.URL.Query().Encode()

app/http.go

Lines changed: 50 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -6,6 +6,7 @@ import (
66
"strconv"
77
"time"
88

9+
"github.com/haozibi/leetcode-badge/internal/statics"
910
"github.com/rs/zerolog/log"
1011
)
1112

@@ -69,6 +70,55 @@ func (a *APP) Basic(badgeType BadgeType, name string, isCN bool, w http.Response
6970
a.write(w, body)
7071
}
7172

73+
// SubCal SubmissionCalendar
74+
func (a *APP) SubCal(_ BadgeType, name string, isCN bool, w http.ResponseWriter, r *http.Request) {
75+
if !isCN {
76+
w.WriteHeader(http.StatusNotFound)
77+
w.Write([]byte("404 not found"))
78+
return
79+
}
80+
81+
body, err := a.getSubCal(name, r)
82+
if err != nil {
83+
if err == ErrUserNotSupport {
84+
a.write(w, statics.SVGNotFound())
85+
} else {
86+
log.Err(err).
87+
Str("Name", name).
88+
Bool("IsCN", isCN).
89+
Msg("get subcal error")
90+
w.WriteHeader(http.StatusInternalServerError)
91+
}
92+
return
93+
}
94+
95+
a.write(w, body)
96+
}
97+
98+
func (a *APP) Card(badgeType BadgeType, name string, isCN bool, w http.ResponseWriter, r *http.Request) {
99+
if !isCN {
100+
w.WriteHeader(http.StatusNotFound)
101+
w.Write([]byte("404 not found"))
102+
return
103+
}
104+
105+
body, err := a.getCard(badgeType, name, r)
106+
if err != nil {
107+
if err == ErrUserNotSupport {
108+
a.write(w, statics.SVGNotFound())
109+
} else {
110+
log.Err(err).
111+
Str("Name", name).
112+
Bool("IsCN", isCN).
113+
Msg("get subcal error")
114+
w.WriteHeader(http.StatusInternalServerError)
115+
}
116+
return
117+
}
118+
119+
a.write(w, body)
120+
}
121+
72122
func (a *APP) Chart(badgeType BadgeType, name string, isCN bool, w http.ResponseWriter, r *http.Request) {
73123
day := 7
74124
dayStr := r.URL.Query().Get("day")

app/router.go

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -74,6 +74,10 @@ func Router(r *mux.Router, a *APP, w io.Writer) {
7474
handlers.CombinedLoggingHandler(w, a.HandlerFunc(BadgeTypeChartSubmissionCalendar, isCN)),
7575
)
7676

77+
// [card] 答题进度卡片
78+
api.Methods(http.MethodGet).Path("/card/question-process/{name:.+}.svg").Handler(
79+
handlers.CombinedLoggingHandler(w, a.HandlerFunc(BadgeTypeQuestionProcessCard, isCN)),
80+
)
7781
// [basic] 获得个人信息
7882
api.Methods(http.MethodGet).Path("/{name:.+}.svg").Handler(
7983
handlers.CombinedLoggingHandler(w, a.HandlerFunc(BadgeTypeProfile, isCN)),
@@ -95,6 +99,8 @@ func (a *APP) HandlerFunc(badgeType BadgeType, isCN bool) http.Handler {
9599
// f = a.Badge
96100
case BadgeTypeChartSubmissionCalendar:
97101
f = a.SubCal
102+
case BadgeTypeQuestionProcessCard:
103+
f = a.Card
98104
}
99105

100106
return http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {

app/types.go

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -17,6 +17,7 @@ const (
1717
BadgeTypeChartRanking
1818
BadgeTypeChartSolved
1919
BadgeTypeChartSubmissionCalendar
20+
BadgeTypeQuestionProcessCard
2021
)
2122

2223
func (b BadgeType) String() string {
@@ -45,6 +46,8 @@ func (b BadgeType) String() string {
4546
return "followers"
4647
case BadgeTypeChartSubmissionCalendar:
4748
return "submission-calendar"
49+
case BadgeTypeQuestionProcessCard:
50+
return "question_process_card"
4851
}
4952

5053
return ""

go.mod

Lines changed: 16 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -6,33 +6,35 @@ require (
66
github.com/ajstarks/svgo v0.0.0-20211024235047-1546f124cd8b
77
github.com/davecgh/go-spew v1.1.1
88
github.com/go-redis/redis v6.15.6+incompatible
9-
github.com/go-sql-driver/mysql v1.4.1
10-
github.com/gorilla/handlers v1.4.2
11-
github.com/gorilla/mux v1.7.3
9+
github.com/go-sql-driver/mysql v1.6.0
10+
github.com/gorilla/handlers v1.5.1
11+
github.com/gorilla/mux v1.8.0
1212
github.com/haozibi/gendry v0.0.0-20181228133255-0e378288a754
13-
github.com/jmoiron/sqlx v1.2.0
14-
github.com/mattn/go-sqlite3 v1.14.4
13+
github.com/jmoiron/sqlx v1.3.5
14+
github.com/mattn/go-sqlite3 v1.14.14
1515
github.com/nikolaydubina/calendarheatmap v1.7.0
1616
github.com/patrickmn/go-cache v2.1.0+incompatible
1717
github.com/pkg/errors v0.9.1
18-
github.com/robfig/cron/v3 v3.0.1-0.20190716002318-e843a09e5b2d
19-
github.com/rs/zerolog v1.20.0
20-
github.com/spf13/cobra v0.0.5
18+
github.com/robfig/cron/v3 v3.0.1
19+
github.com/rs/zerolog v1.27.0
20+
github.com/spf13/cobra v1.5.0
2121
github.com/wcharczuk/go-chart v2.0.2-0.20190910040548-3a7bc5543113+incompatible
22-
golang.org/x/image v0.0.0-20200927104501-e162460cd6b5
23-
golang.org/x/sync v0.0.0-20201020160332-67f06af15bc9
22+
golang.org/x/image v0.0.0-20220722155232-062f8c9fd539
23+
golang.org/x/sync v0.0.0-20220722155255-886fb9371eb4
2424
)
2525

2626
require (
2727
github.com/blend/go-sdk v2.0.0+incompatible // indirect
28+
github.com/felixge/httpsnoop v1.0.3 // indirect
2829
github.com/golang/freetype v0.0.0-20170609003504-e2365dfdc4a0 // indirect
2930
github.com/inconshreveable/mousetrap v1.0.0 // indirect
31+
github.com/mattn/go-colorable v0.1.12 // indirect
32+
github.com/mattn/go-isatty v0.0.14 // indirect
3033
github.com/onsi/ginkgo v1.10.2 // indirect
3134
github.com/onsi/gomega v1.7.0 // indirect
32-
github.com/spf13/pflag v1.0.3 // indirect
35+
github.com/spf13/pflag v1.0.5 // indirect
3336
github.com/stretchr/testify v1.3.0 // indirect
34-
golang.org/x/text v0.3.3 // indirect
35-
google.golang.org/appengine v1.6.5 // indirect
37+
golang.org/x/sys v0.0.0-20220804214406-8e32c043e418 // indirect
38+
golang.org/x/text v0.3.7 // indirect
3639
gopkg.in/DATA-DOG/go-sqlmock.v1 v1.3.0 // indirect
37-
gopkg.in/yaml.v2 v2.4.0 // indirect
3840
)

0 commit comments

Comments
 (0)