Permalink
Browse files

Adding pp is now supported (raw version)

Also added distance calculation method
  • Loading branch information...
1 parent 2ccd502 commit 0c24cbc61030534685afb9197528fb8cbee37b29 @legzo committed Dec 22, 2011
View
1 app/Bootstrap.java
@@ -11,6 +11,7 @@ public void doJob() {
if (User.count() == 0) {
Fixtures.loadModels("initial-data.yml");
}
+
}
}
View
17 app/controllers/PickupPoints.java
@@ -0,0 +1,17 @@
+package controllers;
+
+import models.PickupPoint;
+import play.Logger;
+import play.mvc.Controller;
+
+public class PickupPoints extends Controller {
+
+ public static void addNew(String email, PickupPoint newPP) {
+ Logger.info("Adding PP %s for %s", newPP.label, email);
+
+ newPP.owner = Users.getUser(email);
+ newPP.save();
+
+ Users.displayPickupPoints(email);
+ }
+}
View
26 app/controllers/Users.java
@@ -20,24 +20,26 @@ public static void addUser() {
}
public static void displayPickupPoints(String email) {
- getUserAndRender(email);
+ User user = getUser(email);
+ render(user);
}
public static void displayUser(String email) {
- getUserAndRender(email);
+ User user = getUser(email);
+ render(user);
}
- private static void getUserAndRender(String email) {
+ public static User getUser(String email) {
List<User> users = User.find("byEmail", email).fetch();
-
+ User user = null;
if (users.size() == 1) {
- User user = users.iterator().next();
+ user = users.iterator().next();
Logger.info("Displaying: %s", user);
- render(user);
} else {
Logger.info("No user found");
// TODO handle error
}
+ return user;
}
public static void updateUser(Long id, User user) {
@@ -54,15 +56,21 @@ public static void updateUser(Long id, User user) {
}
public static void displayMyProfile() {
- getUserAndRender(getConnectedUserLogin());
+ User user = getUser(getConnectedUserLogin());
+ render(user);
}
public static void displayMyPickupPoints() {
- getUserAndRender(getConnectedUserLogin());
+ User user = getUser(getConnectedUserLogin());
+ render(user);
}
- private static String getConnectedUserLogin() {
+ public static String getConnectedUserLogin() {
return "legzo@gmail.com";
}
+ public static User getConnectedUser() {
+ return getUser(getConnectedUserLogin());
+ }
+
}
View
37 app/models/PickupPoint.java
@@ -1,13 +1,20 @@
package models;
+import java.text.DecimalFormat;
+
import javax.persistence.Entity;
import javax.persistence.ManyToOne;
+import javax.persistence.Transient;
import play.db.jpa.Model;
@Entity
public class PickupPoint extends Model {
+ @Transient
+ private final static DecimalFormat distanceFormat = new DecimalFormat(
+ "#.##");
+
public String label;
public float latitude;
@@ -18,13 +25,41 @@
@ManyToOne
public User owner;
+ public PickupPoint(String label, Direction direction) {
+ this(null, label, direction);
+ }
+
public PickupPoint(User owner, String label, Direction direction) {
super();
this.label = label;
this.direction = direction;
this.owner = owner;
- owner.pickupPoints.add(this);
+ if (owner != null) {
+ owner.pickupPoints.add(this);
+ }
+ }
+
+ public double distanceTo(PickupPoint otherPP) {
+ double distance = -1f;
+
+ double theta = this.longitude - otherPP.longitude;
+ distance = Math.sin(Math.toRadians(this.latitude))
+ * Math.sin(Math.toRadians(otherPP.latitude))
+ + Math.cos(Math.toRadians(this.latitude))
+ * Math.cos(Math.toRadians(otherPP.latitude))
+ * Math.cos(Math.toRadians(theta));
+ distance = Math.acos(distance);
+ distance = Math.toDegrees(distance);
+ distance = distance * 60 * 1.1515;
+ distance = distance * 1.609344;
+
+ return distance;
+ }
+
+ public String distanceToAsString(PickupPoint otherPP) {
+ double distance = distanceTo(otherPP);
+ return String.format("%s kms", distanceFormat.format(distance));
}
}
View
23 app/views/Users/displayPickupPoints.html
@@ -9,4 +9,25 @@
#{/list}
</ul>
-<a href="@{Users.displayUsers()}">- accueil</a>
+<a id="addNewPP" href="#">Ajouter un pickup point</a>
+
+<form id="addPP" action="@{PickupPoints.addNew(user.email)}" method="POST">
+ <div>
+ <label for="label">Nom:</label>
+ <input id="label" type="text" name="newPP.label">
+ </div>
+</form>
+
+<br/>
+
+<a href="@{Users.displayUsers()}">- accueil</a>
+
+<script type="text/javascript">
+ $(document).ready(function() {
+ $('#addNewPP').click(function() {
+ $('#addPP').fadeToggle();
+ });
+ });
+
+
+</script>
View
2 app/views/Users/displayUsers.html
@@ -1,5 +1,5 @@
#{extends 'main.html' /}
-#{set title: 'Home' /}
+#{set title: 'Accueil' /}
Salut tout le monde:
View
1 conf/routes
@@ -8,6 +8,7 @@ GET /users Users.displayUsers
GET /user/{email} Users.displayUser
GET /user/{email}/pp Users.displayPickupPoints
POST /user Users.updateUser
+POST /user/{email}/pp PickupPoints.addNew
GET /me Users.displayMyProfile
GET /me/pp Users.displayMyPickupPoints
View
3 public/stylesheets/main.css
@@ -0,0 +1,3 @@
+#addPP {
+ display: none;
+}
View
59 test/UserTest.java
@@ -16,16 +16,71 @@ public void setup() {
}
@Test
- public void savePickupPoints() {
+ public void shouldSavePickupPointsUserRelationCorrectly() {
User user = new User("Test").save();
new PickupPoint(user, "Mi casa", Direction.to).save();
assertEquals(1, User.count());
assertEquals(1, PickupPoint.count());
- new PickupPoint(user, "L'école de mon fils", Direction.to).save();
+ PickupPoint pp = new PickupPoint(user, "L'école de mon fils",
+ Direction.to);
+ pp.save();
assertEquals(2, user.pickupPoints.size());
}
+
+ @Test
+ public void shouldNotFailToCreatePPWithoutOwner() {
+ new PickupPoint("test", Direction.from);
+ }
+
+ @Test
+ public void shouldCalculateNullDistancesBetweenPPWhenNoCoords() {
+ PickupPoint bordeaux = new PickupPoint("Bordeaux", Direction.to);
+ PickupPoint fargues = new PickupPoint("Fargues", Direction.to);
+
+ double distanceTo = bordeaux.distanceTo(fargues);
+
+ assertTrue(distanceTo == 0);
+ }
+
+ @Test
+ public void shouldCalculateDistancesBetweenPP() {
+ PickupPoint bordeaux = new PickupPoint("Bordeaux", Direction.to);
+ PickupPoint fargues = new PickupPoint("Fargues", Direction.to);
+ PickupPoint taff = new PickupPoint("Pessac", Direction.to);
+
+ bordeaux.latitude = 44.843899f;
+ bordeaux.longitude = -0.5641846999999416f;
+
+ fargues.latitude = 44.824248f;
+ fargues.longitude = -0.445824000000016f;
+
+ taff.latitude = 44.77674f;
+ taff.longitude = -0.6526569999999765f;
+
+ double farguesToBx = bordeaux.distanceTo(fargues);
+ double farguesToPessac = taff.distanceTo(fargues);
+
+ assertEquals(17.15202038330252, farguesToPessac, 0);
+ assertEquals(9.585097776031919, farguesToBx, 0);
+ }
+
+ @Test
+ public void shouldCalculateDistancesBetweenPPAsString() {
+ PickupPoint bordeaux = new PickupPoint("Bordeaux", Direction.to);
+ PickupPoint fargues = new PickupPoint("Fargues", Direction.to);
+
+ bordeaux.latitude = 44.843899f;
+ bordeaux.longitude = -0.5641846999999416f;
+
+ fargues.latitude = 44.824248f;
+ fargues.longitude = -0.445824000000016f;
+
+ String farguesToBx = bordeaux.distanceToAsString(fargues);
+
+ assertEquals("9,59 kms", farguesToBx);
+ }
}
View
16 test/UsersTest.java
@@ -5,12 +5,12 @@
public class UsersTest extends FunctionalTest {
- @Test
- public void testThatIndexPageWorks() {
- Response response = GET("/");
- assertIsOk(response);
- assertContentType("text/html", response);
- assertCharset(play.Play.defaultWebEncoding, response);
- }
-
+ @Test
+ public void testThatIndexPageWorks() {
+ Response response = GET("/");
+ assertIsOk(response);
+ assertContentType("text/html", response);
+ assertCharset(play.Play.defaultWebEncoding, response);
+ }
+
}

0 comments on commit 0c24cbc

Please sign in to comment.