Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 1 addition & 0 deletions build.gradle
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,7 @@ repositories {

dependencies {
implementation 'org.springframework.boot:spring-boot-starter-web'
implementation 'org.projectlombok:lombok:1.18.12'
testImplementation('org.springframework.boot:spring-boot-starter-test') {
exclude group: 'org.junit.vintage', module: 'junit-vintage-engine'
}
Expand Down
35 changes: 35 additions & 0 deletions src/main/java/com/example/javaweb/controller/BookController.java
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);
}
}
36 changes: 36 additions & 0 deletions src/main/java/com/example/javaweb/db/Repository.java
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());
}
}
11 changes: 11 additions & 0 deletions src/main/java/com/example/javaweb/model/Board.java
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 src/main/java/com/example/javaweb/service/BookService.java
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;
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

만약에 null check를 하셔야한다면 Optional을 써보시는건 어떨까요?

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();
}
}