Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Scaffold remaining actions and test protocol #203

Merged
merged 9 commits into from
Jul 16, 2022
55 changes: 50 additions & 5 deletions example/basic/controller/controller.go
Original file line number Diff line number Diff line change
@@ -1,18 +1,63 @@
package controller

import (
context "context"
)

// Controller for posts
type Controller struct {
}

// Post struct
type Post struct {
HTML string `json:"html"`
ID int `json:"id"`
}

// Index of posts
// GET
func (c *Controller) Index(ctx context.Context) (posts []*Post, err error) {
return []*Post{}, nil
}

func (c *Controller) Index() string {
return "hello world"
// New returns a view for creating a new post
// GET new
func (c *Controller) New(ctx context.Context) {
}

func (c *Controller) Show(id string) (*Post, error) {
// Create post
// POST
func (c *Controller) Create(ctx context.Context) (post *Post, err error) {
return &Post{
HTML: "<b>hello " + id + "</b></script><script>console.log('log', '!', '!')</script>",
ID: 0,
}, nil
}

// Show post
// GET :id
func (c *Controller) Show(ctx context.Context, id int) (post *Post, err error) {
return &Post{
ID: id,
}, nil
}

// Edit returns a view for editing a post
// GET :id/edit
func (c *Controller) Edit(ctx context.Context, id int) (post *Post, err error) {
return &Post{
ID: id,
}, nil
}

// Update post
// PATCH :id
func (c *Controller) Update(ctx context.Context, id int) (post *Post, err error) {
return &Post{
ID: id,
}, nil
}

// Delete post
// DELETE :id
func (c *Controller) Delete(ctx context.Context, id int) error {
return nil
}
17 changes: 17 additions & 0 deletions example/basic/view/edit.svelte
Original file line number Diff line number Diff line change
@@ -0,0 +1,17 @@
<script>
export let post = {}
</script>

<h1>Edit Post</h1>

<form method="post" action={`/${post.id || 0}`}>
<input type="hidden" name="_method" value="patch">
<!-- Add input fields here -->
<input type="submit" value="Update Post" />
</form>

<br />

<a href={`/`}>Back</a>
<span> | </span>
<a href={`/${post.id || 0}`}>Show Post</a>
37 changes: 29 additions & 8 deletions example/basic/view/index.svelte
Original file line number Diff line number Diff line change
@@ -1,15 +1,36 @@
<script>
export let _string = ""
export let posts = []
</script>

<h1>{_string}</h1>
<h1>Post Index</h1>

<table border="1" cellpadding="10">
{#if posts.length > 0}
<thead>
{#each Object.keys(posts[0]) as key}
<th>{key}</th>
{/each}
</thead>
{/if}
{#each posts as post}
<tr>
{#each Object.keys(post) as key}
{#if key.toLowerCase() === "id"}
<td><a href={`/${post.id || 0}`}>{post[key]}</a></td>
{:else}
<td>{post[key]}</td>
{/if}
{/each}
</tr>
{/each}
</table>

<br />

<a href={`/new`}>New Post</a>

<style>
h1 {
margin: 0;
background: whitesmoke;
border-bottom: 2px dashed red;
padding: 20px;
color: red;
table {
border-collapse: collapse;
}
</style>
10 changes: 10 additions & 0 deletions example/basic/view/new.svelte
Original file line number Diff line number Diff line change
@@ -0,0 +1,10 @@
<h1>New Post</h1>

<form method="post" action={`/`}>
<!-- Add input fields here -->
<input type="submit" value="Create Post" />
</form>

<br />

<a href={`/`}>Back</a>
27 changes: 26 additions & 1 deletion example/basic/view/show.svelte
Original file line number Diff line number Diff line change
Expand Up @@ -2,4 +2,29 @@
export let post = {}
</script>

<h1>{post.html}</h1>
<h1>Show Post</h1>

<table border="1" cellpadding="10">
<thead>
{#each Object.keys(post) as key}
<th>{key}</th>
{/each}
</thead>
<tr>
{#each Object.keys(post) as key}
<td>{post[key]}</td>
{/each}
</tr>
</table>

<br />

<a href={`/`}>Back</a>
<span> | </span>
<a href={`/${post.id || 0}/edit`}>Edit</a>

<style>
table {
border-collapse: collapse;
}
</style>
Loading