Skip to content

Commit

Permalink
Merge pull request #417 from NDLANO/multiple-resource-types-stats
Browse files Browse the repository at this point in the history
myndla-api: Add option to pass multiple resourceTypes to favorites endpoint
  • Loading branch information
jnatten authored Mar 5, 2024
2 parents fe0a401 + 6c019f3 commit fdbc12c
Show file tree
Hide file tree
Showing 4 changed files with 22 additions and 8 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,7 @@ import sttp.tapir.EndpointInput
import sttp.tapir.server.ServerEndpoint
import sttp.tapir._
import sttp.tapir.generic.auto._
import sttp.tapir.model.CommaSeparated

trait StatsController {
this: FolderReadService with TapirErrorHelpers =>
Expand All @@ -36,8 +37,9 @@ trait StatsController {
case None => returnLeftError(NotFoundException("No stats found"))
}
}
private val pathResourceType = path[String]("resourceType").description("The type of the resource to look up")
private val pathResourceId = path[String]("resourceId").description("ID of the resource to look up")
private val pathResourceType =
path[CommaSeparated[String]]("resourceType").description("The type of the resource to look up")
private val pathResourceId = path[String]("resourceId").description("ID of the resource to look up")

def getFolderResourceFavorites: ServerEndpoint[Any, Eff] = endpoint.get
.summary("Get folder resource favorites")
Expand All @@ -46,7 +48,7 @@ trait StatsController {
.out(jsonBody[SingleResourceStats])
.errorOut(errorOutputsFor(404))
.serverLogicPure { case (resourceType, resourceId) =>
folderReadService.getFavouriteStatsForResource(resourceId, resourceType).handleErrorsOrOk
folderReadService.getFavouriteStatsForResource(resourceId, resourceType.values).handleErrorsOrOk
}

override val endpoints: List[ServerEndpoint[Any, Eff]] = List(
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -9,12 +9,14 @@ package no.ndla.myndlaapi.controller

import cats.effect.IO
import cats.effect.unsafe.implicits.global
import no.ndla.myndla.model.api.Stats
import no.ndla.myndla.model.api.{SingleResourceStats, Stats}
import no.ndla.myndlaapi.{Eff, TestEnvironment}
import no.ndla.network.tapir.Service
import no.ndla.scalatestsuite.UnitTestSuite
import sttp.client3.quick._

import scala.util.Success

class StatsControllerTest extends UnitTestSuite with TestEnvironment {
val serverPort: Int = findFreePort

Expand All @@ -33,6 +35,16 @@ class StatsControllerTest extends UnitTestSuite with TestEnvironment {
response.code.code should be(200)
}

test("That getting multiple resourceTypes for id works") {
when(folderReadService.getFavouriteStatsForResource(any, any)).thenReturn(Success(SingleResourceStats(1)))
val response = simpleHttpClient.send(
quickRequest.get(uri"http://localhost:$serverPort/myndla-api/v1/stats/favorites/article,multidisciplinary/123")
)
response.code.code should be(200)

verify(folderReadService, times(1)).getFavouriteStatsForResource("123", List("article", "multidisciplinary"))
}

test("That no endpoints are shadowed") {
import sttp.tapir.testing.EndpointVerifier
val errors = EndpointVerifier(controller.endpoints.map(_.endpoint))
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -303,14 +303,14 @@ trait FolderRepository {
Success(resourceId)
}

def numberOfFavouritesForResource(resourceId: String, resourceType: String)(implicit
def numberOfFavouritesForResource(resourceId: String, resourceTypes: List[String])(implicit
session: DBSession
): Try[Long] = Try {
sql"""
select count(*) as count from ${DBFolderResource.table} fr
inner join ${DBResource.table} r on fr.resource_id = r.id
where r.document->>'resourceId' = $resourceId
and r.resource_type = $resourceType
and r.resource_type in ($resourceTypes)
"""
.map(rs => rs.long("count"))
.single()
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -266,10 +266,10 @@ trait FolderReadService {
withFeideId(maybeFeideToken)(feideId => exportUserDataAuthenticated(maybeFeideToken, feideId))
}

def getFavouriteStatsForResource(resourceId: String, resourceType: String): Try[SingleResourceStats] = {
def getFavouriteStatsForResource(resourceId: String, resourceTypes: List[String]): Try[SingleResourceStats] = {
implicit val session: DBSession = folderRepository.getSession(true)
folderRepository
.numberOfFavouritesForResource(resourceId, resourceType)
.numberOfFavouritesForResource(resourceId, resourceTypes)
.map(totalCount => api.SingleResourceStats(totalCount))
}

Expand Down

0 comments on commit fdbc12c

Please sign in to comment.