Edit / remove

This commit is contained in:
m15o 2021-11-27 10:39:34 +01:00
parent 041cc4c65a
commit ea7088f8c0
10 changed files with 73 additions and 20 deletions

View file

@ -24,7 +24,12 @@ a {
} }
.status-content { .status-content {
margin: 0 1em; margin: 0 1em 0.5em 1em;
}
.status nav {
font-size: 0.8em;
margin-left: 1em;
} }
.status { .status {

View file

@ -152,10 +152,37 @@ func (s *Storage) UpdateStatus(status model.Status) error {
} }
func (s *Storage) DeleteStatus(id int64, author string) 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 { if err != nil {
return err 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 return err
} }

View file

@ -37,9 +37,7 @@ var TplCommonMap = map[string]string{
{{ define "head" }}{{ end }} {{ define "head" }}{{ end }}
`, `,
"status": `{{ define "status" }} "status": `{{ define "status" }}
<article class="status"> <div class="status-username"><a href="/users/{{ .User }}">{{ .User }}</a>, {{ .TimeAgo }}</div>
<div class="status-username"><a href="/users/{{ .User }}">{{ .User }}</a>, {{ .TimeAgo }}</div> <p class="status-content">{{ .Content }}</p>
<p class="status-content">{{ .Content }}</p>
</article>
{{ end }}`, {{ end }}`,
} }

View file

@ -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-new", h.showNewStatusView).Methods(http.MethodGet)
router.HandleFunc("/status-save", h.saveStatus).Methods(http.MethodPost) 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}/edit", h.showEditStatusView).Methods(http.MethodGet)
router.HandleFunc("/statuses/{id}/update", h.updateStatus).Methods(http.MethodPost) router.HandleFunc("/statuses/{id}/update", h.updateStatus).Methods(http.MethodPost)

View file

@ -6,7 +6,7 @@ var TplMap = map[string]string{
"confirm_remove_status": `{{ define "content" }} "confirm_remove_status": `{{ define "content" }}
Are you sure you you want to delete the following status? Are you sure you you want to delete the following status?
<p>{{ .status.Content }}</p> <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="hidden" name="id"/>
<input type="submit" value="Submit"> <input type="submit" value="Submit">
</form> </form>
@ -96,7 +96,12 @@ Are you sure you you want to delete the following status?
<section> <section>
<h2>Status stream</h2> <h2>Status stream</h2>
{{ range .statuses }} {{ 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 }} {{ end }}
</section> </section>
</div> </div>
@ -190,7 +195,12 @@ Are you sure you you want to delete the following status?
<section> <section>
<h2>Statuses</h2> <h2>Statuses</h2>
{{ range .statuses }} {{ 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 }} {{ end }}
{{ if or .showMore (ne 0 .page) }} {{ if or .showMore (ne 0 .page) }}
<p> <p>

View file

@ -1,6 +1,4 @@
{{ define "status" }} {{ define "status" }}
<article class="status"> <div class="status-username"><a href="/users/{{ .User }}">{{ .User }}</a>, {{ .TimeAgo }}</div>
<div class="status-username"><a href="/users/{{ .User }}">{{ .User }}</a>, {{ .TimeAgo }}</div> <p class="status-content">{{ .Content }}</p>
<p class="status-content">{{ .Content }}</p>
</article>
{{ end }} {{ end }}

View file

@ -1,7 +1,7 @@
{{ define "content" }} {{ define "content" }}
Are you sure you you want to delete the following status? Are you sure you you want to delete the following status?
<p>{{ .status.Content }}</p> <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="hidden" name="id"/>
<input type="submit" value="Submit"> <input type="submit" value="Submit">
</form> </form>

View file

@ -22,7 +22,12 @@
<section> <section>
<h2>Status stream</h2> <h2>Status stream</h2>
{{ range .statuses }} {{ 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 }} {{ end }}
</section> </section>
</div> </div>

View file

@ -17,7 +17,12 @@
<section> <section>
<h2>Statuses</h2> <h2>Statuses</h2>
{{ range .statuses }} {{ 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 }} {{ end }}
{{ if or .showMore (ne 0 .page) }} {{ if or .showMore (ne 0 .page) }}
<p> <p>

View file

@ -2,7 +2,7 @@ package handler
import ( import (
"net/http" "net/http"
"status/web/handler/form" "strconv"
) )
func (h *Handler) handleRemoveStatus(w http.ResponseWriter, r *http.Request) { 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) unauthorized(w)
return return
} }
f := form.NewStatusForm(r) id, err := strconv.ParseInt(r.URL.Query().Get("id"), 10, 64)
status, err := h.storage.StatusById(f.Id) if err != nil {
serverError(w, err)
return
}
status, err := h.storage.StatusById(id)
if err != nil { if err != nil {
serverError(w, err) serverError(w, err)
return return