Skip to content

Commit f32f52f

Browse files
committed
Adds sort-parameter to my learningpaths
1 parent 07cc83b commit f32f52f

File tree

8 files changed

+44
-20
lines changed

8 files changed

+44
-20
lines changed

learningpath-api/src/main/scala/no/ndla/learningpathapi/controller/LearningpathControllerV2.scala

Lines changed: 8 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -17,6 +17,7 @@ import no.ndla.language.Language
1717
import no.ndla.language.Language.AllLanguages
1818
import no.ndla.learningpathapi.Props
1919
import no.ndla.learningpathapi.model.api.*
20+
import no.ndla.learningpathapi.model.domain.Sort.ByCreatedDesc
2021
import no.ndla.learningpathapi.model.domain.{License as _, *}
2122
import no.ndla.learningpathapi.service.search.{SearchConverterServiceComponent, SearchService}
2223
import no.ndla.learningpathapi.service.{ConverterService, ReadService, UpdateService}
@@ -392,11 +393,16 @@ class LearningpathControllerV2(using
392393
.summary("Fetch all learningspaths you have created")
393394
.description("Shows your learningpaths.")
394395
.in("mine")
396+
.in(sort)
395397
.out(jsonBody[List[LearningPathV2DTO]])
396398
.errorOut(errorOutputsFor(401, 403, 404))
397399
.withRequiredMyNDLAUserOrTokenUser
398-
.serverLogicPure { user => _ =>
399-
readService.withOwnerV2(user, props.DefaultLanguage, true).asRight
400+
.serverLogicPure { user =>
401+
{ sortStr =>
402+
readService
403+
.withOwnerV2(user, Sort.valueOf(sortStr).getOrElse(ByCreatedDesc), props.DefaultLanguage, true)
404+
.asRight
405+
}
400406
}
401407

402408
def getLicenses: ServerEndpoint[Any, Eff] = endpoint

learningpath-api/src/main/scala/no/ndla/learningpathapi/model/domain/Sort.scala

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -27,10 +27,10 @@ object Sort extends Enum[Sort] with CirceEnum[Sort] {
2727
case object ByRelevanceAsc extends Sort("relevance")
2828
case object ByLastUpdatedDesc extends Sort("-lastUpdated")
2929
case object ByLastUpdatedAsc extends Sort("lastUpdated")
30-
case object ByDurationDesc extends Sort("-duration")
31-
case object ByDurationAsc extends Sort("duration")
3230
case object ByTitleDesc extends Sort("-title")
3331
case object ByTitleAsc extends Sort("title")
32+
case object ByCreatedDesc extends Sort("-created")
33+
case object ByCreatedAsc extends Sort("created")
3434

3535
def valueOf(s: String): Option[Sort] = Sort.values.find(_.entryName == s)
3636

learningpath-api/src/main/scala/no/ndla/learningpathapi/repository/LearningPathRepository.scala

Lines changed: 14 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -48,9 +48,21 @@ class LearningPathRepository extends StrictLogging {
4848
learningPathWhere(sqls"lp.external_id = $externalId")
4949
}
5050

51-
def withOwner(owner: String): List[LearningPath] = {
51+
def withOwner(owner: String, sort: Sort): List[LearningPath] = {
52+
val sortingOrder = sort match {
53+
case Sort.ByCreatedDesc => sqls"order by lp.document->>'created' DESC"
54+
case Sort.ByCreatedAsc => sqls"order by lp.document->>'created' ASC"
55+
case Sort.ByLastUpdatedDesc => sqls"order by lp.document->>'lastUpdated' DESC"
56+
case Sort.ByLastUpdatedAsc => sqls"order by lp.document->>'lastUpdated' ASC"
57+
case Sort.ByIdDesc => sqls"order by lp.id DESC"
58+
case Sort.ByIdAsc => sqls"order by lp.id ASC"
59+
case Sort.ByRelevanceDesc => sqls"order by lp.id DESC"
60+
case Sort.ByRelevanceAsc => sqls"order by lp.id ASC"
61+
case Sort.ByTitleDesc => sqls"order by lp.document->'title'->0->>'title' DESC"
62+
case Sort.ByTitleAsc => sqls"order by lp.document->'title'->0->>'title' ASC"
63+
}
5264
learningPathsWhere(
53-
sqls"lp.document->>'owner' = $owner AND lp.document->>'status' <> ${LearningPathStatus.DELETED.toString} order by lp.document->>'created' DESC"
65+
sqls"lp.document->>'owner' = $owner AND lp.document->>'status' <> ${LearningPathStatus.DELETED.toString} $sortingOrder"
5466
)
5567
}
5668

learningpath-api/src/main/scala/no/ndla/learningpathapi/service/ReadService.scala

Lines changed: 7 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -37,9 +37,14 @@ class ReadService(using
3737
learningPathRepository.allPublishedContributors.map(author => commonApi.AuthorDTO(author.`type`, author.name))
3838
}
3939

40-
def withOwnerV2(user: CombinedUserRequired, language: String, fallback: Boolean): List[LearningPathV2DTO] = {
40+
def withOwnerV2(
41+
user: CombinedUserRequired,
42+
sort: Sort,
43+
language: FeideID,
44+
fallback: Boolean,
45+
): List[LearningPathV2DTO] = {
4146
learningPathRepository
42-
.withOwner(user.id)
47+
.withOwner(user.id, sort)
4348
.flatMap(value => converterService.asApiLearningpathV2(value, language, fallback, user).toOption)
4449
}
4550

learningpath-api/src/main/scala/no/ndla/learningpathapi/service/search/SearchService.scala

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -264,8 +264,8 @@ class SearchService(using
264264
case AllLanguages => fieldSort("defaultTitle").order(SortOrder.Desc).missing("_last")
265265
case _ => fieldSort(s"titles.$sortLanguage.raw").order(SortOrder.Desc).missing("_last").unmappedType("long")
266266
}
267-
case Sort.ByDurationAsc => fieldSort("duration").order(SortOrder.Asc).missing("_last")
268-
case Sort.ByDurationDesc => fieldSort("duration").order(SortOrder.Desc).missing("_last")
267+
case Sort.ByCreatedAsc => fieldSort("created").order(SortOrder.Asc).missing("_last")
268+
case Sort.ByCreatedDesc => fieldSort("created").order(SortOrder.Desc).missing("_last")
269269
case Sort.ByLastUpdatedAsc => fieldSort("lastUpdated").order(SortOrder.Asc).missing("_last")
270270
case Sort.ByLastUpdatedDesc => fieldSort("lastUpdated").order(SortOrder.Desc).missing("_last")
271271
case Sort.ByRelevanceAsc => fieldSort("_score").order(SortOrder.Asc)

learningpath-api/src/test/scala/no/ndla/learningpathapi/controller/LearningpathControllerV2Test.scala

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -82,7 +82,7 @@ class LearningpathControllerV2Test extends UnitSuite with TestEnvironment with T
8282
withIdIn = List(1, 2),
8383
taggedWith = Some(tag),
8484
language = Some(language),
85-
sort = Sort.ByDurationDesc,
85+
sort = Sort.ByCreatedDesc,
8686
page = Some(page),
8787
pageSize = Some(pageSize),
8888
verificationStatus = Some(verificationStatus),
@@ -94,7 +94,7 @@ class LearningpathControllerV2Test extends UnitSuite with TestEnvironment with T
9494
"query" -> query,
9595
"tag" -> tag,
9696
"language" -> language,
97-
"sort" -> "-duration",
97+
"sort" -> "-created",
9898
"page-size" -> s"$pageSize",
9999
"page" -> s"$page",
100100
"ids" -> s"$ids",
@@ -148,14 +148,14 @@ class LearningpathControllerV2Test extends UnitSuite with TestEnvironment with T
148148
query = Some(query),
149149
taggedWith = Some(tag),
150150
language = Some(language),
151-
sort = Sort.ByDurationDesc,
151+
sort = Sort.ByCreatedDesc,
152152
page = Some(page),
153153
pageSize = Some(pageSize),
154154
)
155155

156156
when(searchService.matchingQuery(eqTo(expectedSettings))).thenReturn(Success(result))
157157
val inputBody =
158-
s"""{"query": "$query", "tag": "$tag", "language": "$language", "page": $page, "pageSize": $pageSize, "ids": [1, 2], "sort": "-duration" }"""
158+
s"""{"query": "$query", "tag": "$tag", "language": "$language", "page": $page, "pageSize": $pageSize, "ids": [1, 2], "sort": "-created" }"""
159159
val res = simpleHttpClient.send(
160160
quickRequest.post(uri"http://localhost:$serverPort/learningpath-api/v2/learningpaths/search/").body(inputBody)
161161
)

learningpath-api/src/test/scala/no/ndla/learningpathapi/repository/LearningPathRepositoryIntegrationTest.scala

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -149,7 +149,7 @@ class LearningPathRepositoryIntegrationTest extends DatabaseIntegrationSuite wit
149149
}
150150
fail("Exception should prevent normal execution")
151151
} catch {
152-
case _: Throwable => repository.withOwner(owner).length should be(0)
152+
case _: Throwable => repository.withOwner(owner, Sort.ByCreatedDesc).length should be(0)
153153
}
154154
}
155155

@@ -423,7 +423,7 @@ class LearningPathRepositoryIntegrationTest extends DatabaseIntegrationSuite wit
423423

424424
def deleteAllWithOwner(owner: String): Unit = {
425425
repository.inTransaction { implicit session =>
426-
repository.withOwner(owner).foreach(lp => repository.deletePath(lp.id.get))
426+
repository.withOwner(owner, Sort.ByCreatedDesc).foreach(lp => repository.deletePath(lp.id.get))
427427
}
428428
}
429429
}

