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.

mod.rs 910B

123456789101112131415161718192021222324252627282930313233343536
  1. use chrono::NaiveDateTime;
  2. use diesel::pg::PgConnection as PGC;
  3. use diesel::prelude::*;
  4. use crate::schema::gists;
  5. #[derive(Queryable, Serialize, Deserialize)]
  6. pub struct Gist {
  7. pub id: i32,
  8. pub title: String,
  9. pub body: String,
  10. pub created_at: Option<NaiveDateTime>,
  11. pub filetype: Option<String>,
  12. }
  13. #[derive(Insertable, AsChangeset, Serialize, Deserialize)]
  14. #[table_name = "gists"]
  15. pub struct InsertableGist {
  16. pub title: String,
  17. pub body: String,
  18. pub filetype: Option<String>,
  19. }
  20. pub fn all(connection: &PGC) -> QueryResult<Vec<Gist>> {
  21. gists::table.load::<Gist>(&*connection)
  22. }
  23. pub fn get(connection: &PGC, id: i32) -> QueryResult<Gist> {
  24. gists::table.find(id).get_result::<Gist>(connection)
  25. }
  26. pub fn insert(gist: InsertableGist, connection: &PGC) -> QueryResult<Gist> {
  27. diesel::insert_into(gists::table)
  28. .values(gist)
  29. .get_result(connection)
  30. }