Updated article loader and added test entry to database
This commit is contained in:
@@ -1,29 +1,57 @@
|
||||
package server
|
||||
|
||||
import (
|
||||
"TheAdversary/config"
|
||||
"TheAdversary/database"
|
||||
"database/sql"
|
||||
"fmt"
|
||||
"github.com/gorilla/mux"
|
||||
"net/http"
|
||||
"text/template"
|
||||
"time"
|
||||
)
|
||||
|
||||
// var tmpl = template.Must(template.ParseFiles(config.ArticleTemplate))
|
||||
var tmpl = template.Must(template.ParseFiles(config.ArticleTemplate))
|
||||
|
||||
type tmplArticle struct {
|
||||
Title string
|
||||
Summary string
|
||||
Image string
|
||||
Authors []string
|
||||
Tags []string
|
||||
Date string
|
||||
Modified bool
|
||||
Content string
|
||||
}
|
||||
|
||||
func Article(w http.ResponseWriter, r *http.Request) {
|
||||
w.Header().Set("Content-Type", "text/html")
|
||||
|
||||
articleName := mux.Vars(r)["article"]
|
||||
var article string
|
||||
if err := database.GetDB().Table("article").Select("html").Where("name = ?", articleName).First(&article).Error; err != nil {
|
||||
if err == sql.ErrNoRows {
|
||||
Error404(w, r)
|
||||
} else {
|
||||
Error500(w, r)
|
||||
}
|
||||
return
|
||||
}
|
||||
var article database.Article
|
||||
if database.GetDB().Table("article").Where("link = ?", articleName).First(&article).RowsAffected == 0 {
|
||||
Error404(w, r)
|
||||
} else if database.GetDB().Error != nil {
|
||||
Error500(w, r)
|
||||
} else {
|
||||
var authors, tags []string
|
||||
database.GetDB().Table("author").Select("id").Where("id IN (?)", database.GetDB().Table("article_author").Select("author_id").Where("article_id = ?", article.Id)).Find(&authors)
|
||||
database.GetDB().Table("article_tag").Where("article_id = ?", article.Id).Find(&tags)
|
||||
|
||||
w.WriteHeader(http.StatusOK)
|
||||
fmt.Fprint(w, article)
|
||||
ta := tmplArticle{
|
||||
Title: article.Title,
|
||||
Summary: article.Summary,
|
||||
Image: article.Image,
|
||||
Authors: authors,
|
||||
Tags: tags,
|
||||
Content: article.Html,
|
||||
}
|
||||
if article.Modified > 0 {
|
||||
ta.Date = time.Unix(article.Modified, 0).Format("Monday, 2. January 2006 | 15:04")
|
||||
ta.Modified = true
|
||||
} else {
|
||||
ta.Date = time.Unix(article.Created, 0).Format("Monday, 2. January 2006 | 15:04")
|
||||
}
|
||||
|
||||
w.WriteHeader(http.StatusOK)
|
||||
tmpl.Execute(w, ta)
|
||||
}
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user