Skip to content

Commit d867d33

Browse files
author
Karl Seguin
committed
chapter 6
1 parent c4eec83 commit d867d33

File tree

4 files changed

+13
-5
lines changed

4 files changed

+13
-5
lines changed

app/controllers/Meetups.java

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -60,8 +60,8 @@ public static void findRecent(int page) {
6060
renderJSON(Recent.find(page));
6161
}
6262

63-
public static void find() {
64-
renderText("this is for later");
63+
public static void find(float x, float y) {
64+
renderJSON(Meetup.find(x, y));
6565
}
6666

6767
@Util

app/jobs/Bootstrap.java

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,11 +1,14 @@
11
package jobs;
22

33
import play.jobs.*;
4+
import com.mongodb.*;
45
import play.modules.morphia.MorphiaPlugin;
56

67
@OnApplicationStart
78
public class Bootstrap extends Job {
89
public void doJob() {
910
MorphiaPlugin.ds().ensureCaps();
11+
DBObject object = BasicDBObjectBuilder.start().add("coordinates", "2d").add("date", 1).get();
12+
MorphiaPlugin.ds().getCollection(models.Meetup.class).ensureIndex(object);
1013
}
1114
}

app/models/Meetup.java

Lines changed: 7 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,7 @@
55
import play.data.validation.*;
66
import org.bson.types.ObjectId;
77
import play.modules.morphia.Model;
8+
import com.google.code.morphia.query.Query;
89
import com.google.code.morphia.annotations.*;
910

1011
@Entity(value="meetups", noClassnameStored=true)
@@ -31,14 +32,19 @@ public class Meetup extends Model {
3132
public String description;
3233

3334
@Required
34-
public ObjectId user;
35+
public String user;
3536

3637
@Required @ExactListSize(value=2, message="Should have 2 values")
3738
public List<Float> coordinates;
3839

3940
@MaxListSize(value=5, message="Can't have more than 5") @MaxListItemSize(value=15, message="A tag should be less than 15 characters")
4041
public Set<String> tags;
4142

43+
public static List<Meetup> find(float x, float y) {
44+
Query<Meetup> query = ds().createQuery(Meetup.class).field("coordinates").near(x, y).field("date").greaterThanOrEq(new Date()).limit(200);
45+
return query.asList();
46+
}
47+
4248
public static PagedResult findUpcoming(int page) {
4349
int limit = 5;
4450
int offset = (page - 1) * limit;

public/javascripts/views/viewHome.js

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -36,9 +36,8 @@ $(document).ready(function() {
3636
$.get('/meetups/find', {'x': center.lng(), 'y': center.lat(), 'tags': tags}, gotMapMeetups, 'json');
3737
}
3838

39-
function gotMapMeetups(r) {
39+
function gotMapMeetups(meetups) {
4040
clearExistingMarkers();
41-
var meetups = r.models;
4241
for(var i = 0; i < meetups.length; ++i) {
4342
var meetup = meetups[i];
4443
var point = new google.maps.LatLng(meetup.coordinates[1], meetup.coordinates[0])

0 commit comments

Comments
 (0)