The backend of a gist server written in Rust
Nelze vybrat více než 25 témat Téma musí začínat písmenem nebo číslem, může obsahovat pomlčky („-“) a může být dlouhé až 35 znaků.

mod.rs 772B

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