mirror of
https://github.com/taigrr/shorturl
synced 2025-01-18 04:03:16 -08:00
Support edit ID
This commit is contained in:
parent
f65255f6eb
commit
31c239e486
16
models.go
16
models.go
@ -56,10 +56,20 @@ func (u *URL) SetName(name string) error {
|
|||||||
return db.Save(&u)
|
return db.Save(&u)
|
||||||
}
|
}
|
||||||
|
|
||||||
func (u *URL) update(target string) error {
|
func (u *URL) update(id, target string) error {
|
||||||
u.URL = target
|
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()
|
u.UpdatedAt = time.Now()
|
||||||
return db.Update(u)
|
return db.Save(u)
|
||||||
}
|
}
|
||||||
|
|
||||||
func del(id string) error {
|
func del(id string) error {
|
||||||
|
@ -256,6 +256,7 @@ func (s *Server) UpdateHandler() httprouter.Handle {
|
|||||||
|
|
||||||
id := p.ByName("id")
|
id := p.ByName("id")
|
||||||
target := r.FormValue("url")
|
target := r.FormValue("url")
|
||||||
|
newID := r.FormValue("id")
|
||||||
if id == "" || target == "" {
|
if id == "" || target == "" {
|
||||||
http.Error(w, "Bad Request", http.StatusBadRequest)
|
http.Error(w, "Bad Request", http.StatusBadRequest)
|
||||||
return
|
return
|
||||||
@ -271,14 +272,14 @@ func (s *Server) UpdateHandler() httprouter.Handle {
|
|||||||
return
|
return
|
||||||
}
|
}
|
||||||
|
|
||||||
err = u.update(target)
|
err = u.update(newID, target)
|
||||||
if err != nil {
|
if err != nil {
|
||||||
log.Printf("error updating %s error: %v", id, err)
|
log.Printf("error updating %s error: %v", id, err)
|
||||||
http.Error(w, "Internal Error", http.StatusInternalServerError)
|
http.Error(w, "Internal Error", http.StatusInternalServerError)
|
||||||
return
|
return
|
||||||
}
|
}
|
||||||
|
|
||||||
redirectURL := fmt.Sprintf("/u/%s", u.ID)
|
redirectURL := fmt.Sprintf("/u/%s", newID)
|
||||||
|
|
||||||
http.Redirect(w, r, redirectURL, http.StatusFound)
|
http.Redirect(w, r, redirectURL, http.StatusFound)
|
||||||
}
|
}
|
||||||
|
@ -5,7 +5,7 @@
|
|||||||
<p>Your short url is: <a href="/r/{{.ID}}">{{.ID}}</a></p>
|
<p>Your short url is: <a href="/r/{{.ID}}">{{.ID}}</a></p>
|
||||||
<form class="mt-10" action="/e/{{.ID}}" method="POST">
|
<form class="mt-10" action="/e/{{.ID}}" method="POST">
|
||||||
<div class="form-group input-group">
|
<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..." />
|
<input class="form-input" type="text" name="url" value="{{.URL}}" placeholder="Enter long url here..." />
|
||||||
<button class="btn btn-primary" type="submit">Edit</button>
|
<button class="btn btn-primary" type="submit">Edit</button>
|
||||||
<a class="btn btn-action" href="/u/{{.ID}}">
|
<a class="btn btn-action" href="/u/{{.ID}}">
|
||||||
@ -16,4 +16,4 @@
|
|||||||
</div>
|
</div>
|
||||||
</div>
|
</div>
|
||||||
</section>
|
</section>
|
||||||
{{end}}
|
{{end}}
|
||||||
|
Loading…
x
Reference in New Issue
Block a user