The backend of a gist server written in Rust
You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

routes.rs 896B

12345678910111213141516171819202122232425262728293031323334
  1. use diesel::result::Error;
  2. use rocket::http::Status;
  3. use rocket_contrib::json::Json;
  4. use rocket_contrib::templates::Template;
  5. use std::collections::HashMap;
  6. use crate::connection::DbConn;
  7. use crate::gists;
  8. use crate::gists::Gist;
  9. fn error_status(error: Error) -> Status {
  10. match error {
  11. Error::NotFound => Status::NotFound,
  12. _ => Status::InternalServerError,
  13. }
  14. }
  15. #[get("/")]
  16. pub fn index(connection: DbConn) -> Result<Json<Vec<Gist>>, Status> {
  17. gists::all(&connection)
  18. .map(|gists| Json(gists))
  19. .map_err(|error| error_status(error))
  20. }
  21. #[get("/gists/<id>")]
  22. pub fn show_gist(connection: DbConn, id: i32) -> Template {
  23. let gist = match gists::get(&connection, id) {
  24. Ok(gist) => Some(gist),
  25. Err(_) => None,
  26. };
  27. let mut context = HashMap::new();
  28. context.insert("gist", gist);
  29. Template::render("gists/show", &context)
  30. }