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.

12345678910111213141516171819202122232425262728293031323334
  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. }
  12. #[derive(Insertable, AsChangeset, Serialize, Deserialize)]
  13. #[table_name = "gists"]
  14. pub struct InsertableGist {
  15. pub title: String,
  16. pub body: String,
  17. }
  18. pub fn all(connection: &PGC) -> QueryResult<Vec<Gist>> {
  19. gists::table.load::<Gist>(&*connection)
  20. }
  21. pub fn get(connection: &PGC, id: i32) -> QueryResult<Gist> {
  22. gists::table.find(id).get_result::<Gist>(connection)
  23. }
  24. pub fn insert(gist: InsertableGist, connection: &PGC) -> QueryResult<Gist> {
  25. diesel::insert_into(gists::table)
  26. .values(gist)
  27. .get_result(connection)
  28. }