mirror of
https://github.com/taigrr/shorturl
synced 2025-01-18 04:03:16 -08:00
Handle edit URL
This commit is contained in:
parent
a55db041cc
commit
e984fce524
@ -55,3 +55,9 @@ func (u *URL) SetName(name string) error {
|
|||||||
u.UpdatedAt = time.Now()
|
u.UpdatedAt = time.Now()
|
||||||
return db.Save(&u)
|
return db.Save(&u)
|
||||||
}
|
}
|
||||||
|
|
||||||
|
func (u *URL) update(target string) error {
|
||||||
|
u.URL = target
|
||||||
|
u.UpdatedAt = time.Now()
|
||||||
|
return db.Update(u)
|
||||||
|
}
|
||||||
|
76
server.go
76
server.go
@ -215,6 +215,75 @@ func (s *Server) StatsHandler() httprouter.Handle {
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
// EditHandler ...
|
||||||
|
func (s *Server) EditHandler() httprouter.Handle {
|
||||||
|
return func(w http.ResponseWriter, r *http.Request, p httprouter.Params) {
|
||||||
|
var u URL
|
||||||
|
|
||||||
|
id := p.ByName("id")
|
||||||
|
if id == "" {
|
||||||
|
http.Error(w, "Bad Request", http.StatusBadRequest)
|
||||||
|
return
|
||||||
|
}
|
||||||
|
|
||||||
|
err := db.One("ID", id, &u)
|
||||||
|
if err != nil && err == storm.ErrNotFound {
|
||||||
|
http.Error(w, "Not Found", http.StatusNotFound)
|
||||||
|
return
|
||||||
|
} else if err != nil {
|
||||||
|
log.Printf("error looking up %s for editing: %s", id, err)
|
||||||
|
http.Error(w, "Iternal Error", http.StatusInternalServerError)
|
||||||
|
return
|
||||||
|
}
|
||||||
|
|
||||||
|
s.render(
|
||||||
|
"edit", w,
|
||||||
|
struct {
|
||||||
|
ID string
|
||||||
|
URL string
|
||||||
|
}{
|
||||||
|
ID: u.ID,
|
||||||
|
URL: u.URL,
|
||||||
|
},
|
||||||
|
)
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
// UpdateHandler ...
|
||||||
|
func (s *Server) UpdateHandler() httprouter.Handle {
|
||||||
|
return func(w http.ResponseWriter, r *http.Request, p httprouter.Params) {
|
||||||
|
var u URL
|
||||||
|
|
||||||
|
id := p.ByName("id")
|
||||||
|
target := r.FormValue("url")
|
||||||
|
if id == "" || target == "" {
|
||||||
|
http.Error(w, "Bad Request", http.StatusBadRequest)
|
||||||
|
return
|
||||||
|
}
|
||||||
|
|
||||||
|
err := db.One("ID", id, &u)
|
||||||
|
if err != nil && err == storm.ErrNotFound {
|
||||||
|
http.Error(w, "Not Found", http.StatusNotFound)
|
||||||
|
return
|
||||||
|
} else if err != nil {
|
||||||
|
log.Printf("error looking up %s for editing: %v", id, err)
|
||||||
|
http.Error(w, "Iternal Error", http.StatusInternalServerError)
|
||||||
|
return
|
||||||
|
}
|
||||||
|
|
||||||
|
err = u.update(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)
|
||||||
|
|
||||||
|
http.Redirect(w, r, redirectURL, http.StatusFound)
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
// ListenAndServe ...
|
// ListenAndServe ...
|
||||||
func (s *Server) ListenAndServe() {
|
func (s *Server) ListenAndServe() {
|
||||||
log.Fatal(
|
log.Fatal(
|
||||||
@ -245,6 +314,8 @@ func (s *Server) initRoutes() {
|
|||||||
s.router.POST("/", s.ShortenHandler())
|
s.router.POST("/", s.ShortenHandler())
|
||||||
s.router.GET("/u/:id", s.ViewHandler())
|
s.router.GET("/u/:id", s.ViewHandler())
|
||||||
s.router.GET("/r/:id", s.RedirectHandler())
|
s.router.GET("/r/:id", s.RedirectHandler())
|
||||||
|
s.router.GET("/e/:id", s.EditHandler())
|
||||||
|
s.router.POST("/e/:id", s.UpdateHandler())
|
||||||
}
|
}
|
||||||
|
|
||||||
// NewServer ...
|
// NewServer ...
|
||||||
@ -278,8 +349,13 @@ func NewServer(bind string, config Config) *Server {
|
|||||||
template.Must(viewTemplate.Parse(box.MustString("view.html")))
|
template.Must(viewTemplate.Parse(box.MustString("view.html")))
|
||||||
template.Must(viewTemplate.Parse(box.MustString("base.html")))
|
template.Must(viewTemplate.Parse(box.MustString("base.html")))
|
||||||
|
|
||||||
|
editTemplate := template.New("edit")
|
||||||
|
template.Must(editTemplate.Parse(box.MustString("edit.html")))
|
||||||
|
template.Must(editTemplate.Parse(box.MustString("base.html")))
|
||||||
|
|
||||||
server.templates.Add("index", indexTemplate)
|
server.templates.Add("index", indexTemplate)
|
||||||
server.templates.Add("view", viewTemplate)
|
server.templates.Add("view", viewTemplate)
|
||||||
|
server.templates.Add("edit", editTemplate)
|
||||||
|
|
||||||
server.initRoutes()
|
server.initRoutes()
|
||||||
|
|
||||||
|
@ -4,6 +4,7 @@
|
|||||||
<head>
|
<head>
|
||||||
<link rel="stylesheet" href="/css/spectre-icons.min.css">
|
<link rel="stylesheet" href="/css/spectre-icons.min.css">
|
||||||
<link rel="stylesheet" href="/css/spectre.min.css">
|
<link rel="stylesheet" href="/css/spectre.min.css">
|
||||||
|
<link rel="icon" href="data:,">
|
||||||
{{ template "stylesheets" . }}
|
{{ template "stylesheets" . }}
|
||||||
<meta name="viewport" content="width=device-width, initial-scale=1" />
|
<meta name="viewport" content="width=device-width, initial-scale=1" />
|
||||||
{{ template "css" . }}
|
{{ template "css" . }}
|
||||||
|
19
templates/edit.html
Normal file
19
templates/edit.html
Normal file
@ -0,0 +1,19 @@
|
|||||||
|
{{define "content"}}
|
||||||
|
<section class="container">
|
||||||
|
<div class="columns">
|
||||||
|
<div class="column">
|
||||||
|
<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" 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}}">
|
||||||
|
<i class="icon icon-forward">View</i>
|
||||||
|
</a>
|
||||||
|
</div>
|
||||||
|
</form>
|
||||||
|
</div>
|
||||||
|
</div>
|
||||||
|
</section>
|
||||||
|
{{end}}
|
@ -7,7 +7,7 @@
|
|||||||
<span class="input-group-addon d-inline-block">
|
<span class="input-group-addon d-inline-block">
|
||||||
{{ $URL.ID }}
|
{{ $URL.ID }}
|
||||||
<i class="icon icon-forward"></i>
|
<i class="icon icon-forward"></i>
|
||||||
{{ $URL.URL }}
|
{{ $URL.URL -}}
|
||||||
</span>
|
</span>
|
||||||
<a class="btn btn-action btn-primary" href="/u/{{$URL.ID}}">
|
<a class="btn btn-action btn-primary" href="/u/{{$URL.ID}}">
|
||||||
<i class="icon icon-forward">View</i>
|
<i class="icon icon-forward">View</i>
|
||||||
@ -23,7 +23,7 @@
|
|||||||
<form class="mt-10" action="" method="POST">
|
<form class="mt-10" action="" method="POST">
|
||||||
<div class="form-group input-group">
|
<div class="form-group input-group">
|
||||||
<label class="form-label" for="input-url"></label>
|
<label class="form-label" for="input-url"></label>
|
||||||
<input class="form-input" id="input-url" type="text" name="url" placeholder="Enter long url here...">
|
<input class="form-input" id="input-url" type="text" name="url" placeholder="Enter long url here..." autofocus>
|
||||||
<button class="btn btn-primary" type="submit">Shorten</button>
|
<button class="btn btn-primary" type="submit">Shorten</button>
|
||||||
</div>
|
</div>
|
||||||
</form>
|
</form>
|
||||||
|
@ -6,6 +6,9 @@
|
|||||||
<div class="form-group input-group">
|
<div class="form-group input-group">
|
||||||
<input class="form-input" id="input-url" value="{{.URL}}" readonly />
|
<input class="form-input" id="input-url" value="{{.URL}}" readonly />
|
||||||
<button class="btn btn-primary hide" id="btn-copy" data-clipboard-target="#input-url">Copy</button>
|
<button class="btn btn-primary hide" id="btn-copy" data-clipboard-target="#input-url">Copy</button>
|
||||||
|
<a class="btn btn-action" href="/e/{{.ID}}">
|
||||||
|
<i class="icon icon-edit">Edit</i>
|
||||||
|
</a>
|
||||||
</div>
|
</div>
|
||||||
</div>
|
</div>
|
||||||
</div>
|
</div>
|
||||||
|
Loading…
x
Reference in New Issue
Block a user