@@ -10,7 +10,7 @@ import {
10
10
listPlacesSchema ,
11
11
} from '~/schemas/places'
12
12
import { db } from '~/server/db/db'
13
- import { places , placesToPlaceCategories } from '~/server/db/schema'
13
+ import { features , places , placesToPlaceCategories } from '~/server/db/schema'
14
14
import { selectPoint } from '~/server/helpers/spatial-data'
15
15
import {
16
16
flattenTranslationsOnExecute ,
@@ -140,6 +140,12 @@ export const placesAdminRouter = router({
140
140
. input ( createPlaceSchema )
141
141
. mutation ( async ( { input } ) => {
142
142
await db . transaction ( async ( tx ) => {
143
+ const insertFeaturesResult = await tx
144
+ . insert ( features )
145
+ . values ( { ...input . features } )
146
+
147
+ const featuresId = Number ( insertFeaturesResult . insertId )
148
+
143
149
const insertPlaceResult = await tx . insert ( places ) . values ( {
144
150
name : input . name ,
145
151
description : input . description ,
@@ -148,6 +154,7 @@ export const placesAdminRouter = router({
148
154
location : pointToString ( input . location ) ,
149
155
content : input . content ,
150
156
verificationRequirementsId : 1 ,
157
+ featuresId,
151
158
} )
152
159
const newPlaceId = Number ( insertPlaceResult . insertId )
153
160
@@ -169,6 +176,28 @@ export const placesAdminRouter = router({
169
176
await db . transaction ( async ( tx ) => {
170
177
const placeId = Number ( input . id )
171
178
179
+ let featuresId = (
180
+ await tx
181
+ . selectDistinct ( { featuresId : places . featuresId } )
182
+ . from ( places )
183
+ . where ( eq ( places . id , placeId ) )
184
+ ) [ 0 ] . featuresId
185
+
186
+ if ( featuresId === null ) {
187
+ const insertFeaturesResult = await tx
188
+ . insert ( features )
189
+ . values ( { ...input . features } )
190
+ featuresId = Number ( insertFeaturesResult . insertId )
191
+ } else {
192
+ await tx
193
+ . update ( features )
194
+ . set ( {
195
+ ...input . features ,
196
+ id : featuresId ,
197
+ } )
198
+ . where ( eq ( features . id , featuresId ) )
199
+ }
200
+
172
201
await tx
173
202
. update ( places )
174
203
. set ( {
@@ -178,6 +207,7 @@ export const placesAdminRouter = router({
178
207
mainImageId : input . mainImageId ,
179
208
location : pointToString ( input . location ) ,
180
209
content : input . content ,
210
+ featuresId : featuresId ,
181
211
} )
182
212
. where ( eq ( places . id , placeId ) )
183
213
0 commit comments