Skip to content

Commit

Permalink
Merge pull request #52 from GDSC-Ewha-5th/HaYunji
Browse files Browse the repository at this point in the history
[하윤지] 6주차 과제 - complete
  • Loading branch information
yunji118 authored Dec 25, 2023
2 parents 74b5fb0 + fda67e5 commit e50e70f
Show file tree
Hide file tree
Showing 7 changed files with 154 additions and 0 deletions.
18 changes: 18 additions & 0 deletions 6주차 Server S-Day 과제/Book.java
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;//해당 책을 좋아하는 이유
}
11 changes: 11 additions & 0 deletions 6주차 Server S-Day 과제/BookForm.java
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;
}
52 changes: 52 additions & 0 deletions 6주차 Server S-Day 과제/RdsController.java
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);

}

}
24 changes: 24 additions & 0 deletions 6주차 Server S-Day 과제/RdsRepository.java
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();
}
}
28 changes: 28 additions & 0 deletions 6주차 Server S-Day 과제/RdsService.java
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(); //값이 저장되었는지 확인하는 용도
}

}
13 changes: 13 additions & 0 deletions 6주차 Server S-Day 과제/applicaion.yml
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
8 changes: 8 additions & 0 deletions 6주차 Server S-Day 과제/하윤지_6주차_과제.md
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

0 comments on commit e50e70f

Please sign in to comment.