-
Notifications
You must be signed in to change notification settings - Fork 0
Board CRUD 기본 완성 입니다. #7
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Open
mingimin
wants to merge
4
commits into
review/java
Choose a base branch
from
alan
base: review/java
Could not load branches
Branch not found: {{ refName }}
Loading
Could not load tags
Nothing to show
Loading
Are you sure you want to change the base?
Some commits from the old base branch may be removed from the timeline,
and old review comments may become outdated.
Open
Changes from all commits
Commits
Show all changes
4 commits
Select commit
Hold shift + click to select a range
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
60 changes: 60 additions & 0 deletions
60
src/main/java/com/example/javaweb/controller/BoardsController.java
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -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<Board> create(@RequestBody HashMap<String, Object> map) { | ||
| return new ResponseEntity<>(boardService.insert(map), HttpStatus.CREATED); | ||
| } | ||
|
|
||
| @GetMapping("/boards/{id}") | ||
| public ResponseEntity<Board> 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<Board> update(@PathVariable int id, @RequestBody HashMap<String, Object> 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<Object> delete(@PathVariable int id) { | ||
| if (!boardService.delete(id)) { | ||
| throw new ResponseStatusException( | ||
| HttpStatus.NOT_FOUND, "board not found" | ||
| ); | ||
| } | ||
|
|
||
| return new ResponseEntity<>(HttpStatus.NO_CONTENT); | ||
| } | ||
| } |
60 changes: 60 additions & 0 deletions
60
src/main/java/com/example/javaweb/controller/UserController.java
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -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<User> create(@RequestBody HashMap<String, Object> map) { | ||
| return new ResponseEntity<>(userService.insert(map), HttpStatus.OK); | ||
| } | ||
|
|
||
| @GetMapping("/users/{id}") | ||
| public ResponseEntity<User> 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<User> update(@PathVariable int id, @RequestBody HashMap<String, Object> 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<Object> delete(@PathVariable int id) { | ||
| if (!userService.delete(id)) { | ||
| throw new ResponseStatusException( | ||
| HttpStatus.NOT_FOUND, "user not found" | ||
| ); | ||
| } | ||
|
|
||
| return new ResponseEntity<>(HttpStatus.NO_CONTENT); | ||
| } | ||
| } |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -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<String, Object> 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"); | ||
| } | ||
| } | ||
| } |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -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<Board> boards) { | ||
| this.id = id; | ||
| this.name = name; | ||
| this.boards = boards; | ||
| } | ||
|
|
||
| public List<Board> getBoards() { | ||
| return boards; | ||
| } | ||
|
|
||
| public void setBoards(List<Board> boards) { | ||
| this.boards = boards; | ||
| } | ||
|
|
||
| @OneToMany(mappedBy = "user", fetch = FetchType.LAZY) | ||
| @JsonIgnore | ||
| private List<Board> 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<String, Object> map) { | ||
| if (map.get("id") != null) { | ||
| this.id = (Long) map.get("id"); | ||
| } | ||
| if (map.get("name") != null) { | ||
| this.name = (String) map.get("name"); | ||
| } | ||
| } | ||
| } |
10 changes: 10 additions & 0 deletions
10
src/main/java/com/example/javaweb/repository/BoardRepository.java
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -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<Board, Long> { | ||
|
|
||
| } | ||
9 changes: 9 additions & 0 deletions
9
src/main/java/com/example/javaweb/repository/UserRepository.java
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -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<User, Long> { | ||
| } |
75 changes: 75 additions & 0 deletions
75
src/main/java/com/example/javaweb/service/BoardService.java
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -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<String, Object> map) { | ||
| final Integer id = (Integer) map.get("user_id"); | ||
| if (id == null) { | ||
| return null; | ||
| } | ||
|
|
||
| final Optional<User> 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<String, Object> map) { | ||
| final Integer user_id = (Integer) map.get("user_id"); | ||
| if (user_id == null) { | ||
| return null; | ||
| } | ||
|
|
||
| final Optional<User> 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<Board> 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()); | ||
| } | ||
| } |
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
빈으로 등록해서 쓰셨네요 굿