Skip to content

Commit

Permalink
refactor: adjust structures
Browse files Browse the repository at this point in the history
  • Loading branch information
masl committed Nov 2, 2023
1 parent d779e64 commit 77f1c48
Show file tree
Hide file tree
Showing 4 changed files with 20 additions and 22 deletions.
8 changes: 8 additions & 0 deletions storage/errors.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
package storage

import "errors"

var (
ErrAlreadyExists = errors.New("cloud already exists")
ErrNotFound = errors.New("cloud not found")
)
14 changes: 4 additions & 10 deletions storage/inmemory/store.go
Original file line number Diff line number Diff line change
@@ -1,15 +1,9 @@
package inmemory

import (
"errors"

"github.com/google/uuid"
"github.com/masl/answertag/cloud"
)

var (
ErrAlreadyExists = errors.New("cloud already exists")
ErrNotFound = errors.New("cloud not found")
"github.com/masl/answertag/storage"
)

// Store represents an in-memory storage.
Expand All @@ -27,7 +21,7 @@ func New() *Store {
// Create creates a new tag-cloud.
func (s *Store) Create(c *cloud.Cloud) error {
if _, ok := s.clouds[c.ID.String()]; ok {
return ErrAlreadyExists
return storage.ErrAlreadyExists
}

s.clouds[c.ID.String()] = c
Expand All @@ -38,7 +32,7 @@ func (s *Store) Create(c *cloud.Cloud) error {
// Update updates an existing tag-cloud.
func (s *Store) Update(c *cloud.Cloud) error {
if _, ok := s.clouds[c.ID.String()]; !ok {
return ErrNotFound
return storage.ErrNotFound
}

s.clouds[c.ID.String()] = c
Expand All @@ -54,5 +48,5 @@ func (s *Store) ReadByID(id string) (*cloud.Cloud, error) {
return cloud, nil
}

return nil, ErrNotFound
return nil, storage.ErrNotFound
}
4 changes: 2 additions & 2 deletions templates/cloud.html
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@
<meta charset="UTF-8">
<meta name="viewport" content="width=device-width, initial-scale=1.0">
<link href="/static/output.css" rel="stylesheet">
<title>{{ .CloudID }}</title>
<title>{{ .ID }}</title>
<script src="/static/htmx.min.js"></script>
<script src="/static/ext/json-enc.js"></script>
<script src="/static/ext/ws.js"></script>
Expand All @@ -18,7 +18,7 @@
<input
class="dark:text-font bg-white hover:bg-font dark:bg-primary dark:hover:bg-seconary border-font dark:border-tertiary border px-2 py-1"
type="text" name="tag" placeholder="Enter your name" required>
<input type="hidden" name="cloudId" value="{{ .CloudID }}">
<input type="hidden" name="cloudId" value="{{ .ID }}">
<button
class="dark:text-font bg-white hover:bg-font dark:bg-primary dark:hover:bg-seconary border-font dark:border-tertiary border px-2 py-1"
type="submit" hx-target="#tags">
Expand Down
16 changes: 6 additions & 10 deletions web/c/handle.go
Original file line number Diff line number Diff line change
Expand Up @@ -6,26 +6,22 @@ import (
"text/template"

"github.com/julienschmidt/httprouter"
"github.com/masl/answertag/cloud"
"github.com/masl/answertag/storage"
)

func Handle(html *template.Template, store storage.Store) httprouter.Handle {
return func(w http.ResponseWriter, r *http.Request, ps httprouter.Params) {
cld, err := store.ReadByID(ps.ByName("id"))
data, err := store.ReadByID(ps.ByName("id"))
if err != nil {
if err == storage.ErrNotFound {
http.NotFound(w, r)
return
}

slog.Error("error reading cloud", "error", err)
http.Error(w, err.Error(), http.StatusInternalServerError)
}

data := struct {
CloudID string
Tags []*cloud.Tag
}{
CloudID: ps.ByName("id"),
Tags: cld.Tags,
}

err = html.ExecuteTemplate(w, "cloud.html", data)
if err != nil {
slog.Error("error executing template", "error", err)
Expand Down

0 comments on commit 77f1c48

Please sign in to comment.