Add edit images #8
This commit is contained in:
parent
54c2e25b5b
commit
5274c8d8da
3 changed files with 46 additions and 6 deletions
|
@ -34,6 +34,11 @@ WHERE created_at < :image-date
|
|||
ORDER BY created_at DESC
|
||||
LIMIT 1
|
||||
|
||||
-- :name edit-image! :! :1
|
||||
UPDATE images
|
||||
SET title = :title, description = :description
|
||||
WHERE id = :id
|
||||
|
||||
|
||||
-- :name create-tag! :i!
|
||||
INSERT OR IGNORE INTO tags (tagname)
|
||||
|
|
|
@ -1,39 +1,53 @@
|
|||
{% extends "layout.html" %}
|
||||
|
||||
{% block content %}
|
||||
{% if image %}
|
||||
<h1>Bild editieren</h1>
|
||||
<p>Hier kann ein bestehendes Bild bearbeitet werden. Das Bild selbst kann nicht
|
||||
verändert werden (hierzu muss das Bild gelöscht und neu angelegt werden)</p>
|
||||
{% else %}
|
||||
<h1>Bild hochladen</h1>
|
||||
<p>Hier kann ein neues Bild der Gallerie hinzugefügt werden. Dabei sind alle
|
||||
gängigen Bildformate und Auflösungen möglich. Das hochgeladene Bild wird
|
||||
zugeschnitten (Thumbnail) und skaliert (Detailseite).</p>
|
||||
{% endif %}
|
||||
|
||||
<form action="/upload" enctype="multipart/form-data" method="POST" class="form-horizontal">
|
||||
<form action="{% if image %}/edit/{{ image.id }}{% else %}/upload{% endif %}" enctype="multipart/form-data" method="POST" class="form-horizontal">
|
||||
{% csrf-field %}
|
||||
|
||||
{% if not image %}
|
||||
<div class="form-group">
|
||||
<label for="file" class="sr-only">Bild</label>
|
||||
<input id="file" class="form-control" name="file" type="file" />
|
||||
</div>
|
||||
{% endif %}
|
||||
|
||||
<div class="form-group">
|
||||
<label for="title">Bildüberschrift</label>
|
||||
<input type="text" name="title" class="form-control"
|
||||
value="{{ image.title }}"
|
||||
placeholder="Leer lassen für Dateiname">
|
||||
</div>
|
||||
|
||||
<div class="form-group">
|
||||
<label for="description">Beschreibung (optional)</label>
|
||||
<textarea name="description" class="form-control" rows="4"></textarea>
|
||||
<textarea name="description" class="form-control" rows="4">{{image.description }}</textarea>
|
||||
</div>
|
||||
|
||||
<div class="form-group">
|
||||
<label for="tags">Tags</label>
|
||||
<input type="text" id="tags" name="tags" class="form-control"
|
||||
value="{% if tags %}{% for tag in tags %}{{ tag.tagname }} {% endfor %}{% endif %}"
|
||||
placeholder="Trennen mit Leerzeichen oder Kommas">
|
||||
</div>
|
||||
|
||||
<button type="submit" class="btn btn-success">
|
||||
<span class="fa fa-floppy-o"></span>
|
||||
{% if image %}
|
||||
Bild bearbeiten
|
||||
{% else %}
|
||||
Bild hochladen
|
||||
{% endif %}
|
||||
</button>
|
||||
</form>
|
||||
{% endblock %}
|
||||
|
|
|
@ -28,11 +28,14 @@
|
|||
(let [{tag-id :id} (db/get-tag {:tagname tag})]
|
||||
(db/add-tag-to-image! {:imageid image-id :tagid tag-id})))
|
||||
|
||||
(defn add-tags-for-image [image-id tag-string]
|
||||
(let [all-tags (str/split tag-string, #"(\s*,\s*|\s+)")]
|
||||
(run! #(add-tag image-id %) all-tags)))
|
||||
|
||||
(defn add-image-to-database [filehash title description tags]
|
||||
(let [img (db/create-image! {:hash filehash :title title :description description})
|
||||
image-id ((keyword "last_insert_rowid()") img)
|
||||
all-tags (str/split tags, #"(\s*,\s*|\s+)")]
|
||||
(run! #(add-tag image-id %) all-tags)))
|
||||
image-id ((keyword "last_insert_rowid()") img)]
|
||||
(add-tags-for-image image-id tags)))
|
||||
|
||||
(defn delete-image! [id request]
|
||||
(let [image (db/get-image {:id id})]
|
||||
|
@ -45,6 +48,16 @@
|
|||
(let [comments (db/get-recent-comments {:num-comments 20})]
|
||||
(layout/render "comments.html" {:comments comments})))
|
||||
|
||||
(defn edit-image-form [id request]
|
||||
(let [image (db/get-image {:id id})
|
||||
tags (db/get-tags-for-image {:id id})]
|
||||
(layout/render "admin/upload.html" {:image image :tags tags})))
|
||||
|
||||
(defn edit-image! [id title description tags request]
|
||||
(db/edit-image! {:id id :title title :description description})
|
||||
(db/delete-image-tags! {:id id})
|
||||
(add-tags-for-image id tags))
|
||||
|
||||
(defroutes admin-routes
|
||||
(GET "/upload" [:as request]
|
||||
(-> (layout/render
|
||||
|
@ -60,7 +73,15 @@
|
|||
(GET "/delete/:id" [id :as request]
|
||||
(delete-image! id request)
|
||||
(-> (redirect "/page/1")
|
||||
(assoc :flash {:message "Bild wurde gelöscht" :type "success"})))
|
||||
(assoc :flash {:message "Bild wurde gelöscht" :type "danger"})))
|
||||
|
||||
(GET "/edit/:id" [id :as request]
|
||||
(edit-image-form id request))
|
||||
|
||||
(POST "/edit/:id" [id title description tags :as request]
|
||||
(edit-image! id title description tags request)
|
||||
(-> (redirect (str "/show/" id))
|
||||
(assoc :flash {:message "Bild wurde bearbeitet" :type "success"})))
|
||||
|
||||
(GET "/statistics" []
|
||||
(layout/render "statistics.html"))
|
||||
|
|
Loading…
Reference in a new issue