@@ -7,9 +7,7 @@ use crate::database::dto::{
77 BatchOperationError , BatchOperationResult , InsertGameData , UpdateGameData ,
88} ;
99use crate :: entity:: prelude:: * ;
10- use crate :: entity:: {
11- bgm_data:: BgmData , games, savedata, vndb_data:: VndbData , ymgal_data:: YmgalData ,
12- } ;
10+ use crate :: entity:: { games, savedata} ;
1311use sea_orm:: * ;
1412use serde:: { Deserialize , Serialize } ;
1513use std:: collections:: HashSet ;
@@ -75,35 +73,48 @@ impl GamesRepository {
7573 . map ( ToOwned :: to_owned)
7674 }
7775
78- fn resolve_source_date (
79- bgm_data : Option < & BgmData > ,
80- vndb_data : Option < & VndbData > ,
81- ymgal_data : Option < & YmgalData > ,
82- ) -> Option < String > {
83- bgm_data
84- . and_then ( |data| Self :: clean_source_date ( data. date . as_deref ( ) ) )
85- . or_else ( || vndb_data. and_then ( |data| Self :: clean_source_date ( data. date . as_deref ( ) ) ) )
86- . or_else ( || ymgal_data. and_then ( |data| Self :: clean_source_date ( data. date . as_deref ( ) ) ) )
76+ fn resolve_source_date < ' a > ( dates : impl IntoIterator < Item = Option < & ' a str > > ) -> Option < String > {
77+ dates. into_iter ( ) . find_map ( Self :: clean_source_date)
78+ }
79+
80+ fn pick_next_data < ' a , T > (
81+ update : & ' a Option < Option < T > > ,
82+ current : & ' a Option < T > ,
83+ ) -> Option < & ' a T > {
84+ update. as_ref ( ) . map_or ( current. as_ref ( ) , Option :: as_ref)
85+ }
86+
87+ fn has_source_data_update ( updates : & UpdateGameData ) -> bool {
88+ [
89+ updates. bgm_data . is_some ( ) ,
90+ updates. vndb_data . is_some ( ) ,
91+ updates. ymgal_data . is_some ( ) ,
92+ updates. kun_data . is_some ( ) ,
93+ ]
94+ . into_iter ( )
95+ . any ( |updated| updated)
8796 }
8897
8998 fn normalize_insert_date ( mut game : InsertGameData ) -> InsertGameData {
9099 if game. date . is_none ( ) {
91- game. date = Self :: resolve_source_date (
92- game. bgm_data . as_ref ( ) ,
93- game. vndb_data . as_ref ( ) ,
94- game. ymgal_data . as_ref ( ) ,
95- ) ;
100+ game. date = Self :: resolve_source_date ( [
101+ game. bgm_data . as_ref ( ) . and_then ( |data| data. date . as_deref ( ) ) ,
102+ game. vndb_data
103+ . as_ref ( )
104+ . and_then ( |data| data. date . as_deref ( ) ) ,
105+ game. ymgal_data
106+ . as_ref ( )
107+ . and_then ( |data| data. date . as_deref ( ) ) ,
108+ game. kun_data . as_ref ( ) . and_then ( |data| data. date . as_deref ( ) ) ,
109+ ] ) ;
96110 }
97111
98112 game
99113 }
100114
101115 fn should_normalize_update_date ( updates : & UpdateGameData ) -> bool {
102116 matches ! ( updates. date, Some ( None ) )
103- || ( updates. date . is_none ( )
104- && ( updates. bgm_data . is_some ( )
105- || updates. vndb_data . is_some ( )
106- || updates. ymgal_data . is_some ( ) ) )
117+ || ( updates. date . is_none ( ) && Self :: has_source_data_update ( updates) )
107118 }
108119
109120 async fn normalize_update_date < C > (
@@ -123,24 +134,16 @@ impl GamesRepository {
123134 . await ?
124135 . ok_or_else ( || DbErr :: RecordNotFound ( format ! ( "game {} not found" , game_id) ) ) ?;
125136
126- let next_bgm_data = match & updates. bgm_data {
127- Some ( data) => data. as_ref ( ) ,
128- None => current. bgm_data . as_ref ( ) ,
129- } ;
130- let next_vndb_data = match & updates. vndb_data {
131- Some ( data) => data. as_ref ( ) ,
132- None => current. vndb_data . as_ref ( ) ,
133- } ;
134- let next_ymgal_data = match & updates. ymgal_data {
135- Some ( data) => data. as_ref ( ) ,
136- None => current. ymgal_data . as_ref ( ) ,
137- } ;
138-
139- updates. date = Some ( Self :: resolve_source_date (
140- next_bgm_data,
141- next_vndb_data,
142- next_ymgal_data,
143- ) ) ;
137+ updates. date = Some ( Self :: resolve_source_date ( [
138+ Self :: pick_next_data ( & updates. bgm_data , & current. bgm_data )
139+ . and_then ( |data| data. date . as_deref ( ) ) ,
140+ Self :: pick_next_data ( & updates. vndb_data , & current. vndb_data )
141+ . and_then ( |data| data. date . as_deref ( ) ) ,
142+ Self :: pick_next_data ( & updates. ymgal_data , & current. ymgal_data )
143+ . and_then ( |data| data. date . as_deref ( ) ) ,
144+ Self :: pick_next_data ( & updates. kun_data , & current. kun_data )
145+ . and_then ( |data| data. date . as_deref ( ) ) ,
146+ ] ) ) ;
144147
145148 Ok ( updates)
146149 }
0 commit comments