14
14
)
15
15
from stac_fastapi .types import stac as stac_types
16
16
from stac_fastapi .types .core import AsyncBaseTransactionsClient
17
+ from stac_pydantic import Collection , Item , ItemCollection
17
18
from starlette .responses import JSONResponse , Response
18
19
19
20
from stac_fastapi .pgstac .config import Settings
@@ -69,11 +70,13 @@ def _validate_item(
69
70
async def create_item (
70
71
self ,
71
72
collection_id : str ,
72
- item : Union [stac_types . Item , stac_types . ItemCollection ],
73
+ item : Union [Item , ItemCollection ],
73
74
request : Request ,
74
75
** kwargs ,
75
76
) -> Optional [Union [stac_types .Item , Response ]]:
76
77
"""Create item."""
78
+ item = item .model_dump (mode = "json" )
79
+
77
80
if item ["type" ] == "FeatureCollection" :
78
81
valid_items = []
79
82
for item in item ["features" ]: # noqa: B020
@@ -100,6 +103,7 @@ async def create_item(
100
103
).get_links (extra_links = item .get ("links" ))
101
104
102
105
return stac_types .Item (** item )
106
+
103
107
else :
104
108
raise HTTPException (
105
109
status_code = 400 ,
@@ -111,10 +115,12 @@ async def update_item(
111
115
request : Request ,
112
116
collection_id : str ,
113
117
item_id : str ,
114
- item : stac_types . Item ,
118
+ item : Item ,
115
119
** kwargs ,
116
120
) -> Optional [Union [stac_types .Item , Response ]]:
117
121
"""Update item."""
122
+ item = item .model_dump (mode = "json" )
123
+
118
124
self ._validate_item (request , item , collection_id , item_id )
119
125
item ["collection" ] = collection_id
120
126
@@ -130,31 +136,49 @@ async def update_item(
130
136
return stac_types .Item (** item )
131
137
132
138
async def create_collection (
133
- self , collection : stac_types .Collection , request : Request , ** kwargs
139
+ self ,
140
+ collection : Collection ,
141
+ request : Request ,
142
+ ** kwargs ,
134
143
) -> Optional [Union [stac_types .Collection , Response ]]:
135
144
"""Create collection."""
145
+ collection = collection .model_dump (mode = "json" )
146
+
136
147
self ._validate_collection (request , collection )
148
+
137
149
async with request .app .state .get_connection (request , "w" ) as conn :
138
150
await dbfunc (conn , "create_collection" , collection )
151
+
139
152
collection ["links" ] = await CollectionLinks (
140
153
collection_id = collection ["id" ], request = request
141
- ).get_links (extra_links = collection . get ( "links" ) )
154
+ ).get_links (extra_links = collection [ "links" ] )
142
155
143
156
return stac_types .Collection (** collection )
144
157
145
158
async def update_collection (
146
- self , collection : stac_types .Collection , request : Request , ** kwargs
159
+ self ,
160
+ collection : Collection ,
161
+ request : Request ,
162
+ ** kwargs ,
147
163
) -> Optional [Union [stac_types .Collection , Response ]]:
148
164
"""Update collection."""
165
+ col = collection .model_dump (mode = "json" )
166
+
149
167
async with request .app .state .get_connection (request , "w" ) as conn :
150
- await dbfunc (conn , "update_collection" , collection )
151
- collection ["links" ] = await CollectionLinks (
152
- collection_id = collection ["id" ], request = request
153
- ).get_links (extra_links = collection .get ("links" ))
154
- return stac_types .Collection (** collection )
168
+ await dbfunc (conn , "update_collection" , col )
169
+
170
+ col ["links" ] = await CollectionLinks (
171
+ collection_id = col ["id" ], request = request
172
+ ).get_links (extra_links = col .get ("links" ))
173
+
174
+ return stac_types .Collection (** col )
155
175
156
176
async def delete_item (
157
- self , item_id : str , collection_id : str , request : Request , ** kwargs
177
+ self ,
178
+ item_id : str ,
179
+ collection_id : str ,
180
+ request : Request ,
181
+ ** kwargs ,
158
182
) -> Optional [Union [stac_types .Item , Response ]]:
159
183
"""Delete item."""
160
184
q , p = render (
@@ -164,6 +188,7 @@ async def delete_item(
164
188
)
165
189
async with request .app .state .get_connection (request , "w" ) as conn :
166
190
await conn .fetchval (q , * p )
191
+
167
192
return JSONResponse ({"deleted item" : item_id })
168
193
169
194
async def delete_collection (
@@ -172,6 +197,7 @@ async def delete_collection(
172
197
"""Delete collection."""
173
198
async with request .app .state .get_connection (request , "w" ) as conn :
174
199
await dbfunc (conn , "delete_collection" , collection_id )
200
+
175
201
return JSONResponse ({"deleted collection" : collection_id })
176
202
177
203
0 commit comments