-
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 #19 from Lukinhasssss/feature/video-gateway
Automated PR from feature/video-gateway
- Loading branch information
Showing
8 changed files
with
909 additions
and
14 deletions.
There are no files selected for viewing
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
77 changes: 77 additions & 0 deletions
77
...c/main/kotlin/com/lukinhasssss/catalogo/infrastructure/video/VideoElasticsearchGateway.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,77 @@ | ||
package com.lukinhasssss.catalogo.infrastructure.video | ||
|
||
import com.lukinhasssss.catalogo.domain.pagination.Pagination | ||
import com.lukinhasssss.catalogo.domain.video.Video | ||
import com.lukinhasssss.catalogo.domain.video.VideoGateway | ||
import com.lukinhasssss.catalogo.domain.video.VideoSearchQuery | ||
import com.lukinhasssss.catalogo.infrastructure.video.VideoQueryBuilder.Companion.containingCastMembers | ||
import com.lukinhasssss.catalogo.infrastructure.video.VideoQueryBuilder.Companion.containingCategories | ||
import com.lukinhasssss.catalogo.infrastructure.video.VideoQueryBuilder.Companion.containingGenres | ||
import com.lukinhasssss.catalogo.infrastructure.video.VideoQueryBuilder.Companion.launchedAtEquals | ||
import com.lukinhasssss.catalogo.infrastructure.video.VideoQueryBuilder.Companion.onlyPublished | ||
import com.lukinhasssss.catalogo.infrastructure.video.VideoQueryBuilder.Companion.ratingEquals | ||
import com.lukinhasssss.catalogo.infrastructure.video.VideoQueryBuilder.Companion.titleOrDescriptionContaining | ||
import com.lukinhasssss.catalogo.infrastructure.video.persistence.VideoDocument | ||
import com.lukinhasssss.catalogo.infrastructure.video.persistence.VideoRepository | ||
import org.springframework.context.annotation.Profile | ||
import org.springframework.data.domain.PageRequest | ||
import org.springframework.data.domain.Sort | ||
import org.springframework.data.domain.Sort.Direction | ||
import org.springframework.data.elasticsearch.client.elc.NativeQuery | ||
import org.springframework.data.elasticsearch.core.SearchOperations | ||
import org.springframework.data.elasticsearch.core.search | ||
import org.springframework.stereotype.Component | ||
|
||
@Component | ||
@Profile("!development") | ||
class VideoElasticsearchGateway( | ||
private val videoRepository: VideoRepository, | ||
private val searchOperations: SearchOperations | ||
) : VideoGateway { | ||
|
||
private companion object { | ||
const val TITLE_PROP = "title" | ||
const val KEYWORD = ".keyword" | ||
} | ||
|
||
override fun save(video: Video): Video { | ||
videoRepository.save(VideoDocument.from(video)) | ||
return video | ||
} | ||
|
||
override fun findById(id: String): Video? = | ||
if (id.isBlank()) null else videoRepository.findById(id).map { it.toVideo() }.orElse(null) | ||
|
||
override fun findAll(aQuery: VideoSearchQuery): Pagination<Video> = with(aQuery) { | ||
val aQueryBuilder = VideoQueryBuilder( | ||
onlyPublished(), | ||
containingCategories(categories), | ||
containingCastMembers(castMembers), | ||
containingGenres(genres), | ||
launchedAtEquals(launchedAt), | ||
ratingEquals(rating), | ||
titleOrDescriptionContaining(terms) | ||
).build() | ||
|
||
val query = NativeQuery.builder() | ||
.withQuery(aQueryBuilder) | ||
.withPageable(PageRequest.of(page, perPage, Sort.by(Direction.fromString(direction), buildSort(sort)))) | ||
.build() | ||
|
||
val result = searchOperations.search<VideoDocument>(query) | ||
|
||
val total = result.totalHits | ||
val videos = result.map { it.content.toVideo() }.toList() | ||
|
||
Pagination( | ||
currentPage = page, | ||
perPage = perPage, | ||
total = total, | ||
items = videos | ||
) | ||
} | ||
|
||
override fun deleteById(id: String) = if (id.isBlank()) Unit else videoRepository.deleteById(id) | ||
|
||
private fun buildSort(sort: String) = if (TITLE_PROP.equals(sort, ignoreCase = true)) sort.plus(KEYWORD) else sort | ||
} |
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
79 changes: 79 additions & 0 deletions
79
...cture/src/main/kotlin/com/lukinhasssss/catalogo/infrastructure/video/VideoQueryBuilder.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,79 @@ | ||
package com.lukinhasssss.catalogo.infrastructure.video | ||
|
||
import co.elastic.clients.elasticsearch._types.FieldValue | ||
import co.elastic.clients.elasticsearch._types.query_dsl.Query | ||
import co.elastic.clients.elasticsearch._types.query_dsl.QueryBuilders | ||
import com.lukinhasssss.catalogo.infrastructure.video.VideoQueryBuilder.Option | ||
|
||
class VideoQueryBuilder(vararg opts: Option) { | ||
private val must: MutableList<Query> = mutableListOf() | ||
|
||
init { | ||
opts.forEach { it.invoke(this) } | ||
} | ||
|
||
fun must(aQuery: Query): VideoQueryBuilder { | ||
must.add(aQuery) | ||
return this | ||
} | ||
|
||
fun build(): Query = QueryBuilders.bool { b -> b.must(must) } | ||
|
||
fun interface Option : (VideoQueryBuilder) -> Unit | ||
|
||
companion object { | ||
private val NOOP: Option = Option { } | ||
|
||
fun onlyPublished(): Option = Option { b -> b.must(QueryBuilders.term { t -> t.field("published").value(true) }) } | ||
|
||
fun containingCategories(categories: Set<String>): Option { | ||
if (categories.isEmpty()) { | ||
return NOOP | ||
} | ||
|
||
return Option { b -> b.must(QueryBuilders.terms { t -> t.field("categories").terms { it.value(fieldValues(categories)) } }) } | ||
} | ||
|
||
fun containingCastMembers(members: Set<String>): Option { | ||
if (members.isEmpty()) { | ||
return NOOP | ||
} | ||
|
||
return Option { b -> b.must(QueryBuilders.terms { t -> t.field("cast_members").terms { it.value(fieldValues(members)) } }) } | ||
} | ||
|
||
fun containingGenres(genres: Set<String>): Option { | ||
if (genres.isEmpty()) { | ||
return NOOP | ||
} | ||
|
||
return Option { b -> b.must(QueryBuilders.terms { t -> t.field("genres").terms { it.value(fieldValues(genres)) } }) } | ||
} | ||
|
||
fun launchedAtEquals(launchedAt: Int?): Option { | ||
if (launchedAt == null) { | ||
return NOOP | ||
} | ||
|
||
return Option { b -> b.must(QueryBuilders.term { t -> t.field("launched_at").value(launchedAt.toLong()) }) } | ||
} | ||
|
||
fun ratingEquals(rating: String?): Option { | ||
if (rating.isNullOrBlank()) { | ||
return NOOP | ||
} | ||
|
||
return Option { b -> b.must(QueryBuilders.term { t -> t.field("rating").value(rating) }) } | ||
} | ||
|
||
fun titleOrDescriptionContaining(terms: String?): Option { | ||
if (terms.isNullOrBlank()) { | ||
return NOOP | ||
} | ||
|
||
return Option { b -> b.must(QueryBuilders.queryString { q -> q.fields("title", "description").query("*$terms*") }) } | ||
} | ||
|
||
private fun fieldValues(ids: Set<String>): List<FieldValue> = ids.map { FieldValue.of(it) } | ||
} | ||
} |
117 changes: 117 additions & 0 deletions
117
...c/main/kotlin/com/lukinhasssss/catalogo/infrastructure/video/persistence/VideoDocument.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,117 @@ | ||
package com.lukinhasssss.catalogo.infrastructure.video.persistence | ||
|
||
import com.lukinhasssss.catalogo.domain.video.Video | ||
import org.springframework.data.annotation.Id | ||
import org.springframework.data.elasticsearch.annotations.Document | ||
import org.springframework.data.elasticsearch.annotations.Field | ||
import org.springframework.data.elasticsearch.annotations.FieldType | ||
import org.springframework.data.elasticsearch.annotations.InnerField | ||
import org.springframework.data.elasticsearch.annotations.MultiField | ||
|
||
@Document(indexName = "videos") | ||
data class VideoDocument( | ||
|
||
@Id | ||
val id: String, | ||
|
||
@MultiField( | ||
mainField = Field(type = FieldType.Text, name = "title"), | ||
otherFields = [ InnerField(suffix = "keyword", type = FieldType.Keyword) ] | ||
) | ||
val title: String, | ||
|
||
@Field(type = FieldType.Text, name = "description") | ||
val description: String, | ||
|
||
@Field(type = FieldType.Integer, name = "launched_at") | ||
val launchedAt: Int, | ||
|
||
@Field(type = FieldType.Double, name = "duration") | ||
val duration: Double, | ||
|
||
@Field(type = FieldType.Keyword, name = "rating") | ||
val rating: String, | ||
|
||
@Field(type = FieldType.Boolean, name = "opened") | ||
val opened: Boolean, | ||
|
||
@Field(type = FieldType.Boolean, name = "published") | ||
val published: Boolean, | ||
|
||
@Field(type = FieldType.Keyword, name = "banner") | ||
val banner: String? = null, | ||
|
||
@Field(type = FieldType.Keyword, name = "thumbnail") | ||
val thumbnail: String? = null, | ||
|
||
@Field(type = FieldType.Keyword, name = "thumbnail_half") | ||
val thumbnailHalf: String? = null, | ||
|
||
@Field(type = FieldType.Keyword, name = "trailer") | ||
val trailer: String? = null, | ||
|
||
@Field(type = FieldType.Keyword, name = "video") | ||
val video: String? = null, | ||
|
||
@Field(type = FieldType.Keyword, name = "categories") | ||
val categories: Set<String> = setOf(), | ||
|
||
@Field(type = FieldType.Keyword, name = "cast_members") | ||
val castMembers: Set<String> = setOf(), | ||
|
||
@Field(type = FieldType.Keyword, name = "genres") | ||
val genres: Set<String> = setOf(), | ||
|
||
@Field(type = FieldType.Date, name = "created_at") | ||
val createdAt: String, | ||
|
||
@Field(type = FieldType.Date, name = "updated_at") | ||
val updatedAt: String | ||
) { | ||
|
||
companion object { | ||
fun from(aVideo: Video) = with(aVideo) { | ||
VideoDocument( | ||
id = id, | ||
title = title, | ||
description = description, | ||
launchedAt = launchedAt.value, | ||
duration = duration, | ||
rating = rating.name, | ||
opened = opened, | ||
published = published, | ||
banner = banner, | ||
thumbnail = thumbnail, | ||
thumbnailHalf = thumbnailHalf, | ||
trailer = trailer, | ||
video = video, | ||
categories = categories, | ||
castMembers = castMembers, | ||
genres = genres, | ||
createdAt = createdAt.toString(), | ||
updatedAt = updatedAt.toString() | ||
) | ||
} | ||
} | ||
|
||
fun toVideo() = Video.with( | ||
id = id, | ||
title = title, | ||
description = description, | ||
launchedAt = launchedAt, | ||
duration = duration, | ||
rating = rating, | ||
opened = opened, | ||
published = published, | ||
banner = banner, | ||
thumbnail = thumbnail, | ||
thumbnailHalf = thumbnailHalf, | ||
trailer = trailer, | ||
video = video, | ||
categories = categories, | ||
castMembers = castMembers, | ||
genres = genres, | ||
createdAt = createdAt, | ||
updatedAt = updatedAt | ||
) | ||
} |
5 changes: 5 additions & 0 deletions
5
...main/kotlin/com/lukinhasssss/catalogo/infrastructure/video/persistence/VideoRepository.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,5 @@ | ||
package com.lukinhasssss.catalogo.infrastructure.video.persistence | ||
|
||
import org.springframework.data.elasticsearch.repository.ElasticsearchRepository | ||
|
||
interface VideoRepository : ElasticsearchRepository<VideoDocument, String> |
Oops, something went wrong.