-
Notifications
You must be signed in to change notification settings - Fork 0
[WIP] CRUD 작성 중 #12
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
beingPracticer
wants to merge
1
commit into
review/java
Choose a base branch
from
aaron
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
[WIP] CRUD 작성 중 #12
Changes from all commits
Commits
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
35 changes: 35 additions & 0 deletions
35
src/main/java/com/example/javaweb/controller/BookController.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,35 @@ | ||
| package com.example.javaweb.controller; | ||
|
|
||
| import com.example.javaweb.model.Board; | ||
| import com.example.javaweb.service.BookService; | ||
| import org.springframework.beans.factory.annotation.Autowired; | ||
| import org.springframework.web.bind.annotation.*; | ||
| import java.util.List; | ||
|
|
||
| @RestController | ||
| @RequestMapping("/api/v1/books") | ||
| public class BookController { | ||
|
|
||
| @Autowired | ||
| BookService bookService; | ||
|
|
||
| @GetMapping | ||
| public List<Board> readBoard() { | ||
| return bookService.selectAll(); | ||
| } | ||
|
|
||
| @PostMapping | ||
| public Board createBoard(@RequestBody Board board) { | ||
| return bookService.create(board); | ||
| } | ||
|
|
||
| @PutMapping("/{id}") | ||
| public Board editBoard(@PathVariable Long id, @RequestBody Board updateData) { | ||
| return bookService.update(id, updateData); | ||
| } | ||
|
|
||
| @DeleteMapping("/{id}") | ||
| public void deleteBoard(@PathVariable Long id) { | ||
| bookService.delete(id); | ||
| } | ||
| } |
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,36 @@ | ||
| package com.example.javaweb.db; | ||
|
|
||
| import com.example.javaweb.model.Board; | ||
| import org.springframework.stereotype.Component; | ||
| import java.util.ArrayList; | ||
| import java.util.HashMap; | ||
| import java.util.List; | ||
| import java.util.Map; | ||
|
|
||
| @Component | ||
| public class Repository { | ||
| private Map<Long, Board> map = new HashMap<>(); | ||
| private Long idCount = 1L; | ||
|
|
||
| public Board save(Board board) { | ||
| map.put(idCount++, board); | ||
| return board; | ||
| } | ||
|
|
||
| public Board update(Long id, Board board) { | ||
| map.put(id, board); | ||
| return board; | ||
| } | ||
|
|
||
| public Board get(Long id) { | ||
| return map.get(id); | ||
| } | ||
|
|
||
| public void delete(Long id) { | ||
| map.remove(id); | ||
| } | ||
|
|
||
| public List<Board> getAll() { | ||
| return new ArrayList<>(map.values()); | ||
| } | ||
| } |
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,11 @@ | ||
| package com.example.javaweb.model; | ||
|
|
||
| import lombok.Data; | ||
|
|
||
| @Data | ||
| public class Board { | ||
| public Long id; | ||
| public String subject; | ||
| public String content; | ||
| public String writer; | ||
| } |
35 changes: 35 additions & 0 deletions
35
src/main/java/com/example/javaweb/service/BookService.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,35 @@ | ||
| package com.example.javaweb.service; | ||
|
|
||
| import com.example.javaweb.db.Repository; | ||
| import com.example.javaweb.model.Board; | ||
| import org.springframework.beans.factory.annotation.Autowired; | ||
| import org.springframework.stereotype.Service; | ||
|
|
||
| import java.util.List; | ||
|
|
||
| @Service | ||
| public class BookService { | ||
|
|
||
| @Autowired | ||
| private Repository repository; | ||
|
|
||
| public Board create(Board board) { | ||
| return repository.save(board); | ||
| } | ||
|
|
||
| public void delete(Long id) { | ||
| repository.delete(id); | ||
| } | ||
|
|
||
| public Board update(Long id, Board updateData) { | ||
| Board updatedBoard = repository.get(id); | ||
| if (updateData.content != null) updatedBoard.content = updateData.content; | ||
| if (updateData.subject != null) updatedBoard.subject = updateData.subject; | ||
| if (updateData.writer != null) updatedBoard.writer = updateData.writer; | ||
| return repository.update(id, updatedBoard); | ||
| } | ||
|
|
||
| public List<Board> selectAll() { | ||
| return repository.getAll(); | ||
| } | ||
| } | ||
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.
만약에 null check를 하셔야한다면 Optional을 써보시는건 어떨까요?