|
|
@@ -0,0 +1,44 @@
|
|
|
+package com.mokasz.image_pool.restful.controller;
|
|
|
+
|
|
|
+import com.mokasz.image_pool.restful.entity.Pool;
|
|
|
+import org.slf4j.Logger;
|
|
|
+import org.slf4j.LoggerFactory;
|
|
|
+import org.springframework.beans.factory.annotation.Autowired;
|
|
|
+import org.springframework.http.HttpStatus;
|
|
|
+import org.springframework.jdbc.core.JdbcTemplate;
|
|
|
+import org.springframework.jdbc.support.GeneratedKeyHolder;
|
|
|
+import org.springframework.jdbc.support.KeyHolder;
|
|
|
+import org.springframework.web.bind.annotation.*;
|
|
|
+
|
|
|
+import java.sql.PreparedStatement;
|
|
|
+import java.sql.ResultSet;
|
|
|
+import java.util.List;
|
|
|
+
|
|
|
+@RestController
|
|
|
+@RequestMapping("/service/pool")
|
|
|
+public class PoolController {
|
|
|
+ private final Logger logger = LoggerFactory.getLogger(this.getClass());
|
|
|
+ @Autowired
|
|
|
+ private JdbcTemplate jdbcTemplate;
|
|
|
+
|
|
|
+ @ResponseStatus(HttpStatus.CREATED)
|
|
|
+ @PutMapping("/")
|
|
|
+ public Pool create (@RequestParam("name") String name) {
|
|
|
+ KeyHolder keyHolder = new GeneratedKeyHolder();
|
|
|
+ jdbcTemplate.update(con -> {
|
|
|
+ PreparedStatement ps = con.prepareStatement("INSERT INTO image_pool.pools (pool_description, created) VALUES(?, NOW())");
|
|
|
+ ps.setString(1, name);
|
|
|
+ return ps;
|
|
|
+ }, keyHolder);
|
|
|
+ Number key = keyHolder.getKey();
|
|
|
+ logger.info("{}", key);
|
|
|
+ return jdbcTemplate.queryForObject("SELECT * FROM image_pool.pools WHERE pool_id = ?", Pool.class, key.intValue());
|
|
|
+ }
|
|
|
+
|
|
|
+ @GetMapping("/")
|
|
|
+ public List<Pool> list () {
|
|
|
+ List<Pool> pools = jdbcTemplate.query("SELECT * FROM image_pool.pools",
|
|
|
+ (ResultSet rs, int num) -> new Pool(rs.getInt("pool_id"), rs.getString("pool_name"), rs.getTimestamp("created")));
|
|
|
+ return pools;
|
|
|
+ }
|
|
|
+}
|