Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add search Video by term in title use case. #37

Open
wants to merge 1 commit into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -6,4 +6,6 @@ import scala.concurrent.Future

final class VideosSearcher(repository: VideoRepository) {
def all(): Future[Seq[Video]] = repository.all()

def findByTermInTitle(term: String): Future[Seq[Video]] = repository.findByTermInTitle(term)
}
Original file line number Diff line number Diff line change
Expand Up @@ -5,5 +5,7 @@ import scala.concurrent.Future
trait VideoRepository {
def all(): Future[Seq[Video]]

def findByTermInTitle(term: String): Future[Seq[Video]]

def save(video: Video): Future[Unit]
}
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,12 @@ final class DoobieMySqlVideoRepository(db: DoobieDbConnection)(implicit executio
override def all(): Future[Seq[Video]] =
db.read(sql"SELECT video_id, title, duration_in_seconds, category, creator_id FROM videos".query[Video].to[Seq])

override def findByTermInTitle(term: String) =
db.read(
sql"SELECT video_id, title, duration_in_seconds, category, creator_id FROM videos WHERE title LIKE CONCAT('%', $term, '%')"
.query[Video]
.to[Seq])

override def save(video: Video): Future[Unit] =
sql"INSERT INTO videos(video_id, title, duration_in_seconds, category, creator_id) VALUES (${video.id}, ${video.title}, ${video.duration}, ${video.category}, ${video.creatorId})".update.run
.transact(db.transactor)
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -16,4 +16,14 @@ final class VideosSearcherShould extends UnitTestCase with VideoRepositoryMock {

searcher.all().futureValue shouldBe existingVideos
}

"search all videos that contain a term in title" in {
val matchingVideo = VideoMother.random
val anotherMatchingVideo = VideoMother.random
val matchingVideos = Seq(matchingVideo, anotherMatchingVideo)
val term = "Term"
repositoryShouldFindByTermInTitle(term, matchingVideos)

searcher.findByTermInTitle(term).futureValue shouldBe matchingVideos
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@ package tv.codely.mooc.video.infrastructure.repository

import tv.codely.mooc.video.VideoIntegrationTestCase
import tv.codely.mooc.video.domain.VideoMother
import tv.codely.mooc.video.domain.VideoTitleMother
import doobie.implicits._
import org.scalatest.BeforeAndAfterEach

Expand Down Expand Up @@ -29,4 +30,16 @@ final class DoobieMySqlVideoRepositoryShould extends VideoIntegrationTestCase wi

repository.all().futureValue shouldBe videos
}

"search all videos that contain a term in title" in {
val term = "Term"
val videoMatch = VideoMother.random.copy(title = VideoTitleMother("This has a Term inside"))
val videoNoMatch = VideoMother.random.copy(title = VideoTitleMother("This has not a ? inside"))

repository.save(videoMatch).futureValue
repository.save(videoNoMatch).futureValue

repository.findByTermInTitle(term).futureValue should contain(videoMatch)
repository.findByTermInTitle(term).futureValue should not contain (videoNoMatch)
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -20,4 +20,9 @@ protected[video] trait VideoRepositoryMock extends MockFactory {
(repository.all _)
.expects()
.returning(Future.successful(videos))

protected def repositoryShouldFindByTermInTitle(term: String, videos: Seq[Video]): Unit =
(repository.findByTermInTitle _)
.expects(term)
.returning(Future.successful(videos))
}