실제 게시판 구조를 만들어 보겠다.
board에서 필요한 component들은 아래와 같다.
위의 파일들은 기존의 user-management의 코드를 참고하여 만들어보겠다.
전에 만들어 준, src -> main -> webapp -> app -> board 에 위의 파일들을 만들어준다.
만들어 준 component들을 board.module에 declarations 해준다.
위의 component파일들의 실제 코드는 만들어준 데이터베이스와 front end를 연결시켜준 후, 구현해보겠다.
database와 front-end를 연결시켜주기 위해서는, class, repository interface, resource(controller) 파일이 필요하다.
(Service, DTO, Mapper)
package com.mycompany.myapp.domain;
import org.hibernate.annotations.Cache;
import org.hibernate.annotations.CacheConcurrencyStrategy;
import javax.persistence.*;
import java.io.Serializable;
import java.time.LocalDate;
@Entity
@Table(name = "board")
@Cache(usage = CacheConcurrencyStrategy.NONSTRICT_READ_WRITE)
public class Board implements Serializable {
private static final long serialVersionUID = 1L;
@Id
@GeneratedValue(strategy = GenerationType.IDENTITY)
private Long id;
@Column(name = "title")
private String title;
@Column(name = "contents")
private String contents;
@Column(name = "created_date")
private LocalDate createdDate;
public Long getId() {
return id;
}
public void setId(Long id) {
this.id = id;
}
public String getTitle() {
return title;
}
public Board title(String title) {
this.title = title;
return this;
}
public void setTitle(String title) {
this.title = title;
}
public String getContents() {
return contents;
}
public Board contents(String contents) {
this.contents = contents;
return this;
}
public void setContents(String contents) {
this.contents = contents;
}
public LocalDate getCreatedDate() {
return createdDate;
}
public Board createdDate(LocalDate createdDate) {
this.createdDate = createdDate;
return this;
}
public void setCreatedDate(LocalDate createdDate) {
this.createdDate = createdDate;
}
@Override
public boolean equals(Object o) {
if (this == o) {
return true;
}
if (!(o instanceof Board)) {
return false;
}
return id != null && id.equals(((Board) o).id);
}
@Override
public int hashCode() {
return 31;
}
@Override
public String toString() {
return "Board{" +
"id=" + getId() +
", title='" + getTitle() + "'" +
", contents='" + getContents() + "'" +
", createdDate='" + getCreatedDate() + "'" +
"}";
}
}
//BoardRepository
package com.mycompany.myapp.repository;
import com.mycompany.myapp.domain.Board;
import org.springframework.data.jpa.repository.JpaRepository;
import org.springframework.stereotype.Repository;
@SuppressWarnings("unused")
@Repository
public interface BoardRepository extends JpaRepository<Board, Long> {
}
//BoardResource
package com.mycompany.myapp.web.rest;
import com.mycompany.myapp.domain.Board;
import com.mycompany.myapp.repository.BoardRepository;
import com.mycompany.myapp.web.rest.errors.BadRequestAlertException;
import io.github.jhipster.web.util.HeaderUtil;
import io.github.jhipster.web.util.ResponseUtil;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.http.ResponseEntity;
import org.springframework.web.bind.annotation.*;
import java.net.URI;
import java.net.URISyntaxException;
import java.util.List;
import java.util.Optional;
@RestController
@RequestMapping("/api")
public class BoardResource {
private final Logger log = LoggerFactory.getLogger(BoardResource.class);
private static final String ENTITY_NAME = "board";
@Value("${jhipster.clientApp.name}")
private String applicationName;
private final BoardRepository boardRepository;
public BoardResource(BoardRepository boardRepository){
this.boardRepository = boardRepository;
}
/* create a board */
@PostMapping("/boards")
public ResponseEntity<Board> createBoard(@RequestBody Board board) throws URISyntaxException {
if(board.getId()!=null){
throw new BadRequestAlertException("A new Board cannot already have an ID", ENTITY_NAME, "idexists");
}
Board result = boardRepository.save(board);
return ResponseEntity.created(new URI("/api/boards/" + result.getId()))
.headers(HeaderUtil.createEntityCreationAlert(applicationName, false, ENTITY_NAME, result.getId().toString()))
.body(result);
}
/* read all boards */
@GetMapping("/boards")
public List<Board> getAllBoards(){
return boardRepository.findAll();
}
/* read 'id' board */
@GetMapping("/boards/{id}")
public ResponseEntity<Board> getBoard(@PathVariable Long id){
Optional<Board> board = boardRepository.findById(id);
return ResponseUtil.wrapOrNotFound(board);
}
/* update a board */
@PutMapping("/boards")
public ResponseEntity<Board> updateBook(@RequestBody Board board) throws URISyntaxException {
if(board.getId() == null){
throw new BadRequestAlertException("Invalid id", ENTITY_NAME, "idnull");
}
Board result = boardRepository.save(board);
return ResponseEntity.ok()
.headers(HeaderUtil.createEntityUpdateAlert(applicationName, false, ENTITY_NAME, board.getId().toString()))
.body(result);
}
/* delete a board */
@DeleteMapping("/boards/{id}")
public ResponseEntity<Void> deleteBoard(@PathVariable Long id){
boardRepository.deleteById(id);
return ResponseEntity.noContent().headers(HeaderUtil.createEntityDeletionAlert(applicationName, false, ENTITY_NAME, id.toString())).build();
}
}