-
Notifications
You must be signed in to change notification settings - Fork 1
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
challenge rank persistent layer 구현 (#217)
## Checklist - [ ] 충분한 양의 자동화 테스트를 작성했는가? - 계단정복지도 서비스는 사이드 프로젝트로 진행되는 만큼 충분한 QA 없이 배포되는 경우가 많습니다. 따라서 자동화 테스트를 꼼꼼하게 작성하는 것이 서비스 품질을 유지하는 데 매우 중요합니다.
- Loading branch information
1 parent
1a13f2f
commit ada091d
Showing
5 changed files
with
192 additions
and
2 deletions.
There are no files selected for viewing
82 changes: 82 additions & 0 deletions
82
...tlin/club/staircrusher/challenge/infra/adapter/out/persistence/ChallengeRankRepository.kt
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 |
---|---|---|
@@ -0,0 +1,82 @@ | ||
package club.staircrusher.challenge.infra.adapter.out.persistence | ||
|
||
import club.staircrusher.challenge.application.port.out.persistence.ChallengeRankRepository | ||
import club.staircrusher.challenge.domain.model.ChallengeRank | ||
import club.staircrusher.challenge.infra.adapter.out.persistence.sqldelight.toDomainModel | ||
import club.staircrusher.challenge.infra.adapter.out.persistence.sqldelight.toPersistenceModel | ||
import club.staircrusher.infra.persistence.sqldelight.DB | ||
import club.staircrusher.stdlib.di.annotation.Component | ||
|
||
@Component | ||
class ChallengeRankRepository( | ||
private val db: DB | ||
): ChallengeRankRepository { | ||
private val queries = db.challengeRankQueries | ||
|
||
override fun findTopNUsers(challengeId: String, n: Int): List<ChallengeRank> { | ||
return queries.findTopNUsers(challengeId, n.toLong()) | ||
.executeAsList() | ||
.map { it.toDomainModel() } | ||
} | ||
|
||
override fun findByUserId(challengeId: String, userId: String): ChallengeRank? { | ||
return queries.findByUserId(challengeId, userId) | ||
.executeAsOneOrNull() | ||
?.toDomainModel() | ||
} | ||
|
||
override fun findByRank(challengeId: String, rank: Long): ChallengeRank? { | ||
return queries.findByRank(challengeId, rank) | ||
.executeAsOneOrNull() | ||
?.toDomainModel() | ||
} | ||
|
||
override fun findNextRank(challengeId: String, rank: Long): ChallengeRank? { | ||
return queries.findNextRank(challengeId, rank) | ||
.executeAsOneOrNull() | ||
?.toDomainModel() | ||
} | ||
|
||
override fun findLastRank(challengeId: String): Long? { | ||
return queries.findLastRank(challengeId).executeAsOneOrNull()?.max | ||
} | ||
|
||
override fun findByContributionCount(challengeId: String, contributionCount: Int): ChallengeRank? { | ||
return queries.findByContributionCount(challengeId, contributionCount) | ||
.executeAsOneOrNull() | ||
?.toDomainModel() | ||
} | ||
|
||
override fun findAll(challengeId: String): List<ChallengeRank> { | ||
return queries.findAll(challengeId) | ||
.executeAsList() | ||
.map { it.toDomainModel() } | ||
} | ||
|
||
override fun save(entity: ChallengeRank): ChallengeRank { | ||
queries.save(entity.toPersistenceModel()) | ||
return entity | ||
} | ||
|
||
override fun saveAll(entities: Collection<ChallengeRank>) { | ||
entities.forEach { save(it) } | ||
} | ||
|
||
override fun removeAll() { | ||
queries.removeAll() | ||
} | ||
|
||
override fun removeAll(challengeId: String) { | ||
queries.removeAllByChallengeId(challengeId) | ||
} | ||
|
||
override fun findById(id: String): ChallengeRank { | ||
return findByIdOrNull(id) ?: throw IllegalArgumentException("ChallengeRank of id $id does not exist.") | ||
} | ||
|
||
override fun findByIdOrNull(id: String): ChallengeRank? { | ||
return queries.findById(id) | ||
.executeAsOneOrNull() | ||
?.toDomainModel() | ||
} | ||
} |
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
14 changes: 14 additions & 0 deletions
14
...qldelight/club/staircrusher/infra/persistence/sqldelight/migration/V12_challenge_rank.sqm
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 |
---|---|---|
@@ -0,0 +1,14 @@ | ||
CREATE TABLE IF NOT EXISTS challenge_rank ( | ||
id VARCHAR(36) NOT NULL, | ||
challenge_id VARCHAR(36) NOT NULL, | ||
user_id VARCHAR(36) NOT NULL, | ||
contribution_count INT NOT NULL, | ||
rank BIGINT NOT NULL, | ||
created_at TIMESTAMP(6) WITH TIME ZONE NOT NULL DEFAULT CURRENT_TIMESTAMP, | ||
updated_at TIMESTAMP(6) WITH TIME ZONE NOT NULL DEFAULT CURRENT_TIMESTAMP, | ||
PRIMARY KEY (id) | ||
); | ||
|
||
CREATE INDEX idx_challenge_rank_1 ON challenge_rank(challenge_id, user_id); | ||
CREATE INDEX idx_challenge_rank_2 ON challenge_rank(challenge_id, rank); | ||
CREATE INDEX idx_challenge_rank_3 ON challenge_rank(challenge_id, contribution_count); |
68 changes: 68 additions & 0 deletions
68
...qldelight/club/staircrusher/infra/persistence/sqldelight/query/challenge/ChallengeRank.sq
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 |
---|---|---|
@@ -0,0 +1,68 @@ | ||
save: | ||
INSERT INTO challenge_rank | ||
VALUES :challenge_rank | ||
ON CONFLICT(id) DO UPDATE SET | ||
contribution_count = EXCLUDED.contribution_count, | ||
rank = EXCLUDED.rank, | ||
updated_at = EXCLUDED.updated_at; | ||
|
||
removeAll: | ||
DELETE FROM challenge_rank; | ||
|
||
findById: | ||
SELECT * | ||
FROM challenge_rank | ||
WHERE challenge_rank.id = :id; | ||
|
||
findByUserId: | ||
SELECT * | ||
FROM challenge_rank | ||
WHERE | ||
challenge_rank.challenge_id = :challenge_id AND | ||
challenge_rank.user_id = :user_id; | ||
|
||
findTopNUsers: | ||
SELECT * | ||
FROM challenge_rank | ||
WHERE challenge_rank.challenge_id = :challenge_id | ||
ORDER BY challenge_rank.rank ASC | ||
LIMIT :n; | ||
|
||
findByRank: | ||
SELECT * | ||
FROM challenge_rank | ||
WHERE | ||
challenge_rank.challenge_id = :challenge_id AND | ||
challenge_rank.rank = :rank | ||
LIMIT 1; | ||
|
||
findNextRank: | ||
SELECT * | ||
FROM challenge_rank | ||
WHERE | ||
challenge_rank.challenge_id = :challenge_id AND | ||
challenge_rank.rank < :rank | ||
ORDER BY challenge_rank.rank DESC | ||
LIMIT 1; | ||
|
||
findLastRank: | ||
SELECT max(challenge_rank.rank) | ||
FROM challenge_rank | ||
WHERE challenge_rank.challenge_id = :challenge_id; | ||
|
||
findAll: | ||
SELECT * | ||
FROM challenge_rank | ||
WHERE challenge_rank.challenge_id = :challenge_id; | ||
|
||
findByContributionCount: | ||
SELECT * | ||
FROM challenge_rank | ||
WHERE | ||
challenge_rank.challenge_id = :challenge_id AND | ||
challenge_rank.contribution_count = :contribution_count | ||
LIMIT 1; | ||
|
||
removeAllByChallengeId: | ||
DELETE FROM challenge_rank | ||
WHERE challenge_rank.challenge_id = :challenge_id; |