Skip to content

Commit b50fda4

Browse files
committed
Attempting to read/write to local MongoDB. Writing, good! Reading, not so good.
1 parent e0f0031 commit b50fda4

File tree

4 files changed

+74
-5
lines changed

4 files changed

+74
-5
lines changed

build.gradle

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -34,6 +34,7 @@ dependencies {
3434
"net.liftweb:lift-actor_$scalaVersion:$liftVersion",
3535
"net.liftweb:lift-common_$scalaVersion:$liftVersion",
3636
"net.liftweb:lift-json_$scalaVersion:$liftVersion",
37+
"net.liftweb:lift-mongodb-record_$scalaVersion:$liftVersion",
3738
"ch.qos.logback:logback-classic:0.9.28",
3839
"ch.qos.logback:logback-core:0.9.28",
3940
"com.h2database:h2:1.2.138"

src/main/scala/bootstrap/liftweb/Boot.scala

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2,14 +2,15 @@ package bootstrap.liftweb
22

33
import net.liftweb._
44
import util._
5+
import mongodb._
56
import Helpers._
67

78
import common._
89
import http._
910
import sitemap._
1011
import Loc._
1112

12-
import performance._
13+
import code.snippet._
1314

1415
/**
1516
* A class that's instantiated early and run. It allows the application
@@ -46,6 +47,8 @@ class Boot {
4647

4748
// Force the request to be UTF-8
4849
LiftRules.early.append(_.setCharacterEncoding("UTF-8"))
50+
51+
MongoDB.defineDb(DefaultMongoIdentifier, MongoAddress(MongoHost("localhost", 27017), "performance"))
4952

5053
}
5154
}
Lines changed: 51 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,51 @@
1+
package code.model
2+
3+
import net.liftweb.record.field._
4+
import net.liftweb.mongodb.record._
5+
import net.liftweb.mongodb.record.field._
6+
7+
class RunDoc private () extends MongoRecord[RunDoc] with MongoId[RunDoc] {
8+
9+
def meta = RunDoc
10+
11+
object name extends StringField(this, 128)
12+
object description extends StringField(this, 32)
13+
object tests extends DBRefField[RunDoc, TestDoc](this, TestDoc)
14+
object timestamp extends DateTimeField(this)
15+
16+
}
17+
18+
object RunDoc extends RunDoc with MongoMetaRecord[RunDoc] {
19+
}
20+
21+
class TestDoc private () extends MongoRecord[TestDoc] with MongoId[TestDoc] {
22+
23+
def meta = TestDoc
24+
25+
object name extends StringField(this, 128)
26+
27+
}
28+
29+
object TestDoc extends TestDoc with MongoMetaRecord[TestDoc] {
30+
}
31+
32+
class SampleDoc private () extends MongoRecord[SampleDoc] with MongoId[SampleDoc] {
33+
34+
def meta = SampleDoc
35+
36+
}
37+
38+
object SampleDoc extends SampleDoc with MongoMetaRecord[SampleDoc] {
39+
}
40+
41+
class PropertyDoc private () extends MongoRecord[PropertyDoc] with MongoId[PropertyDoc] {
42+
43+
def meta = PropertyDoc
44+
45+
object name extends StringField(this, 32)
46+
object value extends StringField(this, 256)
47+
48+
}
49+
50+
object PropertyDoc extends PropertyDoc with MongoMetaRecord[PropertyDoc] {
51+
}
Lines changed: 18 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1,21 +1,35 @@
1-
package performance
1+
package code {
2+
package snippet {
23

34
import net.liftweb.http._
45
import net.liftweb.http.rest._
56
import net.liftweb.json.JsonAST._
67

8+
import code.model._
9+
10+
import org.bson.types._
11+
712
object PerformanceRest extends RestHelper {
813

914
serve {
1015

11-
case Req("runs" :: id, "json", GetRequest) => {
12-
JObject(Nil)
16+
case Req("runs" :: id :: Nil, "json", GetRequest) => {
17+
JObject(RunDoc.find(id.toString).map(r => JField("name", JString(r.name.toString))).toList)
1318
}
1419

15-
case Req("tests" :: id, "json", GetRequest) => {
20+
case Req("runs" :: Nil, "json", PostRequest) => {
21+
val run = RunDoc.createRecord
22+
run.save
23+
JObject(JField("id", JString(run.id.toString)) :: Nil)
24+
}
25+
26+
case Req("tests" :: name, "json", GetRequest) => {
1627
JObject(Nil)
1728
}
1829

1930
}
2031

2132
}
33+
34+
}
35+
}

0 commit comments

Comments
 (0)