Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
10 changes: 5 additions & 5 deletions api/README.md
Original file line number Diff line number Diff line change
Expand Up @@ -50,7 +50,7 @@ information on the total posts, pages and posts per page.
{
"url": "/api/v1/post/example-post",
"title": "An Example Post",
"date": "2018-05-18T00:00:00Z",
"date": "2018-05-18",
"content": "TEST",
"created_at": "2018-05-18T15:16:17Z",
"updated_at": "2018-05-18T15:16:17Z"
Expand Down Expand Up @@ -79,7 +79,7 @@ Contains the single post.
{
"url": "/api/v1/post/example-post",
"title": "An Example Post",
"date": "2018-05-18T00:00:00Z",
"date": "2018-05-18",
"content": "TEST",
"created_at": "2018-05-18T15:16:17Z",
"updated_at": "2018-05-18T15:16:17Z"
Expand Down Expand Up @@ -117,7 +117,7 @@ The date can be provided in the following formats:
{
"url": "/api/v1/post/a-brand-new-post",
"title": "A Brand New Post",
"date": "2018-06-28T00:42:12Z",
"date": "2018-06-28",
"content": "This is a brand new post, completely."
}
```
Expand All @@ -141,7 +141,7 @@ Contains a randomly selected post.
{
"url": "/api/v1/post/example-post",
"title": "An Example Post",
"date": "2018-05-18T12:53:22Z",
"date": "2018-05-18",
"content": "TEST"
}
```
Expand Down Expand Up @@ -187,7 +187,7 @@ When updating the post, the slug remains constant, even when the title changes.
{
"url": "/api/v1/post/a-brand-new-post",
"title": "Even Braver New World",
"date": "2018-06-21T09:12:00Z",
"date": "2018-06-21",
"content": "I changed a bit more on this attempt."
}
```
Expand Down
2 changes: 1 addition & 1 deletion internal/app/controller/apiv1/create.go
Original file line number Diff line number Diff line change
Expand Up @@ -37,7 +37,7 @@ func (c *Create) Run(response http.ResponseWriter, request *http.Request) {
response.WriteHeader(http.StatusCreated)
encoder := json.NewEncoder(response)
encoder.SetEscapeHTML(false)
encoder.Encode(journal)
encoder.Encode(MapJournalToJSON(journal))
}
}
}
2 changes: 1 addition & 1 deletion internal/app/controller/apiv1/data.go
Original file line number Diff line number Diff line change
Expand Up @@ -21,7 +21,7 @@ func MapJournalToJSON(journal model.Journal) journalToJSON {
result := journalToJSON{
URL: "/api/v1/post/" + journal.Slug,
Title: journal.Title,
Date: journal.Date,
Date: journal.GetEditableDate(),
Content: journal.Content,
}

Expand Down
2 changes: 1 addition & 1 deletion internal/app/controller/apiv1/stats.go
Original file line number Diff line number Diff line change
Expand Up @@ -52,7 +52,7 @@ func (c *Stats) Run(response http.ResponseWriter, request *http.Request) {

if stats.Posts.Count > 0 {
firstPost := allJournals[stats.Posts.Count-1]
stats.Posts.FirstPostDate = firstPost.GetDate()
stats.Posts.FirstPostDate = firstPost.GetEditableDate()
}

stats.Configuration.Title = container.Configuration.Title
Expand Down
2 changes: 1 addition & 1 deletion internal/app/controller/apiv1/update.go
Original file line number Diff line number Diff line change
Expand Up @@ -51,7 +51,7 @@ func (c *Update) Run(response http.ResponseWriter, request *http.Request) {
journal = js.Save(journal)
encoder := json.NewEncoder(response)
encoder.SetEscapeHTML(false)
encoder.Encode(journal)
encoder.Encode(MapJournalToJSON(journal))
}
}
}
Expand Down
9 changes: 3 additions & 6 deletions internal/app/controller/web/view_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -63,7 +63,7 @@ func TestView_Run(t *testing.T) {
t.Error("Expected previous and next links to be shown in page")
}

// Test that timestamp metadata section is NOT displayed when timestamps are nil
// Test that timestamp labels are displayed when timestamps are present
response.Reset()
request, _ = http.NewRequest("GET", "/slug", strings.NewReader(""))
// Reset database to single mode
Expand All @@ -72,10 +72,7 @@ func TestView_Run(t *testing.T) {
db.Rows = &database.MockJournal_SingleRow{}
controller.Init(container, []string{"", "slug"}, request)
controller.Run(response, request)
if strings.Contains(response.Content, "class=\"metadata\"") {
t.Error("Expected metadata section to NOT be displayed when timestamps are nil")
}
if strings.Contains(response.Content, "Created:") || strings.Contains(response.Content, "Last updated:") {
t.Error("Expected timestamp labels to NOT be displayed when timestamps are nil")
if !strings.Contains(response.Content, "Created:") || !strings.Contains(response.Content, "Last Updated:") {
t.Error("Expected timestamp labels to be displayed when timestamps are present")
}
}
2 changes: 1 addition & 1 deletion internal/app/model/visit.go
Original file line number Diff line number Diff line change
Expand Up @@ -110,7 +110,7 @@ func (vs *Visits) GetDailyStats(days int) []DailyVisit {

query := `
SELECT
date,
DATE(date),
COALESCE(SUM(CASE WHEN url LIKE '/api/%' THEN hits ELSE 0 END), 0) as api_hits,
COALESCE(SUM(CASE WHEN url NOT LIKE '/api/%' THEN hits ELSE 0 END), 0) as web_hits,
COALESCE(SUM(hits), 0) as total
Expand Down
12 changes: 6 additions & 6 deletions journal_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -96,7 +96,7 @@ func TestApiv1List(t *testing.T) {

defer res.Body.Close()
body, _ := io.ReadAll(res.Body)
expected := `{"links":{},"pagination":{"current_page":1,"total_pages":1,"posts_per_page":20,"total_posts":3},"posts":[{"url":"/api/v1/post/test-3","title":"A Final Test","date":"2018-03-01T00:00:00Z","content":"<p>Test finally!</p>"},{"url":"/api/v1/post/test-2","title":"Another Test","date":"2018-02-01T00:00:00Z","content":"<p>Test again!</p>"},{"url":"/api/v1/post/test","title":"Test","date":"2018-01-01T00:00:00Z","content":"<p>Test!</p>"}]}`
expected := `{"links":{},"pagination":{"current_page":1,"total_pages":1,"posts_per_page":20,"total_posts":3},"posts":[{"url":"/api/v1/post/test-3","title":"A Final Test","date":"2018-03-01","content":"<p>Test finally!</p>"},{"url":"/api/v1/post/test-2","title":"Another Test","date":"2018-02-01","content":"<p>Test again!</p>"},{"url":"/api/v1/post/test","title":"Test","date":"2018-01-01","content":"<p>Test!</p>"}]}`

// Use contains to get rid of any extra whitespace that we can discount
if !strings.Contains(string(body[:]), expected) {
Expand All @@ -122,7 +122,7 @@ func TestApiV1Single(t *testing.T) {

defer res.Body.Close()
body, _ := io.ReadAll(res.Body)
expected := `{"url":"/api/v1/post/test","title":"Test","date":"2018-01-01T00:00:00Z","content":"<p>Test!</p>"}`
expected := `{"url":"/api/v1/post/test","title":"Test","date":"2018-01-01","content":"<p>Test!</p>"}`

// Use contains to get rid of any extra whitespace that we can discount
if !strings.Contains(string(body[:]), expected) {
Expand Down Expand Up @@ -191,7 +191,7 @@ func TestApiV1Create(t *testing.T) {
bodyStr := string(body[:])

// Check for expected fields
expectedFields := []string{`"id":4`, `"slug":"test-4"`, `"title":"Test 4"`, `"date":"2018-06-01T00:00:00Z"`, `"content":"<p>Test 4!</p>"`, `"created_at"`, `"updated_at"`}
expectedFields := []string{`"url":"/api/v1/post/test-4"`, `"title":"Test 4"`, `"date":"2018-06-01"`, `"content":"<p>Test 4!</p>"`, `"created_at"`, `"updated_at"`}
for _, field := range expectedFields {
if !strings.Contains(bodyStr, field) {
t.Errorf("Expected response to contain %s\nGot:\n\t%s", field, bodyStr)
Expand Down Expand Up @@ -265,7 +265,7 @@ func TestApiV1Create_RepeatTitles(t *testing.T) {
bodyStr := string(body[:])

// Check for expected fields
expectedFields := []string{`"url":"/api/v1/post/repeated-1"`, `"title":"Repeated"`, `"date":"2019-02-01T00:00:00Z"`, `"content":"<p>Repeated content test again!</p>"`, `"created_at"`, `"updated_at"`}
expectedFields := []string{`"url":"/api/v1/post/repeated-1"`, `"title":"Repeated"`, `"date":"2019-02-01"`, `"content":"<p>Repeated content test again!</p>"`, `"created_at"`, `"updated_at"`}
for _, field := range expectedFields {
if !strings.Contains(bodyStr, field) {
t.Errorf("Expected response to contain %s\nGot:\n\t%s", field, bodyStr)
Expand Down Expand Up @@ -293,7 +293,7 @@ func TestApiV1Update(t *testing.T) {
bodyStr := string(body[:])

// Check for expected fields
expectedFields := []string{`"id":1`, `"slug":"test"`, `"title":"A different title"`, `"date":"2018-01-01T00:00:00Z"`, `"content":"<p>Test!</p>"`, `"updated_at"`}
expectedFields := []string{`"url":"/api/v1/post/test"`, `"title":"A different title"`, `"date":"2018-01-01"`, `"content":"<p>Test!</p>"`, `"updated_at"`}
for _, field := range expectedFields {
if !strings.Contains(bodyStr, field) {
t.Errorf("Expected response to contain %s\nGot:\n\t%s", field, bodyStr)
Expand Down Expand Up @@ -359,7 +359,7 @@ func TestApiV1Stats(t *testing.T) {
now := time.Now()
date := now.Format("2006-01-02")
month := now.Format("2006-01")
expected := fmt.Sprintf(`{"posts":{"count":3,"first_post_date":"Monday January 1, 2018"},"configuration":{"title":"Jamie's Journal","description":"A private journal containing Jamie's innermost thoughts","theme":"default","posts_per_page":20,"google_analytics":false,"create_enabled":true,"edit_enabled":true},"visits":{"daily":[{"date":"%sT00:00:00Z","api_hits":1,"web_hits":0,"total":1}],"monthly":[{"month":"%s","api_hits":1,"web_hits":0,"total":1}]}}`, date, month)
expected := fmt.Sprintf(`{"posts":{"count":3,"first_post_date":"2018-01-01"},"configuration":{"title":"Jamie's Journal","description":"A private journal containing Jamie's innermost thoughts","theme":"default","posts_per_page":20,"google_analytics":false,"create_enabled":true,"edit_enabled":true},"visits":{"daily":[{"date":"%s","api_hits":1,"web_hits":0,"total":1}],"monthly":[{"month":"%s","api_hits":1,"web_hits":0,"total":1}]}}`, date, month)

// Use contains to get rid of any extra whitespace that we can discount
if !strings.Contains(string(body[:]), expected) {
Expand Down
2 changes: 2 additions & 0 deletions pkg/session/store.go
Original file line number Diff line number Diff line change
Expand Up @@ -76,6 +76,8 @@ func (s *DefaultStore) Get(r *http.Request) (*Session, error) {
}
if err == nil {
s.cachedSession = session
} else {
s.cachedSession = NewSession()
}
}

Expand Down
6 changes: 4 additions & 2 deletions test/mocks/database/database.go
Original file line number Diff line number Diff line change
Expand Up @@ -90,8 +90,10 @@ func (m *MockJournal_SingleRow) Scan(dest ...interface{}) error {
*dest[2].(*string) = "Title"
*dest[3].(*string) = "2018-02-01"
*dest[4].(*string) = "Content"
*dest[5].(**time.Time) = nil
*dest[6].(**time.Time) = nil
createdAt := time.Date(2018, 2, 1, 10, 0, 0, 0, time.UTC)
updatedAt := time.Date(2018, 2, 1, 10, 0, 0, 0, time.UTC)
*dest[5].(**time.Time) = &createdAt
*dest[6].(**time.Time) = &updatedAt
}
return nil
}
Expand Down
15 changes: 8 additions & 7 deletions web/static/openapi.yml
Original file line number Diff line number Diff line change
Expand Up @@ -113,8 +113,8 @@ components:
example: 'My Journal Post'
date:
type: string
format: date-time
example: '2018-06-21T09:12:00Z'
format: date
example: '2018-06-21'
content:
type: string
example: 'Some post content.'
Expand Down Expand Up @@ -172,7 +172,7 @@ components:
date:
type: string
format: date
example: '2018-06-2'
example: '2018-06-21'
content:
type: string
example: 'Some post content.'
Expand Down Expand Up @@ -206,7 +206,8 @@ components:
example: 42
first_post_date:
type: string
example: 'Monday January 1, 2018'
format: date
example: '2018-01-01'
configuration:
type: object
required:
Expand All @@ -226,7 +227,7 @@ components:
example: "A private journal containing Jamie's innermost thoughts"
theme:
type: string
example: "default"
example: 'default'
posts_per_page:
type: integer
example: 20
Expand All @@ -251,7 +252,7 @@ components:
date:
type: string
format: date
example: "2023-12-25"
example: '2023-12-25'
api_hits:
type: integer
example: 15
Expand All @@ -269,7 +270,7 @@ components:
properties:
month:
type: string
example: "2023-12"
example: '2023-12'
api_hits:
type: integer
example: 450
Expand Down