Skip to content

Commit 16cf246

Browse files
committed
Add new /api/dictionary/entries/:guid public API.
1 parent d89b2e5 commit 16cf246

File tree

5 files changed

+47
-4
lines changed

5 files changed

+47
-4
lines changed

cmd/dictpress/admin.go

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -128,7 +128,7 @@ func handleGetEntry(c echo.Context) error {
128128
id, _ = strconv.Atoi(c.Param("id"))
129129
)
130130

131-
e, err := app.data.GetEntry(id)
131+
e, err := app.data.GetEntry(id, "")
132132
if err != nil {
133133
if err == sql.ErrNoRows {
134134
return echo.NewHTTPError(http.StatusBadRequest, "entry not found")

cmd/dictpress/handlers.go

Lines changed: 37 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,7 @@ package main
22

33
import (
44
"bytes"
5+
"database/sql"
56
"errors"
67
"fmt"
78
"net/http"
@@ -73,6 +74,42 @@ func handleSearch(c echo.Context) error {
7374
return c.JSON(http.StatusOK, okResp{out})
7475
}
7576

77+
// handleGetEntryPublic returns an entry by its guid.
78+
func handleGetEntryPublic(c echo.Context) error {
79+
var (
80+
app = c.Get("app").(*App)
81+
guid = c.Param("guid")
82+
)
83+
84+
e, err := app.data.GetEntry(0, guid)
85+
if err != nil {
86+
if err == sql.ErrNoRows {
87+
return echo.NewHTTPError(http.StatusBadRequest, "entry not found")
88+
}
89+
90+
return echo.NewHTTPError(http.StatusInternalServerError, err.Error())
91+
}
92+
93+
e.Relations = make([]data.Entry, 0)
94+
95+
out := []data.Entry{e}
96+
if err := app.data.SearchAndLoadRelations(out, data.Query{}); err != nil {
97+
app.lo.Printf("error loading relations: %v", err)
98+
return echo.NewHTTPError(http.StatusInternalServerError, "error loading relations")
99+
}
100+
101+
for i := range out {
102+
out[i].ID = 0
103+
104+
for j := range out[i].Relations {
105+
out[i].Relations[j].ID = 0
106+
out[i].Relations[j].Relation.ID = 0
107+
}
108+
}
109+
110+
return c.JSON(http.StatusOK, okResp{out[0]})
111+
}
112+
76113
// handleServeBundle serves concatenated JS or CSS files based on query parameters
77114
func handleServeBundle(c echo.Context, bundleType string, staticDir string) error {
78115
files := c.QueryParams()["f"]

cmd/dictpress/init.go

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -190,6 +190,7 @@ func initHTTPServer(app *App, ko *koanf.Koanf) *echo.Echo {
190190
// Public APIs.
191191
p.GET("/api/config", handleGetConfig)
192192
p.GET("/api/dictionary/:fromLang/:toLang/:q", handleSearch)
193+
p.GET("/api/dictionary/entries/:guid", handleGetEntryPublic)
193194

194195
// Public user submission APIs.
195196
if ko.Bool("app.enable_submissions") {

internal/data/data.go

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -244,9 +244,9 @@ func (d *Data) GetGlossaryWords(lang, initial string, offset, limit int) ([]Glos
244244
}
245245

246246
// GetEntry returns an entry by its id.
247-
func (d *Data) GetEntry(id int) (Entry, error) {
247+
func (d *Data) GetEntry(id int, guid string) (Entry, error) {
248248
var out Entry
249-
if err := d.queries.GetEntry.Get(&out, id); err != nil {
249+
if err := d.queries.GetEntry.Get(&out, id, guid); err != nil {
250250
return out, err
251251
}
252252

queries.sql

Lines changed: 6 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -107,7 +107,12 @@ r AS (
107107
UPDATE relations WHERE from_id = $1;
108108

109109
-- name: get-entry
110-
SELECT * FROM entries WHERE id=$1;
110+
SELECT * FROM entries WHERE
111+
CASE
112+
WHEN $1 > 0 THEN id = $1
113+
WHEN $2 != '' THEN guid = $2::UUID
114+
ELSE FALSE
115+
END;
111116

112117
-- name: get-parent-relations
113118
SELECT entries.*, relations.id as relation_id FROM entries

0 commit comments

Comments
 (0)