1
0
mirror of https://github.com/taigrr/shorturl synced 2025-01-18 04:03:16 -08:00

Support edit ID

This commit is contained in:
srxr 2021-03-27 14:34:21 +00:00
parent f65255f6eb
commit 31c239e486
No known key found for this signature in database
GPG Key ID: 5F5DB27AF89FA243
3 changed files with 18 additions and 7 deletions

View File

@ -56,10 +56,20 @@ func (u *URL) SetName(name string) error {
return db.Save(&u)
}
func (u *URL) update(target string) error {
u.URL = target
func (u *URL) update(id, target string) error {
url, err := parse(target)
if err != nil {
return err
}
if err := del(u.ID); err != nil {
return err
}
u.ID = id
u.URL = url.String()
u.UpdatedAt = time.Now()
return db.Update(u)
return db.Save(u)
}
func del(id string) error {

View File

@ -256,6 +256,7 @@ func (s *Server) UpdateHandler() httprouter.Handle {
id := p.ByName("id")
target := r.FormValue("url")
newID := r.FormValue("id")
if id == "" || target == "" {
http.Error(w, "Bad Request", http.StatusBadRequest)
return
@ -271,14 +272,14 @@ func (s *Server) UpdateHandler() httprouter.Handle {
return
}
err = u.update(target)
err = u.update(newID, target)
if err != nil {
log.Printf("error updating %s error: %v", id, err)
http.Error(w, "Internal Error", http.StatusInternalServerError)
return
}
redirectURL := fmt.Sprintf("/u/%s", u.ID)
redirectURL := fmt.Sprintf("/u/%s", newID)
http.Redirect(w, r, redirectURL, http.StatusFound)
}

View File

@ -5,7 +5,7 @@
<p>Your short url is: <a href="/r/{{.ID}}">{{.ID}}</a></p>
<form class="mt-10" action="/e/{{.ID}}" method="POST">
<div class="form-group input-group">
<input class="form-input" type="hidden" name="id" value="{{.ID}}" />
<input class="form-input column col-1" type="text" name="id" value="{{.ID}}" />
<input class="form-input" type="text" name="url" value="{{.URL}}" placeholder="Enter long url here..." />
<button class="btn btn-primary" type="submit">Edit</button>
<a class="btn btn-action" href="/u/{{.ID}}">