Creating two different types of users (Scala, Lift)

I am creating a website that needs two types of users: students and providers. In a traditional java setup, I would create a user class (or interface), and then create two classes inherited from the user. Is this the best course in scala too, using the "extends" and "with" modifiers? If this is really the best way (I suspect it is), what is the best way to map this to the DB? Would it be better to save the type column and then set it to one or the other?

The second question is how to work with the view. The display will be very different depending on what type of user it is, and therefore I believe that there will be some serious routing logic, or at least the logic embedded in the fragments in the view.

I think the main question is: is there a “preferred” way to do this (for example, a recipe on rails or some such), or am I kind of like myself?

thanks

+3
source share
1 answer

If this is really the best way (I suppose it is), what is the best way to map this to the DB? Would it be better to save the type column and then set it to one or the other?

, " " . DRY.

, , Student Provider , . , , , OO.

, , - "UserType" User. , . , , , Homework, StudentID, .

- , , - , , , Student Provider, Scala.

Lift -:

O-R .

class WikiEntry extends KeyedMapper[Long, WikiEntry] {
  def getSingleton = WikiEntry // what the "meta" object
  def primaryKeyField = id

  // the primary key
  object id extends MappedLongIndex(this)

  // the name of the entry
  object name extends MappedString(this, 32) {
    override def dbIndexed_? = true // indexed in the DB
  }

  object owner extends MappedLongForeignKey(this, User)

  // the text of the entry
  object entry extends MappedTextarea(this, 8192) {
    override def textareaRows  = 10
    override def textareaCols = 50
  }
}

.

:

Scala:

trait Posting[MyType <: Mapper[MyType]] { // Defines some common fields for posted user content 
  self: MyType => 
  def primaryKeyField = id 
  object id extends MappedLongIndex(this) 
  object creator extends MappedLongForeignKey(this, User) 
  object createdAt extends MappedLong(this) { 
    override def defaultValue = System.currentTimeMillis 
  } 
} 

class FooPosting extends KeyedMapper[FooPosting] with Posting[MyType]
+8

Source: https://habr.com/ru/post/1719131/


All Articles