Skip to content

Commit

Permalink
Merge pull request mrdoob#63 from jfontan/feature/find-siblings
Browse files Browse the repository at this point in the history
Find children
  • Loading branch information
jfontan authored Jul 29, 2021
2 parents 6d8314a + 78b3dce commit 8382f9c
Show file tree
Hide file tree
Showing 5 changed files with 89 additions and 9 deletions.
2 changes: 1 addition & 1 deletion README.md
Original file line number Diff line number Diff line change
Expand Up @@ -26,7 +26,7 @@ $ tar xvf glslsandbox-data.tar.gz
$ go build ./server/cmd/glslsandbox
```

* Alternatively you can download and uncompress the binary in the repository directory from https://github.com/jfontan/glsl-sandbox/releases/latest
* Alternatively you can download and uncompress the binary in the repository directory from https://github.com/mrdoob/glsl-sandbox/releases/latest

* Run server:

Expand Down
10 changes: 8 additions & 2 deletions server/assets/gallery.html
Original file line number Diff line number Diff line change
Expand Up @@ -87,6 +87,11 @@ <h1><a href="/">GLSL Sandbox</a></h1>
<div id="gallery">

{{ if .Admin }}
<form action="/admin" method="GET">
<label style="color:#009DE9" for="parent">Effect ID</label>
<input type="text" id="parent" name="parent">
<input type="submit" value="Submit">
</form>
<form action="/admin" method="POST">
<input type="hidden" id="page" name="page" value="{{ .Page }}">
{{ end }}
Expand All @@ -99,6 +104,7 @@ <h1><a href="/">GLSL Sandbox</a></h1>
{{ if $admin }}
{{ $name := checkboxID .ID }}
<div>
<a style="color:#009DE9;" href="/admin?parent={{ .ID }}">Children</a>
<label style="color:#009DE9" for="{{ $name }}">Hidden</label>
<input type="checkbox" id="{{ $name }}" name="{{ $name }}" {{ checked .Hidden }}>
<input type="hidden" name="effects" value="{{ .ID }}">
Expand All @@ -116,7 +122,7 @@ <h1><a href="/">GLSL Sandbox</a></h1>

<div id="paginate">
{{ if .IsPrevious }}
<a href='{{ .URL }}?page={{ .PreviousPage }}'>Previous page</a>
<a href='{{ .PreviousPage }}'>Previous page</a>

{{ if .IsNext }}
&nbsp;&nbsp;
Expand All @@ -125,7 +131,7 @@ <h1><a href="/">GLSL Sandbox</a></h1>
{{ end }}

{{ if .IsNext }}
<a href='{{ .URL }}?page={{ .NextPage }}'>Next page</a>
<a href='{{ .NextPage }}'>Next page</a>
{{ end }}
</div>

Expand Down
32 changes: 27 additions & 5 deletions server/server.go
Original file line number Diff line number Diff line change
Expand Up @@ -214,11 +214,11 @@ type galleryData struct {
// IsPrevious is true if there is a previous page.
IsPrevious bool
// PreviousPage is the previous page number.
PreviousPage int
PreviousPage string
// IsNext is true if there is a next page.
IsNext bool
// NextPage is the next page number.
NextPage int
NextPage string
// Admin is true when accessing "/admin" path.
Admin bool
}
Expand All @@ -233,7 +233,20 @@ func (s *Server) indexRender(c echo.Context, admin bool) error {
page = 0
}

p, err := s.effects.Page(page, perPage, admin)
parent := -1
if c.QueryParam("parent") != "" {
parent, err = strconv.Atoi(c.QueryParam("parent"))
if err != nil {
parent = -1
}
}

var p []store.Effect
if parent > 0 {
p, err = s.effects.PageSiblings(page, perPage, parent)
} else {
p, err = s.effects.Page(page, perPage, admin)
}
if err != nil {
return c.String(http.StatusInternalServerError, "error")
}
Expand All @@ -252,14 +265,23 @@ func (s *Server) indexRender(c echo.Context, admin bool) error {
if admin {
url = "/admin"
}

nextPage := fmt.Sprintf("%s?page=%d", url, page+1)
previousPage := fmt.Sprintf("%s?page=%d", url, page-1)

if parent > 0 {
nextPage = fmt.Sprintf("%s&parent=%d", nextPage, parent)
previousPage = fmt.Sprintf("%s&parent=%d", previousPage, parent)
}

d := galleryData{
Effects: effects,
URL: url,
Page: page,
IsNext: len(effects) == perPage,
NextPage: page + 1,
NextPage: nextPage,
IsPrevious: page > 0,
PreviousPage: page - 1,
PreviousPage: previousPage,
Admin: admin,
}

Expand Down
20 changes: 19 additions & 1 deletion server/store/effects.go
Original file line number Diff line number Diff line change
Expand Up @@ -192,6 +192,14 @@ SELECT * FROM effects
LIMIT ? OFFSET ?
`

sqlSelectEffectsSiblings = `
SELECT * FROM effects
WHERE id = ? OR
parent = ?
ORDER BY modified_at DESC
LIMIT ? OFFSET ?
`

sqlSelectVersions = `
SELECT * FROM versions
WHERE effect = ?
Expand Down Expand Up @@ -335,7 +343,17 @@ func (s *Effects) Page(num int, size int, hidden bool) ([]Effect, error) {
query = sqlSelectEffectsAll
}

iter, err := s.db.Queryx(query, size, num*size)
return s.page(query, []interface{}{size, num * size})
}

func (s *Effects) PageSiblings(num int, size int, parent int) ([]Effect, error) {
query := sqlSelectEffectsSiblings

return s.page(query, []interface{}{parent, parent, size, num * size})
}

func (s *Effects) page(query string, qargs []interface{}) ([]Effect, error) {
iter, err := s.db.Queryx(query, qargs...)
if err != nil {
return nil, fmt.Errorf("could not get effects: %w", err)
}
Expand Down
34 changes: 34 additions & 0 deletions server/store/effects_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,7 @@ var tests = []helper{
{"hidden", testHidden},
{"add version", testAddVersion},
{"hide", testHide},
{"siblings", testSiblings},
}

func TestEffects(t *testing.T) {
Expand Down Expand Up @@ -148,3 +149,36 @@ func testHide(t *testing.T, s *Effects) {
require.Error(t, err)
require.Equal(t, ErrNotFound, err)
}

func testSiblings(t *testing.T, s *Effects) {
pid, err := s.Add(-1, -1, "user", "parent")
require.NoError(t, err)

expected := []int{pid}
for i := 0; i < 10; i++ {
id, err := s.Add(pid, 0, "user", "child")
require.NoError(t, err)
expected = append(expected, id)
}

for i := 0; i < 10; i++ {
_, err = s.Add(-1, -1, "user", "no")
require.NoError(t, err)
}

effects, err := s.PageSiblings(0, 50, pid)
require.NoError(t, err)

var ids []int
for _, e := range effects {
require.Len(t, e.Versions, 1)
if e.ID == pid {
require.Equal(t, "parent", e.Versions[0].Code)
} else {
require.Equal(t, "child", e.Versions[0].Code)
}
ids = append(ids, e.ID)
}

require.ElementsMatch(t, expected, ids)
}

0 comments on commit 8382f9c

Please sign in to comment.