-
Notifications
You must be signed in to change notification settings - Fork 0
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 #46 from LeejuButU/refactor/repository
Spring Data Repository 적용
- Loading branch information
Showing
10 changed files
with
26 additions
and
218 deletions.
There are no files selected for viewing
43 changes: 3 additions & 40 deletions
43
src/main/java/LeejuButU/BidCycle/domain/bidHistory/repository/BidHistoryRepository.java
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 |
---|---|---|
@@ -1,45 +1,8 @@ | ||
package LeejuButU.BidCycle.domain.bidHistory.repository; | ||
|
||
import LeejuButU.BidCycle.domain.bidHistory.domain.BidHistory; | ||
import jakarta.persistence.EntityManager; | ||
import lombok.RequiredArgsConstructor; | ||
import org.springframework.stereotype.Repository; | ||
import org.springframework.data.jpa.repository.JpaRepository; | ||
|
||
import java.util.List; | ||
import java.util.Optional; | ||
public interface BidHistoryRepository extends JpaRepository<BidHistory, Long> { | ||
|
||
@Repository | ||
@RequiredArgsConstructor | ||
public class BidHistoryRepository { | ||
private final EntityManager entityManager; | ||
|
||
public Long save(BidHistory bidHistory) { | ||
entityManager.persist(bidHistory); | ||
return bidHistory.getBidHistoryId(); | ||
} | ||
|
||
public Optional<BidHistory> findById(Long bidHistoryId) { | ||
BidHistory bidHistory = entityManager.find(BidHistory.class, bidHistoryId); | ||
return Optional.ofNullable(bidHistory); | ||
} | ||
|
||
public List<BidHistory> findAll() { | ||
return entityManager | ||
.createQuery("select h from BidHistory h", BidHistory.class) | ||
.getResultList(); | ||
} | ||
|
||
public Long update(BidHistory bidHistory) { | ||
entityManager.merge(bidHistory); | ||
return bidHistory.getBidHistoryId(); | ||
} | ||
|
||
public void delete(BidHistory bidHistory) { | ||
entityManager.remove(bidHistory); | ||
} | ||
|
||
public void deleteById(Long bidHistoryId) { | ||
BidHistory history = findById(bidHistoryId).orElseThrow(IllegalArgumentException::new); | ||
entityManager.remove(history); | ||
} | ||
} | ||
} |
43 changes: 3 additions & 40 deletions
43
src/main/java/LeejuButU/BidCycle/domain/chat/message/repository/ChatMessageRepository.java
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 |
---|---|---|
@@ -1,44 +1,7 @@ | ||
package LeejuButU.BidCycle.domain.chat.message.repository; | ||
|
||
import LeejuButU.BidCycle.domain.chat.message.domain.ChatMessage; | ||
import jakarta.persistence.EntityManager; | ||
import lombok.RequiredArgsConstructor; | ||
import org.springframework.stereotype.Repository; | ||
import org.springframework.data.jpa.repository.JpaRepository; | ||
|
||
import java.util.List; | ||
import java.util.Optional; | ||
|
||
@Repository | ||
@RequiredArgsConstructor | ||
public class ChatMessageRepository { | ||
private final EntityManager em; | ||
|
||
public Long save(ChatMessage chatMessage) { | ||
em.persist(chatMessage); | ||
return chatMessage.getChatMessageId(); | ||
} | ||
|
||
public Optional<ChatMessage> findById(Long chatMessageId) { | ||
ChatMessage chatMessage = em.find(ChatMessage.class, chatMessageId); | ||
return Optional.ofNullable(chatMessage); | ||
} | ||
|
||
public List<ChatMessage> findAll() { | ||
return em.createQuery("select m from ChatMessage m", ChatMessage.class) | ||
.getResultList(); | ||
} | ||
|
||
public Long update(ChatMessage chatMessage) { | ||
em.merge(chatMessage); | ||
return chatMessage.getChatMessageId(); | ||
} | ||
|
||
public void delete(ChatMessage chatMessage) { | ||
em.remove(chatMessage); | ||
} | ||
|
||
public void deleteById(Long chatMessageId) { | ||
ChatMessage chatMessage = findById(chatMessageId).orElseThrow(IllegalArgumentException::new); | ||
em.remove(chatMessage); | ||
} | ||
} | ||
interface ChatMessageRepository extends JpaRepository<ChatMessage, Long> { | ||
} |
42 changes: 2 additions & 40 deletions
42
src/main/java/LeejuButU/BidCycle/domain/chat/room/repository/ChatRoomRepository.java
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 |
---|---|---|
@@ -1,44 +1,6 @@ | ||
package LeejuButU.BidCycle.domain.chat.room.repository; | ||
|
||
import LeejuButU.BidCycle.domain.chat.room.domain.ChatRoom; | ||
import jakarta.persistence.EntityManager; | ||
import lombok.RequiredArgsConstructor; | ||
import org.springframework.stereotype.Repository; | ||
import org.springframework.data.jpa.repository.JpaRepository; | ||
|
||
import java.util.List; | ||
import java.util.Optional; | ||
|
||
@Repository | ||
@RequiredArgsConstructor | ||
public class ChatRoomRepository { | ||
private final EntityManager em; | ||
|
||
public Long save(ChatRoom chatRoom) { | ||
em.persist(chatRoom); | ||
return chatRoom.getChatRoomId(); | ||
} | ||
|
||
public Optional<ChatRoom> findById(Long chatRoomId) { | ||
ChatRoom chatRoom = em.find(ChatRoom.class, chatRoomId); | ||
return Optional.ofNullable(chatRoom); | ||
} | ||
|
||
public List<ChatRoom> findAll() { | ||
return em.createQuery("select r from ChatRoom r", ChatRoom.class) | ||
.getResultList(); | ||
} | ||
|
||
public Long update(ChatRoom chatRoom) { | ||
em.merge(chatRoom); | ||
return chatRoom.getChatRoomId(); | ||
} | ||
|
||
public void delete(ChatRoom chatRoom) { | ||
em.remove(chatRoom); | ||
} | ||
|
||
public void deleteById(Long chatRoomId) { | ||
ChatRoom chatRoom = findById(chatRoomId).orElseThrow(IllegalArgumentException::new); | ||
em.remove(chatRoom); | ||
} | ||
} | ||
interface ChatRoomRepository extends JpaRepository<ChatRoom, Long> {} |
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
58 changes: 7 additions & 51 deletions
58
src/main/java/LeejuButU/BidCycle/domain/member/repository/MemberRepository.java
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 |
---|---|---|
@@ -1,57 +1,13 @@ | ||
package LeejuButU.BidCycle.domain.member.repository; | ||
// findByNickname | ||
|
||
import LeejuButU.BidCycle.domain.member.domain.Member; | ||
import jakarta.persistence.EntityManager; | ||
import lombok.RequiredArgsConstructor; | ||
import org.springframework.stereotype.Repository; | ||
import org.springframework.data.jpa.repository.JpaRepository; | ||
|
||
import java.util.List; | ||
import java.util.Optional; | ||
|
||
@Repository | ||
@RequiredArgsConstructor | ||
public class MemberRepository { | ||
|
||
private final EntityManager em; | ||
|
||
// CREATE | ||
public Long save(Member member) { | ||
em.persist(member); | ||
return member.getMemberId(); | ||
} | ||
|
||
// READ | ||
public Optional<Member> findById(Long memberId) { | ||
Member member = em.find(Member.class, memberId); | ||
return Optional.ofNullable(member); | ||
} | ||
|
||
public Optional<Member> findByNickname(String nickname) { | ||
String jpql = "SELECT m FROM Member m WHERE m.nickname = :nickname"; | ||
Member member = em.createQuery(jpql, Member.class) | ||
.setParameter("nickname", nickname) | ||
.getSingleResult(); | ||
return Optional.ofNullable(member); | ||
} | ||
|
||
public List<Member> findAll() { | ||
String jpql = "SELECT m FROM Member m"; | ||
return em.createQuery(jpql, Member.class) | ||
.getResultList(); | ||
} | ||
|
||
// UPDATE | ||
public void update(Member member){ | ||
em.merge(member); | ||
} | ||
|
||
// DELETE | ||
public void delete(Member member){ | ||
em.remove(member); | ||
} | ||
|
||
public void deleteById(Long memberId){ | ||
Member member = findById(memberId).orElseThrow(IllegalArgumentException::new); | ||
em.remove(member); | ||
} | ||
} | ||
public interface MemberRepository extends JpaRepository<Member, Long> { | ||
// 이제 find by Nickname 을 만들겡 | ||
// 여러가지 방식이 있긴한데, 그냥 간단하게 메서드 이름으로 만들어볼겡 | ||
Optional<Member> findByNickname(String nickname); // 왜 이럴깡 흠 | ||
} |
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
38 changes: 2 additions & 36 deletions
38
src/main/java/LeejuButU/BidCycle/domain/product/repository/ProductRepository.java
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 |
---|---|---|
@@ -1,40 +1,6 @@ | ||
package LeejuButU.BidCycle.domain.product.repository; | ||
|
||
import LeejuButU.BidCycle.domain.product.domain.Product; | ||
import jakarta.persistence.EntityManager; | ||
import lombok.RequiredArgsConstructor; | ||
import org.springframework.stereotype.Repository; | ||
import org.springframework.data.jpa.repository.JpaRepository; | ||
|
||
import java.util.List; | ||
import java.util.Optional; | ||
|
||
@Repository | ||
@RequiredArgsConstructor | ||
public class ProductRepository { | ||
|
||
private final EntityManager em; | ||
|
||
public Long save(Product product){ | ||
em.persist(product); | ||
return product.getProductId(); | ||
} | ||
|
||
public Optional<Product> findById(Long productId){ | ||
Product product = em.find(Product.class, productId); | ||
return Optional.ofNullable(product); | ||
} | ||
|
||
public List<Product> findAll(){ | ||
return em.createQuery("select m from Product m", Product.class) | ||
.getResultList(); | ||
} | ||
|
||
public void delete(Product product){ | ||
em.remove(product); | ||
} | ||
|
||
public void deleteById(Long productId){ | ||
Product product = findById(productId).orElseThrow(IllegalArgumentException::new); | ||
em.remove(product); | ||
} | ||
} | ||
public interface ProductRepository extends JpaRepository<Product, Long> { } |
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
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
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