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
2 changes: 2 additions & 0 deletions build.gradle
Original file line number Diff line number Diff line change
Expand Up @@ -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'
Expand Down
60 changes: 60 additions & 0 deletions src/main/java/com/example/javaweb/controller/BoardsController.java
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 src/main/java/com/example/javaweb/controller/UserController.java
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);
}
}
85 changes: 85 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,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");
}
}
}
71 changes: 71 additions & 0 deletions src/main/java/com/example/javaweb/model/User.java
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 src/main/java/com/example/javaweb/repository/BoardRepository.java
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
Copy link
Member

Choose a reason for hiding this comment

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

빈으로 등록해서 쓰셨네요 굿

public interface BoardRepository extends JpaRepository<Board, Long> {

}
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 src/main/java/com/example/javaweb/service/BoardService.java
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());
}
}
Loading