@@ -8,9 +8,12 @@ export class ItineraryService {
88 async list ( ) : Promise < Itinerary [ ] > {
99 const result = await this . db
1010 . prepare ( `
11- SELECT i.*, s.enabled as secret_enabled, s.offset_minutes as secret_offset
11+ SELECT i.*,
12+ s.enabled as secret_enabled, s.offset_minutes as secret_offset,
13+ w.walica_id as walica_id
1214 FROM itineraries i
1315 LEFT JOIN itinerary_secrets s ON i.id = s.itinerary_id
16+ LEFT JOIN itinerary_walica_settings w ON i.id = w.itinerary_id
1417 ORDER BY i.created_at DESC
1518 ` )
1619 . all ( ) ;
@@ -21,9 +24,12 @@ export class ItineraryService {
2124 async get ( id : string ) : Promise < Itinerary | null > {
2225 const result = await this . db
2326 . prepare ( `
24- SELECT i.*, s.enabled as secret_enabled, s.offset_minutes as secret_offset
27+ SELECT i.*,
28+ s.enabled as secret_enabled, s.offset_minutes as secret_offset,
29+ w.walica_id as walica_id
2530 FROM itineraries i
2631 LEFT JOIN itinerary_secrets s ON i.id = s.itinerary_id
32+ LEFT JOIN itinerary_walica_settings w ON i.id = w.itinerary_id
2733 WHERE i.id = ?
2834 ` )
2935 . bind ( id )
@@ -41,6 +47,7 @@ export class ItineraryService {
4147 title : input . title ,
4248 theme_id : input . theme_id || 'minimal' ,
4349 memo : input . memo ?? null ,
50+ walica_id : input . walica_id ?? null ,
4451 password : input . password ?? null ,
4552 secret_settings : input . secret_settings ? {
4653 enabled : input . secret_settings . enabled ,
@@ -70,6 +77,14 @@ export class ItineraryService {
7077 . run ( ) ;
7178 }
7279
80+ // Insert into walica table if exists
81+ if ( itinerary . walica_id ) {
82+ await this . db
83+ . prepare ( 'INSERT INTO itinerary_walica_settings (itinerary_id, walica_id, created_at, updated_at) VALUES (?, ?, ?, ?)' )
84+ . bind ( itinerary . id , itinerary . walica_id , now , now )
85+ . run ( ) ;
86+ }
87+
7388 return itinerary ;
7489 }
7590
@@ -116,9 +131,6 @@ export class ItineraryService {
116131 . run ( ) ;
117132 } else {
118133 // Upsert settings
119- // Check if exists first (D1 doesn't support INSERT OR REPLACE nicely with timestamps preservation if we want that, but here we just overwrite)
120- // Actually, standard SQL UPSERT or just DELETE+INSERT or UPDATE/INSERT check.
121- // Let's try INSERT OR REPLACE
122134 await this . db
123135 . prepare ( `
124136 INSERT INTO itinerary_secrets (itinerary_id, enabled, offset_minutes, created_at, updated_at)
@@ -139,12 +151,34 @@ export class ItineraryService {
139151 }
140152 }
141153
154+ // Handle walica settings update
155+ if ( input . walica_id !== undefined ) {
156+ if ( input . walica_id === null ) {
157+ // Remove settings
158+ await this . db
159+ . prepare ( 'DELETE FROM itinerary_walica_settings WHERE itinerary_id = ?' )
160+ . bind ( id )
161+ . run ( ) ;
162+ } else {
163+ // Upsert settings
164+ await this . db
165+ . prepare ( `
166+ INSERT INTO itinerary_walica_settings (itinerary_id, walica_id, created_at, updated_at)
167+ VALUES (?, ?, ?, ?)
168+ ON CONFLICT(itinerary_id) DO UPDATE SET
169+ walica_id = excluded.walica_id,
170+ updated_at = excluded.updated_at
171+ ` )
172+ . bind ( id , input . walica_id , now , now )
173+ . run ( ) ;
174+ }
175+ }
176+
142177 return await this . get ( id ) ;
143178 }
144179
145180 async delete ( id : string ) : Promise < boolean > {
146- // Foreign key cascade should handle the secrets table, but let's be safe or rely on DB
147- // Since we defined ON DELETE CASCADE in migration, deleting from itineraries is enough.
181+ // Foreign key cascade should handle the secrets and walica tables
148182 const result = await this . db
149183 . prepare ( 'DELETE FROM itineraries WHERE id = ?' )
150184 . bind ( id )
@@ -159,6 +193,7 @@ export class ItineraryService {
159193 title : row . title ,
160194 theme_id : row . theme_id ,
161195 memo : row . memo ,
196+ walica_id : row . walica_id ,
162197 password : row . password ,
163198 created_at : row . created_at ,
164199 updated_at : row . updated_at ,
0 commit comments