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
Original file line number Diff line number Diff line change
@@ -0,0 +1,24 @@
ALTER TABLE books ADD COLUMN release_date_sort DATE;

DO $$
BEGIN
SET LOCAL lc_time = 'en_US.UTF-8';

UPDATE books
SET release_date_sort = CASE
-- "January 2, 2006"
WHEN release_date ~* '^[a-z]+ \d{1,2}, \d{4}$'
THEN TO_DATE(release_date, 'Month DD, YYYY')

-- "January 2006"
WHEN release_date ~* '^[a-z]+ \d{4}$'
THEN TO_DATE(release_date, 'Month YYYY')

-- "2006"
ELSE TO_DATE(release_date, 'YYYY')
END;
END $$;

ALTER TABLE books ALTER COLUMN release_date_sort SET NOT NULL;

CREATE INDEX idx_books_release_date_sort ON books (release_date_sort DESC);
16 changes: 9 additions & 7 deletions app/ds/book.go
Original file line number Diff line number Diff line change
Expand Up @@ -15,12 +15,13 @@ var bookCtxKey ctxKey = "book"
type Book struct {
*Entity

DescriptionRaw string `json:"-"`
Description string `json:"description"`
CoverFileID ID `json:"cover_file_id"`
Authors []BookAuthor `json:"authors"`
Homepage string `json:"homepage"`
ReleaseDate string `json:"release_date"`
DescriptionRaw string `json:"-"`
Description string `json:"description"`
CoverFileID ID `json:"cover_file_id"`
Authors []BookAuthor `json:"authors"`
Homepage string `json:"homepage"`
ReleaseDate string `json:"release_date"`
ReleaseDateSort time.Time `json:"-"`
}

// Data returns the editable fields of the Book as a key-value map.
Expand Down Expand Up @@ -76,8 +77,9 @@ func (b *Book) CreateRules() z.Shape {
}

for _, layout := range ReleaseDateLayouts {
_, err := time.Parse(layout, *val)
t, err := time.Parse(layout, *val)
if err == nil {
b.ReleaseDateSort = t
return true
}
}
Expand Down
28 changes: 15 additions & 13 deletions app/repo/book_repo.go
Original file line number Diff line number Diff line change
Expand Up @@ -21,13 +21,14 @@ func (r *Repo) CreateBook(ctx context.Context, b *ds.Book) error {
defer span.End()

return r.insert(ctx, "books", data{
"id": b.ID,
"description_raw": b.DescriptionRaw,
"description": b.Description,
"cover_file_id": b.CoverFileID,
"authors": b.Authors,
"homepage": b.Homepage,
"release_date": b.ReleaseDate,
"id": b.ID,
"description_raw": b.DescriptionRaw,
"description": b.Description,
"cover_file_id": b.CoverFileID,
"authors": b.Authors,
"homepage": b.Homepage,
"release_date": b.ReleaseDate,
"release_date_sort": b.ReleaseDateSort,
})
}

