Skip to content

Commit

Permalink
hola it works a lot
Browse files Browse the repository at this point in the history
  • Loading branch information
jnatten committed Dec 6, 2023
1 parent b1cf86e commit 0a29a20
Show file tree
Hide file tree
Showing 10 changed files with 87 additions and 33 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,6 @@ import no.ndla.learningpathapi.integration.DataSource
import no.ndla.learningpathapi.model.api.{Error, ErrorHelpers, ValidationError}
import no.ndla.learningpathapi.model.domain._
import no.ndla.learningpathapi.service.ConverterService
import no.ndla.myndla.model.api.ImportReport
import no.ndla.myndla.model.domain.{FolderStatus, InvalidStatusException}
import no.ndla.myndla.service.FolderConverterService
import no.ndla.network.model.HttpRequestException
Expand Down Expand Up @@ -58,8 +57,6 @@ trait NdlaController {
BadRequest(body = ValidationError(VALIDATION, VALIDATION_DESCRIPTION, messages = v.errors))
case a: AccessDeniedException =>
Forbidden(body = Error(ACCESS_DENIED, a.getMessage))
case dfe: DeleteFavoriteException =>
BadRequest(body = Error(DELETE_FAVORITE, dfe.getMessage))
case _: OptimisticLockException =>
Conflict(body = Error(RESOURCE_OUTDATED, RESOURCE_OUTDATED_DESCRIPTION))
case nfe: NotFoundException =>
Expand All @@ -74,7 +71,6 @@ trait NdlaController {
InternalServerError(body = IndexMissingError)
case i: ElasticIndexingException =>
InternalServerError(body = Error(GENERIC, i.getMessage))
case ir: ImportReport => UnprocessableEntity(body = ir)
case _: PSQLException =>
DataSource.connectToDatabase()
InternalServerError(DatabaseUnavailableError)
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -8,14 +8,10 @@

package no.ndla.learningpathapi.model.domain

case class MultipleUniqueKeysException(message: String) extends RuntimeException(message)
class OptimisticLockException(message: String) extends RuntimeException(message)
class ImportException(message: String) extends RuntimeException(message)
case class ElasticIndexingException(message: String) extends RuntimeException(message)
class ResultWindowTooLargeException(message: String) extends RuntimeException(message)
case class LanguageNotSupportedException(message: String) extends RuntimeException(message)
case class SearchException(message: String) extends RuntimeException(message)
case class TaxonomyUpdateException(message: String) extends RuntimeException(message)
case class InvalidOembedResponse(message: String) extends RuntimeException(message)
case class MissingIdException(message: String) extends RuntimeException(message)
case class DeleteFavoriteException(message: String) extends RuntimeException(message)
Original file line number Diff line number Diff line change
@@ -0,0 +1,4 @@
CREATE TABLE configtable (
configkey TEXT PRIMARY KEY,
value JSONB
);
Original file line number Diff line number Diff line change
@@ -0,0 +1,53 @@
/*
* Part of NDLA myndla-api
* Copyright (C) 2023 NDLA
*
* See LICENSE
*/

package no.ndla.myndlaapi.controller

import no.ndla.myndla.model.api.config.ConfigMetaRestricted
import no.ndla.myndla.model.domain.config.ConfigKey
import no.ndla.myndla.service.ConfigService
import no.ndla.myndlaapi.Eff
import no.ndla.network.tapir.NoNullJsonPrinter.jsonBody
import no.ndla.network.tapir.TapirErrors.errorOutputsFor
import no.ndla.network.tapir.{Service, TapirErrorHelpers}
import sttp.tapir.EndpointInput
import sttp.tapir.server.ServerEndpoint
import sttp.tapir._
import sttp.tapir.generic.auto._
import sttp.tapir.codec.enumeratum._
import io.circe.generic.auto._

trait ConfigController {
this: ErrorHelpers with TapirErrorHelpers with ConfigService =>
class ConfigController extends Service[Eff] {
override protected val prefix: EndpointInput[Unit] = "myndla-api" / "v1" / "config"

import ErrorHelpers._

// val queryConfigKey = query[ConfigKey]("config-key").description(s"The of configuration value. Can only be one of '${ConfigKey.all.mkString("', '")}'")

val pathConfigKey =
query[ConfigKey]("config-key")
.description(s"The of configuration value. Can only be one of '${ConfigKey.all.mkString("', '")}'")

import ConfigMetaRestricted.encoder

def getConfig: ServerEndpoint[Any, Eff] = endpoint.get
.summary("Get db configuration by key")
.description("Get db configuration by key")
.in(pathConfigKey)
.out(jsonBody[ConfigMetaRestricted])
.errorOut(errorOutputsFor(401, 403, 404))
.serverLogicPure(configKey =>
configService.getConfig(configKey).handleErrorsOrOk)

override protected val endpoints: List[ServerEndpoint[Any, Eff]] = List(
getConfig
)
}

}
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
/*
* Part of NDLA article-api.
* Copyright (C) 2016 NDLA
* Part of NDLA myndla-api.
* Copyright (C) 2023 NDLA
*
* See LICENSE
*
Expand All @@ -10,13 +10,15 @@ package no.ndla.myndlaapi.controller

import com.typesafe.scalalogging.StrictLogging
import no.ndla.common.Clock
import no.ndla.common.errors.{AccessDeniedException, NotFoundException}
import no.ndla.common.errors.{AccessDeniedException, NotFoundException, ValidationException}
import no.ndla.myndla.model.domain.InvalidStatusException
import no.ndla.myndlaapi.Props
import no.ndla.network.tapir.{AllErrors, ErrorBody, TapirErrorHelpers}
import no.ndla.myndlaapi.integration.DataSource
import no.ndla.network.tapir.{AllErrors, ErrorBody, TapirErrorHelpers, ValidationErrorBody}
import org.postgresql.util.PSQLException

trait ErrorHelpers extends TapirErrorHelpers with StrictLogging {
this: Props with Clock =>
this: Props with Clock with DataSource =>

import ErrorHelpers._

Expand All @@ -29,5 +31,10 @@ trait ErrorHelpers extends TapirErrorHelpers with StrictLogging {
ErrorBody(ACCESS_DENIED, a.getMessage, clock.now(), 403)
case mse: InvalidStatusException =>
ErrorBody(MISSING_STATUS, mse.getMessage, clock.now(), 400)
case _: PSQLException =>
DataSource.connectToDatabase()
ErrorHelpers.generic
case v: ValidationException =>
ValidationErrorBody(VALIDATION, VALIDATION_DESCRIPTION, clock.now(), Some(v.errors), 400)
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,7 @@ import sttp.tapir.EndpointInput
import sttp.tapir.server.ServerEndpoint
import sttp.tapir._
import sttp.tapir.generic.auto._
import io.circe.generic.auto._

trait UserController {
this: ErrorHelpers with UserService with TapirErrorHelpers with FolderWriteService with FolderReadService =>
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -17,8 +17,7 @@ case class ExportedUserData(
@(ApiModelProperty @field)(description = "The users folders") folders: List[Folder]
)

object ExportedUserData {
implicit def encoder: Encoder[ExportedUserData] = deriveEncoder[ExportedUserData]
implicit def decoder: Decoder[ExportedUserData] = deriveDecoder[ExportedUserData]

}
//object ExportedUserData {
// implicit def encoder: Encoder[ExportedUserData] = deriveEncoder[ExportedUserData]
// implicit def decoder: Decoder[ExportedUserData] = deriveDecoder[ExportedUserData]
//}
15 changes: 0 additions & 15 deletions myndla/src/main/scala/no/ndla/myndla/model/api/ImportReport.scala

This file was deleted.

Original file line number Diff line number Diff line change
Expand Up @@ -7,8 +7,11 @@

package no.ndla.myndla.model.api.config

import io.circe.Encoder
import io.circe.syntax._
import org.scalatra.swagger.annotations.ApiModelProperty
import org.scalatra.swagger.runtime.annotations.ApiModel
import io.circe.generic.auto._

import scala.annotation.meta.field

Expand All @@ -17,3 +20,9 @@ case class ConfigMetaRestricted(
@(ApiModelProperty @field)(description = "Configuration key") key: String,
@(ApiModelProperty @field)(description = "Configuration value.") value: Either[Boolean, List[String]]
)

object ConfigMetaRestricted {
implicit val encoder: Encoder[ConfigMetaRestricted] = {
Encoder.instance(x => x.asJson)
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,10 @@
package no.ndla.myndla.model.domain.config

import enumeratum._
import io.circe.Encoder
import io.circe.generic.semiauto.deriveEncoder
import sttp.tapir.CodecFormat.TextPlain
import sttp.tapir.{Codec, CodecFormat, DecodeResult, Schema}

sealed abstract class ConfigKey(override val entryName: String) extends EnumEntry

Expand Down

0 comments on commit 0a29a20

Please sign in to comment.