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

View File

@@ -20,7 +20,7 @@ func TestArticlePost(t *testing.T) {
"title": "Upload test",
"summary": "An example article to test the upload api endpoint",
"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",
"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",
"summary": "example summary",
"created": time.Now().Unix(),
"link": "/article/test-article",
"link": "test-article",
"markdown": "Just a simple test article",
"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",
"created": time.Now().Unix(),
"modified": time.Now().Unix(),
"link": "/article/test",
"link": "test",
"markdown": "# Title",
"html": "<h1>Title</h1>",
},
{
"title": "owo",
"created": time.Now().Unix(),
"link": "/article/owo",
"link": "owo",
"markdown": "owo",
"html": "<p>owo<p>",
},

View File

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

View File

@@ -1,10 +1,12 @@
package api
import (
"TheAdversary/config"
"TheAdversary/database"
"TheAdversary/schema"
"encoding/json"
"net/http"
"path"
"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)
summary.Tags = []string{}
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
}

View File

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

View File

@@ -1,10 +1,12 @@
package api
import (
"TheAdversary/config"
"TheAdversary/database"
"TheAdversary/schema"
"encoding/json"
"net/http"
"path"
"strconv"
"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)
summary.Tags = []string{}
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
}

View File

@@ -20,7 +20,7 @@ func TestSearch(t *testing.T) {
{
"title": "First article",
"created": now.Unix(),
"link": "/article/first-article",
"link": "first-article",
"markdown": "This is my first article",
"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",
"created": now.Unix(),
"modified": now.Add(24 * time.Hour).Unix(),
"link": "/article/test",
"link": "test",
"markdown": "# Title",
"html": "<h1>Title</h1>",
},
@@ -38,7 +38,7 @@ func TestSearch(t *testing.T) {
"title": "owo",
"created": now.Unix(),
"modified": now.Add(12 * time.Hour).Unix(),
"link": "/article/owo",
"link": "owo",
"markdown": "owo",
"html": "<p>owo<p>",
},