Skip to content

Commit

Permalink
feat: true source in codereview
Browse files Browse the repository at this point in the history
  • Loading branch information
Gornak40 committed Apr 26, 2024
1 parent e4b0596 commit 786950b
Show file tree
Hide file tree
Showing 5 changed files with 43 additions and 4 deletions.
9 changes: 8 additions & 1 deletion controller/codereview.go
Original file line number Diff line number Diff line change
Expand Up @@ -31,7 +31,14 @@ func (s *Server) CodereviewGET(c *gin.Context) {
return
}

// TODO: get code from ejudge
source, err := s.ej.GetRunSource(submit.ContestID, submit.RunID)
if err != nil {
c.AbortWithStatusJSON(http.StatusInternalServerError, gin.H{"error": err.Error()})

return
}

submit.Source = source
c.HTML(http.StatusOK, "codereview.html", gin.H{
"Title": "Review",
"User": user,
Expand Down
2 changes: 1 addition & 1 deletion controller/index.go
Original file line number Diff line number Diff line change
Expand Up @@ -115,7 +115,7 @@ func (s *Server) IndexPOST(c *gin.Context) {
}

rctx := reviewContext{
RunID: dbRun.ID,
RunID: dbRun.EjudgeID,
ContestID: dbRun.EjudgeContestID,
Problem: dbRun.EjudgeName,
}
Expand Down
15 changes: 13 additions & 2 deletions pkg/ejudge/client.go
Original file line number Diff line number Diff line change
@@ -1,10 +1,12 @@
package ejudge

import (
"bytes"
"context"
"encoding/json"
"errors"
"fmt"
"io"
"net/http"
"net/http/cookiejar"
"net/url"
Expand Down Expand Up @@ -42,7 +44,7 @@ type ejAnswer struct {
Result json.RawMessage `json:"result"`
}

func (ej *EjClient) shootAPI(ctx context.Context, method string, params url.Values) (*ejAnswer, error) {
func (ej *EjClient) shootRaw(ctx context.Context, method string, params url.Values) ([]byte, error) {
link, err := url.JoinPath(ej.cfg.URL, method)
if err != nil {
return nil, err
Expand All @@ -63,8 +65,17 @@ func (ej *EjClient) shootAPI(ctx context.Context, method string, params url.Valu
return nil, fmt.Errorf("%w: %d", ErrBadStatusCode, resp.StatusCode)
}

return io.ReadAll(resp.Body)
}

func (ej *EjClient) shootAPI(ctx context.Context, method string, params url.Values) (*ejAnswer, error) {
data, err := ej.shootRaw(ctx, method, params)
if err != nil {
return nil, err
}

var answer ejAnswer
if err := json.NewDecoder(resp.Body).Decode(&answer); err != nil {
if err := json.NewDecoder(bytes.NewReader(data)).Decode(&answer); err != nil {
return nil, err
}
if !answer.OK {
Expand Down
20 changes: 20 additions & 0 deletions pkg/ejudge/source.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,20 @@
package ejudge

import (
"context"
"net/url"
"strconv"
)

func (ej *EjClient) GetRunSource(contestID uint, runID uint) (string, error) {
params := url.Values{
"contest_id": {strconv.Itoa(int(contestID))},
"run_id": {strconv.Itoa(int(runID))},
}
answer, err := ej.shootRaw(context.TODO(), "ej/api/v1/master/download-run", params)
if err != nil {
return "", err
}

return string(answer), err
}
1 change: 1 addition & 0 deletions templates/codereview.html
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,7 @@ <h5 class="card-header font-monospace">
<text class="text-warning">{{ .ContestID }}</text>
<i class="bi bi-arrow-right"></i>
<text class="text-info">{{ .Problem }}</text>
{{ .RunID }}
</h5>
<div class="card-body" style="overflow-y: auto; max-height: 750px;">
<pre><code>{{ .Source }}</code></pre>
Expand Down

0 comments on commit 786950b

Please sign in to comment.