Skip to content

Commit

Permalink
Refactor changes to comments everywhere.
Browse files Browse the repository at this point in the history
  • Loading branch information
knadh committed Jun 11, 2022
1 parent d78cd4f commit 0d61526
Show file tree
Hide file tree
Showing 9 changed files with 50 additions and 50 deletions.
8 changes: 4 additions & 4 deletions admin/pending.html
Original file line number Diff line number Diff line change
Expand Up @@ -26,10 +26,10 @@ <h3>
</h3>
<sup class="lang meta" x-text="config.languages[e.lang].name"></sup>

<template x-if="e.id in changes" x-data="{ c: changes[e.id]}">
<template x-if="e.id in comments" x-data="{ c: comments[e.id]}">
<div class="comments">
<div x-text="c.comments"></div>
<button class="button-outline button-small" @click.prevent="onClearComment(c.id)">Clear</button>
<button class="button-outline button-small" @click.prevent="onClearComments(c.id)">Clear</button>
</div>
</template>
</div>
Expand All @@ -48,10 +48,10 @@ <h3>
<span class="meta lang" x-text="config.languages[r.lang].name"></span>
<span class="meta types" x-text="r.relation.types"></span> <span x-text="r.content" class="content"></span>
</p>
<template x-if="`${e.id}-${r.id}` in changes" x-data="{ c: changes[`${e.id}-${r.id}`]}">
<template x-if="`${e.id}-${r.id}` in comments" x-data="{ c: comments[`${e.id}-${r.id}`]}">
<div class="comments">
<div x-text="c.comments"></div>
<button class="button-outline button-small" @click.prevent="onClearComment(c.id)">Clear</button>
<button class="button-outline button-small" @click.prevent="onClearComments(c.id)">Clear</button>
</div>
</template>
<div class="actions">
Expand Down
16 changes: 8 additions & 8 deletions admin/static/main.js
Original file line number Diff line number Diff line change
Expand Up @@ -159,8 +159,8 @@ function searchResultsComponent(typ) {
order: {},
hasRelReordered: {},

// from_id-to_id -> change{}
changes: {},
// from_id-to_id -> comments{}
comments: {},

onLoad() {
this.refresh();
Expand All @@ -173,7 +173,7 @@ function searchResultsComponent(typ) {
}

this.getPending();
this.getChanges();
this.getComments();
},

getPending() {
Expand All @@ -183,8 +183,8 @@ function searchResultsComponent(typ) {
})
},

getChanges() {
this.api('entries.search', '/entries/changes').then((data) => {
getComments() {
this.api('entries.search', '/entries/comments').then((data) => {
let out = {};

// Create a from_id-to_id lookup map that can be used in
Expand All @@ -197,7 +197,7 @@ function searchResultsComponent(typ) {
}
});

this.changes = out;
this.comments = out;
})
},

Expand All @@ -220,8 +220,8 @@ function searchResultsComponent(typ) {
}
},

