diff --git a/learningpath-api/src/main/scala/no/ndla/learningpathapi/model/api/Stats.scala b/learningpath-api/src/main/scala/no/ndla/learningpathapi/model/api/Stats.scala index 03d72c8d8f..8c3ea009d0 100644 --- a/learningpath-api/src/main/scala/no/ndla/learningpathapi/model/api/Stats.scala +++ b/learningpath-api/src/main/scala/no/ndla/learningpathapi/model/api/Stats.scala @@ -19,5 +19,11 @@ case class Stats( @(ApiModelProperty @field)(description = "The total number of favourited resources ") numberOfResources: Long, @(ApiModelProperty @field)(description = "The total number of created tags") numberOfTags: Long, @(ApiModelProperty @field)(description = "The total number of favourited subjects") numberOfSubjects: Long, - @(ApiModelProperty @field)(description = "The total number of shared folders") numberOfSharedFolders: Long + @(ApiModelProperty @field)(description = "The total number of shared folders") numberOfSharedFolders: Long, + @(ApiModelProperty @field)(description = "Stats for type resources") favouritedResources: List[ResourceStats] +) {} + +case class ResourceStats( + @(ApiModelProperty @field)(description = "The type of favourited resouce") `type`: String, + @(ApiModelProperty @field)(description = "The number of favourited resource") number: Long ) {} diff --git a/learningpath-api/src/main/scala/no/ndla/learningpathapi/repository/FolderRepository.scala b/learningpath-api/src/main/scala/no/ndla/learningpathapi/repository/FolderRepository.scala index 3795d647dd..1817dca12a 100644 --- a/learningpath-api/src/main/scala/no/ndla/learningpathapi/repository/FolderRepository.scala +++ b/learningpath-api/src/main/scala/no/ndla/learningpathapi/repository/FolderRepository.scala @@ -589,5 +589,10 @@ trait FolderRepository { .single() } + def numberOfResourcesGrouped()(implicit session: DBSession = ReadOnlyAutoSession): List[(Long, String)] = { + sql"select count(*) as antall, resource_type from ${DBResource.table} group by resource_type" + .map(rs => (rs.long("antall"), rs.string("resource_type"))) + .list() + } } } diff --git a/learningpath-api/src/main/scala/no/ndla/learningpathapi/service/ReadService.scala b/learningpath-api/src/main/scala/no/ndla/learningpathapi/service/ReadService.scala index 9a3838e61d..154eeac875 100644 --- a/learningpath-api/src/main/scala/no/ndla/learningpathapi/service/ReadService.scala +++ b/learningpath-api/src/main/scala/no/ndla/learningpathapi/service/ReadService.scala @@ -448,6 +448,7 @@ trait ReadService { def getStats: Option[Stats] = { implicit val session: DBSession = folderRepository.getSession(true) + val groupedResources = folderRepository.numberOfResourcesGrouped() for { numberOfUsers <- userRepository.numberOfUsers() numberOfFolders <- folderRepository.numberOfFolders() @@ -461,7 +462,8 @@ trait ReadService { numberOfResources, numberOfTags, numberOfSubjects, - numberOfSharedFolders + numberOfSharedFolders, + favouritedResources = groupedResources.map(gr => ResourceStats(gr._2, gr._1)) ) } yield stats } diff --git a/learningpath-api/src/test/scala/no/ndla/learningpathapi/controller/StatsControllerTest.scala b/learningpath-api/src/test/scala/no/ndla/learningpathapi/controller/StatsControllerTest.scala index 28eb5b57c4..00fafc2b2e 100644 --- a/learningpath-api/src/test/scala/no/ndla/learningpathapi/controller/StatsControllerTest.scala +++ b/learningpath-api/src/test/scala/no/ndla/learningpathapi/controller/StatsControllerTest.scala @@ -25,7 +25,7 @@ class StatsControllerTest extends UnitSuite with TestEnvironment with ScalatraFu } test("That getting stats returns in fact stats") { - when(readService.getStats).thenReturn(Some(Stats(1, 2, 3, 4, 5, 6))) + when(readService.getStats).thenReturn(Some(Stats(1, 2, 3, 4, 5, 6, List.empty))) get("/") { status should be(200) } diff --git a/learningpath-api/src/test/scala/no/ndla/learningpathapi/service/ReadServiceTest.scala b/learningpath-api/src/test/scala/no/ndla/learningpathapi/service/ReadServiceTest.scala index e397370146..7cf4173bc9 100644 --- a/learningpath-api/src/test/scala/no/ndla/learningpathapi/service/ReadServiceTest.scala +++ b/learningpath-api/src/test/scala/no/ndla/learningpathapi/service/ReadServiceTest.scala @@ -705,7 +705,8 @@ class ReadServiceTest extends UnitSuite with UnitTestEnvironment { when(folderRepository.numberOfTags()(any)).thenReturn(Some(10)) when(userRepository.numberOfSubjects()(any)).thenReturn(Some(5)) when(folderRepository.numberOfSharedFolders()(any)).thenReturn(Some(5)) + when(folderRepository.numberOfResourcesGrouped()(any)).thenReturn(List.empty) - service.getStats.get should be(Stats(5, 10, 20, 10, 5, 5)) + service.getStats.get should be(Stats(5, 10, 20, 10, 5, 5, List.empty)) } }