Made link a single word

This commit is contained in:
2022-01-24 15:44:12 +01:00
parent 000f030d9a
commit b358e1a97c
7 changed files with 37 additions and 20 deletions

View File

@@ -10,6 +10,7 @@ import (
"github.com/gomarkdown/markdown" "github.com/gomarkdown/markdown"
"go.uber.org/zap" "go.uber.org/zap"
"net/http" "net/http"
"net/url"
"path" "path"
"strings" "strings"
"time" "time"
@@ -69,7 +70,7 @@ func articlePost(w http.ResponseWriter, r *http.Request) {
Image: payload.Image, Image: payload.Image,
Created: time.Now().Unix(), Created: time.Now().Unix(),
Modified: time.Unix(0, 0).Unix(), Modified: time.Unix(0, 0).Unix(),
Link: "/" + path.Join(config.Prefix, "article", payload.Link), Link: payload.Link,
Markdown: string(rawMarkdown), Markdown: string(rawMarkdown),
Html: string(markdown.ToHTML(rawMarkdown, nil, nil)), Html: string(markdown.ToHTML(rawMarkdown, nil, nil)),
} }
@@ -82,14 +83,16 @@ func articlePost(w http.ResponseWriter, r *http.Request) {
}) })
} }
database.GetDB().Table("article_author").Create(&authors) database.GetDB().Table("article_author").Create(&authors)
var tags []map[string]interface{} if len(payload.Tags) > 0 {
for _, tag := range payload.Tags { var tags []map[string]interface{}
authors = append(authors, map[string]interface{}{ for _, tag := range payload.Tags {
"article_id": a.ID, authors = append(authors, map[string]interface{}{
"tag": tag, "article_id": a.ID,
}) "tag": tag,
})
}
database.GetDB().Table("article_tag").Create(&tags)
} }
database.GetDB().Table("article_tag").Create(&tags)
var articleSummary schema.ArticleSummary var articleSummary schema.ArticleSummary
database.GetDB().Table("article").Find(&articleSummary, &a.ID) database.GetDB().Table("article").Find(&articleSummary, &a.ID)
@@ -99,6 +102,7 @@ func articlePost(w http.ResponseWriter, r *http.Request) {
} else { } else {
articleSummary.Tags = []string{} articleSummary.Tags = []string{}
} }
articleSummary.Link = "/" + path.Join(config.Prefix, "article", url.PathEscape(articleSummary.Link))
w.WriteHeader(http.StatusCreated) w.WriteHeader(http.StatusCreated)
json.NewEncoder(w).Encode(articleSummary) json.NewEncoder(w).Encode(articleSummary)
@@ -175,7 +179,7 @@ func articlePatch(w http.ResponseWriter, r *http.Request) {
updates["image"] = *payload.Image updates["image"] = *payload.Image
} }
if payload.Link != nil { if payload.Link != nil {
updates["link"] = path.Join(config.Prefix, "article", *payload.Link) updates["link"] = *payload.Link
} }
if payload.Content != nil { if payload.Content != nil {
rawMarkdown, err := base64.StdEncoding.DecodeString(*payload.Content) rawMarkdown, err := base64.StdEncoding.DecodeString(*payload.Content)
@@ -212,6 +216,7 @@ func articlePatch(w http.ResponseWriter, r *http.Request) {
} else { } else {
articleSummary.Tags = []string{} articleSummary.Tags = []string{}
} }
articleSummary.Link = "/" + path.Join(config.Prefix, "article", url.PathEscape(articleSummary.Link))
w.WriteHeader(http.StatusOK) w.WriteHeader(http.StatusOK)
json.NewEncoder(w).Encode(articleSummary) json.NewEncoder(w).Encode(articleSummary)

View File

@@ -20,7 +20,7 @@ func TestArticlePost(t *testing.T) {
"title": "Upload test", "title": "Upload test",
"summary": "An example article to test the upload api endpoint", "summary": "An example article to test the upload api endpoint",
"created": time.Now().Unix(), "created": time.Now().Unix(),
"link": "/article/upload-test", "link": "upload-test",
"markdown": "Oh god i have to test all this, what am i doing with my life", "markdown": "Oh god i have to test all this, what am i doing with my life",
"html": "<p>Oh god i have to test all this, what am i doing with my life<p>", "html": "<p>Oh god i have to test all this, what am i doing with my life<p>",
}, },
@@ -93,7 +93,7 @@ func TestArticlePatch(t *testing.T) {
"title": "test article", "title": "test article",
"summary": "example summary", "summary": "example summary",
"created": time.Now().Unix(), "created": time.Now().Unix(),
"link": "/article/test-article", "link": "test-article",
"markdown": "Just a simple test article", "markdown": "Just a simple test article",
"html": "<p>Just a simple test article<p>", "html": "<p>Just a simple test article<p>",
}, },
@@ -190,14 +190,14 @@ func TestArticleDelete(t *testing.T) {
"image": "https://upload.wikimedia.org/wikipedia/commons/0/05/Go_Logo_Blue.svg", "image": "https://upload.wikimedia.org/wikipedia/commons/0/05/Go_Logo_Blue.svg",
"created": time.Now().Unix(), "created": time.Now().Unix(),
"modified": time.Now().Unix(), "modified": time.Now().Unix(),
"link": "/article/test", "link": "test",
"markdown": "# Title", "markdown": "# Title",
"html": "<h1>Title</h1>", "html": "<h1>Title</h1>",
}, },
{ {
"title": "owo", "title": "owo",
"created": time.Now().Unix(), "created": time.Now().Unix(),
"link": "/article/owo", "link": "owo",
"markdown": "owo", "markdown": "owo",
"html": "<p>owo<p>", "html": "<p>owo<p>",
}, },

