feat(scaffold): add src/main/java/com/kyndryl/platform/service/ItemsController.java [skip ci]
This commit is contained in:
@@ -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<Long, Item> store = new ConcurrentHashMap<>();
|
||||
private final AtomicLong counter = new AtomicLong(1);
|
||||
|
||||
@GetMapping
|
||||
public Collection<Item> list() {
|
||||
return store.values();
|
||||
}
|
||||
|
||||
@PostMapping
|
||||
@ResponseStatus(HttpStatus.CREATED)
|
||||
public Item create(@RequestBody Map<String, String> 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<String, String> 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<String, Object> delete(@PathVariable long id) {
|
||||
store.remove(id);
|
||||
return Map.of("deleted", id);
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user