@@ -34,6 +34,8 @@ pub type EventId = i64;
3434pub struct EventRecord {
3535 pub id : EventId ,
3636 pub name : String ,
37+ pub stage : i64 ,
38+ pub stage_count : i64 ,
3739 pub place : String ,
3840 pub start_time : QxDateTime ,
3941 pub owner : String ,
@@ -45,6 +47,8 @@ impl EventRecord {
4547 Self {
4648 id : 0 ,
4749 name : "" . to_string ( ) ,
50+ stage : 1 ,
51+ stage_count : 1 ,
4852 place : "" . to_string ( ) ,
4953 start_time,
5054 // time_zone: "Europe/Prague".to_string(),
@@ -74,26 +78,33 @@ pub async fn load_event_info_for_api_token(qx_api_token: &QxApiToken, db: &State
7478 . fetch_one ( pool)
7579 . await
7680 . map_err ( |e| {
77- warn ! ( "Unauthorized request for api token: {}" , qx_api_token. 0 ) ;
81+ warn ! ( "Unauthorized request for api token: {}, error: {} " , qx_api_token. 0 , e ) ;
7882 Custom ( Status :: Unauthorized , e. to_string ( ) )
7983 } ) ?;
8084 Ok ( event)
8185}
8286pub ( crate ) async fn save_event ( event : & EventRecord , db : & State < DbPool > ) -> anyhow:: Result < EventId > {
8387 let id = if event. id > 0 {
84- query ( "UPDATE events SET name=?, place=?, start_time=? WHERE id=?" )
88+ query ( "UPDATE events SET name=?, place=?, stage=?, stage_count=?, start_time=? WHERE id=?" )
8589 . bind ( & event. name )
8690 . bind ( & event. place )
91+ . bind ( & event. stage )
92+ . bind ( & event. stage_count )
8793 . bind ( event. start_time . 0 )
8894 // .bind(&event.time_zone)
8995 . bind ( event. id )
9096 . execute ( & db. 0 )
9197 . await . map_err ( |e| anyhow ! ( "{e}" ) ) ?;
9298 event. id
9399 } else {
94- let id: ( i64 , ) = query_as ( "INSERT INTO events(name, place, start_time, api_token, owner) VALUES (?, ?, ?, ?, ?) RETURNING id" )
100+ let id: ( i64 , ) = query_as (
101+ "INSERT INTO events(name, place, stage, stage_count, start_time, api_token, owner)
102+ VALUES (?, ?, ?, ?, ?, ?, ?) RETURNING id"
103+ )
95104 . bind ( & event. name )
96105 . bind ( & event. place )
106+ . bind ( & event. stage )
107+ . bind ( & event. stage_count )
97108 . bind ( event. start_time . 0 )
98109 . bind ( & event. api_token . 0 )
99110 . bind ( & event. owner )
@@ -109,6 +120,8 @@ struct EventFormValues<'v> {
109120 id : EventId ,
110121 name : & ' v str ,
111122 place : & ' v str ,
123+ stage : i64 ,
124+ stage_count : i64 ,
112125 start_time : & ' v str ,
113126 // #[field(validate = len(1..))]
114127 // owner: &'v str,
@@ -124,22 +137,26 @@ async fn post_event<'r>(form: Form<Contextual<'r, EventFormValues<'r>>>, session
124137 let vals = form. value . as_ref ( ) . ok_or ( Custom ( Status :: BadRequest , "Form data invalid" . to_string ( ) ) ) ?;
125138 let start_time = QxDateTime :: parse_from_iso ( vals. start_time )
126139 . map_err ( |e| Custom ( Status :: BadRequest , format ! ( "Unrecognized date-time string: {}, error: {e}" , vals. start_time) ) ) ?;
127- let event = if vals. id > 0 {
128- let event = load_event_info ( vals. id , db) . await ?;
140+ let event = if vals. id == 0 {
129141 EventRecord {
142+ id : 0 ,
130143 name : vals. name . to_string ( ) ,
144+ stage : vals. stage ,
145+ stage_count : vals. stage_count ,
131146 place : vals. place . to_string ( ) ,
132147 start_time,
133- ..event
148+ owner : user. email . clone ( ) ,
149+ api_token : QxApiToken ( vals. api_token . to_string ( ) ) ,
134150 }
135151 } else {
152+ let event = load_event_info ( vals. id , db) . await ?;
136153 EventRecord {
137- id : 0 ,
138154 name : vals. name . to_string ( ) ,
139155 place : vals. place . to_string ( ) ,
156+ stage : vals. stage ,
157+ stage_count : vals. stage_count ,
140158 start_time,
141- owner : user. email . clone ( ) ,
142- api_token : QxApiToken ( vals. api_token . to_string ( ) ) ,
159+ ..event
143160 }
144161 } ;
145162 if event. owner != user. email {
@@ -167,13 +184,6 @@ pub async fn get_user_info(session_id: &QxSessionId, state: &State<SharedQxState
167184 Ok ( user_info)
168185}
169186
170- // pub async fn event_owner_opt(event_id: EventId, session_id: MaybeSessionId, state: &State<SharedQxState>, gdb: &State<DbPool>) -> anyhow::Result<Option<UserInfo>> {
171- // let event = load_event(event_id, gdb).await?;
172- // let user = user_info_opt(session_id.0.as_ref(), state).await?
173- // .and_then(|user| if user.email == event.owner {Some(user)} else {None});
174- // Ok(user)
175- // }
176-
177187pub fn is_event_owner ( event : & EventRecord , user : Option < & UserInfo > ) -> bool {
178188 if let Some ( user) = user {
179189 user. email == event. owner
@@ -263,6 +273,8 @@ async fn get_api_event_current(api_token: QxApiToken, db: &State<DbPool>) -> Res
263273#[ derive( Serialize , Deserialize , Clone , Debug ) ]
264274pub struct PostedEvent {
265275 pub name : String ,
276+ pub stage : i64 ,
277+ pub stage_count : i64 ,
266278 pub place : String ,
267279 pub start_time : DateTime < FixedOffset > ,
268280}
@@ -272,6 +284,8 @@ async fn post_api_event_current(api_token: QxApiToken, posted_event: Json<Posted
272284 return Err ( string_to_custom_error ( "Event not found" ) ) ;
273285 } ;
274286 event_info. name = posted_event. name . clone ( ) ;
287+ event_info. stage = posted_event. stage ;
288+ event_info. stage_count = posted_event. stage_count ;
275289 event_info. place = posted_event. place . clone ( ) ;
276290 event_info. start_time = posted_event. start_time . into ( ) ;
277291 debug ! ( "Post event info, start00: {}" , event_info. start_time. to_iso_string( ) ) ;
@@ -465,12 +479,14 @@ pub async fn import_runs(edb: &SqlitePool) -> anyhow::Result<()> {
465479 Ok ( ( ) )
466480}
467481
482+ pub ( crate ) const TEST_API_TOKEN : & str = "plelababamak" ;
483+
468484#[ get( "/event/create-demo" ) ]
469485async fn create_demo_event ( state : & State < SharedQxState > , gdb : & State < DbPool > ) -> Result < Redirect , Custom < String > > {
470486 let mut event_info = EventRecord :: new ( "fanda.vacek@gmail.com" ) ;
471487 event_info. name = String :: from ( "Demo event" ) ;
472488 event_info. place = String :: from ( "Deep forest 42" ) ;
473- event_info. api_token = QxApiToken ( String :: from ( "plelababamak" ) ) ;
489+ event_info. api_token = QxApiToken ( String :: from ( TEST_API_TOKEN ) ) ;
474490 let event_id = save_event ( & event_info, gdb) . await . map_err ( |e| Custom ( Status :: BadRequest , e. to_string ( ) ) ) ?;
475491 {
476492 // upload demo start list
0 commit comments