View File

@@ -1,6 +1,7 @@
package api package api
import ( import (
"TheAdversary/config"
"TheAdversary/database" "TheAdversary/database"
"TheAdversary/schema" "TheAdversary/schema"
"encoding/base64" "encoding/base64"
@@ -56,6 +57,10 @@ func assetsGet(w http.ResponseWriter, r *http.Request) {
var assets []schema.Asset var assets []schema.Asset
request.Find(&assets) request.Find(&assets)
for _, asset := range assets {
asset.Link = "/" + path.Join(config.Prefix, "assets", asset.Link)
}
w.WriteHeader(http.StatusOK) w.WriteHeader(http.StatusOK)
json.NewEncoder(w).Encode(&assets) json.NewEncoder(w).Encode(&assets)
} }
@@ -84,7 +89,7 @@ func assetsPost(w http.ResponseWriter, r *http.Request) {
Name string Name string
Data []byte Data []byte
Link string Link string
}{Name: payload.Name, Data: rawData, Link: "/" + path.Join("assets", url.PathEscape(payload.Name))} }{Name: payload.Name, Data: rawData, Link: url.PathEscape(payload.Name)}
if database.GetDB().Table("assets").Clauses(clause.OnConflict{ if database.GetDB().Table("assets").Clauses(clause.OnConflict{
Columns: []clause.Column{{Name: "name"}}, Columns: []clause.Column{{Name: "name"}},
@@ -96,7 +101,7 @@ func assetsPost(w http.ResponseWriter, r *http.Request) {
json.NewEncoder(w).Encode(schema.Asset{ json.NewEncoder(w).Encode(schema.Asset{
Id: tmpDatabaseSchema.Id, Id: tmpDatabaseSchema.Id,
Name: tmpDatabaseSchema.Name, Name: tmpDatabaseSchema.Name,
Link: tmpDatabaseSchema.Link, Link: "/" + path.Join(config.Prefix, "assets", tmpDatabaseSchema.Link),
}) })
} }
} }

View File

@@ -1,10 +1,12 @@
package api package api
import ( import (
"TheAdversary/config"
"TheAdversary/database" "TheAdversary/database"
"TheAdversary/schema" "TheAdversary/schema"
"encoding/json" "encoding/json"
"net/http" "net/http"
"path"
"strconv" "strconv"
) )
@@ -33,6 +35,7 @@ func Recent(w http.ResponseWriter, r *http.Request) {
database.GetDB().Table("author").Where("id IN (?)", database.GetDB().Table("article_author").Select("author_id").Where("article_id = ?", summary.Id)).Find(&summary.Authors) database.GetDB().Table("author").Where("id IN (?)", database.GetDB().Table("article_author").Select("author_id").Where("article_id = ?", summary.Id)).Find(&summary.Authors)
summary.Tags = []string{} summary.Tags = []string{}
database.GetDB().Table("article_tag").Select("tag").Where("article_id = ?", summary.Id).Find(&summary.Tags) database.GetDB().Table("article_tag").Select("tag").Where("article_id = ?", summary.Id).Find(&summary.Tags)
summary.Link = "/" + path.Join(config.Prefix, "article", summary.Link)
articleSummaries[i] = summary articleSummaries[i] = summary
} }

