11use std:: cmp:: PartialEq ;
22use std:: fmt;
33use serde:: { Deserialize , Deserializer , Serialize , Serializer } ;
4+ use surrealdb:: Datetime ;
45use surrealdb:: sql:: Id ;
56
7+ #[ derive( Debug , Serialize , Deserialize , Clone ) ]
8+ pub struct IntelliThing {
9+ pub ( crate ) id : Id ,
10+ }
11+
612impl fmt:: Display for IntelliThing {
713 fn fmt ( & self , f : & mut fmt:: Formatter ) -> fmt:: Result {
814 write ! ( f, "{}" , self . id)
915 }
1016}
1117
12- #[ derive( Debug , Serialize , Deserialize , Clone ) ]
13- pub struct IntelliThing {
14- pub ( crate ) id : Id ,
18+ impl PartialEq for IntelliThing {
19+ fn eq ( & self , other : & Self ) -> bool {
20+ other. id == self . id
21+ }
1522}
1623
1724#[ derive( Serialize , Deserialize , Debug ) ]
@@ -27,10 +34,16 @@ pub struct User {
2734 pub ( crate ) lastname : Option < String > ,
2835}
2936
30- impl PartialEq for IntelliThing {
31- fn eq ( & self , other : & Self ) -> bool {
32- other. id == self . id
33- }
37+ #[ derive( Serialize , Deserialize , Debug ) ]
38+ pub struct Post {
39+ #[ serde( serialize_with = "serialize_record_id" ) ]
40+ pub ( crate ) id : IntelliThing ,
41+ #[ serde( serialize_with = "serialize_record_id" ) ]
42+ pub ( crate ) author : IntelliThing ,
43+ pub ( crate ) likes : i32 ,
44+ pub ( crate ) views : i32 ,
45+ pub ( crate ) title : String ,
46+ pub ( crate ) posted : Datetime ,
3447}
3548
3649impl User {
@@ -59,6 +72,16 @@ pub struct BodyUser {
5972 pub ( crate ) lastname : Option < String > ,
6073}
6174
75+ // Used by the http endpoint to allow patching the post
76+ #[ derive( Serialize , Deserialize , Debug ) ]
77+ pub struct BodyPost {
78+ #[ serde( serialize_with = "serialize_option_record_id" , deserialize_with = "deserialize_record_id" ) ]
79+ pub ( crate ) author : Option < IntelliThing > ,
80+ pub ( crate ) likes : Option < i32 > ,
81+ pub ( crate ) views : Option < i32 > ,
82+ pub ( crate ) title : Option < String > ,
83+ pub ( crate ) posted : Option < Datetime > ,
84+ }
6285
6386fn serialize_record_id < S > ( record_id : & IntelliThing , serializer : S ) -> Result < S :: Ok , S :: Error >
6487 where
0 commit comments