aboutsummaryrefslogtreecommitdiffhomepage
path: root/backend/admin
diff options
context:
space:
mode:
Diffstat (limited to 'backend/admin')
-rw-r--r--backend/admin/handler.go122
-rw-r--r--backend/admin/templates/dashboard.html3
-rw-r--r--backend/admin/templates/problem_edit.html36
-rw-r--r--backend/admin/templates/problem_new.html32
-rw-r--r--backend/admin/templates/problems.html20
5 files changed, 187 insertions, 26 deletions
diff --git a/backend/admin/handler.go b/backend/admin/handler.go
index aec7b37..5a0f0a1 100644
--- a/backend/admin/handler.go
+++ b/backend/admin/handler.go
@@ -53,16 +53,24 @@ func (h *Handler) RegisterHandlers(g *echo.Group) {
g.Use(h.newAdminMiddleware())
g.GET("/dashboard", h.getDashboard)
+
+ g.GET("/online-qualifying-ranking", h.getOnlineQualifyingRanking)
+
g.GET("/users", h.getUsers)
g.GET("/users/:userID", h.getUserEdit)
g.POST("/users/:userID", h.postUserEdit)
g.POST("/users/:userID/fetch-icon", h.postUserFetchIcon)
+
g.GET("/games", h.getGames)
g.GET("/games/:gameID", h.getGameEdit)
g.POST("/games/:gameID", h.postGameEdit)
g.POST("/games/:gameID/start", h.postGameStart)
- g.GET("/online-qualifying-ranking", h.getOnlineQualifyingRanking)
- g.POST("/fix", h.postFix)
+
+ g.GET("/problems", h.getProblems)
+ g.GET("/problems/new", h.getProblemNew)
+ g.POST("/problems/new", h.postProblemNew)
+ g.GET("/problems/:problemID", h.getProblemEdit)
+ g.POST("/problems/:problemID", h.postProblemEdit)
}
func (h *Handler) getDashboard(c echo.Context) error {
@@ -428,39 +436,101 @@ func (h *Handler) getOnlineQualifyingRanking(c echo.Context) error {
})
}
-func (h *Handler) postFix(c echo.Context) error {
- rows, err := h.q.ListSubmissionIDs(c.Request().Context())
+func (h *Handler) getProblems(c echo.Context) error {
+ rows, err := h.q.ListProblems(c.Request().Context())
if err != nil {
return echo.NewHTTPError(http.StatusInternalServerError, err.Error())
}
- for _, submissionID := range rows {
- as, err := h.q.AggregateTestcaseResults(c.Request().Context(), submissionID)
- if err != nil {
- return echo.NewHTTPError(http.StatusInternalServerError, err.Error())
- }
- err = h.q.UpdateSubmissionStatus(c.Request().Context(), db.UpdateSubmissionStatusParams{
- SubmissionID: submissionID,
- Status: as,
- })
- if err != nil {
- return echo.NewHTTPError(http.StatusInternalServerError, err.Error())
+ problems := make([]echo.Map, len(rows))
+ for i, p := range rows {
+ problems[i] = echo.Map{
+ "ProblemID": p.ProblemID,
+ "Title": p.Title,
+ "Description": p.Description,
+ "Language": p.Language,
}
}
- rows2, err := h.q.ListGameStateIDs(c.Request().Context())
+ return c.Render(http.StatusOK, "problems", echo.Map{
+ "BasePath": h.conf.BasePath,
+ "Title": "Problems",
+ "Problems": problems,
+ })
+}
+
+func (h *Handler) getProblemNew(c echo.Context) error {
+ return c.Render(http.StatusOK, "problem_new", echo.Map{
+ "BasePath": h.conf.BasePath,
+ "Title": "New Problem",
+ })
+}
+
+func (h *Handler) postProblemNew(c echo.Context) error {
+ title := c.FormValue("title")
+ description := c.FormValue("description")
+ language := c.FormValue("language")
+ sampleCode := c.FormValue("sample_code")
+
+ _, err := h.q.CreateProblem(c.Request().Context(), db.CreateProblemParams{
+ Title: title,
+ Description: description,
+ Language: language,
+ SampleCode: sampleCode,
+ })
if err != nil {
return echo.NewHTTPError(http.StatusInternalServerError, err.Error())
}
- for _, r := range rows2 {
- gameID := r.GameID
- userID := r.UserID
- err := h.q.SyncGameStateBestScoreSubmission(c.Request().Context(), db.SyncGameStateBestScoreSubmissionParams{
- GameID: gameID,
- UserID: userID,
- })
- if err != nil {
- return echo.NewHTTPError(http.StatusInternalServerError, err.Error())
+
+ return c.Redirect(http.StatusSeeOther, h.conf.BasePath+"admin/problems")
+}
+
+func (h *Handler) getProblemEdit(c echo.Context) error {
+ problemID, err := strconv.Atoi(c.Param("problemID"))
+ if err != nil {
+ return echo.NewHTTPError(http.StatusBadRequest, "Invalid problem id")
+ }
+ row, err := h.q.GetProblemByID(c.Request().Context(), int32(problemID))
+ if err != nil {
+ if errors.Is(err, pgx.ErrNoRows) {
+ return echo.NewHTTPError(http.StatusNotFound)
}
+ return echo.NewHTTPError(http.StatusInternalServerError, err.Error())
+ }
+
+ return c.Render(http.StatusOK, "problem_edit", echo.Map{
+ "BasePath": h.conf.BasePath,
+ "Title": "Problem Edit",
+ "Problem": echo.Map{
+ "ProblemID": row.ProblemID,
+ "Title": row.Title,
+ "Description": row.Description,
+ "Language": row.Language,
+ "SampleCode": row.SampleCode,
+ },
+ })
+}
+
+func (h *Handler) postProblemEdit(c echo.Context) error {
+ problemID, err := strconv.Atoi(c.Param("problemID"))
+ if err != nil {
+ return echo.NewHTTPError(http.StatusBadRequest, "Invalid problem_id")
+ }
+
+ title := c.FormValue("title")
+ description := c.FormValue("description")
+ language := c.FormValue("language")
+ sampleCode := c.FormValue("sample_code")
+
+ err = h.q.UpdateProblem(c.Request().Context(), db.UpdateProblemParams{
+ ProblemID: int32(problemID),
+ Title: title,
+ Description: description,
+ Language: language,
+ SampleCode: sampleCode,
+ })
+ if err != nil {
+ return echo.NewHTTPError(http.StatusInternalServerError, err.Error())
}
- return c.Redirect(http.StatusSeeOther, h.conf.BasePath+"admin/dashboard")
+
+ return c.Redirect(http.StatusSeeOther, h.conf.BasePath+"admin/problems")
}
diff --git a/backend/admin/templates/dashboard.html b/backend/admin/templates/dashboard.html
index dcc71ba..1ea40c9 100644
--- a/backend/admin/templates/dashboard.html
+++ b/backend/admin/templates/dashboard.html
@@ -8,6 +8,9 @@
<a href="{{ .BasePath }}admin/games">Games</a>
</p>
<p>
+ <a href="{{ .BasePath }}admin/problems">Problems</a>
+</p>
+<p>
<a href="{{ .BasePath }}admin/online-qualifying-ranking?game_1=7&game_2=8">Online Qualifying Ranking</a>
</p>
<form method="post" action="{{ .BasePath }}admin/fix">
diff --git a/backend/admin/templates/problem_edit.html b/backend/admin/templates/problem_edit.html
new file mode 100644
index 0000000..cc700f4
--- /dev/null
+++ b/backend/admin/templates/problem_edit.html
@@ -0,0 +1,36 @@
+{{ template "base.html" . }}
+
+{{ define "breadcrumb" }}
+<a href="{{ .BasePath }}admin/dashboard">Dashboard</a> | <a href="{{ .BasePath }}admin/problems">Problems</a>
+{{ end }}
+
+{{ define "content" }}
+<form method="post">
+ <div>
+ <label>Problem ID</label>
+ <input type="text" name="problem_id" value="{{ .Problem.ProblemID }}" readonly required>
+ </div>
+ <div>
+ <label>Title</label>
+ <input type="text" name="title" value="{{ .Problem.Title }}" required>
+ </div>
+ <div>
+ <label>Description</label>
+ <textarea name="description" rows="10" required>{{ .Problem.Description }}</textarea>
+ </div>
+ <div>
+ <label>Language</label>
+ <select name="language" required>
+ <option value="php"{{ if eq .Problem.Language "php" }} selected{{ end }}>PHP</option>
+ <option value="swift"{{ if eq .Problem.Language "swift" }} selected{{ end }}>Swift</option>
+ </select>
+ </div>
+ <div>
+ <label>Sample Code</label>
+ <textarea name="sample_code" rows="15" required>{{ .Problem.SampleCode }}</textarea>
+ </div>
+ <div>
+ <button type="submit">Save</button>
+ </div>
+</form>
+{{ end }}
diff --git a/backend/admin/templates/problem_new.html b/backend/admin/templates/problem_new.html
new file mode 100644
index 0000000..ed8ad2a
--- /dev/null
+++ b/backend/admin/templates/problem_new.html
@@ -0,0 +1,32 @@
+{{ template "base.html" . }}
+
+{{ define "breadcrumb" }}
+<a href="{{ .BasePath }}admin/dashboard">Dashboard</a> | <a href="{{ .BasePath }}admin/problems">Problems</a>
+{{ end }}
+
+{{ define "content" }}
+<form method="post">
+ <div>
+ <label>Title</label>
+ <input type="text" name="title" required>
+ </div>
+ <div>
+ <label>Description</label>
+ <textarea name="description" rows="10" required></textarea>
+ </div>
+ <div>
+ <label>Language</label>
+ <select name="language" required>
+ <option value="php">PHP</option>
+ <option value="swift">Swift</option>
+ </select>
+ </div>
+ <div>
+ <label>Sample Code</label>
+ <textarea name="sample_code" rows="15" required></textarea>
+ </div>
+ <div>
+ <button type="submit">Create</button>
+ </div>
+</form>
+{{ end }}
diff --git a/backend/admin/templates/problems.html b/backend/admin/templates/problems.html
new file mode 100644
index 0000000..120789e
--- /dev/null
+++ b/backend/admin/templates/problems.html
@@ -0,0 +1,20 @@
+{{ template "base.html" . }}
+
+{{ define "breadcrumb" }}
+<a href="{{ .BasePath }}admin/dashboard">Dashboard</a>
+{{ end }}
+
+{{ define "content" }}
+<div>
+ <a href="{{ .BasePath }}admin/problems/new">Create New Problem</a>
+</div>
+<ul>
+ {{ range .Problems }}
+ <li>
+ <a href="{{ $.BasePath }}admin/problems/{{ .ProblemID }}">
+ {{ .Title }} (id={{ .ProblemID }} language={{ .Language }})
+ </a>
+ </li>
+ {{ end }}
+</ul>
+{{ end }}