onClearComment(id) {
this.api('entries.delete', `/entries/changes/${id}`, 'DELETE').then(() => this.refresh());
onClearComments(id) {
this.api('entries.delete', `/entries/comments/${id}`, 'DELETE').then(() => this.refresh());
},

onReorderRelation(entry, rel, n, d) {
Expand Down
12 changes: 6 additions & 6 deletions cmd/dictpress/admin_handlers.go
Original file line number Diff line number Diff line change
Expand Up @@ -241,12 +241,12 @@ func handleDeleteRelation(c echo.Context) error {
return c.JSON(http.StatusOK, okResp{true})
}

func handleGetChangeSubmissions(c echo.Context) error {
func handleGetComments(c echo.Context) error {
var (
app = c.Get("app").(*App)
)

out, err := app.data.GetChangeSubmissions()
out, err := app.data.GetComments()
if err != nil {
return echo.NewHTTPError(http.StatusInternalServerError,
fmt.Sprintf("error deleting relation: %v", err))
Expand All @@ -255,19 +255,19 @@ func handleGetChangeSubmissions(c echo.Context) error {
return c.JSON(http.StatusOK, okResp{out})
}

func handleDeleteChangeSubmission(c echo.Context) error {
func handleDeletecomments(c echo.Context) error {
var (
app = c.Get("app").(*App)
id, _ = strconv.Atoi(c.Param("changeID"))
id, _ = strconv.Atoi(c.Param("commentID"))
)

if id < 1 {
return echo.NewHTTPError(http.StatusBadRequest, "Invalid `id`.")
}

if err := app.data.DeleteChangeSubmission(id); err != nil {
if err := app.data.DeleteComments(id); err != nil {
return echo.NewHTTPError(http.StatusInternalServerError,
fmt.Sprintf("error deleting change: %v", err))
fmt.Sprintf("error deleting comments: %v", err))
}

return c.JSON(http.StatusOK, okResp{true})
Expand Down
6 changes: 3 additions & 3 deletions cmd/dictpress/init.go
Original file line number Diff line number Diff line change
Expand Up @@ -150,7 +150,7 @@ func initHTTPServer(app *App, ko *koanf.Koanf) *echo.Echo {
// Public user submission APIs.
if ko.Bool("app.enable_submissions") {
p.POST("/api/submissions/new", handleNewSubmission)
p.POST("/api/submissions/change", handleChangeSubmission)
p.POST("/api/submissions/comments", handleCommentSubmission)
p.GET("/submit", handleSubmissionPage)
p.POST("/submit", handleNewSubmission)
}
Expand All @@ -163,8 +163,8 @@ func initHTTPServer(app *App, ko *koanf.Koanf) *echo.Echo {

a.GET("/api/stats", handleGetStats)
a.GET("/api/entries/pending", handleGetPendingEntries)
a.GET("/api/entries/changes", handleGetChangeSubmissions)
a.DELETE("/api/entries/changes/:changeID", handleDeleteChangeSubmission)
a.GET("/api/entries/comments", handleGetComments)
a.DELETE("/api/entries/comments/:commentID", handleDeletecomments)
a.GET("/api/entries/:id", handleGetEntry)
a.GET("/api/entries/:id/parents", handleGetParentEntries)
a.POST("/api/entries", handleInsertEntry)
Expand Down
8 changes: 4 additions & 4 deletions cmd/dictpress/submission_handlers.go
Original file line number Diff line number Diff line change
Expand Up @@ -141,10 +141,10 @@ func handleNewSubmission(c echo.Context) error {
return nil
}

// handleChangeSubmission records a suggestion for change from the public in the changes table.
// handleCommentSubmission records a suggestion for change from the public in the changes table.
// These suggestions are reviewed in the admin and any change involves manually incorporating
// them to the linked entries.
func handleChangeSubmission(c echo.Context) error {
func handleCommentSubmission(c echo.Context) error {
app := c.Get("app").(*App)

var s changeSubmission
Expand All @@ -157,11 +157,11 @@ func handleChangeSubmission(c echo.Context) error {
return echo.NewHTTPError(http.StatusBadRequest, "Invalid `from_guid`.")
}

if err := app.data.InsertChangeSubmission(s.FromGUID, s.ToGUID, s.Comments); err != nil {
if err := app.data.InsertComments(s.FromGUID, s.ToGUID, s.Comments); err != nil {
app.logger.Printf("error inserting change submission: %v", err)
return echo.NewHTTPError(http.StatusInternalServerError,
fmt.Sprintf("Error saving submission.", err))
}

return nil
return c.JSON(http.StatusOK, okResp{true})
}
26 changes: 13 additions & 13 deletions internal/data/data.go
Original file line number Diff line number Diff line change
Expand Up @@ -66,9 +66,9 @@ type Queries struct {
GetPendingEntries *sqlx.Stmt `query:"get-pending-entries"`
InsertSubmissionEntry *sqlx.Stmt `query:"insert-submission-entry"`
InsertSubmissionRelation *sqlx.Stmt `query:"insert-submission-relation"`
InsertChangeSubmission *sqlx.Stmt `query:"insert-change"`
GetChanges *sqlx.Stmt `query:"get-changes"`
DeleteChangeSubmission *sqlx.Stmt `query:"delete-change"`
InsertComments *sqlx.Stmt `query:"insert-comments"`
GetComments *sqlx.Stmt `query:"get-comments"`
DeleteComments *sqlx.Stmt `query:"delete-comments"`
ApproveSubmission *sqlx.Stmt `query:"approve-submission"`
RejectSubmission *sqlx.Stmt `query:"reject-submission"`
}
Expand Down Expand Up @@ -317,26 +317,26 @@ func (s *Data) DeleteRelation(fromID, toID int) error {
return err
}

// InsertChangeSubmission inserts a change suggestion from the public.
func (d *Data) InsertChangeSubmission(fromGUID, toGUID, comments string) error {
_, err := d.queries.InsertChangeSubmission.Exec(fromGUID, toGUID, comments)
// InsertComments inserts a change suggestion from the public.
func (d *Data) InsertComments(fromGUID, toGUID, comments string) error {
_, err := d.queries.InsertComments.Exec(fromGUID, toGUID, comments)
return err
}

// GetChangeSubmissions retrieves change submissions.
func (d *Data) GetChangeSubmissions() ([]Change, error) {
var out []Change
// GetComments retrieves change submissions.
func (d *Data) GetComments() ([]Comments, error) {
var out []Comments

if err := d.queries.GetChanges.Select(&out); err != nil {
if err := d.queries.GetComments.Select(&out); err != nil {
return nil, err
}

return out, nil
}

// DeleteChangeSubmission deletes a change suggestion from the public.
func (d *Data) DeleteChangeSubmission(id int) error {
_, err := d.queries.DeleteChangeSubmission.Exec(id)
// DeleteComments deletes a change suggestion from the public.
func (d *Data) DeleteComments(id int) error {
_, err := d.queries.DeleteComments.Exec(id)
return err
}

Expand Down
2 changes: 1 addition & 1 deletion internal/data/models.go
Original file line number Diff line number Diff line change
Expand Up @@ -69,7 +69,7 @@ type Stats struct {
Languages map[string]int `json:"languages"`
}

type Change struct {
type Comments struct {
ID int `json:"id" db:"id"`
FromID int `json:"from_id" db:"from_id"`
ToID null.Int `json:"to_id" db:"to_id"`
Expand Down
16 changes: 8 additions & 8 deletions queries.sql
Original file line number Diff line number Diff line change
Expand Up @@ -68,7 +68,7 @@ ORDER BY relations.weight;
WITH ids AS (
SELECT DISTINCT from_id FROM relations WHERE status = 'pending'
UNION
SELECT DISTINCT from_id FROM changes
SELECT DISTINCT from_id FROM comments
)
SELECT COUNT(*) OVER () AS total, e.* FROM entries e
INNER JOIN ids ON (ids.from_id = e.id)
Expand Down Expand Up @@ -258,15 +258,15 @@ e2 AS (
)
DELETE FROM relations WHERE from_id = $1 AND status = 'pending';

-- name: insert-change
-- Insert change suggestions coming from the public.
-- name: insert-comments
-- Insert comments / suggestions coming from the public.
WITH f AS (SELECT id FROM entries WHERE $1::TEXT != '' AND guid = $1::UUID),
t AS (SELECT id FROM entries WHERE $2::TEXT != '' AND guid = $2::UUID)
INSERT INTO changes (from_id, to_id, comments)
INSERT INTO comments (from_id, to_id, comments)
VALUES((SELECT id FROM f), (SELECT id FROM t), $3);

-- name: get-changes
SELECT * FROM changes;
-- name: get-comments
SELECT * FROM comments;

-- name: delete-change
DELETE FROM changes WHERE id = $1;
-- name: delete-comments
DELETE FROM comments WHERE id = $1;
6 changes: 3 additions & 3 deletions schema.sql
Original file line number Diff line number Diff line change
Expand Up @@ -63,11 +63,11 @@ CREATE TABLE relations (
);
DROP INDEX IF EXISTS idx_relations; CREATE UNIQUE INDEX idx_relations ON relations(from_id, to_id);

-- changes
-- comments
-- This table holds change suggestions submitted by the public. It can either be on an entry
-- or on a relation.
DROP TABLE IF EXISTS changes CASCADE;
CREATE TABLE changes (
DROP TABLE IF EXISTS comments CASCADE;
CREATE TABLE comments (
id SERIAL PRIMARY KEY,
from_id INTEGER NOT NULL REFERENCES entries(id) ON DELETE CASCADE ON UPDATE CASCADE,
to_id INTEGER NULL REFERENCES entries(id) ON DELETE CASCADE ON UPDATE CASCADE,
Expand Down

0 comments on commit 0d61526

Please sign in to comment.