Skip to content

Commit

Permalink
feat: profile page + longer review userpath
Browse files Browse the repository at this point in the history
  • Loading branch information
Gornak40 committed Apr 26, 2024
1 parent 2c884bc commit b30457e
Show file tree
Hide file tree
Showing 10 changed files with 170 additions and 44 deletions.
88 changes: 74 additions & 14 deletions controller/codereview.go
Original file line number Diff line number Diff line change
@@ -1,32 +1,30 @@
package controller

import (
"encoding/json"
"errors"
"net/http"

"github.com/Gornak40/crosspawn/internal/alerts"
"github.com/Gornak40/crosspawn/models"
"github.com/gin-contrib/sessions"
"github.com/gin-gonic/gin"
)

var (
errInvalidSessionSubmit = errors.New("invalid submit in session")
)

type codereviewFrom struct {
RatingDelta int `binding:"required" form:"ratingDelta"`
}

func (s *Server) CodereviewGET(c *gin.Context) {
session := sessions.Default(c)
user := session.Get("user")

submitEn, ok := session.Get("submit").(string)
submit, ok := session.Get("submit").(reviewContext)
if !ok {
_ = alerts.Add(session, alerts.Alert{
Message: "Select contest and problem",
Type: alerts.TypeInfo,
})
c.Redirect(http.StatusFound, "/")

return
}

var submit reviewContext
if err := json.Unmarshal([]byte(submitEn), &submit); err != nil {
c.AbortWithStatusJSON(http.StatusInternalServerError, gin.H{"error": "failed to unmarshal submit"})
c.AbortWithStatusJSON(http.StatusInternalServerError, gin.H{"error": errInvalidSessionSubmit.Error()})

return
}
Expand All @@ -46,3 +44,65 @@ func (s *Server) CodereviewGET(c *gin.Context) {
"Flashes": alerts.Get(session),
})
}

func (s *Server) CodereviewPOST(c *gin.Context) {
var form codereviewFrom

if err := c.ShouldBind(&form); err != nil {
c.AbortWithStatusJSON(http.StatusBadRequest, gin.H{"error": err.Error()})

return
}

session := sessions.Default(c)

submit, ok := session.Get("submit").(reviewContext)
if !ok {
c.AbortWithStatusJSON(http.StatusInternalServerError, gin.H{"error": errInvalidSessionSubmit.Error()})

return
}

var dbRun models.Run
quRun := models.Run{EjudgeID: submit.RunID, EjudgeContestID: submit.ContestID}
if err := s.db.Where(&quRun).First(&dbRun).Error; err != nil {
c.AbortWithStatusJSON(http.StatusInternalServerError, gin.H{"error": err.Error()})

return
}

dbRun.ReviewCount++
dbRun.Rating += form.RatingDelta

if err := s.db.Save(&dbRun).Error; err != nil {
c.AbortWithStatusJSON(http.StatusInternalServerError, gin.H{"error": err.Error()})

return
}

session.Delete("submit")
_ = session.Save()

_ = alerts.Add(session, alerts.Alert{
Message: "Review processed",
Type: alerts.TypeSuccess,
})
c.Redirect(http.StatusFound, "/")
}

func (s *Server) codereviewMiddleware(c *gin.Context) {
session := sessions.Default(c)

if session.Get("submit") == nil {
_ = alerts.Add(session, alerts.Alert{
Message: "Select contest and problem",
Type: alerts.TypeInfo,
})
c.Redirect(http.StatusFound, "/")
c.Abort()

return
}

c.Next()
}
4 changes: 2 additions & 2 deletions controller/index.go
Original file line number Diff line number Diff line change
Expand Up @@ -119,8 +119,8 @@ func (s *Server) IndexPOST(c *gin.Context) {
ContestID: dbRun.EjudgeContestID,
Problem: dbRun.EjudgeName,
}
data, _ := json.Marshal(rctx) //nolint:errchkjson // It's my JSON
session.Set("submit", string(data))
_, _ = json.Marshal(rctx) //nolint:errchkjson // It's my JSON
session.Set("submit", &rctx)
_ = session.Save()