Expand Down Expand Up @@ -82,12 +83,13 @@ func (r *Repo) UpdateBook(ctx context.Context, b *ds.Book) error {
defer span.End()

err := r.update(ctx, b.ID, "books", data{
"description_raw": b.DescriptionRaw,
"description": b.Description,
"cover_file_id": b.CoverFileID,
"authors": b.Authors,
"homepage": b.Homepage,
"release_date": b.ReleaseDate,
"description_raw": b.DescriptionRaw,
"description": b.Description,
"cover_file_id": b.CoverFileID,
"authors": b.Authors,
"homepage": b.Homepage,
"release_date": b.ReleaseDate,
"release_date_sort": b.ReleaseDateSort,
})
if err != nil {
return fmt.Errorf("update book: %w", err)
Expand Down
30 changes: 21 additions & 9 deletions frontend/assets/output.css
Original file line number Diff line number Diff line change
Expand Up @@ -31,6 +31,7 @@
--text-lg--line-height: calc(1.75 / 1.125);
--text-3xl: 1.875rem;
--text-3xl--line-height: calc(2.25 / 1.875);
--font-weight-normal: 400;
--font-weight-bold: 700;
--radius-lg: 0.5rem;
--ease-out: cubic-bezier(0, 0, 0.2, 1);
Expand Down Expand Up @@ -3590,6 +3591,9 @@
.left-0 {
left: calc(var(--spacing) * 0);
}
.left-1 {
left: calc(var(--spacing) * 1);
}
.left-1\/2 {
left: calc(1/2 * 100%);
}
Expand Down Expand Up @@ -5920,6 +5924,9 @@
.w-6 {
width: calc(var(--spacing) * 6);
}
.w-11 {
width: calc(var(--spacing) * 11);
}
.w-11\/12 {
width: calc(11/12 * 100%);
}
Expand Down Expand Up @@ -5977,6 +5984,10 @@
.border-collapse {
border-collapse: collapse;
}
.-translate-x-1 {
--tw-translate-x: calc(var(--spacing) * -1);
translate: var(--tw-translate-x) var(--tw-translate-y);
}
.-translate-x-1\/2 {
--tw-translate-x: calc(calc(1/2 * 100%) * -1);
translate: var(--tw-translate-x) var(--tw-translate-y);
Expand Down Expand Up @@ -6615,6 +6626,9 @@
.border-info {
border-color: var(--color-info);
}
.border-neutral-content {
border-color: var(--color-neutral-content);
}
.border-neutral-content\/30 {
border-color: var(--color-neutral-content);
@supports (color: color-mix(in lab, red, red)) {
Expand Down Expand Up @@ -7228,9 +7242,16 @@
--tw-font-weight: var(--font-weight-bold);
font-weight: var(--font-weight-bold);
}
.font-normal {
--tw-font-weight: var(--font-weight-normal);
font-weight: var(--font-weight-normal);
}
.text-wrap {
text-wrap: wrap;
}
.whitespace-nowrap {
white-space: nowrap;
}
.whitespace-pre-wrap {
white-space: pre-wrap;
}
Expand Down Expand Up @@ -7653,10 +7674,6 @@
}
}
}
.blur {
--tw-blur: blur(8px);
filter: var(--tw-blur,) var(--tw-brightness,) var(--tw-contrast,) var(--tw-grayscale,) var(--tw-hue-rotate,) var(--tw-invert,) var(--tw-saturate,) var(--tw-sepia,) var(--tw-drop-shadow,);
}
.filter {
filter: var(--tw-blur,) var(--tw-brightness,) var(--tw-contrast,) var(--tw-grayscale,) var(--tw-hue-rotate,) var(--tw-invert,) var(--tw-saturate,) var(--tw-sepia,) var(--tw-drop-shadow,);
}
Expand Down Expand Up @@ -8369,11 +8386,6 @@
width: auto;
}
}
.lg\:grid-cols-2 {
@media (width >= 64rem) {
grid-template-columns: repeat(2, minmax(0, 1fr));
}
}
.lg\:grid-cols-4 {
@media (width >= 64rem) {
grid-template-columns: repeat(4, minmax(0, 1fr));
Expand Down
4 changes: 3 additions & 1 deletion frontend/page/filter_books.templ
Original file line number Diff line number Diff line change
Expand Up @@ -271,7 +271,7 @@ templ FilterBooksPage() {
</template>

<template x-for="b in books" :key="b.id">
<div class="card rounded-none card-side bg-base-100 shadow-sm mb-2">
<div class="card rounded-none card-side bg-base-100 mb-3">
<template x-if="b.cover_file_id">
<figure>
<img
Expand All @@ -292,8 +292,10 @@ templ FilterBooksPage() {
:href="'/books/' + b.public_id + '/'"
x-text="b.title"
></a>
<span x-text="b.release_date" class="italic font-normal text-gray-400"></span>
</h2>


<a
class="btn btn-ghost btn-sm btn-square"
title="Edit"
Expand Down
2 changes: 1 addition & 1 deletion frontend/page/filter_books_templ.go

Large diffs are not rendered by default.

2 changes: 1 addition & 1 deletion server/handler/book_handler.go
Original file line number Diff line number Diff line change
Expand Up @@ -188,7 +188,7 @@ func (h *Handler) FilterBooks(w http.ResponseWriter, r *http.Request) {
Status: req.Status,
Visibility: req.Visibility,
Topics: req.Topics,
OrderBy: "created_at",
OrderBy: "b.release_date_sort",
OrderDirection: "desc",
}

Expand Down
20 changes: 16 additions & 4 deletions test/api_test/book_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@ import (
"fmt"
"net/http"
"testing"
"time"

"github.com/gopl-dev/server/app"
"github.com/gopl-dev/server/app/ds"
Expand Down Expand Up @@ -47,6 +48,15 @@ func TestCreateBook_Basic(t *testing.T) {
test.CheckErr(t, err)
assert.Equal(t, resp.Description, descriptionHTML)

var releaseDateSort time.Time
for _, layout := range ds.ReleaseDateLayouts {
t, err := time.Parse(layout, req.ReleaseDate)
if err == nil {
releaseDateSort = t
break
}
}

// check entity created
test.AssertInDB(t, tt.DB, "entities", test.Data{
"id": resp.ID,
Expand All @@ -61,10 +71,12 @@ func TestCreateBook_Basic(t *testing.T) {

// check book created
test.AssertInDB(t, tt.DB, "books", test.Data{
"authors": req.Authors,
"homepage": req.Homepage,
"description_raw": req.Description,
"description": descriptionHTML,
"authors": req.Authors,
"homepage": req.Homepage,
"description_raw": req.Description,
"description": descriptionHTML,
"release_date": req.ReleaseDate,
"release_date_sort": releaseDateSort,
})

// check log created
Expand Down
Loading