Edit / remove
This commit is contained in:
parent
041cc4c65a
commit
ea7088f8c0
10 changed files with 73 additions and 20 deletions
|
|
@ -24,7 +24,12 @@ a {
|
|||
}
|
||||
|
||||
.status-content {
|
||||
margin: 0 1em;
|
||||
margin: 0 1em 0.5em 1em;
|
||||
}
|
||||
|
||||
.status nav {
|
||||
font-size: 0.8em;
|
||||
margin-left: 1em;
|
||||
}
|
||||
|
||||
.status {
|
||||
|
|
|
|||
|
|
@ -152,10 +152,37 @@ func (s *Storage) UpdateStatus(status model.Status) error {
|
|||
}
|
||||
|
||||
func (s *Storage) DeleteStatus(id int64, author string) error {
|
||||
stmt, err := s.db.Prepare(`DELETE from statuses WHERE id = $1 and author = $2;`)
|
||||
ctx := context.Background()
|
||||
tx, err := s.db.BeginTx(ctx, nil)
|
||||
if err != nil {
|
||||
return err
|
||||
}
|
||||
_, err = stmt.Exec(id, author)
|
||||
var latestId int64
|
||||
if err := tx.QueryRowContext(ctx, `select status_id from users where name = $1`,
|
||||
author).Scan(&latestId); err != nil {
|
||||
tx.Rollback()
|
||||
return err
|
||||
}
|
||||
if _, err := tx.ExecContext(ctx, `UPDATE users set status_id=$1 where name=$2`, nil, author); err != nil {
|
||||
tx.Rollback()
|
||||
return err
|
||||
}
|
||||
if _, err := tx.ExecContext(ctx, `DELETE from statuses WHERE id = $1 and author = $2;`, id, author); err != nil {
|
||||
tx.Rollback()
|
||||
return err
|
||||
}
|
||||
if latestId == id {
|
||||
var newId int64
|
||||
if err := tx.QueryRowContext(ctx, `select id from statuses where author = $1 order by created_at desc limit 1;`,
|
||||
author).Scan(&newId); err != nil {
|
||||
tx.Rollback()
|
||||
return err
|
||||
}
|
||||
if _, err := tx.ExecContext(ctx, `UPDATE users set status_id=$1 where name=$2`, newId, author); err != nil {
|
||||
tx.Rollback()
|
||||
return err
|
||||
}
|
||||
}
|
||||
err = tx.Commit()
|
||||
return err
|
||||
}
|
||||
|
|
|
|||
|
|
@ -37,9 +37,7 @@ var TplCommonMap = map[string]string{
|
|||
{{ define "head" }}{{ end }}
|
||||
`,
|
||||
"status": `{{ define "status" }}
|
||||
<article class="status">
|
||||
<div class="status-username"><a href="/users/{{ .User }}">{{ .User }}</a>, {{ .TimeAgo }}</div>
|
||||
<p class="status-content">{{ .Content }}</p>
|
||||
</article>
|
||||
<div class="status-username"><a href="/users/{{ .User }}">{{ .User }}</a>, {{ .TimeAgo }}</div>
|
||||
<p class="status-content">{{ .Content }}</p>
|
||||
{{ end }}`,
|
||||
}
|
||||
|
|
|
|||
|
|
@ -75,6 +75,7 @@ func New(cfg *config.Config, sess *session.Session, data *storage.Storage) (http
|
|||
|
||||
router.HandleFunc("/status-new", h.showNewStatusView).Methods(http.MethodGet)
|
||||
router.HandleFunc("/status-save", h.saveStatus).Methods(http.MethodPost)
|
||||
router.HandleFunc("/status-remove", h.handleRemoveStatus)
|
||||
router.HandleFunc("/statuses/{id}/edit", h.showEditStatusView).Methods(http.MethodGet)
|
||||
router.HandleFunc("/statuses/{id}/update", h.updateStatus).Methods(http.MethodPost)
|
||||
|
||||
|
|
|
|||
|
|
@ -6,7 +6,7 @@ var TplMap = map[string]string{
|
|||
"confirm_remove_status": `{{ define "content" }}
|
||||
Are you sure you you want to delete the following status?
|
||||
<p>{{ .status.Content }}</p>
|
||||
<form action="/remove-status" method="post">
|
||||
<form action="/status-remove?id={{ .status.Id }}" method="post">
|
||||
<input type="hidden" name="id"/>
|
||||
<input type="submit" value="Submit">
|
||||
</form>
|
||||
|
|
@ -96,7 +96,12 @@ Are you sure you you want to delete the following status?
|
|||
<section>
|
||||
<h2>Status stream</h2>
|
||||
{{ range .statuses }}
|
||||
{{ template "status" . }}
|
||||
<article class="status">
|
||||
{{ template "status" . }}
|
||||
{{ if eq $.logged .User }}
|
||||
<nav><a href="/status-remove?id={{ .Id }}">Delete</a> <a href="/statuses/{{ .Id }}/edit">Edit</a></nav>
|
||||
{{ end }}
|
||||
</article>
|
||||
{{ end }}
|
||||
</section>
|
||||
</div>
|
||||
|
|
@ -190,7 +195,12 @@ Are you sure you you want to delete the following status?
|
|||
<section>
|
||||
<h2>Statuses</h2>
|
||||
{{ range .statuses }}
|
||||
{{ template "status" . }}
|
||||
<article class="status">
|
||||
{{ template "status" . }}
|
||||
{{ if eq $.logged .User }}
|
||||
<nav><a href="/status-remove?id={{ .Id }}">Delete</a> <a href="/status-edit?id={{ .Id }}">Edit</a></nav>
|
||||
{{ end }}
|
||||
</article>
|
||||
{{ end }}
|
||||
{{ if or .showMore (ne 0 .page) }}
|
||||
<p>
|
||||
|
|
|
|||
|
|
@ -1,6 +1,4 @@
|
|||
{{ define "status" }}
|
||||
<article class="status">
|
||||
<div class="status-username"><a href="/users/{{ .User }}">{{ .User }}</a>, {{ .TimeAgo }}</div>
|
||||
<p class="status-content">{{ .Content }}</p>
|
||||
</article>
|
||||
<div class="status-username"><a href="/users/{{ .User }}">{{ .User }}</a>, {{ .TimeAgo }}</div>
|
||||
<p class="status-content">{{ .Content }}</p>
|
||||
{{ end }}
|
||||
|
|
@ -1,7 +1,7 @@
|
|||
{{ define "content" }}
|
||||
Are you sure you you want to delete the following status?
|
||||
<p>{{ .status.Content }}</p>
|
||||
<form action="/remove-status" method="post">
|
||||
<form action="/status-remove?id={{ .status.Id }}" method="post">
|
||||
<input type="hidden" name="id"/>
|
||||
<input type="submit" value="Submit">
|
||||
</form>
|
||||
|
|
|
|||
|
|
@ -22,7 +22,12 @@
|
|||
<section>
|
||||
<h2>Status stream</h2>
|
||||
{{ range .statuses }}
|
||||
{{ template "status" . }}
|
||||
<article class="status">
|
||||
{{ template "status" . }}
|
||||
{{ if eq $.logged .User }}
|
||||
<nav><a href="/status-remove?id={{ .Id }}">Delete</a> <a href="/statuses/{{ .Id }}/edit">Edit</a></nav>
|
||||
{{ end }}
|
||||
</article>
|
||||
{{ end }}
|
||||
</section>
|
||||
</div>
|
||||
|
|
|
|||
|
|
@ -17,7 +17,12 @@
|
|||
<section>
|
||||
<h2>Statuses</h2>
|
||||
{{ range .statuses }}
|
||||
{{ template "status" . }}
|
||||
<article class="status">
|
||||
{{ template "status" . }}
|
||||
{{ if eq $.logged .User }}
|
||||
<nav><a href="/status-remove?id={{ .Id }}">Delete</a> <a href="/statuses/{{ .Id }}/edit">Edit</a></nav>
|
||||
{{ end }}
|
||||
</article>
|
||||
{{ end }}
|
||||
{{ if or .showMore (ne 0 .page) }}
|
||||
<p>
|
||||
|
|
|
|||
|
|
@ -2,7 +2,7 @@ package handler
|
|||
|
||||
import (
|
||||
"net/http"
|
||||
"status/web/handler/form"
|
||||
"strconv"
|
||||
)
|
||||
|
||||
func (h *Handler) handleRemoveStatus(w http.ResponseWriter, r *http.Request) {
|
||||
|
|
@ -11,8 +11,12 @@ func (h *Handler) handleRemoveStatus(w http.ResponseWriter, r *http.Request) {
|
|||
unauthorized(w)
|
||||
return
|
||||
}
|
||||
f := form.NewStatusForm(r)
|
||||
status, err := h.storage.StatusById(f.Id)
|
||||
id, err := strconv.ParseInt(r.URL.Query().Get("id"), 10, 64)
|
||||
if err != nil {
|
||||
serverError(w, err)
|
||||
return
|
||||
}
|
||||
status, err := h.storage.StatusById(id)
|
||||
if err != nil {
|
||||
serverError(w, err)
|
||||
return
|
||||
|
|
|
|||
Loading…
Reference in a new issue