parent
0297aafbff
commit
d451a4f05c
10
src/main.rs
10
src/main.rs
@ -118,8 +118,8 @@ async fn main() -> std::io::Result<()> {
|
||||
// logout
|
||||
.route("/logout/", web::to(views::logout))
|
||||
// submit a new url for shortening
|
||||
.route("/submit/", web::get().to(views::submission))
|
||||
.route("/submit/", web::post().to(views::process_submission))
|
||||
.route("/submit/", web::get().to(views::create_link))
|
||||
.route("/submit/", web::post().to(views::process_link_creation))
|
||||
// view an existing url
|
||||
.service(
|
||||
web::scope("/view")
|
||||
@ -136,7 +136,11 @@ async fn main() -> std::io::Result<()> {
|
||||
web::scope("/edit")
|
||||
.service(
|
||||
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(
|
||||
web::scope("/profile")
|
||||
|
51
src/views.rs
51
src/views.rs
@ -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>,
|
||||
id: Identity,
|
||||
) -> Result<HttpResponse, ServerError> {
|
||||
@ -371,7 +371,7 @@ pub(crate) async fn submission(
|
||||
Ok(redirect_builder("/admin/login/"))
|
||||
}
|
||||
|
||||
pub(crate) async fn process_submission(
|
||||
pub(crate) async fn process_link_creation(
|
||||
data: web::Form<LinkForm>,
|
||||
id: Identity,
|
||||
) -> Result<HttpResponse, ServerError> {
|
||||
@ -399,3 +399,50 @@ pub(crate) async fn process_submission(
|
||||
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/"))
|
||||
}
|
||||
|
@ -62,6 +62,7 @@ nav li:last-child {
|
||||
}
|
||||
svg {
|
||||
width: 100px;
|
||||
height: 100px;
|
||||
}
|
||||
|
||||
div.actions {
|
||||
|
20
templates/edit_link.html
Normal file
20
templates/edit_link.html
Normal 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 %}
|
@ -29,4 +29,7 @@
|
||||
</td>
|
||||
</tr>
|
||||
</table>
|
||||
<div class="actions">
|
||||
<a class="button" href="/admin/edit/link/{{ link.code }}">Editieren</a>
|
||||
</div>
|
||||
{% endblock %}
|
Loading…
Reference in New Issue
Block a user