Skip to content

Commit

Permalink
Merge branch 'feat/migrate-db'
Browse files Browse the repository at this point in the history
  • Loading branch information
ohksj77 committed Mar 14, 2024
2 parents 15681c5 + e5ee9dc commit 1a7b40b
Show file tree
Hide file tree
Showing 66 changed files with 761 additions and 566 deletions.
6 changes: 4 additions & 2 deletions backend/build.gradle
Original file line number Diff line number Diff line change
Expand Up @@ -61,6 +61,7 @@ dependencies {
implementation 'org.flywaydb:flyway-core'
implementation 'org.flywaydb:flyway-mysql'

implementation 'com.github.f4b6a3:ulid-creator:5.2.3'
implementation 'com.querydsl:querydsl-jpa:5.0.0:jakarta'
implementation 'org.springframework.cloud:spring-cloud-gcp-starter:1.2.5.RELEASE'
implementation 'org.springframework.cloud:spring-cloud-gcp-storage:1.2.5.RELEASE'
Expand All @@ -71,8 +72,8 @@ dependencies {
annotationProcessor "com.querydsl:querydsl-apt:${dependencyManagement.importedProperties['querydsl.version']}:jakarta"

compileOnly 'org.projectlombok:lombok'
runtimeOnly 'org.mariadb.jdbc:mariadb-java-client'
testRuntimeOnly 'com.h2database:h2'
runtimeOnly 'com.mysql:mysql-connector-j'
testRuntimeOnly 'com.mysql:mysql-connector-j'
runtimeOnly 'io.micrometer:micrometer-registry-prometheus'
annotationProcessor 'org.springframework.boot:spring-boot-configuration-processor'
annotationProcessor 'org.projectlombok:lombok'
Expand All @@ -91,6 +92,7 @@ dependencies {
testImplementation "org.testcontainers:junit-jupiter:1.19.7"
testImplementation "com.redis:testcontainers-redis:2.0.1"
testImplementation "org.testcontainers:rabbitmq:1.19.7"
testImplementation "org.testcontainers:mysql:1.17.2"
testCompileOnly 'org.projectlombok:lombok'
testAnnotationProcessor 'org.projectlombok:lombok'
}
Expand Down
2 changes: 1 addition & 1 deletion backend/docker-compose.yml
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@ version: "3"

services:
db:
image: mariadb:10
image: mysql
container_name: db
ports:
- 3306:3306
Expand Down

This file was deleted.

This file was deleted.

Original file line number Diff line number Diff line change
@@ -1,45 +1,29 @@
package com.twtw.backend.domain.friend.entity;

import com.github.f4b6a3.ulid.UlidCreator;
import com.twtw.backend.domain.friend.exception.InvalidFriendMemberException;
import com.twtw.backend.domain.member.entity.Member;
import com.twtw.backend.global.audit.AuditListener;
import com.twtw.backend.global.audit.Auditable;
import com.twtw.backend.global.audit.BaseTime;

import jakarta.persistence.Column;
import jakarta.persistence.Embedded;
import jakarta.persistence.Entity;
import jakarta.persistence.EntityListeners;
import jakarta.persistence.EnumType;
import jakarta.persistence.Enumerated;
import jakarta.persistence.FetchType;
import jakarta.persistence.GeneratedValue;
import jakarta.persistence.Id;
import jakarta.persistence.JoinColumn;
import jakarta.persistence.ManyToOne;

import lombok.AccessLevel;
import lombok.Builder;
import lombok.Getter;
import lombok.NoArgsConstructor;
import lombok.Setter;

import jakarta.persistence.*;
import lombok.*;
import org.hibernate.annotations.Where;

import java.time.LocalDateTime;
import java.util.UUID;

@Getter
@Entity
@EqualsAndHashCode(of = "id")
@Where(clause = "deleted_at is null and friend_status != 'EXPIRED'")
@EntityListeners(AuditListener.class)
@NoArgsConstructor(access = AccessLevel.PROTECTED)
public class Friend implements Auditable {

@Id
@GeneratedValue(generator = "uuid2")
@Column(name = "id", columnDefinition = "BINARY(16)")
private UUID id;
@Column(columnDefinition = "BINARY(16)")
private UUID id = UlidCreator.getMonotonicUlid().toUuid();

@ManyToOne(fetch = FetchType.LAZY)
@JoinColumn(columnDefinition = "BINARY(16)")
Expand Down Expand Up @@ -104,4 +88,8 @@ private boolean isRequestNotExpired() {
.getCreatedAt()
.isAfter(LocalDateTime.now().minusMinutes(30L)));
}

public boolean nicknameContains(final String nickname) {
return this.fromMember.nicknameContains(nickname) || this.toMember.nicknameContains(nickname);
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,13 @@
package com.twtw.backend.domain.friend.repository;

import com.twtw.backend.domain.friend.entity.Friend;
import org.springframework.stereotype.Repository;

import java.util.List;
import java.util.UUID;

@Repository
public interface FriendCommandRepository {
Friend save(final Friend friend);
List<Friend> findByMemberAndMemberNickname(final UUID memberId, final String nickname);
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,18 @@
package com.twtw.backend.domain.friend.repository;

import com.twtw.backend.domain.friend.entity.Friend;
import org.springframework.data.jpa.repository.JpaRepository;
import org.springframework.data.jpa.repository.Query;
import org.springframework.data.repository.query.Param;
import org.springframework.stereotype.Repository;

import java.util.List;
import java.util.UUID;

@Repository
public interface FriendCommandRepositoryImpl extends JpaRepository<Friend, UUID>, FriendCommandRepository {

@Query(value = "SELECT f.* FROM friend f JOIN member fm ON f.from_member_id = fm.id JOIN member tm ON f.to_member_id = tm.id WHERE f.friend_status = 'ACCEPTED' AND ((f.to_member_id = UNHEX(REPLACE(:memberId, '-', '')) AND MATCH(fm.nickname) AGAINST(:nickname IN BOOLEAN MODE)) OR (f.from_member_id = UNHEX(REPLACE(:memberId, '-', '')) AND MATCH(tm.nickname) AGAINST(:nickname IN BOOLEAN MODE)))", nativeQuery = true)
List<Friend> findByMemberAndMemberNickname(@Param("memberId") final UUID memberId, @Param("nickname") final String nickname);
Friend save(final Friend friend);
}
Original file line number Diff line number Diff line change
Expand Up @@ -3,19 +3,19 @@
import com.twtw.backend.domain.friend.entity.Friend;
import com.twtw.backend.domain.friend.entity.FriendStatus;
import com.twtw.backend.domain.member.entity.Member;
import org.springframework.stereotype.Repository;

import java.util.List;
import java.util.Optional;
import java.util.UUID;

@Repository
public interface FriendQueryRepository {
Optional<Friend> findByTwoMemberId(final UUID loginMemberId, final UUID memberId);

List<Friend> findByMember(final Member member);

List<Friend> findByMemberAndFriendStatus(final Member member, final FriendStatus friendStatus);

List<Friend> findByMemberAndMemberNickname(final Member member, final String nickname);

boolean existsByTwoMemberId(final UUID loginMemberId, final UUID memberId);
List<Friend> findByMemberAndMemberNicknameContaining(final UUID memberId, final String nickname);
}
Original file line number Diff line number Diff line change
@@ -1,22 +1,18 @@
package com.twtw.backend.domain.friend.repository;

import static com.twtw.backend.domain.friend.entity.QFriend.friend;

import com.querydsl.jpa.impl.JPAQueryFactory;
import com.twtw.backend.domain.friend.entity.Friend;
import com.twtw.backend.domain.friend.entity.FriendStatus;
import com.twtw.backend.domain.member.entity.Member;

import jakarta.persistence.LockModeType;

import lombok.RequiredArgsConstructor;

import org.springframework.stereotype.Repository;

import java.util.List;
import java.util.Optional;
import java.util.UUID;

import static com.twtw.backend.domain.friend.entity.QFriend.friend;

@Repository
@RequiredArgsConstructor
public class FriendQueryRepositoryImpl implements FriendQueryRepository {
Expand Down Expand Up @@ -63,48 +59,13 @@ public List<Friend> findByMemberAndFriendStatus(
}

@Override
public List<Friend> findByMemberAndMemberNickname(final Member member, final String nickname) {
return jpaQueryFactory
.selectFrom(friend)
public List<Friend> findByMemberAndMemberNicknameContaining(final UUID memberId, final String nickname) {
return jpaQueryFactory.selectFrom(friend)
.where(
friend.friendStatus
.eq(FriendStatus.ACCEPTED)
.and(
friend.toMember
.eq(member)
.and(
friend.fromMember.nickname
.containsIgnoreCase(nickname))
.or(
friend.fromMember
.eq(member)
.and(
friend.toMember.nickname
.containsIgnoreCase(
nickname)))))
(friend.friendStatus.eq(FriendStatus.ACCEPTED))
.and
(friend.toMember.id.eq(memberId).and(friend.fromMember.nickname.contains(nickname)))
.or(friend.fromMember.id.eq(memberId).and(friend.toMember.nickname.contains(nickname))))
.fetch();
}

@Override
public boolean existsByTwoMemberId(final UUID loginMemberId, final UUID memberId) {
return Optional.ofNullable(
jpaQueryFactory
.selectFrom(friend)
.setLockMode(LockModeType.PESSIMISTIC_READ)
.setHint("javax.persistence.lock.timeout", 3)
.where(
(friend.toMember
.id
.eq(loginMemberId)
.and(friend.fromMember.id.eq(memberId))
.or(
friend.fromMember
.id
.eq(loginMemberId)
.and(
friend.toMember.id.eq(
memberId)))))
.fetchFirst())
.isPresent();
}
}

This file was deleted.

Loading

0 comments on commit 1a7b40b

Please sign in to comment.