diff --git a/cmd/main.go b/cmd/main.go index 1d191f6..659b629 100644 --- a/cmd/main.go +++ b/cmd/main.go @@ -26,50 +26,41 @@ func newTemplate() *Templates { var id = 0 -type Contact struct { - Name string - Email string - Id int +type Entry struct { + Name string + Message string + Id int } -func newContact(name, email string) Contact { +func newEntry(name, message string) Entry { id++ - return Contact{ - Name: name, - Email: email, - Id: id, + return Entry{ + Name: name, + Message: message, + Id: id, } } -type Contacts = []Contact +type Entries = []Entry type Data struct { - Contacts Contacts + Entries Entries } func (d *Data) indexOf(id int) int { - for i, contact := range d.Contacts { - if contact.Id == id { + for i, entry := range d.Entries { + if entry.Id == id { return i } } return -1 } -func (d *Data) hasEmail(email string) bool { - for _, contact := range d.Contacts { - if contact.Email == email { - return true - } - } - return false -} - func newData() Data { return Data{ - Contacts: []Contact{ - newContact("John Smith", "johndoe@me.com"), - newContact("Jane Doe", "janedoe@me.com"), + Entries: []Entry{ + newEntry("John Smith", "Hello world"), + newEntry("Jane Doe", "This is a great guestbook"), }, } } @@ -113,27 +104,18 @@ func main() { return c.Render(200, "index", page) }) - e.POST("/contacts", func(c echo.Context) error { + e.POST("/guestbook", func(c echo.Context) error { name := c.FormValue("name") - email := c.FormValue("email") + message := c.FormValue("message") - if page.Data.hasEmail(email) { - formData := newFormData() - formData.Values["name"] = name - formData.Values["email"] = email - formData.Errors["email"] = "Email already exists" - - return c.Render(422, "form", formData) - } - - contact := newContact(name, email) - page.Data.Contacts = append(page.Data.Contacts, contact) + entry := newEntry(name, message) + page.Data.Entries = append(page.Data.Entries, entry) c.Render(200, "form", newFormData()) - return c.Render(200, "oob-contact", contact) + return c.Render(200, "oob-entry", entry) }) - e.DELETE("/contacts/:id", func(c echo.Context) error { + e.DELETE("/guestbook/:id", func(c echo.Context) error { // TODO: remove this to make server faster LOL time.Sleep(1 * time.Second) @@ -146,10 +128,10 @@ func main() { i := page.Data.indexOf(id) if i == -1 { - return c.String(404, "Contact not found") + return c.String(404, "Entry not found") } - page.Data.Contacts = append(page.Data.Contacts[:i], page.Data.Contacts[i+1:]...) + page.Data.Entries = append(page.Data.Entries[:i], page.Data.Entries[i+1:]...) return c.NoContent(200) }) diff --git a/tmp/main b/tmp/main index 4d6fed3..c093627 100755 Binary files a/tmp/main and b/tmp/main differ diff --git a/views/index.html b/views/index.html index 4badfec..e9b677c 100644 --- a/views/index.html +++ b/views/index.html @@ -12,55 +12,41 @@ {{ template "form" .Form }}
{{ template "display" .Data }} - - {{ end }} {{ block "form" . }} -
- name: + Name: - email: + Message: - {{ if .Errors.email }} - {{ .Errors.email }} + {{ if .Errors.message }} + {{ .Errors.message }} {{ end }} - +
{{ end }} {{ block "display" . }} -
- {{ range .Contacts }} - {{ template "contact" . }} +
+ {{ range .Entries }} + {{ template "entry" . }} {{ end }}
{{ end }} -{{ block "contact" . }} -
+{{ block "entry" . }} +
@@ -68,16 +54,17 @@
Name: {{ .Name }} - Email: {{ .Email }} + Message: {{ .Message }} -
+
loading
{{ end }} -{{ block "oob-contact" . }} -
- {{ template "contact" . }} +{{ block "oob-entry" . }} +
+ {{ template "entry" . }}
{{ end }} +