-
Notifications
You must be signed in to change notification settings - Fork 18
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
feat: Add route that sets projectRestrictedViewSetting size (DEV-2304) (
#2794) Signed-off-by: Marcin Procyk <[email protected]> Co-authored-by: Christian Kleinbölting <[email protected]>
- Loading branch information
Showing
18 changed files
with
388 additions
and
26 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
45 changes: 45 additions & 0 deletions
45
webapi/src/main/scala/dsp/valueobjects/RestrictedViewSize.scala
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,45 @@ | ||
/* | ||
* Copyright © 2021 - 2023 Swiss National Data and Service Center for the Humanities and/or DaSCH Service Platform contributors. | ||
* SPDX-License-Identifier: Apache-2.0 | ||
*/ | ||
|
||
package dsp.valueobjects | ||
|
||
import zio.json.JsonCodec | ||
|
||
import scala.util.matching.Regex | ||
|
||
/** | ||
* RestrictedViewSize value object. | ||
*/ | ||
sealed abstract case class RestrictedViewSize private (value: String) | ||
|
||
object RestrictedViewSize { | ||
def make(value: String): Either[String, RestrictedViewSize] = { | ||
val trimmed: String = value.trim | ||
// matches strings "pct:1-100" | ||
val percentagePattern: Regex = "pct:[1-9][0-9]?0?$".r | ||
// matches strings "!x,x" where x is a number of pixels | ||
val dimensionsPattern: Regex = "!\\d+,\\d+$".r | ||
def isSquare: Boolean = { | ||
val substr = trimmed.substring(1).split(",").toSeq | ||
substr.head == substr.last | ||
} | ||
|
||
if (value.isEmpty) Left(ErrorMessages.RestrictedViewSizeMissing) | ||
else if (percentagePattern.matches(trimmed)) Right(new RestrictedViewSize(trimmed) {}) | ||
else if (dimensionsPattern.matches(trimmed) && isSquare) Right(new RestrictedViewSize(trimmed) {}) | ||
else Left(ErrorMessages.RestrictedViewSizeInvalid(value)) | ||
} | ||
|
||
def unsafeFrom(value: String): RestrictedViewSize = | ||
make(value).fold(s => throw new IllegalArgumentException(s), identity) | ||
|
||
implicit val codec: JsonCodec[RestrictedViewSize] = | ||
JsonCodec[String].transformOrFail(RestrictedViewSize.make, _.value) | ||
} | ||
|
||
object ErrorMessages { | ||
val RestrictedViewSizeMissing = "RestrictedViewSize cannot be empty." | ||
val RestrictedViewSizeInvalid = (v: String) => s"Invalid RestrictedViewSize: $v" | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.