Skip to content

Commit

Permalink
feat(connection) : 커넥션 도메인 정의
Browse files Browse the repository at this point in the history
  • Loading branch information
waterfogSW committed Jan 12, 2025
1 parent 5b9072f commit a5a4a29
Show file tree
Hide file tree
Showing 5 changed files with 168 additions and 2 deletions.
Original file line number Diff line number Diff line change
@@ -1,13 +1,34 @@
package com.threedays.domain.connection.entity

import com.threedays.domain.user.entity.User
import com.threedays.support.common.base.domain.AggregateRoot
import com.threedays.support.common.base.domain.UUIDTypeId
import java.util.UUID
import java.time.LocalDateTime
import java.util.*

data class Connection(
override val id: Id,
): AggregateRoot<Connection, Connection.Id>() {
val participant1: Participant,
val participant2: Participant,
val connectedAt: LocalDateTime,
) : AggregateRoot<Connection, Connection.Id>() {

data class Id(override val value: UUID) : UUIDTypeId(value)

companion object {

fun match(
user1: User.Id,
user2: User.Id
): Connection {
return Connection(
id = UUIDTypeId.random(),
participant1 = Participant(user1),
participant2 = Participant(user2),
connectedAt = LocalDateTime.now()
)
}
}


}
Original file line number Diff line number Diff line change
@@ -0,0 +1,25 @@
package com.threedays.domain.connection.entity

import com.threedays.domain.user.entity.User
import com.threedays.support.common.base.domain.DomainEntity

/**
* 매칭에 참여하는 유저
* @param id: 유저의 ID
* @param connectionResponse: 매칭에 대한 응답
*/
data class Participant(
override val id: User.Id,
val connectionResponse: ConnectionResponse = ConnectionResponse.NO_RESPONSE
) : DomainEntity<Participant, User.Id>() {

/**
* 유저의 매칭에 대한 응답
*/
enum class ConnectionResponse {
NO_RESPONSE, // 응답하지 않음
KEEP_CONNECT, // 커넥션 유지
DISCONNECT, // 연결 끊기
}

}
Original file line number Diff line number Diff line change
@@ -0,0 +1,65 @@
package com.threedays.persistence.connection.entity

import com.threedays.domain.connection.entity.Connection
import jakarta.persistence.CascadeType
import jakarta.persistence.Column
import jakarta.persistence.Entity
import jakarta.persistence.FetchType
import jakarta.persistence.Id
import jakarta.persistence.OneToOne
import jakarta.persistence.Table
import java.time.LocalDateTime
import java.util.*

@Entity
@Table(name = "connection")
class ConnectionJpaEntity(
id: UUID,
participant1: ParticipantJpaEntity,
participant2: ParticipantJpaEntity,
connectedAt: LocalDateTime,
) {

@Id
var id: UUID = id
private set

@OneToOne(
fetch = FetchType.EAGER,
cascade = [CascadeType.ALL],
)
var participant1: ParticipantJpaEntity = participant1
private set

@OneToOne(
fetch = FetchType.EAGER,
cascade = [CascadeType.ALL],
)
var participant2: ParticipantJpaEntity = participant2
private set

@Column(name = "connected_at", nullable = false)
var connectedAt: LocalDateTime = connectedAt
private set

fun toDomain(): Connection {
return Connection(
id = Connection.Id(id),
participant1 = participant1.toDomain(),
participant2 = participant2.toDomain(),
connectedAt = connectedAt
)
}

companion object {

fun from(domain: Connection): ConnectionJpaEntity {
return ConnectionJpaEntity(
id = domain.id.value,
participant1 = ParticipantJpaEntity.from(domain.participant1),
participant2 = ParticipantJpaEntity.from(domain.participant2),
connectedAt = domain.connectedAt
)
}
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,39 @@
package com.threedays.persistence.connection.entity

import com.threedays.domain.connection.entity.Participant
import com.threedays.domain.user.entity.User
import jakarta.persistence.*
import java.util.*

@Entity
@Table(name = "participant")
class ParticipantJpaEntity(
id: UUID,
connectionResponse: Participant.ConnectionResponse
) {

@Id
var id: UUID = id
private set

@Column(name = "connection_response", nullable = false)
@Enumerated(EnumType.STRING)
var connectionResponse: Participant.ConnectionResponse = connectionResponse
private set

fun toDomain(): Participant {
return Participant(
id = User.Id(id),
connectionResponse = connectionResponse
)
}

companion object {
fun from(domain: Participant): ParticipantJpaEntity {
return ParticipantJpaEntity(
id = domain.id.value,
connectionResponse = domain.connectionResponse
)
}
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,16 @@
CREATE TABLE participant
(
id BINARY(16) PRIMARY KEY COMMENT '사용자 ID',
connection_response VARCHAR(20) NOT NULL COMMENT '연결 응답 (ACCEPTED, REJECTED, WAITING)'
);

CREATE TABLE connection
(
id BINARY(16) PRIMARY KEY COMMENT '커넥션 ID',
participant1_id BINARY(16) NOT NULL COMMENT '참가자1 ID',
participant2_id BINARY(16) NOT NULL COMMENT '참가자2 ID',
connected_at DATETIME(6) NOT NULL COMMENT '커넥션 생성 일시',

INDEX idx_connection_participant1_id (participant1_id),
INDEX idx_connection_participant2_id (participant2_id)
);

0 comments on commit a5a4a29

Please sign in to comment.