Skip to content
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 #57

Merged
merged 1 commit into from
Dec 26, 2023
Merged
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
18 changes: 18 additions & 0 deletions 6주차 Server S-Day 과제/LeeSeohyun/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
@Getter @Setter
@Table(name= "book_info")
public class Book {
@Id
@GeneratedValue
@Column(name="book_id")
private Long id;

private String name; //책 이름
private String reason; //해당 책을 좋아하는 이유
}
12 changes: 12 additions & 0 deletions 6주차 Server S-Day 과제/LeeSeohyun/BookForm.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,12 @@
package ServerStudy6Cloud.ServerStudy6Cloud.Controller;

import lombok.Getter;
import lombok.Setter;

//BookForm: 폼에서 입력받은 내용과 entity에 넣을 내용 분리 위해 만든 클래스
@Getter @Setter
public class BookForm {
//책 이름, 책 좋아하는 이유
private String name;
private String reason;
}
54 changes: 54 additions & 0 deletions 6주차 Server S-Day 과제/LeeSeohyun/RdsController.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,54 @@
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.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RestController;

import java.util.List;

@RestController //API 만들기
@RequiredArgsConstructor
public class RdsController {
private final RdsService rdsService;
//AWS RDS에서 Book list를 가져오는 GetMapping

@GetMapping("/")
public ResponseEntity<List<Book>> readDB(){
List<Book> bookList = rdsService.findBooks();
return new ResponseEntity<>(bookList, HttpStatus.OK);
}

@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);
}


//AWS RDS에 Book 객체를 저장하는 PostMapping
}

//@Controller에서 view에 리턴할 때의 컨트롤러 코드
/*@GetMapping("/")
public String readDB(Model model){
model.addAttribute("bookForm", new BookForm()); //BookForm 객체 넘기기
model.addAttribute("books", rdsService.findBooks());
return "index";
}
@PostMapping("/upload")
public String updateDB(BookForm form){
Book book = new Book();
book.setName(form.getName());
book.setReason(form.getReason());
rdsService.saveBook(book);
return "redirect:/";
}
*/
Comment on lines +39 to +54
Copy link
Member

Choose a reason for hiding this comment

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

강의의 과정을 하나하나 따라해주신 게 보여서 감동적이에요...!🥹

23 changes: 23 additions & 0 deletions 6주차 Server S-Day 과제/LeeSeohyun/RdsRepository.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,23 @@
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);
}
//DB에서 모든 책 리스트 가져오는 메서드
public List<Book> findAll() {
return em.createQuery("select b from Book b", Book.class) //JPQL 쿼리와 조회할 class
.getResultList();
}
}
27 changes: 27 additions & 0 deletions 6주차 Server S-Day 과제/LeeSeohyun/RdsService.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,27 @@
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
public class RdsService {
//RdsRepository를 사용해 DB에 저장하는 로직
private final RdsRepository rdsRepository; //레포지토리 주입받음

@Transactional(readOnly=true) //조회에 최적화되게 Transaction 사용가능
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 과제/LeeSeohyun/application.yml
Original file line number Diff line number Diff line change
@@ -0,0 +1,13 @@
# RDS
spring:
datasource:
url: jdbc:mysql://gdsc-rds2.cf0i42aimwar.ap-northeast-2.rds.amazonaws.com:3306/study_6
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