Giter Site home page Giter Site logo

Comments (7)

magnusandy avatar magnusandy commented on August 15, 2024

I do not really understand where the implicit class will be added, I created a Implicit ActiveDatabase for the default database,

class ActiveDatabase(name : String) {
  val dbName = name
}

package object globals {
  implicit lazy val database = new ActiveDatabase("default")
}

But I don't really understand how the implicit parameters work

from eatsafe.

KatrinaHoffert avatar KatrinaHoffert commented on August 15, 2024

First of all, note that the class can be simplified to merely class ActiveDatabase(val name: String). That will do the same as you've written, but the parameter will be name.

Anyway, now when you import globals (should be named Globals for consistency), the implicit value is in scope of the entire file. So then our model functions would take in a curried, implicit parameter (implicit parameters have to be curried).

Sample syntax: def getLocation(locationId: Int)(implicit activeDb: ActiveDatabase): Try[Location]. But it would be used the exact same way (ie, we don't have to specify the implicit parameter -- it'll be passed automatically).

We'd then use the variant of play.api.db.DB.withConnection that takes in the database name as a parameter (see the docs).

EDIT: The above link isn't working because Github breaks it... Copy it from here: https://www.playframework.com/documentation/2.0/api/scala/play/api/db/DB$.html.

from eatsafe.

magnusandy avatar magnusandy commented on August 15, 2024

This is what I have been trying to do but have been running into problems.
I changed the getLocation exactly as you just said, but then the calls in the controller need a implicit database, which I cannot seem to get from just importing the globals._

from eatsafe.

KatrinaHoffert avatar KatrinaHoffert commented on August 15, 2024

You imported that in the controller, right? If you're getting an error, what is it?

from eatsafe.

magnusandy avatar magnusandy commented on August 15, 2024

globals._ is imported in LocationController but there is an error

  def showLocation(locationId: Int) = Action {
    Location.getLocationById(locationId) match {
      case Success(location) =>
        Ok(views.html.locations.displayLocation(location))
      case Failure(ex) =>
        // TODO: Create a prettier error page
        InternalServerError("Encountered an error.\n\nDetails: " + ex.toString)
    }
  }

on line 2 the error is

could not find implicit value for parameter db: globals.ActiveDatabase
not enough arguments for method getLocationById: (implicit db: globals.ActiveDatabase)scala.util.Try[models.Location]. Unspecified value parameter db.

from eatsafe.

KatrinaHoffert avatar KatrinaHoffert commented on August 15, 2024

What's the full signature of getLocationById? What does the globals file look like?

I quickly threw together some code on my end and there's no errors. I copied the package object you previously mentioned into a file named Globals.scala and imported it into the LocationController. The signature of getLocationById is def getLocationById(locationId: Int)(implicit active: ActiveDatabase): Try[Location]. And I defined ActiveDatabase as class ActiveDatabase(val name: String).

from eatsafe.

magnusandy avatar magnusandy commented on August 15, 2024

This is now finished
Any methods that work with the database should have (implicit val activeDB: ActiveDatabase) as a parameter and use that ActiveDatabase to specify which database to connect to via

DB.getConnection(activeDB.name)  {  conn =>
  //use the connection in some way
}

from eatsafe.

Related Issues (20)

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. 📊📈🎉

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google ❤️ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.