@@ -3,13 +3,14 @@ use crate::{
33 types:: { mod_json:: ModJson , models:: mod_entity:: Mod } ,
44} ;
55use chrono:: { DateTime , SecondsFormat , Utc } ;
6- use sqlx:: PgConnection ;
6+ use sqlx:: { PgConnection , Postgres , QueryBuilder } ;
77
88#[ derive( sqlx:: FromRow ) ]
99struct ModRecordGetOne {
1010 id : String ,
1111 repository : Option < String > ,
1212 featured : bool ,
13+ unlisted : bool ,
1314 download_count : i32 ,
1415 #[ sqlx( default ) ]
1516 about : Option < String > ,
@@ -25,6 +26,7 @@ impl ModRecordGetOne {
2526 id : self . id ,
2627 repository : self . repository ,
2728 featured : self . featured ,
29+ unlisted : self . unlisted ,
2830 download_count : self . download_count ,
2931 versions : Default :: default ( ) ,
3032 tags : Default :: default ( ) ,
@@ -50,7 +52,7 @@ pub async fn get_one(
5052 sqlx:: query_as!(
5153 ModRecordGetOne ,
5254 "SELECT
53- m.id, m.repository, m.about, m.changelog, m.featured,
55+ m.id, m.repository, m.about, m.changelog, m.featured, m.unlisted,
5456 m.download_count, m.created_at, m.updated_at
5557 FROM mods m
5658 WHERE id = $1" ,
@@ -65,7 +67,7 @@ pub async fn get_one(
6567 sqlx:: query_as!(
6668 ModRecordGetOne ,
6769 "SELECT
68- m.id, m.repository, NULL as about, NULL as changelog, m.featured,
70+ m.id, m.repository, NULL as about, NULL as changelog, m.featured, m.unlisted,
6971 m.download_count, m.created_at, m.updated_at
7072 FROM mods m
7173 WHERE id = $1" ,
@@ -92,7 +94,7 @@ pub async fn create(json: &ModJson, conn: &mut PgConnection) -> Result<Mod, Data
9294 ) VALUES ($1, $2, $3, $4, $5)
9395 RETURNING
9496 id, repository, about,
95- changelog, featured,
97+ changelog, featured, unlisted,
9698 download_count, created_at,
9799 updated_at" ,
98100 & json. id,
@@ -234,11 +236,13 @@ pub async fn update_with_json(
234236 about = $2,
235237 changelog = $3,
236238 image = $4,
237- updated_at = NOW()" ,
239+ updated_at = NOW()
240+ WHERE id = $5" ,
238241 json. repository,
239242 json. about,
240243 json. changelog,
241- json. logo
244+ json. logo,
245+ the_mod. id
242246 )
243247 . execute ( conn)
244248 . await
@@ -281,6 +285,48 @@ pub async fn update_with_json_moved(
281285 Ok ( the_mod)
282286}
283287
288+ pub async fn user_update (
289+ mut the_mod : Mod ,
290+ featured : Option < bool > ,
291+ unlisted : Option < bool > ,
292+ pool : & mut PgConnection ,
293+ ) -> Result < Mod , DatabaseError > {
294+ if featured. is_none ( ) && unlisted. is_none ( ) {
295+ return Err ( DatabaseError :: InvalidInput (
296+ "No new fields were supplied" . into ( ) ,
297+ ) ) ;
298+ }
299+
300+ let mut builder: QueryBuilder < Postgres > = QueryBuilder :: new ( "UPDATE mods SET " ) ;
301+ let mut split = builder. separated ( ", " ) ;
302+
303+ if let Some ( featured) = featured {
304+ split. push ( "featured = " ) ;
305+ split. push_bind ( featured) ;
306+ }
307+
308+ if let Some ( unlisted) = unlisted {
309+ split. push ( "unlisted = " ) ;
310+ split. push_bind ( unlisted) ;
311+ }
312+
313+ builder
314+ . build ( )
315+ . execute ( & mut * pool)
316+ . await
317+ . inspect_err ( |e| log:: error!( "Failed to update mod {}: {e}" , the_mod. id) )
318+ . map ( |_| ( ) ) ?;
319+
320+ if let Some ( featured) = featured {
321+ the_mod. featured = featured;
322+ }
323+ if let Some ( unlisted) = unlisted {
324+ the_mod. unlisted = unlisted;
325+ }
326+
327+ Ok ( the_mod)
328+ }
329+
284330/// Used when first version goes from pending to accepted.
285331/// Makes it so versions that stay a lot in pending appear at the top of the newly created lists
286332pub async fn touch_created_at ( id : & str , conn : & mut PgConnection ) -> Result < ( ) , DatabaseError > {
0 commit comments