diff --git a/build.gradle b/build.gradle index 4cb1340..75f7d65 100644 --- a/build.gradle +++ b/build.gradle @@ -13,6 +13,8 @@ repositories { } dependencies { + implementation('org.springframework.boot:spring-boot-starter-data-jpa') + runtimeOnly("mysql:mysql-connector-java") implementation 'org.springframework.boot:spring-boot-starter-web' testImplementation('org.springframework.boot:spring-boot-starter-test') { exclude group: 'org.junit.vintage', module: 'junit-vintage-engine' diff --git a/src/main/java/com/example/javaweb/controller/BoardsController.java b/src/main/java/com/example/javaweb/controller/BoardsController.java new file mode 100644 index 0000000..6c9ce2b --- /dev/null +++ b/src/main/java/com/example/javaweb/controller/BoardsController.java @@ -0,0 +1,60 @@ +package com.example.javaweb.controller; + +import com.example.javaweb.model.Board; +import com.example.javaweb.service.BoardService; +import org.springframework.http.HttpStatus; +import org.springframework.http.ResponseEntity; +import org.springframework.web.bind.annotation.*; +import org.springframework.web.server.ResponseStatusException; + +import java.util.HashMap; + +@RestController +public class BoardsController { + + private final BoardService boardService; + + public BoardsController(BoardService boardService) { + this.boardService = boardService; + } + + @PostMapping("/boards") + public ResponseEntity create(@RequestBody HashMap map) { + return new ResponseEntity<>(boardService.insert(map), HttpStatus.CREATED); + } + + @GetMapping("/boards/{id}") + public ResponseEntity find(@PathVariable long id) { + Board board = boardService.find(id); + if (board == null) { + throw new ResponseStatusException( + HttpStatus.NOT_FOUND, "board not found" + ); + } + + return new ResponseEntity<>(board, HttpStatus.OK); + } + + @PutMapping("/boards/{id}") + public ResponseEntity update(@PathVariable int id, @RequestBody HashMap map) { + Board board = boardService.update(id, map); + if (board == null) { + throw new ResponseStatusException( + HttpStatus.NOT_FOUND, "board not found" + ); + } + + return new ResponseEntity<>(board, HttpStatus.OK); + } + + @DeleteMapping("/boards/{id}") + public ResponseEntity delete(@PathVariable int id) { + if (!boardService.delete(id)) { + throw new ResponseStatusException( + HttpStatus.NOT_FOUND, "board not found" + ); + } + + return new ResponseEntity<>(HttpStatus.NO_CONTENT); + } +} diff --git a/src/main/java/com/example/javaweb/controller/UserController.java b/src/main/java/com/example/javaweb/controller/UserController.java new file mode 100644 index 0000000..55a538c --- /dev/null +++ b/src/main/java/com/example/javaweb/controller/UserController.java @@ -0,0 +1,60 @@ +package com.example.javaweb.controller; + +import com.example.javaweb.model.User; +import com.example.javaweb.service.UserService; +import org.springframework.http.HttpStatus; +import org.springframework.http.ResponseEntity; +import org.springframework.web.bind.annotation.*; +import org.springframework.web.server.ResponseStatusException; + +import java.util.HashMap; + +@RestController +public class UserController { + + private final UserService userService; + + public UserController(UserService userService) { + this.userService = userService; + } + + @PostMapping("/users") + public ResponseEntity create(@RequestBody HashMap map) { + return new ResponseEntity<>(userService.insert(map), HttpStatus.OK); + } + + @GetMapping("/users/{id}") + public ResponseEntity find(@PathVariable long id) { + User user = userService.find(id); + if (user == null) { + throw new ResponseStatusException( + HttpStatus.NOT_FOUND, "user not found" + ); + } + + return new ResponseEntity<>(user, HttpStatus.OK); + } + + @PutMapping("/users/{id}") + public ResponseEntity update(@PathVariable int id, @RequestBody HashMap map) { + User user = userService.update(id, map); + if (user == null) { + throw new ResponseStatusException( + HttpStatus.NOT_FOUND, "user not found" + ); + } + + return new ResponseEntity<>(user, HttpStatus.OK); + } + + @DeleteMapping("/users/{id}") + public ResponseEntity delete(@PathVariable int id) { + if (!userService.delete(id)) { + throw new ResponseStatusException( + HttpStatus.NOT_FOUND, "user not found" + ); + } + + return new ResponseEntity<>(HttpStatus.NO_CONTENT); + } +} diff --git a/src/main/java/com/example/javaweb/model/Board.java b/src/main/java/com/example/javaweb/model/Board.java new file mode 100644 index 0000000..e773ef1 --- /dev/null +++ b/src/main/java/com/example/javaweb/model/Board.java @@ -0,0 +1,85 @@ +package com.example.javaweb.model; + +import com.fasterxml.jackson.annotation.JsonIgnoreProperties; +import com.fasterxml.jackson.annotation.JsonProperty; + +import javax.persistence.*; +import java.io.Serializable; +import java.util.HashMap; + +@Entity +@Table(name = "boards") +public class Board implements Serializable { + + @Id + @GeneratedValue(strategy = GenerationType.IDENTITY) + @JsonProperty + private Long id = null; + @JsonProperty + private String title = null; + @JsonProperty + private String body = null; + + public User getUser() { + return user; + } + + public void setUser(User user) { + this.user = user; + } + + @ManyToOne(fetch = FetchType.LAZY) + @JoinColumn(name = "user_id") + @JsonIgnoreProperties(value = {"applications", "hibernateLazyInitializer"}) + private User user; + + public Long getId() { + return id; + } + public void setTitle(String title) { + this.title = title; + } + public void setBody(String body) { + this.body = body; + } + + public String getTitle() { + return title; + } + public String getBody() { + return body; + } + public void setId(Long id) { + this.id = id; + } + + public Board() {} + + public Board(Long id, String title, String body, User user) { + this.id = id; + this.title = title; + this.body = body; + this.user = user; + } + + public Board(Long id, String title, String body) { + this.id = id; + this.title = title; + this.body = body; + } + + public Board(HashMap map) { + if (map.get("id") != null) { + this.id = (Long) map.get("id"); + } + if (map.get("title") != null) { + this.title = (String) map.get("title"); + } + if (map.get("body") != null) { + this.body = (String) map.get("body"); + } + if (map.get("user") != null) { + this.user = (User) map.get("user"); + } + } +} diff --git a/src/main/java/com/example/javaweb/model/User.java b/src/main/java/com/example/javaweb/model/User.java new file mode 100644 index 0000000..e79fd7c --- /dev/null +++ b/src/main/java/com/example/javaweb/model/User.java @@ -0,0 +1,71 @@ +package com.example.javaweb.model; + +import com.fasterxml.jackson.annotation.JsonIgnore; +import com.fasterxml.jackson.annotation.JsonProperty; + +import javax.persistence.*; +import java.io.Serializable; +import java.util.HashMap; +import java.util.List; + +@Entity +@Table(name = "users") +public class User implements Serializable { + + @Id + @GeneratedValue(strategy = GenerationType.IDENTITY) + @JsonProperty + private Long id = null; + @JsonProperty + private String name = null; + + public User(Long id, String name, List boards) { + this.id = id; + this.name = name; + this.boards = boards; + } + + public List getBoards() { + return boards; + } + + public void setBoards(List boards) { + this.boards = boards; + } + + @OneToMany(mappedBy = "user", fetch = FetchType.LAZY) + @JsonIgnore + private List boards; + + public Long getId() { + return id; + } + + public String getName() { + return name; + } + + public void setName(String name) { + this.name = name; + } + + public void setId(Long id) { + this.id = id; + } + + public User(Long id, String name) { + this.id = id; + this.name = name; + } + + public User() {} + + public User(HashMap map) { + if (map.get("id") != null) { + this.id = (Long) map.get("id"); + } + if (map.get("name") != null) { + this.name = (String) map.get("name"); + } + } +} diff --git a/src/main/java/com/example/javaweb/repository/BoardRepository.java b/src/main/java/com/example/javaweb/repository/BoardRepository.java new file mode 100644 index 0000000..3793213 --- /dev/null +++ b/src/main/java/com/example/javaweb/repository/BoardRepository.java @@ -0,0 +1,10 @@ +package com.example.javaweb.repository; + +import com.example.javaweb.model.Board; +import org.springframework.data.jpa.repository.JpaRepository; +import org.springframework.stereotype.Repository; + +@Repository +public interface BoardRepository extends JpaRepository { + +} diff --git a/src/main/java/com/example/javaweb/repository/UserRepository.java b/src/main/java/com/example/javaweb/repository/UserRepository.java new file mode 100644 index 0000000..b58b079 --- /dev/null +++ b/src/main/java/com/example/javaweb/repository/UserRepository.java @@ -0,0 +1,9 @@ +package com.example.javaweb.repository; + +import com.example.javaweb.model.User; +import org.springframework.data.jpa.repository.JpaRepository; +import org.springframework.stereotype.Repository; + +@Repository +public interface UserRepository extends JpaRepository { +} diff --git a/src/main/java/com/example/javaweb/service/BoardService.java b/src/main/java/com/example/javaweb/service/BoardService.java new file mode 100644 index 0000000..c0cb7d6 --- /dev/null +++ b/src/main/java/com/example/javaweb/service/BoardService.java @@ -0,0 +1,75 @@ +package com.example.javaweb.service; + +import com.example.javaweb.model.Board; +import com.example.javaweb.model.User; +import com.example.javaweb.repository.BoardRepository; +import com.example.javaweb.repository.UserRepository; +import org.springframework.stereotype.Service; + +import java.util.HashMap; +import java.util.Optional; + +@Service +public class BoardService { + final private BoardRepository boardRepository; + final private UserRepository userRepository; + + public BoardService(BoardRepository boardRepository, UserRepository userRepository) { + this.boardRepository = boardRepository; + this.userRepository = userRepository; + } + + public Board find(long id) { + return boardRepository.findById(id).orElse(null); + } + + public Board insert(HashMap map) { + final Integer id = (Integer) map.get("user_id"); + if (id == null) { + return null; + } + + final Optional user = userRepository.findById(Long.valueOf(id)); + if (user.isEmpty()) { + return null; + } + + map.put("user", user.get()); + final Board board = new Board(map); + return boardRepository.save(board); + } + + public boolean delete(long id) { + if (!boardRepository.existsById(id)) { + return false; + } + + boardRepository.deleteById(id); + return true; + } + + public Board update(long id, HashMap map) { + final Integer user_id = (Integer) map.get("user_id"); + if (user_id == null) { + return null; + } + + final Optional user = userRepository.findById(Long.valueOf(user_id)); + if (user.isEmpty()) { + return null; + } + + map.put("user", user.get()); + final Board updateBoard = new Board(map); + final Optional savedBoard = boardRepository.findById(id); + if (savedBoard.isEmpty()) { + return null; + } + + savedBoard.get().setTitle(updateBoard.getTitle()); + savedBoard.get().setBody(updateBoard.getBody()); + savedBoard.get().setUser(updateBoard.getUser()); + + return boardRepository.save(savedBoard.get()); + } +} diff --git a/src/main/java/com/example/javaweb/service/UserService.java b/src/main/java/com/example/javaweb/service/UserService.java new file mode 100644 index 0000000..6d47dc5 --- /dev/null +++ b/src/main/java/com/example/javaweb/service/UserService.java @@ -0,0 +1,49 @@ +package com.example.javaweb.service; + +import com.example.javaweb.model.User; +import com.example.javaweb.repository.BoardRepository; +import com.example.javaweb.repository.UserRepository; +import org.springframework.stereotype.Service; + +import java.util.HashMap; +import java.util.Optional; + +@Service +public class UserService { + + final private UserRepository userRepository; + + public UserService(UserRepository userRepository) { + this.userRepository = userRepository; + } + + public User insert(HashMap map) { + User user = new User(map); + return userRepository.save(user); + } + + public User find(long id) { + return userRepository.findById(id).orElse(null); + } + + public boolean delete(long id) { + if (!userRepository.existsById(id)) { + return false; + } + + userRepository.deleteById(id); + return true; + } + + public User update(long id, HashMap map) { + final User updateUser = new User(map); + final Optional savedUser = userRepository.findById(id); + if (savedUser.isEmpty()) { + return null; + } + + savedUser.get().setName(updateUser.getName()); + + return userRepository.save(savedUser.get()); + } +} diff --git a/src/main/resources/application.properties b/src/main/resources/application.properties deleted file mode 100644 index 8b13789..0000000 --- a/src/main/resources/application.properties +++ /dev/null @@ -1 +0,0 @@ - diff --git a/src/main/resources/application.yml b/src/main/resources/application.yml new file mode 100644 index 0000000..4576483 --- /dev/null +++ b/src/main/resources/application.yml @@ -0,0 +1,18 @@ +spring: + datasource: + driver-class-name: com.mysql.cj.jdbc.Driver + url: jdbc:mysql://127.0.0.1:3308/javaweb?serverTimezone=UTC&characterEncoding=UTF-8 + username: root + hikari: + maximum-pool-size: 50 + minimum-idle: 10 + idle-timeout: 30000 + connection-test-query: "SELECT 1" + jpa: + show-sql: true + database-platform: org.hibernate.dialect.MySQL5InnoDBDialect + hibernate: + ddl-auto: update + properties: + hibernate: + format_sql: true \ No newline at end of file