_ = alerts.Add(session, alerts.Alert{
Expand Down
11 changes: 10 additions & 1 deletion controller/login.go
Original file line number Diff line number Diff line change
Expand Up @@ -20,9 +20,18 @@ func (s *Server) LoginGET(c *gin.Context) {
session := sessions.Default(c)
user := session.Get("user")

if user != nil {
_ = alerts.Add(session, alerts.Alert{
Message: "Logout first",
Type: alerts.TypeWarning,
})
c.Redirect(http.StatusFound, "/profile")

return
}

c.HTML(http.StatusOK, "login.html", gin.H{
"Title": "Login",
"User": user,
"Flashes": alerts.Get(session),
})
}
Expand Down
20 changes: 20 additions & 0 deletions controller/profile.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,20 @@
package controller

import (
"net/http"

"github.com/Gornak40/crosspawn/internal/alerts"
"github.com/gin-contrib/sessions"
"github.com/gin-gonic/gin"
)

func (s *Server) ProfileGET(c *gin.Context) {
session := sessions.Default(c)
user := session.Get("user")

c.HTML(http.StatusOK, "profile.html", gin.H{
"Title": "Profile GET",
"User": user,
"Flashes": alerts.Get(session),
})
}
19 changes: 16 additions & 3 deletions controller/server.go
Original file line number Diff line number Diff line change
@@ -1,6 +1,8 @@
package controller

import (
"encoding/gob"

"github.com/Gornak40/crosspawn/config"
"github.com/Gornak40/crosspawn/pkg/ejudge"
"github.com/gin-contrib/sessions"
Expand Down Expand Up @@ -28,6 +30,8 @@ func NewServer(db *gorm.DB, ej *ejudge.EjClient, cfg *config.ServerConfig) *Serv
func (s *Server) InitRouter() *gin.Engine {
r := gin.Default()

gob.Register(reviewContext{})

store := cookie.NewStore([]byte(s.cfg.GinSecret))
r.Use(sessions.Sessions(sessionName, store))

Expand All @@ -42,18 +46,27 @@ func (s *Server) InitRouter() *gin.Engine {
ua := r.Group("/", s.userMiddleware)
{
ua.GET("/", s.IndexGET)
ua.GET("/codereview", s.CodereviewGET)
ua.POST("/", s.IndexPOST)

ua.GET("/admin", s.AdminGET)
ua.POST("/admin", s.AdminPOST)

ua.GET("/profile", s.ProfileGET)

ua.POST("/logout", s.LogoutPOST)
ua.POST("/", s.IndexPOST)
ua.POST("/admin", s.AdminPOST)
}

ca := ua.Group("/codereview", s.codereviewMiddleware)
{
ca.GET("/", s.CodereviewGET)
ca.POST("/", s.CodereviewPOST)
}

aa := ua.Group("/manage", s.adminMiddleware)
{
aa.GET("/", s.ManageGET)
aa.POST("/", s.ManagePOST)

aa.POST("/flip", s.ManageFlipPOST)
}

Expand Down
1 change: 1 addition & 0 deletions models/run.go
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,7 @@ type Run struct {
EjudgeName string `gorm:"not null;type:varchar(32)"`

ReviewCount uint `gorm:"not null"`
Rating int `gorm:"not null"`
}

func NewRunFromEj(run *ejudge.EjRun) *Run {
Expand Down
22 changes: 9 additions & 13 deletions templates/codereview.html
Original file line number Diff line number Diff line change
Expand Up @@ -25,9 +25,15 @@ <h5 class="card-header font-monospace">
</div>
</div>

<div class="btn-group my-3" role="group">
<button class="btn btn-success btn-lg" id="reviewAprove">Aprove</button>
<button class="btn btn-danger btn-lg" id="reviewReject">Reject</button>
<div class="d-flex my-3">
<form method="POST" action="/codereview">
<button class="btn btn-success btn-lg" id="reviewAprove">Aprove</button>
<input type="hidden" name="ratingDelta" value="1">
</form>
<form method="POST" action="/codereview">
<button class="btn btn-danger btn-lg ms-2" id="reviewReject">Reject</button>
<input type="hidden" name="ratingDelta" value="-1">
</form>
</div>
{{ end }}
</div>
Expand All @@ -42,18 +48,8 @@ <h5 class="card-header font-monospace">
e.preventDefault();
alert('Comment is required for the reject.');
$('#reviewComment').focus();
} else {
// TODO: REJECT
alert('REJECT');
}
});

$('#reviewAprove').click(function (e) {
var comment = $('#reviewComment').val().trim();
// TODO: APPROVE
alert('APPROVE');
});

});
</script>

Expand Down
2 changes: 1 addition & 1 deletion templates/header.html
Original file line number Diff line number Diff line change
Expand Up @@ -25,7 +25,7 @@
<div class="collapse navbar-collapse" id="navbarNav">
<ul class="navbar-nav">
<li class="nav-item">
<a class="nav-link" href="/login">Profile</a>
<a class="nav-link" href="/profile">Profile</a>
</li>
<li class="nav-item">
<a class="nav-link" href="/codereview">Code Review</a>
Expand Down
10 changes: 0 additions & 10 deletions templates/login.html
Original file line number Diff line number Diff line change
Expand Up @@ -5,15 +5,6 @@
<div class="col-md-6 offset-md-3">
<div class="card">
<div class="card-body">
{{ if .User }}
<form action="/logout" method="POST">
<legend>User Profile</legend>
<div class="mb-3">
<label>You logged as <b>{{ .User }}</b></label>
</div>
<button type="submit" class="btn btn-primary btn-lg">Logout</button>
</form>
{{ else }}
<form action="/login" method="POST">
<legend>User Login</legend>
<div class="mb-3">
Expand All @@ -31,7 +22,6 @@
</div>
<button type="submit" class="btn btn-primary btn-lg">Submit</button>
</form>
{{ end }}
</div>
</div>
</div>
Expand Down
37 changes: 37 additions & 0 deletions templates/profile.html
Original file line number Diff line number Diff line change
@@ -0,0 +1,37 @@
{{ template "header.html" . }}

<div class="container mt-5">
<div class="row">
<div class="col-md-6 offset-md-3">
<div class="card">
<div class="card-body">
<form action="/logout" method="POST">
<legend class="mb-3">User Profile</legend>
<div class="d-flex">
<h5>
<span class="badge text-bg-info">{{ .User }}</span>
</h5>
</div>
<div class="mb-3 d-flex">
<!-- TODO: add stats -->
<h5>
<span class="badge text-bg-success">
<i class="bi bi-patch-plus-fill"></i> 0
</span>
</h5>
<h5 class="ms-2">
<span class="badge text-bg-danger">
<i class="bi bi-patch-minus-fill"></i> 0
</span>
</h5>
</div>
<button type="submit" class="btn btn-primary btn-lg">Logout</button>
</form>
</div>
</div>
</div>
</div>
</div>


{{ template "footer.html" . }}

0 comments on commit b30457e

Please sign in to comment.