From 45913bdcdc861bd2ddef02c69625536efef1850a Mon Sep 17 00:00:00 2001 From: danoloan10 Date: Sat, 22 Jul 2023 23:06:33 +0200 Subject: [PATCH] =?UTF-8?q?Funcionalidad=20de=20adici=C3=B3n=20sin=20javas?= =?UTF-8?q?cript?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- main.go | 27 +++++++++++---------------- storage/entry.go | 10 +++++----- storage/jstime.go | 6 +++++- templates/admin.html | 2 +- 4 files changed, 22 insertions(+), 23 deletions(-) diff --git a/main.go b/main.go index fd25295..860cdb3 100644 --- a/main.go +++ b/main.go @@ -52,24 +52,19 @@ func (template Template) TemplateController(c echo.Context) (err error) { return } -func AddController(c echo.Context) error { - var res Response - var status int - var err error - +func AddController(c echo.Context) (err error) { entry := new(storage.Entry) - if err = c.Bind(entry); err != nil { - res = Response{false, err.Error()} - status = http.StatusBadRequest - } else if err = list.AddEntry(entry); err != nil { - res = Response{false, "Failed to write into list"} - status = http.StatusInternalServerError - } else { - res = Response{true, "OK"} - status = http.StatusOK + err = c.Bind(entry) + if err == nil { + if c.FormValue("date") == "" { + entry.Date = storage.JSTime(time.Now()) + } + err = list.AddEntry(entry) } - - return c.JSON(status, res) + if err == nil { + err = c.Redirect(http.StatusFound, "..") + } + return } func DelController(c echo.Context) error { diff --git a/storage/entry.go b/storage/entry.go index f410039..7519761 100644 --- a/storage/entry.go +++ b/storage/entry.go @@ -5,11 +5,11 @@ import "fmt" type ( Entry struct { Artist string `json:"artist" form:"artist"` - Linkto string `json:"linkto,omitempty" form:"linkto,omitempty"` - Track string `json:"track,omitempty" form:"track,omitempty"` - Album string `json:"album,omitempty" form:"album,omitempty"` - Cover string `json:"cover,omitempty" form:"cover,omitempty"` - Date JSTime `json:"date" form:"date"` + Linkto string `json:"linkto" form:"linkto,omitempty"` + Track string `json:"track" form:"track,omitempty"` + Album string `json:"album" form:"album,omitempty"` + Cover string `json:"cover" form:"cover,omitempty"` + Date JSTime `json:"date" form:"date,omitempty"` } ) diff --git a/storage/jstime.go b/storage/jstime.go index 507901f..8d1f0ff 100644 --- a/storage/jstime.go +++ b/storage/jstime.go @@ -17,7 +17,11 @@ const ( ) func (ct JSTime) UnmarshalJSON(b []byte) (err error) { - s := strings.Trim(string(b), `"`) + return ct.UnmarshalParam(string(b)) +} + +func (ct JSTime) UnmarshalParam(param string) (err error) { + s := strings.Trim(param, `"`) loc, _ := time.LoadLocation("Europe/Madrid") nt, err := time.ParseInLocation(jsTimeLite, s, loc) ct = JSTime(nt) diff --git a/templates/admin.html b/templates/admin.html index f082508..8ea50be 100644 --- a/templates/admin.html +++ b/templates/admin.html @@ -16,7 +16,7 @@ Añadir canción -
+