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); } }