This commit is contained in:
@@ -40,7 +40,7 @@ type PostDTO struct {
|
|||||||
PublishedAt string `json:"publishedAt"`
|
PublishedAt string `json:"publishedAt"`
|
||||||
UpdatedAt string `json:"updatedAt"`
|
UpdatedAt string `json:"updatedAt"`
|
||||||
CreatedAt string `json:"createdAt,omitempty"`
|
CreatedAt string `json:"createdAt,omitempty"`
|
||||||
Tags []string `json:"tags,omitempty"`
|
Tags []string `json:"tags"`
|
||||||
}
|
}
|
||||||
|
|
||||||
type PostListResponse struct {
|
type PostListResponse struct {
|
||||||
@@ -310,6 +310,32 @@ func loadPostTagNames(ctx context.Context, pool *pgxpool.Pool, postID uuid.UUID,
|
|||||||
return names, rows.Err()
|
return names, rows.Err()
|
||||||
}
|
}
|
||||||
|
|
||||||
|
func loadPostTagNamesByPostIDs(ctx context.Context, pool *pgxpool.Pool, ids []uuid.UUID, lang string) (map[uuid.UUID][]string, error) {
|
||||||
|
out := make(map[uuid.UUID][]string)
|
||||||
|
if len(ids) == 0 {
|
||||||
|
return out, nil
|
||||||
|
}
|
||||||
|
rows, err := pool.Query(ctx, `
|
||||||
|
SELECT pt.post_id, t.name, COALESCE(t.name_en,''), COALESCE(t.name_ja,''), COALESCE(t.name_ko,''),
|
||||||
|
COALESCE(t.name_vi,''), COALESCE(t.name_id,''), COALESCE(t.name_ms,'')
|
||||||
|
FROM post_tags pt JOIN tags t ON t.id = pt.tag_id
|
||||||
|
WHERE pt.post_id = ANY($1)
|
||||||
|
ORDER BY pt.post_id, t.slug`, ids)
|
||||||
|
if err != nil {
|
||||||
|
return nil, err
|
||||||
|
}
|
||||||
|
defer rows.Close()
|
||||||
|
for rows.Next() {
|
||||||
|
var pid uuid.UUID
|
||||||
|
var name, en, ja, ko, vi, idLoc, ms string
|
||||||
|
if err := rows.Scan(&pid, &name, &en, &ja, &ko, &vi, &idLoc, &ms); err != nil {
|
||||||
|
return nil, err
|
||||||
|
}
|
||||||
|
out[pid] = append(out[pid], tagDisplayName(name, en, ja, ko, vi, idLoc, ms, lang))
|
||||||
|
}
|
||||||
|
return out, rows.Err()
|
||||||
|
}
|
||||||
|
|
||||||
func replacePostTags(ctx context.Context, pool *pgxpool.Pool, postID uuid.UUID, tagSlugs []string) error {
|
func replacePostTags(ctx context.Context, pool *pgxpool.Pool, postID uuid.UUID, tagSlugs []string) error {
|
||||||
_, err := pool.Exec(ctx, `DELETE FROM post_tags WHERE post_id = $1`, postID)
|
_, err := pool.Exec(ctx, `DELETE FROM post_tags WHERE post_id = $1`, postID)
|
||||||
if err != nil {
|
if err != nil {
|
||||||
|
|||||||
@@ -86,6 +86,9 @@ func GetPost(w http.ResponseWriter, r *http.Request) {
|
|||||||
dto.Attachments = []AttachmentDTO{}
|
dto.Attachments = []AttachmentDTO{}
|
||||||
}
|
}
|
||||||
tags, _ := loadPostTagNames(r.Context(), pool, id, requestLangCode(r))
|
tags, _ := loadPostTagNames(r.Context(), pool, id, requestLangCode(r))
|
||||||
|
if tags == nil {
|
||||||
|
tags = []string{}
|
||||||
|
}
|
||||||
dto.Tags = tags
|
dto.Tags = tags
|
||||||
writeJSON(w, dto)
|
writeJSON(w, dto)
|
||||||
}
|
}
|
||||||
@@ -198,6 +201,7 @@ func collectPostRows(r *http.Request, rows pgx.Rows) ([]PostDTO, error) {
|
|||||||
uid, _ := uuid.Parse(dto.ID)
|
uid, _ := uuid.Parse(dto.ID)
|
||||||
ids = append(ids, uid)
|
ids = append(ids, uid)
|
||||||
dto.Attachments = []AttachmentDTO{}
|
dto.Attachments = []AttachmentDTO{}
|
||||||
|
dto.Tags = []string{}
|
||||||
list = append(list, dto)
|
list = append(list, dto)
|
||||||
}
|
}
|
||||||
if err := rows.Err(); err != nil {
|
if err := rows.Err(); err != nil {
|
||||||
@@ -207,11 +211,21 @@ func collectPostRows(r *http.Request, rows pgx.Rows) ([]PostDTO, error) {
|
|||||||
if err != nil {
|
if err != nil {
|
||||||
return nil, err
|
return nil, err
|
||||||
}
|
}
|
||||||
|
lang := requestLangCode(r)
|
||||||
|
tagsByPost, err := loadPostTagNamesByPostIDs(r.Context(), poolFrom(r), ids, lang)
|
||||||
|
if err != nil {
|
||||||
|
return nil, err
|
||||||
|
}
|
||||||
for i := range list {
|
for i := range list {
|
||||||
uid, _ := uuid.Parse(list[i].ID)
|
uid, _ := uuid.Parse(list[i].ID)
|
||||||
if a, ok := atts[uid]; ok {
|
if a, ok := atts[uid]; ok {
|
||||||
list[i].Attachments = a
|
list[i].Attachments = a
|
||||||
}
|
}
|
||||||
|
if t, ok := tagsByPost[uid]; ok {
|
||||||
|
list[i].Tags = t
|
||||||
|
} else {
|
||||||
|
list[i].Tags = []string{}
|
||||||
|
}
|
||||||
}
|
}
|
||||||
return list, nil
|
return list, nil
|
||||||
}
|
}
|
||||||
|
|||||||
Reference in New Issue
Block a user