2024-04-09 09:51:57 +03:00

52 lines
1.5 KiB
Java

package com.example.demo;
import java.util.ArrayList;
import java.util.List;
import org.slf4j.LoggerFactory;
import org.springframework.web.bind.annotation.DeleteMapping;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.PutMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;
@RestController
@RequestMapping("/api")
public class ApiController {
private final Logger log = LoggerFactory.getLogger(ApiController.class);
List<UserInfo> objects = new ArrayList<>();
@GetMapping
public List<UserInfo> getAll() {
return objects;
}
@GetMapping("/{id}")
public UserInfo getId(@PathVariable(name = "id") int id) {
return objects.get(id);
}
@PostMapping
public UserInfo create(@RequestBody UserInfo UserInfo) {
objects.add(UserInfo);
return UserInfo;
}
@PutMapping("/{id}")
public UserInfo update(@PathVariable(name = "id") int id, @RequestBody UserInfo entity) {
objects.get(id);
objects.add(id, entity);
return entity;
}
@DeleteMapping("/{id}")
public UserInfo delete(@PathVariable(name = "id") int id) {
var data = objects.get(id);
objects.remove(id);
return data;
}
}