New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
refactor(projects): refactor projects route with value objects (DEV-64) #1909
Changes from 3 commits
367980b
8520336
3b3f32b
a0d5f97
1dff6e9
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,59 @@ | ||
package org.knora.webapi.messages.admin.responder.usersmessages | ||
|
||
import org.knora.webapi.IRI | ||
|
||
trait ProjectCreatePayloadTraitADM { | ||
def create( | ||
id: Option[IRI], | ||
shortname: Shortname, | ||
shortcode: Shortcode, | ||
longname: Option[Longname], | ||
description: Description, | ||
keywords: Keywords, | ||
logo: Option[Logo], | ||
status: Status, | ||
selfjoin: Selfjoin | ||
): ProjectCreatePayloadADM | ||
} | ||
|
||
/** | ||
* Project payload | ||
*/ | ||
sealed abstract case class ProjectCreatePayloadADM( | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. @irinaschubert I found the example for smart constructors. There is a private keyword missing:
|
||
id: Option[IRI], | ||
shortname: Shortname, | ||
shortcode: Shortcode, | ||
longname: Option[Longname], | ||
description: Description, | ||
keywords: Keywords, | ||
logo: Option[Logo], | ||
status: Status, | ||
selfjoin: Selfjoin | ||
) | ||
|
||
object ProjectCreatePayloadADM extends ProjectCreatePayloadTraitADM { | ||
|
||
/** The create constructor */ | ||
override def create( | ||
id: Option[IRI] = None, | ||
shortname: Shortname, | ||
shortcode: Shortcode, | ||
longname: Option[Longname] = None, | ||
description: Description, | ||
keywords: Keywords, | ||
logo: Option[Logo] = None, | ||
status: Status, | ||
selfjoin: Selfjoin | ||
): ProjectCreatePayloadADM = | ||
new ProjectCreatePayloadADM( | ||
id = id, | ||
shortname = shortname, | ||
shortcode = shortcode, | ||
longname = longname, | ||
description = description, | ||
keywords = keywords, | ||
logo = logo, | ||
status = status, | ||
selfjoin = selfjoin | ||
) {} | ||
} |
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -2,13 +2,6 @@ package org.knora.webapi.messages.admin.responder.usersmessages | |
|
||
import org.knora.webapi.IRI | ||
|
||
sealed trait ValidationError | ||
case object InvalidUsername extends ValidationError | ||
case object InvalidEmail extends ValidationError | ||
case object InvalidGivenOrFamilyName extends ValidationError | ||
case object InvalidPassword extends ValidationError | ||
case object InvalidLanguageCode extends ValidationError | ||
|
||
trait UserCreatePayloadTraitADM { | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. same here, trait is not needed. There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. a trait can be seen as a kind of an interface. you don't need an interface here, at is only used here and nowhere else. |
||
def create( | ||
id: Option[IRI], | ||
|
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,11 +1,14 @@ | ||
package org.knora.webapi.messages.admin.responder.usersmessages | ||
|
||
import org.knora.webapi.LanguageCodes | ||
import org.knora.webapi.exceptions.BadRequestException | ||
import org.knora.webapi.exceptions.{AssertionException, BadRequestException} | ||
import org.knora.webapi.messages.StringFormatter | ||
import org.knora.webapi.messages.store.triplestoremessages.StringLiteralV2 | ||
|
||
import scala.util.matching.Regex | ||
|
||
trait ValueObject[T, K] { | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. I don't like the ValueObject trait. It brings hardly any benefits, but makes the implementations look more complicated than necessary. Don't over engineer things. Please keep them simple. There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Do you mean by this, no trait at all here? |
||
val stringFormatter = StringFormatter.getGeneralInstance | ||
def create(value: K): Either[Throwable, T] | ||
} | ||
|
||
|
@@ -144,3 +147,105 @@ object SystemAdmin extends ValueObject[SystemAdmin, Boolean] { | |
override def create(value: Boolean): Either[Throwable, SystemAdmin] = | ||
Right(new SystemAdmin(value) {}) | ||
} | ||
|
||
/** | ||
* Project shortcode value object. | ||
*/ | ||
sealed abstract case class Shortcode(value: String) | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. missing |
||
|
||
object Shortcode extends ValueObject[Shortcode, String] { | ||
|
||
override def create(value: String): Either[Throwable, Shortcode] = | ||
if (value.isEmpty) { | ||
Left(BadRequestException("Missing shortcode")) | ||
} else { | ||
val shortcode: String = stringFormatter.validateProjectShortcode(value, throw AssertionException("not valid")) | ||
Right(new Shortcode(shortcode) {}) | ||
} | ||
} | ||
|
||
/** | ||
* Project shortname value object. | ||
*/ | ||
sealed abstract case class Shortname(value: String) | ||
|
||
object Shortname extends ValueObject[Shortname, String] { | ||
|
||
override def create(value: String): Either[Throwable, Shortname] = | ||
if (value.isEmpty) { | ||
Left(BadRequestException("Missing shortname")) | ||
} else { | ||
val shortname = stringFormatter.validateAndEscapeProjectShortname(value, throw AssertionException("not valid")) | ||
Right(new Shortname(shortname) {}) | ||
} | ||
} | ||
|
||
/** | ||
* Project longname value object. | ||
*/ | ||
sealed abstract case class Longname(value: String) | ||
|
||
object Longname extends ValueObject[Longname, String] { | ||
|
||
override def create(value: String): Either[Throwable, Longname] = | ||
if (value.isEmpty) { | ||
Left(BadRequestException("Missing long name")) | ||
} else { | ||
Right(new Longname(value) {}) | ||
} | ||
} | ||
|
||
/** | ||
* Project description value object. | ||
*/ | ||
sealed abstract case class Description(value: Seq[StringLiteralV2]) | ||
|
||
object Description extends ValueObject[Description, Seq[StringLiteralV2]] { | ||
|
||
override def create(value: Seq[StringLiteralV2]): Either[Throwable, Description] = | ||
if (value.isEmpty) { | ||
Left(BadRequestException("Missing description")) | ||
} else { | ||
Right(new Description(value) {}) | ||
} | ||
} | ||
|
||
/** | ||
* Project keywords value object. | ||
*/ | ||
sealed abstract case class Keywords(value: Seq[String]) | ||
|
||
object Keywords extends ValueObject[Keywords, Seq[String]] { | ||
|
||
override def create(value: Seq[String]): Either[Throwable, Keywords] = | ||
if (value.isEmpty) { | ||
Left(BadRequestException("Missing keywords")) | ||
} else { | ||
Right(new Keywords(value) {}) | ||
} | ||
} | ||
|
||
/** | ||
* Project logo value object. | ||
*/ | ||
sealed abstract case class Logo(value: String) | ||
|
||
object Logo extends ValueObject[Logo, String] { | ||
|
||
override def create(value: String): Either[Throwable, Logo] = | ||
if (value.isEmpty) { | ||
Left(BadRequestException("Missing logo")) | ||
} else { | ||
Right(new Logo(value) {}) | ||
} | ||
} | ||
|
||
/** | ||
* selfjoin value object. | ||
*/ | ||
sealed abstract case class Selfjoin(value: Boolean) | ||
|
||
object Selfjoin extends ValueObject[Selfjoin, Boolean] { | ||
override def create(value: Boolean): Either[Throwable, Selfjoin] = | ||
Right(new Selfjoin(value) {}) | ||
} |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Trait is superfluous here.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
call it something else
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
in this case you don't even need it, just delete it