View File

@@ -5,6 +5,7 @@ import (
"TheAdversary/schema" "TheAdversary/schema"
"net/http" "net/http"
"net/http/httptest" "net/http/httptest"
"path"
"testing" "testing"
"time" "time"
) )
@@ -49,7 +50,7 @@ func TestRecent(t *testing.T) {
"summary": articles[0].Summary, "summary": articles[0].Summary,
"created": articles[0].Created, "created": articles[0].Created,
"modified": articles[0].Modified, "modified": articles[0].Modified,
"link": articles[0].Link, "link": path.Base(articles[0].Link),
"markdown": "# Title", "markdown": "# Title",
"html": "<h1>Title</h1>", "html": "<h1>Title</h1>",
}, },
@@ -57,7 +58,7 @@ func TestRecent(t *testing.T) {
"title": articles[1].Title, "title": articles[1].Title,
"summary": articles[1].Summary, "summary": articles[1].Summary,
"created": articles[1].Created, "created": articles[1].Created,
"link": articles[1].Link, "link": path.Base(articles[1].Link),
"markdown": "This is the most recent article", "markdown": "This is the most recent article",
"html": "<p>This is the most recent article</p>", "html": "<p>This is the most recent article</p>",
}, },

View File

@@ -1,10 +1,12 @@
package api package api
import ( import (
"TheAdversary/config"
"TheAdversary/database" "TheAdversary/database"
"TheAdversary/schema" "TheAdversary/schema"
"encoding/json" "encoding/json"
"net/http" "net/http"
"path"
"strconv" "strconv"
"strings" "strings"
) )
@@ -70,6 +72,7 @@ func Search(w http.ResponseWriter, r *http.Request) {
database.GetDB().Table("author").Where("id IN (?)", database.GetDB().Table("article_author").Select("author_id").Where("article_id = ?", summary.Id)).Find(&summary.Authors) database.GetDB().Table("author").Where("id IN (?)", database.GetDB().Table("article_author").Select("author_id").Where("article_id = ?", summary.Id)).Find(&summary.Authors)
summary.Tags = []string{} summary.Tags = []string{}
database.GetDB().Table("article_tag").Select("tag").Where("article_id = ?", summary.Id).Find(&summary.Tags) database.GetDB().Table("article_tag").Select("tag").Where("article_id = ?", summary.Id).Find(&summary.Tags)
summary.Link = "/" + path.Join(config.Prefix, "article", summary.Link)
articleSummaries[i] = summary articleSummaries[i] = summary
} }

View File

@@ -20,7 +20,7 @@ func TestSearch(t *testing.T) {
{ {
"title": "First article", "title": "First article",
"created": now.Unix(), "created": now.Unix(),
"link": "/article/first-article", "link": "first-article",
"markdown": "This is my first article", "markdown": "This is my first article",
"html": "<p>This is my first article</p>", "html": "<p>This is my first article</p>",
}, },
@@ -30,7 +30,7 @@ func TestSearch(t *testing.T) {
"image": "https://upload.wikimedia.org/wikipedia/commons/0/05/Go_Logo_Blue.svg", "image": "https://upload.wikimedia.org/wikipedia/commons/0/05/Go_Logo_Blue.svg",
"created": now.Unix(), "created": now.Unix(),
"modified": now.Add(24 * time.Hour).Unix(), "modified": now.Add(24 * time.Hour).Unix(),
"link": "/article/test", "link": "test",
"markdown": "# Title", "markdown": "# Title",
"html": "<h1>Title</h1>", "html": "<h1>Title</h1>",
}, },
@@ -38,7 +38,7 @@ func TestSearch(t *testing.T) {
"title": "owo", "title": "owo",
"created": now.Unix(), "created": now.Unix(),
"modified": now.Add(12 * time.Hour).Unix(), "modified": now.Add(12 * time.Hour).Unix(),
"link": "/article/owo", "link": "owo",
"markdown": "owo", "markdown": "owo",
"html": "<p>owo<p>", "html": "<p>owo<p>",
}, },