Note that there are some explanatory texts on larger screens.

plurals
  1. POHow to refactor following function to reduce duplication? Slick, Pattern Matching, Play Framework
    primarykey
    data
    text
    <p>Anyone know how I can reduce duplication in the patterning matching statement in the following function? specifically i want to generalize the if statement. I'm thinking if I could somehow pass the object properties into a function then I could do it, but I have no idea how. Any help is MUCH APPRECIATED. THANKS!!</p> <pre><code> Ex: if (ASC) _.uid.asc else _.uid.desc </code></pre> <p>Below is the function</p> <pre><code> /** * Finds all the users and sorts by column ascending or descending. Ascending or Descending is determined by * if the orderBy value is positive or negative. These values are held in constants in @package models.AdminPage * @param orderBy The column to be sorted. * @return a list of sorted users. */ def findAll(orderBy: Int = DefaultSortByUid) = DB.withSession { implicit s: Session =&gt; val q = for(u &lt;- User) yield u // Get the absolute value to determine the column to be sorted. A valid value will be an Int that corresponds // to one of the constants defined in @package models.AdminPage. val sortByColumn = orderBy.abs val ASC = orderBy &gt; 0 val users = q.sortBy(sortByColumn match { case `SortByUid` =&gt; if (ASC) _.uid.asc else _.uid.desc case `SortByUserName` =&gt; if(ASC) _.userId.asc else _.userId.desc case `SortByAuthMethod` =&gt; if(ASC) _.authMethod.asc else _.authMethod.desc case `SortByRole` =&gt; if(ASC) _.role.asc else _.role.desc case `SortByEmail` =&gt; if(ASC) _.email.asc else _.email.desc case `SortByFirstName` =&gt; if(ASC) _.firstName.asc else _.firstName.desc case `SortByLastName` =&gt; if(ASC) _.lastName.asc else _.lastName.desc //An invalid value just goes to table main page and to default sort of ascending by uid case _ =&gt; _.uid.asc }).list users } </code></pre>
    singulars
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    plurals
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload