-
Notifications
You must be signed in to change notification settings - Fork 2
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #52 from GDSC-Ewha-5th/HaYunji
[하윤지] 6주차 과제 - complete
- Loading branch information
Showing
7 changed files
with
154 additions
and
0 deletions.
There are no files selected for viewing
This file contains 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,18 @@ | ||
package ServerStudy6Cloud.ServerStudy6Cloud.Domain; | ||
|
||
import jakarta.persistence.*; | ||
import lombok.Getter; | ||
import lombok.Setter; | ||
|
||
@Entity | ||
@Setter @Getter | ||
@Table(name = "book_info") | ||
public class Book { | ||
@Id | ||
@GeneratedValue | ||
@Column(name = "book_id") | ||
private Long id; | ||
|
||
private String name;//책 이름 | ||
private String reason;//해당 책을 좋아하는 이유 | ||
} |
This file contains 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 ServerStudy6Cloud.ServerStudy6Cloud.Controller; | ||
|
||
import lombok.Getter; | ||
import lombok.Setter; | ||
|
||
@Getter @Setter | ||
public class BookForm { //폼에서 입력받은 필드를 그대로 적음 | ||
//책 이름, 책 좋아하는 이유 | ||
private String name; | ||
private String reason; | ||
} |
This file contains 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,52 @@ | ||
package ServerStudy6Cloud.ServerStudy6Cloud.Controller; | ||
|
||
import ServerStudy6Cloud.ServerStudy6Cloud.Domain.Book; | ||
import ServerStudy6Cloud.ServerStudy6Cloud.Service.RdsService; | ||
import lombok.RequiredArgsConstructor; | ||
import org.springframework.http.HttpStatus; | ||
import org.springframework.http.ResponseEntity; | ||
import org.springframework.stereotype.Controller; | ||
import org.springframework.ui.Model; | ||
import org.springframework.web.bind.annotation.GetMapping; | ||
import org.springframework.web.bind.annotation.PostMapping; | ||
import org.springframework.web.bind.annotation.RestController; | ||
|
||
import java.util.List; | ||
|
||
@RestController | ||
@RequiredArgsConstructor | ||
public class RdsController { | ||
private final RdsService rdsService; | ||
//AWS RDS에서 Book list를 가져오는 GetMapping | ||
// @GetMapping("/") | ||
// public String readDB(Model model){ | ||
// model.addAttribute("bookForm", new BookForm()); | ||
// model.addAttribute("books", rdsService.findBooks()); | ||
// return "index"; | ||
// } | ||
@GetMapping("/") | ||
public ResponseEntity<List<Book>> readDB(){ | ||
List<Book> bookList = rdsService.findBooks(); | ||
return new ResponseEntity<>(bookList, HttpStatus.OK); | ||
} | ||
|
||
//AWS RDS에 Book 객체를 저장하는 PostMapping | ||
// @PostMapping("/upload") | ||
// public String updateDB(BookForm form){ | ||
// Book book = new Book(); | ||
// book.setName(form.getName()); | ||
// book.setReason(form.getReason()); | ||
// rdsService.saveBook(book); | ||
// return "redirect:/"; | ||
// } | ||
@PostMapping("/upload") | ||
public ResponseEntity<Void> updateDB(BookForm form){ | ||
Book book = new Book(); | ||
book.setName(form.getName()); | ||
book.setReason(form.getReason()); | ||
rdsService.saveBook(book); | ||
return new ResponseEntity<>(HttpStatus.CREATED); | ||
|
||
} | ||
|
||
} |
This file contains 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,24 @@ | ||
package ServerStudy6Cloud.ServerStudy6Cloud.Repository; | ||
|
||
import ServerStudy6Cloud.ServerStudy6Cloud.Domain.Book; | ||
import jakarta.persistence.EntityManager; | ||
import lombok.RequiredArgsConstructor; | ||
import org.springframework.stereotype.Repository; | ||
|
||
import java.util.List; | ||
|
||
@Repository | ||
@RequiredArgsConstructor | ||
public class RdsRepository { | ||
private final EntityManager em; | ||
//DB에 새로운 책 저장하는 메서드 | ||
public void save(Book book){ | ||
em.persist(book); // book 객체 저장 | ||
} | ||
|
||
//DB에서 모든 책 리스트 가져오는 메서드 | ||
public List<Book> findAll(){ | ||
return em.createQuery("select b from Book b", Book.class) //JPQL 쿼리와 조회할 class | ||
.getResultList(); | ||
} | ||
} |
This file contains 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,28 @@ | ||
package ServerStudy6Cloud.ServerStudy6Cloud.Service; | ||
|
||
import ServerStudy6Cloud.ServerStudy6Cloud.Domain.Book; | ||
import ServerStudy6Cloud.ServerStudy6Cloud.Repository.RdsRepository; | ||
import lombok.RequiredArgsConstructor; | ||
import org.springframework.stereotype.Service; | ||
import org.springframework.transaction.annotation.Transactional; | ||
|
||
import java.util.List; | ||
|
||
@Service | ||
@Transactional | ||
@RequiredArgsConstructor //final이 있는 argument의 생성자를 자동으로 만들어준다. | ||
public class RdsService { | ||
//RdsRepository를 사용해 DB에 저장하는 로직 | ||
private final RdsRepository rdsRepository; | ||
|
||
@Transactional(readOnly = true) //위의 transactional을 override | ||
public List<Book> findBooks(){ | ||
return rdsRepository.findAll(); | ||
} | ||
|
||
public Long saveBook(Book book){ | ||
rdsRepository.save(book); | ||
return book.getId(); //값이 저장되었는지 확인하는 용도 | ||
} | ||
|
||
} |
This file contains 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,13 @@ | ||
# RDS | ||
spring: | ||
datasource: | ||
url: jdbc:mysql://gdsc-rds.cbsa892orvdn.ap-northeast-2.rds.amazonaws.com:3306/study6 | ||
username: admin | ||
password: [password] | ||
driver-class-name: com.mysql.cj.jdbc.Driver | ||
# hibernate | ||
jpa: | ||
show-sql: true | ||
hibernate: | ||
ddl-auto: update | ||
dialect: org.hibernate.dialect.MySQL8Dialect |
This file contains 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,8 @@ | ||
## 📖6주차 과제 | ||
좋아하는 책의 이름, 좋아하는 이유 입력 받고 DB에 저장하기 및 데이터 불러오기 | ||
1. Amazon RDS 환경 구축 | ||
2. 강의 영상 대로 코드 작성하기 | ||
3. postman을 이용해 API 테스트 | ||
|
||
## ✌️과제 인증 | ||
https://drive.google.com/file/d/1yfBd62XxBBkL7_C4YVLGPu3woH7yTbZY/view?usp=sharing |