diff --git a/cmd/api/url.go b/cmd/api/url.go index 0b03c5c..72d9e3c 100644 --- a/cmd/api/url.go +++ b/cmd/api/url.go @@ -37,7 +37,6 @@ func (app *application) createUrlFormHandler(c echo.Context) error { func (app *application) createUrlHandlerPost(c echo.Context) error { original := c.FormValue("original") shortCode := c.FormValue("short_code") - qrCodeStr := c.FormValue("qr_code") user, err := app.userFromContext(c) if err != nil { @@ -45,26 +44,27 @@ func (app *application) createUrlHandlerPost(c echo.Context) error { return c.Render(http.StatusBadRequest, "login.tmpl.html", app.newTemplateData(c)) } - var qrCodeURL string - if qrCodeStr == "on" { - qrCodeURL, err = app.createQRCode(original) - if err != nil { - app.sessionManager.Put(c.Request().Context(), "flash_error", "Failed to create QR Code.") - } - } - urlReq := &model.UrlCreateRequest{ Original: original, ShortCode: shortCode, UserID: user.ID, - QRCodeURL: qrCodeURL, } - _, err = app.models.Urls.Create(urlReq) + url, err := app.models.Urls.Create(urlReq) if err != nil { app.sessionManager.Put(c.Request().Context(), "flash_error", "Internal Server Error. Please try again later.") return c.Render(http.StatusBadRequest, "create_url.tmpl.html", app.newTemplateData(c)) } + qrCodeURL, err := app.createQRCode(genFullUrl(fmt.Sprintf(c.Scheme()+"://"+c.Request().Host), url.ShortUrl)) + if err != nil { + app.sessionManager.Put(c.Request().Context(), "flash_error", "Failed to create QR Code.") + } + + err = app.models.Urls.SetQRCodeURL(&url, qrCodeURL) + if err != nil { + app.sessionManager.Put(c.Request().Context(), "flash_error", "Failed to set QR Code URL.") + } + app.sessionManager.Put(c.Request().Context(), "flash", "Url created successfully!") data := app.newTemplateData(c) data.User = user diff --git a/internal/model/url.go b/internal/model/url.go index 4b03f26..dab52a7 100644 --- a/internal/model/url.go +++ b/internal/model/url.go @@ -30,7 +30,6 @@ type UrlCreateRequest struct { Original string `json:"original" validate:"required,url"` ShortCode string `json:"short_code,omitempty" validate:"alphanum,min=3,max=11"` UserID uuid.UUID `json:"user_id"` - QRCodeURL string `json:"qr_code_url,omitempty"` } type UrlResponse struct { @@ -80,7 +79,6 @@ func (u *UrlModel) Create(urlReq *UrlCreateRequest) (Url, error) { url.Original = urlReq.Original url.UserID = urlReq.UserID - url.QRCodeURL = urlReq.QRCodeURL result := u.DB.Create(url) if result.Error != nil { @@ -90,6 +88,16 @@ func (u *UrlModel) Create(urlReq *UrlCreateRequest) (Url, error) { return *url, nil } +// SetQRCodeURL sets the QRCodeURL for a given url +func (u *UrlModel) SetQRCodeURL(url *Url, qrCodeURL string) error { + result := u.DB.Model(url).Update("qr_code_url", qrCodeURL) + if result.Error != nil { + return result.Error + } + + return nil +} + func (u *UrlModel) GetRedirect(shortUrl string) (Url, error) { url := new(Url) result := u.DB.Where("short_url = ?", shortUrl).First(&url) diff --git a/ui/html/pages/create_url.tmpl.html b/ui/html/pages/create_url.tmpl.html index 696b3cf..e6ffb31 100644 --- a/ui/html/pages/create_url.tmpl.html +++ b/ui/html/pages/create_url.tmpl.html @@ -16,12 +16,6 @@

Create URL

- -
- - -