I have been fascinated by the Sudoku puzzle. It has been my dream to build the sudoku puzzle all by myself. Learning Scala has helped me fulfill that easier.

I have used AngularJS to make it a single page application. Using Bootstrap from Twitter helped me make the page look prettier.

Then I used Heroku to deploy it in cloud. Here’s the link. Sudoku in Scala It may not be perfect, but I loved working on it. Learned a lot in the way.

I want to write how I developed this one day.

I was playing with Play and Scala. I decided to use Squeryl for Data access.

Here’s my model class models/Product.scala

package models

import org.squeryl.KeyedEntity
import org.squeryl.PrimitiveTypeMode.inTransaction
import org.squeryl.PrimitiveTypeMode._
import org.squeryl.Schema

object AppDB extends Schema {
  val productsTable = table[Product]("products")
}

case class Product (name:String,price:Long) extends KeyedEntity[Long] {
	val id:Long = 0;
}

object Product {
  def create (product:Product)= transaction {
    AppDB.productsTable insert(product)
  }

  def all:Iterable[Product] = inTransaction {
    val products = from (AppDB.productsTable)(productsTable => 
    		select(productsTable))
    products.toIterable
  }
}

Here’s my controller controllers/Products.scala

package controllers

import play.mvc.Controller
import play.api.mvc._
import play.api.mvc.Results._
object Products extends Controller {

	def index = Action {
		  val products = models.Product.all
		  Ok(views.html.products.index(products))

	}

}

Now, when I run my app localhost:8080/products I get the following error.
[RuntimeException: no session is bound to current thread, a session must be created via Session.create and bound to the thread via ‘work’ or ‘bindToCurrentThread’]

It appears that I should have wrapped the index action in Products controller around a transaction block. Here’s the updated code.

package controllers

import play.mvc.Controller
import play.api.mvc._
import play.api.mvc.Results._
object Products extends Controller {

	def index = Action { 
                  transaction {
		  val products = models.Product.all
		  Ok(views.html.products.index(products))
                  }

	}

}

After this I was able to load products pages. I have to dig deeper to get the details. Will update when I find more information.

I am diving into Play! framework for Scala. While I’ve been working on MySQL all this time, I thought I’ll use a new database to learn Play! for Scala. Thats how I decided to use PostgreSQL. So, here’s how I got it setup.

1. Add Dependency project/Build.scala

val appDependencies = Seq(
      // Add your project dependencies here,
        "postgresql" % "postgresql" % "9.1-901-1.jdbc4"
    )

 2.  Configure database access in conf/application.conf

db.default.driver=org.postgresql.Driver
db.default.url="jdbc:postgresql://localhost:5432/YOUR_POST_GRE_SQL_DATABASE"
db.default.user=USERNAME
db.default.password=PASSWORD

3. Using evolutions

Evolutions help you propagate your schema changes easily to your database.  Its similar to migrations in Ruby on Rails. Each evolution script is a plain sql script. The file name goes as 1.sql, 2.sql and so on. Evolutions are placed under the  directoryconf/evolutions/<DATABASE_NAME>. Here is an example.

conf/evolutions/warehouse/1.sql

# --- !Ups

CREATE TABLE products (
	id serial primary key,
	name text,
	price float
);

# -- !Downs

DROP TABLE products;