diff --git a/plume-models/src/users.rs b/plume-models/src/users.rs index de2ebc1..b8e0105 100644 --- a/plume-models/src/users.rs +++ b/plume-models/src/users.rs @@ -491,6 +491,13 @@ impl User { } }) } + + pub fn set_avatar(&self, conn: &PgConnection, id: i32) { + diesel::update(self) + .set(users::avatar_id.eq(id)) + .execute(conn) + .expect("Couldn't update user avatar"); + } } impl<'a, 'r> FromRequest<'a, 'r> for User { diff --git a/src/main.rs b/src/main.rs index 54dfee3..07699b6 100644 --- a/src/main.rs +++ b/src/main.rs @@ -68,6 +68,7 @@ fn main() { routes::medias::upload, routes::medias::details, routes::medias::delete, + routes::medias::set_avatar, routes::medias::static_files, routes::notifications::paginated_notifications, diff --git a/src/routes/medias.rs b/src/routes/medias.rs index d5e3910..5a9ee85 100644 --- a/src/routes/medias.rs +++ b/src/routes/medias.rs @@ -104,8 +104,14 @@ fn delete(id: i32, _user: User, conn: DbConn) -> Redirect { Redirect::to(uri!(list)) } +#[get("/medias//avatar")] +fn set_avatar(id: i32, user: User, conn: DbConn) -> Redirect { + let media = Media::get(&*conn, id).expect("Media to delete not found"); + user.set_avatar(&*conn, media.id); + Redirect::to(uri!(details: id = id)) +} + #[get("/static/media/", rank = 1)] fn static_files(file: PathBuf) -> Option { NamedFile::open(Path::new("media/").join(file)).ok() } -