Add descriptions to Works
This commit is contained in:
parent
7e7608b7d1
commit
767da66f63
1
db.go
1
db.go
@ -204,6 +204,7 @@ CREATE TABLE IF NOT EXISTS works(
|
||||
promo MEDIUMINT NOT NULL,
|
||||
grp VARCHAR(255) NOT NULL,
|
||||
shown BOOLEAN NOT NULL DEFAULT FALSE,
|
||||
description TEXT NOT NULL,
|
||||
submission_URL VARCHAR(255) NULL,
|
||||
corrected BOOLEAN NOT NULL DEFAULT FALSE,
|
||||
start_availability TIMESTAMP NOT NULL DEFAULT CURRENT_TIMESTAMP,
|
||||
|
@ -42,7 +42,21 @@ func declareAPIAuthRepositoriesRoutes(router *gin.RouterGroup) {
|
||||
return
|
||||
}
|
||||
|
||||
w := c.MustGet("work").(*Work)
|
||||
var w *Work
|
||||
if work, ok := c.Get("work"); ok {
|
||||
w = work.(*Work)
|
||||
} else if repository.IdWork > 0 {
|
||||
var err error
|
||||
w, err = getWork(int(repository.IdWork))
|
||||
if err != nil {
|
||||
c.AbortWithStatusJSON(http.StatusNotFound, gin.H{"errmsg": "Unable to find the given work identifier."})
|
||||
return
|
||||
}
|
||||
} else {
|
||||
c.AbortWithStatusJSON(http.StatusBadRequest, gin.H{"errmsg": "Unable to find the given work identifier."})
|
||||
return
|
||||
}
|
||||
|
||||
k, err := u.NewRepository(w, repository.URI)
|
||||
if err != nil {
|
||||
log.Println("Unable to NewRepository:", err)
|
||||
@ -158,13 +172,13 @@ func (u *User) GetRepositories() (repositories []*Repository, err error) {
|
||||
|
||||
func getRepository(id int) (r *Repository, err error) {
|
||||
r = new(Repository)
|
||||
err = DBQueryRow("SELECT id_repository, id_user, id_work, uri, lastcheck FROM user_work_repositories WHERE id_repository=?", id).Scan(&r.Id, &r.IdUser, &r.IdWork, &r.URI, &r.LastCheck)
|
||||
err = DBQueryRow("SELECT id_repository, id_user, id_work, uri, last_check FROM user_work_repositories WHERE id_repository=?", id).Scan(&r.Id, &r.IdUser, &r.IdWork, &r.URI, &r.LastCheck)
|
||||
return
|
||||
}
|
||||
|
||||
func (u *User) getRepository(id int) (r *Repository, err error) {
|
||||
r = new(Repository)
|
||||
err = DBQueryRow("SELECT id_repository, id_user, id_work, uri FROM user_work_repositories WHERE id_repository=? AND id_user=?", id, u.Id).Scan(&r.Id, &r.IdUser, &r.IdWork, &r.URI, &r.LastCheck)
|
||||
err = DBQueryRow("SELECT id_repository, id_user, id_work, uri, last_check FROM user_work_repositories WHERE id_repository=? AND id_user=?", id, u.Id).Scan(&r.Id, &r.IdUser, &r.IdWork, &r.URI, &r.LastCheck)
|
||||
return
|
||||
}
|
||||
|
||||
|
@ -106,6 +106,15 @@
|
||||
</div>
|
||||
</div>
|
||||
|
||||
<div class="row mt-2">
|
||||
<div class="col-sm-3 text-sm-end">
|
||||
<label for="work_description" class="col-form-label col-form-label-sm">Description</label>
|
||||
</div>
|
||||
<div class="col-8">
|
||||
<textarea class="form-control" id="work_description" bind:value={work.descr_raw}></textarea>
|
||||
</div>
|
||||
</div>
|
||||
|
||||
<div class="row row-cols-3 mx-1 my-2">
|
||||
<div class="form-check">
|
||||
<input class="form-check-input" type="checkbox" id="shown" bind:checked={work.shown}>
|
||||
|
@ -6,12 +6,14 @@ export class Work {
|
||||
}
|
||||
}
|
||||
|
||||
update({ id, title, promo, group, shown, submission_url, corrected, start_availability, end_availability }) {
|
||||
update({ id, title, promo, group, shown, description, descr_raw, submission_url, corrected, start_availability, end_availability }) {
|
||||
this.id = id;
|
||||
this.title = title;
|
||||
this.promo = promo;
|
||||
this.group = group;
|
||||
this.shown = shown;
|
||||
this.description = description;
|
||||
this.descr_raw = descr_raw;
|
||||
this.submission_url = submission_url;
|
||||
this.corrected = corrected;
|
||||
if (this.start_availability != start_availability) {
|
||||
|
34
works.go
34
works.go
@ -11,6 +11,7 @@ import (
|
||||
"time"
|
||||
|
||||
"github.com/gin-gonic/gin"
|
||||
"github.com/russross/blackfriday/v2"
|
||||
)
|
||||
|
||||
func declareAPIWorksRoutes(router *gin.RouterGroup) {
|
||||
@ -100,7 +101,7 @@ func declareAPIAdminWorksRoutes(router *gin.RouterGroup) {
|
||||
new.Promo = currentPromo
|
||||
}
|
||||
|
||||
work, err := NewWork(new.Title, new.Promo, new.Group, new.Shown, new.SubmissionURL, new.StartAvailability, new.EndAvailability)
|
||||
work, err := NewWork(new.Title, new.Promo, new.Group, new.Shown, new.DescriptionRaw, new.SubmissionURL, new.StartAvailability, new.EndAvailability)
|
||||
if err != nil {
|
||||
log.Println("Unable to NewWork:", err)
|
||||
c.AbortWithStatusJSON(http.StatusInternalServerError, gin.H{"errmsg": "An error occurs during work creation"})
|
||||
@ -241,6 +242,8 @@ type OneWork struct {
|
||||
Promo uint `json:"promo"`
|
||||
Group string `json:"group"`
|
||||
Shown bool `json:"shown"`
|
||||
Description string `json:"description"`
|
||||
DescriptionRaw string `json:"descr_raw,omitempty"`
|
||||
Direct *int64 `json:"direct"`
|
||||
SubmissionURL *string `json:"submission_url"`
|
||||
Corrected bool `json:"corrected"`
|
||||
@ -249,14 +252,14 @@ type OneWork struct {
|
||||
}
|
||||
|
||||
func allWorks(cnd string, param ...interface{}) (items []*OneWork, err error) {
|
||||
if rows, errr := DBQuery("SELECT kind, id, title, promo, grp, shown, direct, submission_url, corrected, start_availability, end_availability FROM all_works "+cnd, param...); errr != nil {
|
||||
if rows, errr := DBQuery("SELECT kind, id, title, promo, grp, shown, description, direct, submission_url, corrected, start_availability, end_availability FROM all_works "+cnd, param...); errr != nil {
|
||||
return nil, errr
|
||||
} else {
|
||||
defer rows.Close()
|
||||
|
||||
for rows.Next() {
|
||||
var w OneWork
|
||||
if err = rows.Scan(&w.Kind, &w.Id, &w.Title, &w.Promo, &w.Group, &w.Shown, &w.Direct, &w.SubmissionURL, &w.Corrected, &w.StartAvailability, &w.EndAvailability); err != nil {
|
||||
if err = rows.Scan(&w.Kind, &w.Id, &w.Title, &w.Promo, &w.Group, &w.Shown, &w.DescriptionRaw, &w.Direct, &w.SubmissionURL, &w.Corrected, &w.StartAvailability, &w.EndAvailability); err != nil {
|
||||
return
|
||||
}
|
||||
items = append(items, &w)
|
||||
@ -275,6 +278,8 @@ type Work struct {
|
||||
Promo uint `json:"promo"`
|
||||
Group string `json:"group"`
|
||||
Shown bool `json:"shown"`
|
||||
Description string `json:"description"`
|
||||
DescriptionRaw string `json:"descr_raw,omitempty"`
|
||||
SubmissionURL *string `json:"submission_url"`
|
||||
Corrected bool `json:"corrected"`
|
||||
StartAvailability time.Time `json:"start_availability"`
|
||||
@ -282,14 +287,14 @@ type Work struct {
|
||||
}
|
||||
|
||||
func getWorks(cnd string, param ...interface{}) (items []*Work, err error) {
|
||||
if rows, errr := DBQuery("SELECT id_work, title, promo, grp, shown, submission_url, corrected, start_availability, end_availability FROM works "+cnd, param...); errr != nil {
|
||||
if rows, errr := DBQuery("SELECT id_work, title, promo, grp, shown, description, submission_url, corrected, start_availability, end_availability FROM works "+cnd, param...); errr != nil {
|
||||
return nil, errr
|
||||
} else {
|
||||
defer rows.Close()
|
||||
|
||||
for rows.Next() {
|
||||
var w Work
|
||||
if err = rows.Scan(&w.Id, &w.Title, &w.Promo, &w.Group, &w.Shown, &w.SubmissionURL, &w.Corrected, &w.StartAvailability, &w.EndAvailability); err != nil {
|
||||
if err = rows.Scan(&w.Id, &w.Title, &w.Promo, &w.Group, &w.Shown, &w.DescriptionRaw, &w.SubmissionURL, &w.Corrected, &w.StartAvailability, &w.EndAvailability); err != nil {
|
||||
return
|
||||
}
|
||||
items = append(items, &w)
|
||||
@ -304,22 +309,23 @@ func getWorks(cnd string, param ...interface{}) (items []*Work, err error) {
|
||||
|
||||
func getWork(id int) (w *Work, err error) {
|
||||
w = new(Work)
|
||||
err = DBQueryRow("SELECT id_work, title, promo, grp, shown, submission_url, corrected, start_availability, end_availability FROM works WHERE id_work=?", id).Scan(&w.Id, &w.Title, &w.Promo, &w.Group, &w.Shown, &w.SubmissionURL, &w.Corrected, &w.StartAvailability, &w.EndAvailability)
|
||||
err = DBQueryRow("SELECT id_work, title, promo, grp, shown, description, submission_url, corrected, start_availability, end_availability FROM works WHERE id_work=?", id).Scan(&w.Id, &w.Title, &w.Promo, &w.Group, &w.Shown, &w.DescriptionRaw, &w.SubmissionURL, &w.Corrected, &w.StartAvailability, &w.EndAvailability)
|
||||
w.Description = string(blackfriday.Run([]byte(w.DescriptionRaw)))
|
||||
return
|
||||
}
|
||||
|
||||
func NewWork(title string, promo uint, group string, shown bool, submissionurl *string, startAvailability time.Time, endAvailability time.Time) (*Work, error) {
|
||||
if res, err := DBExec("INSERT INTO works (title, promo, grp, shown, submission_url, start_availability, end_availability) VALUES (?, ?, ?, ?, ?, ?, ?)", title, promo, group, shown, submissionurl, startAvailability, endAvailability); err != nil {
|
||||
func NewWork(title string, promo uint, group string, shown bool, description string, submissionurl *string, startAvailability time.Time, endAvailability time.Time) (*Work, error) {
|
||||
if res, err := DBExec("INSERT INTO works (title, promo, grp, shown, description, submission_url, start_availability, end_availability) VALUES (?, ?, ?, ?, ?, ?, ?)", title, promo, group, shown, description, submissionurl, startAvailability, endAvailability); err != nil {
|
||||
return nil, err
|
||||
} else if wid, err := res.LastInsertId(); err != nil {
|
||||
return nil, err
|
||||
} else {
|
||||
return &Work{wid, title, promo, group, shown, submissionurl, false, startAvailability, endAvailability}, nil
|
||||
return &Work{wid, title, promo, group, shown, description, description, submissionurl, false, startAvailability, endAvailability}, nil
|
||||
}
|
||||
}
|
||||
|
||||
func (w *Work) Update() (*Work, error) {
|
||||
if _, err := DBExec("UPDATE works SET title = ?, promo = ?, grp = ?, shown = ?, submission_url = ?, corrected = ?, start_availability = ?, end_availability = ? WHERE id_work = ?", w.Title, w.Promo, w.Group, w.Shown, w.SubmissionURL, w.Corrected, w.StartAvailability, w.EndAvailability, w.Id); err != nil {
|
||||
if _, err := DBExec("UPDATE works SET title = ?, promo = ?, grp = ?, shown = ?, description = ?, submission_url = ?, corrected = ?, start_availability = ?, end_availability = ? WHERE id_work = ?", w.Title, w.Promo, w.Group, w.Shown, w.DescriptionRaw, w.SubmissionURL, w.Corrected, w.StartAvailability, w.EndAvailability, w.Id); err != nil {
|
||||
return nil, err
|
||||
} else {
|
||||
return w, err
|
||||
@ -348,12 +354,12 @@ func ClearWorks() (int64, error) {
|
||||
|
||||
type WorkGrade struct {
|
||||
Id int64 `json:"id"`
|
||||
Login string `json:"login,omit_empty"`
|
||||
IdUser int64 `json:"id_user,omit_empty"`
|
||||
IdWork int64 `json:"id_work,omit_empty"`
|
||||
Login string `json:"login,omitempty"`
|
||||
IdUser int64 `json:"id_user,omitempty"`
|
||||
IdWork int64 `json:"id_work,omitempty"`
|
||||
Date time.Time `json:"date"`
|
||||
Grade float64 `json:"score"`
|
||||
Comment string `json:"comment,omit_empty"`
|
||||
Comment string `json:"comment,omitempty"`
|
||||
}
|
||||
|
||||
func (w *Work) GetGrades(cnd string, param ...interface{}) (grades []WorkGrade, err error) {
|
||||
|
Reference in New Issue
Block a user