Make links editable

closes #4
This commit is contained in:
Dietrich 2021-02-08 20:44:32 +01:00
parent 0297aafbff
commit d451a4f05c
Signed by: dietrich
GPG Key ID: 9F3C20C0F85DF67C
5 changed files with 80 additions and 5 deletions

View File

@ -118,8 +118,8 @@ async fn main() -> std::io::Result<()> {
// logout // logout
.route("/logout/", web::to(views::logout)) .route("/logout/", web::to(views::logout))
// submit a new url for shortening // submit a new url for shortening
.route("/submit/", web::get().to(views::submission)) .route("/submit/", web::get().to(views::create_link))
.route("/submit/", web::post().to(views::process_submission)) .route("/submit/", web::post().to(views::process_link_creation))
// view an existing url // view an existing url
.service( .service(
web::scope("/view") web::scope("/view")
@ -136,7 +136,11 @@ async fn main() -> std::io::Result<()> {
web::scope("/edit") web::scope("/edit")
.service( .service(
web::scope("/link") web::scope("/link")
.route("/{redirect_id}", web::get().to(views::view_link)), .route("/{redirect_id}", web::get().to(views::edit_link))
.route(
"/{redirect_id}",
web::post().to(views::process_link_edit),
),
) )
.service( .service(
web::scope("/profile") web::scope("/profile")

View File

@ -356,7 +356,7 @@ pub(crate) async fn redirect_fhs() -> Result<HttpResponse, ServerError> {
)) ))
} }
pub(crate) async fn submission( pub(crate) async fn create_link(
tera: web::Data<Tera>, tera: web::Data<Tera>,
id: Identity, id: Identity,
) -> Result<HttpResponse, ServerError> { ) -> Result<HttpResponse, ServerError> {
@ -371,7 +371,7 @@ pub(crate) async fn submission(
Ok(redirect_builder("/admin/login/")) Ok(redirect_builder("/admin/login/"))
} }
pub(crate) async fn process_submission( pub(crate) async fn process_link_creation(
data: web::Form<LinkForm>, data: web::Form<LinkForm>,
id: Identity, id: Identity,
) -> Result<HttpResponse, ServerError> { ) -> Result<HttpResponse, ServerError> {
@ -399,3 +399,50 @@ pub(crate) async fn process_submission(
Ok(redirect_builder("/admin/login/")) Ok(redirect_builder("/admin/login/"))
} }
} }
pub(crate) async fn edit_link(
tera: web::Data<Tera>,
id: Identity,
link_id: web::Path<String>,
) -> Result<HttpResponse, ServerError> {
if let Some(id) = id.identity() {
use super::schema::links::dsl::{code, links};
let connection = establish_connection()?;
let link: Link = links
.filter(code.eq(&link_id.0))
.first::<Link>(&connection)?;
let mut data = Context::new();
data.insert("title", "Submit a Post");
data.insert("link", &link);
data.insert("name", &id);
let rendered = tera.render("edit_link.html", &data)?;
return Ok(HttpResponse::Ok().body(rendered));
}
Ok(redirect_builder("/admin/login/"))
}
pub(crate) async fn process_link_edit(
data: web::Form<LinkForm>,
id: Identity,
link_id: web::Path<String>,
) -> Result<HttpResponse, ServerError> {
if let Some(_id) = id.identity() {
use super::schema::links::dsl::{code, links, target, title};
let connection = establish_connection()?;
diesel::update(links.filter(code.eq(&link_id.0)))
.set((
code.eq(&data.code),
target.eq(&data.target),
title.eq(&data.title),
))
.execute(&connection)?;
return Ok(redirect_builder(&format!(
"/admin/view/link/{}",
&data.code
)));
}
Ok(redirect_builder("/admin/login/"))
}

View File

@ -62,6 +62,7 @@ nav li:last-child {
} }
svg { svg {
width: 100px; width: 100px;
height: 100px;
} }
div.actions { div.actions {

20
templates/edit_link.html Normal file
View File

@ -0,0 +1,20 @@
{% extends "admin.html" %}
{% block admin %}
<h1>Link Editieren: {{ link.title }}</h1>
<form action="" method="POST">
<div>
<label for="title">Beschreibung:</label>
<input type="text" name="title" value="{{ link.title }}">
</div>
<div>
<label for="target">Ziel:</label>
<input type="text" name="target" value="{{link.target}}">
</div>
<div>
<label for="code">Code:</label>
<input type="text" name="code" value="{{link.code}}">
</div>
<input type="submit" value="Speichern">
</form>
{% endblock %}

View File

@ -29,4 +29,7 @@
</td> </td>
</tr> </tr>
</table> </table>
<div class="actions">
<a class="button" href="/admin/edit/link/{{ link.code }}">Editieren</a>
</div>
{% endblock %} {% endblock %}