-
Notifications
You must be signed in to change notification settings - Fork 2
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
[하윤지] 6주차 과제 - complete #52
Changes from all commits
9e682e3
9a019e9
a654369
ae44339
4c2a04e
d116904
fda67e5
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
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;//해당 책을 좋아하는 이유 | ||
} |
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; | ||
} |
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); | ||
|
||
} | ||
|
||
} |
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(); | ||
} | ||
} |
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(); //값이 저장되었는지 확인하는 용도 | ||
} | ||
|
||
} |
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] | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. 패스워드 깃허브에 안 올린 것, 최고에요👍 |
||
driver-class-name: com.mysql.cj.jdbc.Driver | ||
# hibernate | ||
jpa: | ||
show-sql: true | ||
hibernate: | ||
ddl-auto: update | ||
dialect: org.hibernate.dialect.MySQL8Dialect |
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 | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. 해리포터를 좋아하는 당신은 진정한 문학인입니다. There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. 이번 주차도 피드백 남겨주셔서 감사합니다 >< 😍 |
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.
감동에 눈물 줄줄...🥹
이번 과제는 사실 안 하셔도 됐을 텐데 이렇게 하나하나 따라해 보신 것 너무 감동이에요🫶
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.
천재 혜승이가 준비한건데 당근 1부터 100까지 따라해야쥐!!