learningpath-api/src/test/scala/no/ndla/learningpathapi/service/search/SearchServiceTest.scala

Lines changed: 5 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -313,16 +313,17 @@ class SearchServiceTest extends ElasticsearchIntegrationSuite with UnitSuite wit
313313
hits(4).id should be(EnglandoId)
314314
}
315315

316-
test("That order by durationDesc orders search result by duration descending") {
317-
val Success(searchResult) = searchService.matchingQuery(searchSettings.copy(sort = Sort.ByDurationDesc)): @unchecked
318-
val hits = searchResult.results
316+
test("That order by lastUpdatedDesc orders search result by lastUpdated descending") {
317+
val Success(searchResult) =
318+
searchService.matchingQuery(searchSettings.copy(sort = Sort.ByLastUpdatedDesc)): @unchecked
319+
val hits = searchResult.results
319320

320321
searchResult.totalCount should be(4)
321322
hits.head.id should be(UnrelatedId)
322323
}
323324

324325
test("That order ByDurationAsc orders search result by duration ascending") {
325-
val Success(searchResult) = searchService.matchingQuery(searchSettings.copy(sort = Sort.ByDurationAsc)): @unchecked
326+
val Success(searchResult) = searchService.matchingQuery(searchSettings.copy(sort = Sort.ByCreatedDesc)): @unchecked
326327
val hits = searchResult.results
327328

328329
searchResult.totalCount should be(4)

0 commit comments

Comments
 (0)