|
5 | 5 | import com.fasterxml.jackson.databind.ObjectMapper; |
6 | 6 | import com.fasterxml.jackson.databind.SerializationFeature; |
7 | 7 | import com.fasterxml.jackson.datatype.jsr310.JavaTimeModule; |
| 8 | +import com.sbxcloud.sbx.annotation.SbxEntity; |
| 9 | +import com.sbxcloud.sbx.annotation.SbxModels; |
8 | 10 | import com.sbxcloud.sbx.exception.SBXException; |
9 | 11 | import com.sbxcloud.sbx.model.*; |
10 | 12 | import com.sbxcloud.sbx.query.FindQuery; |
@@ -201,6 +203,119 @@ public SBXResponse<Void> delete(String model, String key) { |
201 | 203 | return delete(model, List.of(key)); |
202 | 204 | } |
203 | 205 |
|
| 206 | + // ==================== Type-safe Operations (with @SbxModel) ==================== |
| 207 | + |
| 208 | + /** |
| 209 | + * Finds all records for an @SbxModel annotated class. |
| 210 | + * |
| 211 | + * @param type class annotated with @SbxModel |
| 212 | + * @return find response with results |
| 213 | + */ |
| 214 | + public <T> SBXFindResponse<T> find(Class<T> type) { |
| 215 | + return find(FindQuery.from(type), type); |
| 216 | + } |
| 217 | + |
| 218 | + /** |
| 219 | + * Creates a new record from an @SbxModel annotated entity. |
| 220 | + * Model name is inferred from the @SbxModel annotation. |
| 221 | + * |
| 222 | + * @param entity the entity to create |
| 223 | + * @return response with created key |
| 224 | + */ |
| 225 | + public <T> SBXResponse<T> create(T entity) { |
| 226 | + String model = SbxModels.getModelName(entity.getClass()); |
| 227 | + Map<String, Object> row = objectMapper.convertValue(entity, new TypeReference<>() {}); |
| 228 | + return create(model, row); |
| 229 | + } |
| 230 | + |
| 231 | + /** |
| 232 | + * Creates multiple records from @SbxModel annotated entities. |
| 233 | + * Model name is inferred from the @SbxModel annotation of the first entity. |
| 234 | + * |
| 235 | + * @param entities the entities to create |
| 236 | + * @return response with created keys |
| 237 | + */ |
| 238 | + @SafeVarargs |
| 239 | + public final <T> SBXResponse<T> create(T... entities) { |
| 240 | + if (entities == null || entities.length == 0) { |
| 241 | + return SBXResponse.failure("No entities provided"); |
| 242 | + } |
| 243 | + String model = SbxModels.getModelName(entities[0].getClass()); |
| 244 | + List<Map<String, Object>> rows = Arrays.stream(entities) |
| 245 | + .map(e -> objectMapper.convertValue(e, new TypeReference<Map<String, Object>>() {})) |
| 246 | + .toList(); |
| 247 | + return create(model, rows); |
| 248 | + } |
| 249 | + |
| 250 | + /** |
| 251 | + * Updates an @SbxModel annotated entity. |
| 252 | + * The entity must have a non-null key. |
| 253 | + * |
| 254 | + * @param entity the entity to update |
| 255 | + * @return response |
| 256 | + */ |
| 257 | + public <T> SBXResponse<T> update(T entity) { |
| 258 | + String model = SbxModels.getModelName(entity.getClass()); |
| 259 | + Map<String, Object> row = objectMapper.convertValue(entity, new TypeReference<>() {}); |
| 260 | + return update(model, row); |
| 261 | + } |
| 262 | + |
| 263 | + /** |
| 264 | + * Updates multiple @SbxModel annotated entities. |
| 265 | + * |
| 266 | + * @param entities the entities to update |
| 267 | + * @return response |
| 268 | + */ |
| 269 | + @SafeVarargs |
| 270 | + public final <T> SBXResponse<T> update(T... entities) { |
| 271 | + if (entities == null || entities.length == 0) { |
| 272 | + return SBXResponse.failure("No entities provided"); |
| 273 | + } |
| 274 | + String model = SbxModels.getModelName(entities[0].getClass()); |
| 275 | + List<Map<String, Object>> rows = Arrays.stream(entities) |
| 276 | + .map(e -> objectMapper.convertValue(e, new TypeReference<Map<String, Object>>() {})) |
| 277 | + .toList(); |
| 278 | + return update(model, rows); |
| 279 | + } |
| 280 | + |
| 281 | + /** |
| 282 | + * Deletes an @SbxEntity by extracting its key. |
| 283 | + * |
| 284 | + * @param entity the entity to delete (must implement SbxEntity) |
| 285 | + * @return response |
| 286 | + */ |
| 287 | + public <T extends SbxEntity> SBXResponse<Void> delete(T entity) { |
| 288 | + if (entity.key() == null) { |
| 289 | + return SBXResponse.failure("Entity has no key"); |
| 290 | + } |
| 291 | + String model = SbxModels.getModelName(entity.getClass()); |
| 292 | + return delete(model, entity.key()); |
| 293 | + } |
| 294 | + |
| 295 | + /** |
| 296 | + * Deletes records by keys using @SbxModel annotation for model name. |
| 297 | + * |
| 298 | + * @param type class annotated with @SbxModel |
| 299 | + * @param keys the keys to delete |
| 300 | + * @return response |
| 301 | + */ |
| 302 | + public SBXResponse<Void> delete(Class<?> type, String... keys) { |
| 303 | + String model = SbxModels.getModelName(type); |
| 304 | + return delete(model, List.of(keys)); |
| 305 | + } |
| 306 | + |
| 307 | + /** |
| 308 | + * Deletes records by keys using @SbxModel annotation for model name. |
| 309 | + * |
| 310 | + * @param type class annotated with @SbxModel |
| 311 | + * @param keys the keys to delete |
| 312 | + * @return response |
| 313 | + */ |
| 314 | + public SBXResponse<Void> delete(Class<?> type, List<String> keys) { |
| 315 | + String model = SbxModels.getModelName(type); |
| 316 | + return delete(model, keys); |
| 317 | + } |
| 318 | + |
204 | 319 | // ==================== Authentication ==================== |
205 | 320 |
|
206 | 321 | /** |
|
0 commit comments