Skip to content
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 @@ -16,8 +16,8 @@ import scalikejdbc.*
class DBArticle(using props: Props) {
object Article extends SQLSyntaxSupport[Article] {

override val tableName = "contentdata"
override lazy val schemaName: Option[String] = Some(props.MetaSchema)
override val tableName = "contentdata"
override val schemaName: Option[String] = Some(props.MetaSchema)

def fromResultSet(lp: SyntaxProvider[Article])(rs: WrappedResultSet): ArticleRow = fromResultSet(lp.resultName)(rs)

Expand Down
19 changes: 11 additions & 8 deletions audio-api/src/main/scala/no/ndla/audioapi/ComponentRegistry.scala
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,7 @@ package no.ndla.audioapi
import no.ndla.audioapi.controller.*
import no.ndla.audioapi.db.migrationwithdependencies.{V5__AddAgreementToAudio, V6__TranslateUntranslatedAuthors}
import no.ndla.audioapi.integration.{NDLAS3Client, TranscribeS3Client}
import no.ndla.audioapi.model.domain.{DBAudioMetaInformation, DBSeries}
import no.ndla.audioapi.repository.{AudioRepository, SeriesRepository}
import no.ndla.audioapi.service.*
import no.ndla.audioapi.service.search.*
Expand All @@ -24,14 +25,16 @@ import no.ndla.network.tapir.{ErrorHelpers, Routes, SwaggerController, TapirAppl
import no.ndla.search.{Elastic4sClientFactory, NdlaE4sClient, SearchLanguage}

class ComponentRegistry(properties: AudioApiProperties) extends TapirApplication[AudioApiProperties] {
given props: AudioApiProperties = properties
given dataSource: DataSource = DataSource.getDataSource
given clock: Clock = new Clock
given migrator: DBMigrator = DBMigrator(new V5__AddAgreementToAudio, new V6__TranslateUntranslatedAuthors)
given errorHelpers: ErrorHelpers = new ErrorHelpers
given errorHandling: ControllerErrorHandling = new ControllerErrorHandling
given searchLanguage: SearchLanguage = new SearchLanguage
given dbUtility: DBUtility = new DBUtility
given props: AudioApiProperties = properties
given dataSource: DataSource = DataSource.getDataSource
given clock: Clock = new Clock
given migrator: DBMigrator = DBMigrator(new V5__AddAgreementToAudio, new V6__TranslateUntranslatedAuthors)
given errorHelpers: ErrorHelpers = new ErrorHelpers
given errorHandling: ControllerErrorHandling = new ControllerErrorHandling
given searchLanguage: SearchLanguage = new SearchLanguage
given dbUtility: DBUtility = new DBUtility
given dbAudioMetaInformation: DBAudioMetaInformation = new DBAudioMetaInformation
given dbSeries: DBSeries = new DBSeries

given s3Client: NDLAS3Client = new NDLAS3Client(props.StorageName, props.StorageRegion)
given s3TranscribeClient: TranscribeS3Client =
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,7 @@ import no.ndla.common.CirceUtil
import no.ndla.common.model.NDLADate
import no.ndla.common.model.domain.article.Copyright
import no.ndla.common.model.domain.{Tag, Title}
import no.ndla.audioapi.Props
import no.ndla.language.Language.getSupportedLanguages
import no.ndla.language.model.LanguageField
import scalikejdbc.*
Expand Down Expand Up @@ -69,8 +70,7 @@ object Audio {
implicit val decoder: Decoder[Audio] = deriveDecoder
}

object AudioMetaInformation extends SQLSyntaxSupport[AudioMetaInformation] {
override val tableName = "audiodata"
object AudioMetaInformation {

implicit val encoder: Encoder[AudioMetaInformation] = deriveEncoder
implicit val decoder: Decoder[AudioMetaInformation] = deriveDecoder
Expand All @@ -94,3 +94,8 @@ object AudioMetaInformation extends SQLSyntaxSupport[AudioMetaInformation] {
rs.longOpt(au.c("id")).map(_ => fromResultSet(au)(rs))
}
}

class DBAudioMetaInformation(using props: Props) extends SQLSyntaxSupport[AudioMetaInformation] {
override val tableName: String = "audiodata"
override val schemaName: Option[String] = Some(props.MetaSchema)
}
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,7 @@ import io.circe.generic.semiauto.{deriveDecoder, deriveEncoder}
import no.ndla.common.CirceUtil
import no.ndla.common.model.NDLADate
import no.ndla.common.model.domain.Title
import no.ndla.audioapi.Props
import no.ndla.language.Language.getSupportedLanguages
import scalikejdbc.*

Expand Down Expand Up @@ -59,8 +60,7 @@ case class Series(
)
}

object Series extends SQLSyntaxSupport[Series] {
override val tableName = "seriesdata"
object Series {

implicit val encoder: Encoder[Series] = deriveEncoder
implicit val decoder: Decoder[Series] = deriveDecoder
Expand Down Expand Up @@ -88,3 +88,8 @@ object Series extends SQLSyntaxSupport[Series] {
meta.map(m => fromId(id = rs.long(s.c("id")), revision = rs.int(s.c("revision")), series = m))
}
}

class DBSeries(using props: Props) extends SQLSyntaxSupport[Series] {
override val tableName: String = "seriesdata"
override val schemaName: Option[String] = Some(props.MetaSchema)
}
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,7 @@ package no.ndla.audioapi.repository

import com.typesafe.scalalogging.StrictLogging
import no.ndla.audioapi.model.api.OptimisticLockException
import no.ndla.audioapi.model.domain.{AudioMetaInformation, Series}
import no.ndla.audioapi.model.domain.{AudioMetaInformation, DBAudioMetaInformation, DBSeries, Series}
import no.ndla.common.CirceUtil
import no.ndla.network.tapir.ErrorHelpers
import org.postgresql.util.PGobject
Expand All @@ -20,11 +20,15 @@ import no.ndla.database.implicits.*

import scala.util.{Failure, Success, Try}

class AudioRepository(using errorHelpers: ErrorHelpers, dbUtility: DBUtility)
extends StrictLogging
class AudioRepository(using
errorHelpers: ErrorHelpers,
dbUtility: DBUtility,
dbAudioMetaInformation: DBAudioMetaInformation,
dbSeries: DBSeries,
) extends StrictLogging
with Repository[AudioMetaInformation] {
def audioCount(implicit session: DBSession = dbUtility.readOnlySession): Long =
tsql"select count(*) from ${AudioMetaInformation.table}"
tsql"select count(*) from ${dbAudioMetaInformation.table}"
.map(rs => rs.long("count"))
.runSingle()
.map(_.getOrElse(0L))
Expand Down Expand Up @@ -105,7 +109,7 @@ class AudioRepository(using errorHelpers: ErrorHelpers, dbUtility: DBUtility)
session: DBSession = dbUtility.autoSession
): Try[Long] = {
tsql"""
update ${AudioMetaInformation.table}
update ${dbAudioMetaInformation.table}
set series_id = $seriesId
where id = $audioMetaId
""".update().map(_ => audioMetaId)
Expand All @@ -125,7 +129,7 @@ class AudioRepository(using errorHelpers: ErrorHelpers, dbUtility: DBUtility)
}

def deleteAudio(audioId: Long)(implicit session: DBSession = dbUtility.autoSession): Int = {
tsql"delete from ${AudioMetaInformation.table} where id=$audioId".update().get
tsql"delete from ${dbAudioMetaInformation.table} where id=$audioId".update().get
}

override def documentsWithIdBetween(min: Long, max: Long): Try[List[AudioMetaInformation]] = {
Expand All @@ -135,12 +139,12 @@ class AudioRepository(using errorHelpers: ErrorHelpers, dbUtility: DBUtility)
private def audioMetaInformationWhere(
whereClause: SQLSyntax
)(implicit session: DBSession): Option[AudioMetaInformation] = {
val au = AudioMetaInformation.syntax("au")
val se = Series.syntax("se")
val au = dbAudioMetaInformation.syntax("au")
val se = dbSeries.syntax("se")
tsql"""
select ${au.result.*}, ${se.result.*}
from ${AudioMetaInformation.as(au)}
left join ${Series.as(se)} on ${au.seriesId} = ${se.id}
from ${dbAudioMetaInformation.as(au)}
left join ${dbSeries.as(se)} on ${au.seriesId} = ${se.id}
where $whereClause
"""
.map { rs =>
Expand All @@ -156,12 +160,12 @@ class AudioRepository(using errorHelpers: ErrorHelpers, dbUtility: DBUtility)
private def audioMetaInformationsWhere(
whereClause: SQLSyntax
)(implicit session: DBSession = dbUtility.readOnlySession): Try[List[AudioMetaInformation]] = {
val au = AudioMetaInformation.syntax("au")
val se = Series.syntax("se")
val au = dbAudioMetaInformation.syntax("au")
val se = dbSeries.syntax("se")
tsql"""
select ${au.result.*}, ${se.result.*}
from ${AudioMetaInformation.as(au)}
left join ${Series.as(se)} on ${au.seriesId} = ${se.id}
from ${dbAudioMetaInformation.as(au)}
left join ${dbSeries.as(se)} on ${au.seriesId} = ${se.id}
where $whereClause
"""
.map { rs =>
Expand All @@ -173,8 +177,8 @@ class AudioRepository(using errorHelpers: ErrorHelpers, dbUtility: DBUtility)
}

def getRandomAudio()(implicit session: DBSession = dbUtility.readOnlySession): Option[AudioMetaInformation] = {
val au = AudioMetaInformation.syntax("au")
tsql"select ${au.result.*} from ${AudioMetaInformation.as(au)} tablesample public.system_rows(1)"
val au = dbAudioMetaInformation.syntax("au")
tsql"select ${au.result.*} from ${dbAudioMetaInformation.as(au)} tablesample public.system_rows(1)"
.map(AudioMetaInformation.fromResultSet(au))
.runSingle()
.get
Expand All @@ -183,10 +187,10 @@ class AudioRepository(using errorHelpers: ErrorHelpers, dbUtility: DBUtility)
def getByPage(pageSize: Int, offset: Int)(implicit
session: DBSession = dbUtility.readOnlySession
): Seq[AudioMetaInformation] = {
val au = AudioMetaInformation.syntax("au")
val au = dbAudioMetaInformation.syntax("au")
tsql"""
select ${au.result.*}
from ${AudioMetaInformation.as(au)}
from ${dbAudioMetaInformation.as(au)}
where document is not null
order by id
offset $offset
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,7 @@
package no.ndla.audioapi.repository

import com.typesafe.scalalogging.StrictLogging
import no.ndla.audioapi.model.domain.{AudioMetaInformation, Series}
import no.ndla.audioapi.model.domain.{AudioMetaInformation, DBAudioMetaInformation, DBSeries, Series}
import no.ndla.audioapi.model.domain
import org.postgresql.util.PGobject
import scalikejdbc.*
Expand All @@ -23,8 +23,12 @@ import no.ndla.database.DBUtility

import scala.util.{Failure, Success, Try}

class SeriesRepository(using helpers: ErrorHelpers, dbUtility: DBUtility)
extends StrictLogging
class SeriesRepository(using
helpers: ErrorHelpers,
dbUtility: DBUtility,
dbSeries: DBSeries,
dbAudioMetaInformation: DBAudioMetaInformation,
) extends StrictLogging
with Repository[Series] {

/** Method to fetch single series from database
Expand All @@ -43,7 +47,7 @@ class SeriesRepository(using helpers: ErrorHelpers, dbUtility: DBUtility)

def deleteWithId(id: Long)(implicit session: DBSession = dbUtility.autoSession): Try[Int] = {
tsql"""
delete from ${Series.table}
delete from ${dbSeries.table}
where id=$id
""".update()
}
Expand All @@ -56,7 +60,7 @@ class SeriesRepository(using helpers: ErrorHelpers, dbUtility: DBUtility)
val newRevision = series.revision + 1

tsql"""
update ${Series.table}
update ${dbSeries.table}
set document=$dataObject, revision=$newRevision
where id=${series.id} and revision=${series.revision}
"""
Expand All @@ -82,13 +86,13 @@ class SeriesRepository(using helpers: ErrorHelpers, dbUtility: DBUtility)
dataObject.setValue(CirceUtil.toJsonString(newSeries))

tsql"""
insert into ${Series.table}(document, revision)
insert into ${dbSeries.table}(document, revision)
values ($dataObject, $startRevision)
""".updateAndReturnGeneratedKey().map(id => Series.fromId(id, startRevision, newSeries))
}

override def minMaxId(implicit session: DBSession = dbUtility.readOnlySession): Try[(Long, Long)] = {
tsql"select coalesce(MIN(id),0) as mi, coalesce(MAX(id),0) as ma from ${Series.table}"
tsql"select coalesce(MIN(id),0) as mi, coalesce(MAX(id),0) as ma from ${dbSeries.table}"
.map(rs => (rs.long("mi"), rs.long("ma")))
.runSingle()
.map(_.getOrElse((0L, 0L)))
Expand All @@ -101,25 +105,25 @@ class SeriesRepository(using helpers: ErrorHelpers, dbUtility: DBUtility)
private def serieWhereNoEpisodes(
whereClause: SQLSyntax
)(implicit session: DBSession = dbUtility.readOnlySession): Try[Option[Series]] = {
val se = Series.syntax("se")
val se = dbSeries.syntax("se")

tsql"""
select ${se.result.*}
from ${Series.as(se)}
from ${dbSeries.as(se)}
where $whereClause
""".map(Series.fromResultSet(se.resultName)).runSingle().map(_.sequence).flatten
}

private def serieWhere(
whereClause: SQLSyntax
)(implicit session: DBSession = dbUtility.readOnlySession): Try[Option[Series]] = {
val se = Series.syntax("se")
val au = AudioMetaInformation.syntax("au")
val se = dbSeries.syntax("se")
val au = dbAudioMetaInformation.syntax("au")

tsql"""
select ${se.result.*}, ${au.result.*}
from ${Series.as(se)}
left join ${AudioMetaInformation.as(au)} on ${se.id} = ${au.seriesId}
from ${dbSeries.as(se)}
left join ${dbAudioMetaInformation.as(au)} on ${se.id} = ${au.seriesId}
where $whereClause
"""
.one(Series.fromResultSet(se.resultName))
Expand All @@ -133,13 +137,13 @@ class SeriesRepository(using helpers: ErrorHelpers, dbUtility: DBUtility)
private def seriesWhere(
whereClause: SQLSyntax
)(implicit session: DBSession = dbUtility.readOnlySession): Try[List[Series]] = {
val se = Series.syntax("se")
val au = AudioMetaInformation.syntax("au")
val se = dbSeries.syntax("se")
val au = dbAudioMetaInformation.syntax("au")

tsql"""
select ${se.result.*}, ${au.result.*}
from ${Series.as(se)}
left join ${AudioMetaInformation.as(au)} on ${se.id} = ${au.seriesId}
from ${dbSeries.as(se)}
left join ${dbAudioMetaInformation.as(au)} on ${se.id} = ${au.seriesId}
where $whereClause
"""
.one(Series.fromResultSet(se.resultName))
Expand Down
11 changes: 7 additions & 4 deletions audio-api/src/test/scala/no/ndla/audioapi/TestEnvironment.scala
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,7 @@ package no.ndla.audioapi

import no.ndla.audioapi.controller.{AudioController, ControllerErrorHandling, InternController, SeriesController}
import no.ndla.audioapi.integration.{NDLAS3Client, TranscribeS3Client}
import no.ndla.audioapi.model.domain.{DBAudioMetaInformation, DBSeries}
import no.ndla.audioapi.repository.{AudioRepository, SeriesRepository}
import no.ndla.audioapi.service.*
import no.ndla.audioapi.service.search.*
Expand Down Expand Up @@ -41,10 +42,12 @@ trait TestEnvironment extends TapirApplication[AudioApiProperties] with MockitoS
implicit lazy val services: List[TapirController] = List.empty
implicit lazy val searchLanguage: SearchLanguage = mock[SearchLanguage]

implicit lazy val dataSource: DataSource = mock[DataSource]
implicit lazy val audioRepository: AudioRepository = mock[AudioRepository]
implicit lazy val seriesRepository: SeriesRepository = mock[SeriesRepository]
implicit lazy val dbUtility: DBUtility = new DBUtility
implicit lazy val dataSource: DataSource = mock[DataSource]
implicit lazy val audioRepository: AudioRepository = mock[AudioRepository]
implicit lazy val seriesRepository: SeriesRepository = mock[SeriesRepository]
implicit lazy val dbUtility: DBUtility = new DBUtility
implicit lazy val dbAudioMetaInformation: DBAudioMetaInformation = new DBAudioMetaInformation
implicit lazy val dbSeries: DBSeries = new DBSeries

implicit lazy val s3Client: NDLAS3Client = mock[NDLAS3Client]
implicit lazy val brightcoveClient: NdlaBrightcoveClient = mock[NdlaBrightcoveClient]
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -79,8 +79,7 @@ case class ConfigMeta(key: ConfigKey, value: ConfigMetaValue, updatedAt: NDLADat
}
}

object ConfigMeta extends SQLSyntaxSupport[ConfigMeta] with StrictLogging {
override val tableName = "configtable"
object ConfigMeta extends StrictLogging {

def fromResultSet(c: SyntaxProvider[ConfigMeta])(rs: WrappedResultSet): Try[ConfigMeta] =
fromResultSet(c.resultName)(rs)
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,7 @@
package no.ndla.conceptapi

import no.ndla.conceptapi.controller.*
import no.ndla.conceptapi.model.domain.{DBConcept, PublishedConcept}
import no.ndla.conceptapi.repository.{DraftConceptRepository, PublishedConceptRepository}
import no.ndla.conceptapi.service.search.*
import no.ndla.conceptapi.service.*
Expand All @@ -33,7 +34,9 @@ import sttp.tapir.stringToPath
import no.ndla.network.tapir.auth.Permission

class ComponentRegistry(properties: ConceptApiProperties) extends TapirApplication[ConceptApiProperties] {
given props: ConceptApiProperties = properties
given props: ConceptApiProperties = properties
given dbConcept: DBConcept = new DBConcept
given publishedConcept: PublishedConcept = new PublishedConcept

given clock: Clock = new Clock
given e4sClient: NdlaE4sClient = Elastic4sClientFactory.getClient(props.SearchServer)
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -10,10 +10,12 @@ package no.ndla.conceptapi.model.domain

import no.ndla.common.CirceUtil
import no.ndla.common.model.domain.concept.Concept
import no.ndla.conceptapi.Props
import scalikejdbc.*

object DBConcept extends SQLSyntaxSupport[Concept] {
override val tableName = "conceptdata"
class DBConcept(using props: Props) extends SQLSyntaxSupport[Concept] {
override val tableName = "conceptdata"
override val schemaName: Option[String] = Some(props.MetaSchema)

def fromResultSet(lp: SyntaxProvider[Concept])(rs: WrappedResultSet): Concept = fromResultSet(lp.resultName)(rs)

Expand Down Expand Up @@ -45,6 +47,7 @@ object DBConcept extends SQLSyntaxSupport[Concept] {
}
}

object PublishedConcept extends SQLSyntaxSupport[Concept] {
override val tableName = "publishedconceptdata"
class PublishedConcept(using props: Props) extends SQLSyntaxSupport[Concept] {
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Bruke samme navngiving?

Suggested change
class PublishedConcept(using props: Props) extends SQLSyntaxSupport[Concept] {
class DBPublishedConcept(using props: Props) extends SQLSyntaxSupport[Concept] {

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Ja jeg er enig i det jeg altså, i fix

override val tableName = "publishedconceptdata"
override val schemaName: Option[String] = Some(props.MetaSchema)
}
Loading