From 5dbd888f61a1df98ff3901e6ff01b010d5424807 Mon Sep 17 00:00:00 2001 From: demo-bot Date: Mon, 20 Apr 2026 15:42:37 +0000 Subject: [PATCH] feat(scaffold): add src/main/java/com/kyndryl/platform/service/ItemsController.java [skip ci] --- .../platform/service/ItemsController.java | 70 +++++++++++++++++++ 1 file changed, 70 insertions(+) create mode 100644 src/main/java/com/kyndryl/platform/service/ItemsController.java diff --git a/src/main/java/com/kyndryl/platform/service/ItemsController.java b/src/main/java/com/kyndryl/platform/service/ItemsController.java new file mode 100644 index 0000000..35ee232 --- /dev/null +++ b/src/main/java/com/kyndryl/platform/service/ItemsController.java @@ -0,0 +1,70 @@ +package com.kyndryl.platform.service; + +import org.springframework.http.HttpStatus; +import org.springframework.web.bind.annotation.*; + +import java.util.*; +import java.util.concurrent.ConcurrentHashMap; +import java.util.concurrent.atomic.AtomicLong; + +/** + * A minimal but fully functional CRUD REST API for "items". + * + * Endpoints: + * GET /api/items — list all items + * POST /api/items — create an item { "name": "...", "description": "..." } + * GET /api/items/{id} — get single item + * PUT /api/items/{id} — update item + * DELETE /api/items/{id} — delete item + * + * Health + metrics are exposed via Spring Actuator at /actuator/health and + * /actuator/prometheus (see application.yml). + */ +@RestController +@RequestMapping("/api/items") +public class ItemsController { + + record Item(long id, String name, String description) {} + + private final Map store = new ConcurrentHashMap<>(); + private final AtomicLong counter = new AtomicLong(1); + + @GetMapping + public Collection list() { + return store.values(); + } + + @PostMapping + @ResponseStatus(HttpStatus.CREATED) + public Item create(@RequestBody Map body) { + long id = counter.getAndIncrement(); + Item item = new Item(id, + body.getOrDefault("name", "unnamed"), + body.getOrDefault("description", "")); + store.put(id, item); + return item; + } + + @GetMapping("/{id}") + public Item get(@PathVariable long id) { + Item item = store.get(id); + if (item == null) throw new NoSuchElementException("Item not found: " + id); + return item; + } + + @PutMapping("/{id}") + public Item update(@PathVariable long id, @RequestBody Map body) { + if (!store.containsKey(id)) throw new NoSuchElementException("Item not found: " + id); + Item updated = new Item(id, + body.getOrDefault("name", store.get(id).name()), + body.getOrDefault("description", store.get(id).description())); + store.put(id, updated); + return updated; + } + + @DeleteMapping("/{id}") + public Map delete(@PathVariable long id) { + store.remove(id); + return Map.of("deleted", id); + } +}