Add descriptions to Works
This commit is contained in:
parent
7e7608b7d1
commit
767da66f63
5 changed files with 50 additions and 18 deletions
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 a new issue