User profile edition

This commit is contained in:
Bat 2018-05-12 16:30:14 +01:00
parent 3d5d03dc08
commit ea08718c23
4 changed files with 65 additions and 0 deletions

View File

@ -69,6 +69,8 @@ fn main() {
routes::user::me, routes::user::me,
routes::user::details, routes::user::details,
routes::user::edit,
routes::user::update,
routes::user::follow, routes::user::follow,
routes::user::activity_details, routes::user::activity_details,
routes::user::outbox, routes::user::outbox,

View File

@ -76,6 +76,17 @@ impl User {
.expect("Error saving new user") .expect("Error saving new user")
} }
pub fn update(&self, conn: &PgConnection, name: String, email: String, summary: String) -> User {
diesel::update(self)
.set((
users::display_name.eq(name),
users::email.eq(email),
users::summary.eq(summary),
)).load::<User>(conn)
.expect("Couldn't update user")
.into_iter().nth(0).unwrap()
}
pub fn get(conn: &PgConnection, id: i32) -> Option<User> { pub fn get(conn: &PgConnection, id: i32) -> Option<User> {
users::table.filter(users::id.eq(id)) users::table.filter(users::id.eq(id))
.limit(1) .limit(1)

View File

@ -61,6 +61,34 @@ fn new(user: Option<User>) -> Template {
})) }))
} }
#[get("/@/<name>/edit")]
fn edit(name: String, user: User) -> Option<Template> {
if user.username == name && !name.contains("@") {
Some(Template::render("users/edit", json!({
"account": user
})))
} else {
None
}
}
#[derive(FromForm)]
struct UpdateUserForm {
display_name: Option<String>,
email: Option<String>,
summary: Option<String>,
}
#[put("/@/<_name>/edit", data = "<data>")]
fn update(_name: String, conn: DbConn, user: User, data: Form<UpdateUserForm>) -> Redirect {
user.update(&*conn,
data.get().display_name.clone().unwrap_or(user.display_name.to_string()).to_string(),
data.get().email.clone().unwrap_or(user.email.clone().unwrap()).to_string(),
data.get().summary.clone().unwrap_or(user.summary.to_string())
);
Redirect::to("/me")
}
#[derive(FromForm)] #[derive(FromForm)]
struct NewUserForm { struct NewUserForm {
username: String, username: String,

24
templates/users/edit.tera Normal file
View File

@ -0,0 +1,24 @@
{% extends "base" %}
{% block title %}
Edit your account
{% endblock title %}
{% block content %}
<h1>Your Profile</h1>
<form method="post">
<!-- Rocket hack to use various HTTP methods -->
<input type=hidden name="_method" value="put">
<label for="display_name">Display Name</label>
<input name="display_name" value="{{ account.display_name }}">
<label for="email">Email</label>
<input name="email" value="{{ account.email }}">
<label for="summary">Summary</label>
<input name="summary" value="{{ account.summary }}">
<input type="submit" value="Update account"/>
</form>
{% endblock content %}