INVITADO CARSAPP
";
+
+ s = s + "
* Ver todos los Posts ";
+ s = s + "
* Contactar con Admin ";
+ s = s +"
";
+ return s;
+ }
+
+}
diff --git a/src/main/java/com/unrc/app/MustacheTemplateEngine.java b/src/main/java/com/unrc/app/MustacheTemplateEngine.java
new file mode 100644
index 0000000..e073d33
--- /dev/null
+++ b/src/main/java/com/unrc/app/MustacheTemplateEngine.java
@@ -0,0 +1,37 @@
+package com.unrc.app;
+
+
+import com.github.mustachejava.DefaultMustacheFactory;
+import com.github.mustachejava.Mustache;
+import com.github.mustachejava.MustacheFactory;
+import java.io.IOException;
+import java.io.StringWriter;
+import org.eclipse.jetty.io.RuntimeIOException;
+import spark.ModelAndView;
+import spark.TemplateEngine;
+
+public class MustacheTemplateEngine extends TemplateEngine {
+
+ private MustacheFactory mustacheFactory;
+
+ /**
+ * Constructs a mustache template engine
+ */
+ public MustacheTemplateEngine() {
+ mustacheFactory = new DefaultMustacheFactory("templates");
+ }
+
+ @Override
+ public String render(ModelAndView modelAndView) {
+ String viewName = modelAndView.getViewName();
+
+ Mustache mustache = mustacheFactory.compile(viewName);
+ StringWriter stringWriter = new StringWriter();
+ try {
+ mustache.execute(stringWriter, modelAndView.getModel()).close();
+ } catch (IOException e) {
+ throw new RuntimeIOException(e);
+ }
+ return stringWriter.toString();
+ }
+ }
diff --git a/src/main/java/com/unrc/app/models/Address.java b/src/main/java/com/unrc/app/models/Address.java
new file mode 100644
index 0000000..72b2d5f
--- /dev/null
+++ b/src/main/java/com/unrc/app/models/Address.java
@@ -0,0 +1,48 @@
+package com.unrc.app.models;
+
+import com.unrc.app.ElasticSearch;
+import java.util.HashMap;
+import java.util.Map;
+import org.javalite.activejdbc.Model;
+
+public class Address extends Model {
+ static {
+
+ validatePresenceOf("street","city","province","postal_code","num");
+ }
+// @Override
+// public void afterCreate(){
+//
+// Map
json = new HashMap<>();
+// json.put("street", this.get("street"));
+// json.put("city", this.get("descrcityiption"));
+// json.put("province", this.get("province"));
+// json.put("postal_code", this.get("postal_code"));
+// json.put("num", this.get("num"));
+// ElasticSearch.client().prepareIndex("addresses", "address",String.valueOf(this.getId()))
+// .setSource(json)
+// .execute()
+// .actionGet();
+//
+// }
+
+ public static Address findByAddress(String direc , int num, String ciudad, String provincia, int codigoPostal){
+ return (Address.findFirst("street = ? and num = ? and city = ? and province = ? and postal_code =?", direc , num, ciudad,provincia,codigoPostal));
+ }
+
+ public static Address createAddress(String direc , int num, String ciudad, String provincia, int codigoPostal){
+ Address address = create("street", direc,"num", num,"city",ciudad,"province",provincia,"postal_code",codigoPostal);
+ address.saveIt();
+ return findByAddress(direc,num,ciudad,provincia,codigoPostal);
+ }
+
+ public static Boolean direcExistente(String direc, int num,String ciudad,String provincia, int codigoPostal){
+ return (Address.first("street = ? and num = ? and city = ? and province = ? and postal_code =?",direc, num, ciudad,provincia,codigoPostal) != null);
+ }
+
+ public static void deleteAddress(String dir, int num, String ciudad,String provincia, int codigoPostal){
+ Address address = Address.findByAddress(dir,num,ciudad,provincia,codigoPostal);
+ if(direcExistente(dir,num, ciudad,provincia,codigoPostal))
+ address.delete();
+ }
+}
\ No newline at end of file
diff --git a/src/main/java/com/unrc/app/models/Answer.java b/src/main/java/com/unrc/app/models/Answer.java
new file mode 100644
index 0000000..188ce0c
--- /dev/null
+++ b/src/main/java/com/unrc/app/models/Answer.java
@@ -0,0 +1,48 @@
+
+package com.unrc.app.models;
+
+import com.unrc.app.ElasticSearch;
+import java.util.HashMap;
+import java.util.Map;
+import org.javalite.activejdbc.Model;
+
+public class Answer extends Model{
+ static {
+ validatePresenceOf("id_user","description","id_question");
+ }
+// @Override
+// public void afterCreate(){
+//
+// Map json = new HashMap<>();
+// json.put("id_user", this.get("id_user"));
+// json.put("description", this.get("description"));
+// json.put("id_question", this.get("id_question"));
+// ElasticSearch.client().prepareIndex("questions", "question",String.valueOf(this.getId()))
+// .setSource(json)
+// .execute()
+// .actionGet();
+//
+// }
+ public static Answer findByAnswer(int id_user, int id_question){
+ return (findFirst("id_question = ? and id_user = ?", id_question, id_user));
+ }
+
+ public static Answer createAnswer(User user, int question, String descripcion){
+ int user2 = user.getInteger("id_user");
+ Answer answer =create("id_user",user2, "description",descripcion,"id_question",question);
+ answer.saveIt();
+ return findByAnswer(user2,question);
+ }
+
+ public static Boolean existAnswer(int id_question){
+ return (Answer.first("id_question = ? ", id_question) != null);
+ }
+
+ public static void deleteAnswer(int id_question){
+ if(existAnswer(id_question)){
+ Answer.delete("id_question = ? ", id_question);
+ }
+ }
+
+
+}
diff --git a/src/main/java/com/unrc/app/models/Message.java b/src/main/java/com/unrc/app/models/Message.java
new file mode 100644
index 0000000..b9d2edc
--- /dev/null
+++ b/src/main/java/com/unrc/app/models/Message.java
@@ -0,0 +1,49 @@
+package com.unrc.app.models;
+
+import com.unrc.app.ElasticSearch;
+import java.util.HashMap;
+import java.util.Map;
+import org.javalite.activejdbc.Model;
+
+
+public class Message extends Model {
+ static {
+ validatePresenceOf("id_user","description");
+ }
+// @Override
+// public void afterCreate(){
+//
+// Map json = new HashMap<>();
+// json.put("description", this.get("description"));
+// json.put("id_user", this.get("id_user"));
+//
+// System.out.println(this.getId()+" ----este es el id---");
+// ElasticSearch.client().prepareIndex("messages", "message",String.valueOf(this.getId()))
+// .setSource(json)
+// .execute()
+// .actionGet();
+//
+// }
+
+ public static Message findByMessage(int id_message ){
+ return (Message.findFirst("id_message =?", id_message ));
+ }
+
+ public static Message createMessage(int id_user, String descripcion){
+ Message message = create("id_user", id_user,"description", descripcion);
+ message.saveIt();
+ Message f = Message.findFirst("id_user =? and description=?", id_user,descripcion );
+ return findByMessage(f.getInteger("id_message"));
+ }
+
+ public static Boolean messageExistente(int id_message){
+ return (Message.first("id_message =?", id_message)!=null);
+ }
+
+ public static void deleteMessage(int id_message){
+ Message message = Message.findByMessage(id_message);
+ if(messageExistente(id_message))
+ message.delete("id_message = ?", id_message);
+ }
+
+}
diff --git a/src/main/java/com/unrc/app/models/MessageGuest.java b/src/main/java/com/unrc/app/models/MessageGuest.java
new file mode 100644
index 0000000..e84964c
--- /dev/null
+++ b/src/main/java/com/unrc/app/models/MessageGuest.java
@@ -0,0 +1,45 @@
+package com.unrc.app.models;
+
+import com.unrc.app.ElasticSearch;
+import java.util.HashMap;
+import java.util.Map;
+import org.javalite.activejdbc.Model;
+
+
+public class MessageGuest extends Model {
+ static {
+ validatePresenceOf("description");
+ }
+// @Override
+// public void afterCreate(){
+//
+// Map json = new HashMap<>();
+// json.put("description", this.get("description"));
+// System.out.println(this.getId()+" ----este es el id---");
+// ElasticSearch.client().prepareIndex("messageguests", "messageguest",String.valueOf(this.getId()))
+// .setSource(json)
+// .execute()
+// .actionGet();
+//
+// }
+
+ public static MessageGuest findByMessageGuest(int id_messageGuest){
+ return (MessageGuest.findFirst("id_messageGuest =?", id_messageGuest ));
+ }
+
+ public static void createMessageGuest(String descripcion){
+ MessageGuest messageGuest = create("description", descripcion);
+ messageGuest.saveIt();
+ }
+
+ public static Boolean messageGuestExistente(int id_messageGuest){
+ return (MessageGuest.first("id_messageGuest =?", id_messageGuest)!=null);
+ }
+
+ public static void deleteMessageGuest(int id_messageGuest){
+ MessageGuest messageGuest = MessageGuest.findByMessageGuest(id_messageGuest);
+ if(messageGuestExistente(id_messageGuest))
+ messageGuest.delete("id_messageGuest = ?", id_messageGuest);
+ }
+
+}
diff --git a/src/main/java/com/unrc/app/models/Post.java b/src/main/java/com/unrc/app/models/Post.java
new file mode 100644
index 0000000..6df42b9
--- /dev/null
+++ b/src/main/java/com/unrc/app/models/Post.java
@@ -0,0 +1,54 @@
+package com.unrc.app.models;
+import com.unrc.app.ElasticSearch;
+import java.util.HashMap;
+import java.util.List;
+import java.util.Map;
+import org.javalite.activejdbc.Model;
+
+public class Post extends Model {
+ static {
+ validatePresenceOf("id_user", "id_address","patent","description");
+ }
+// @Override
+// public void afterCreate(){
+//
+// Map json = new HashMap<>();
+// json.put("id_user", this.get("id_user"));
+// json.put("id_address", this.get("id_address"));
+// json.put("patent", this.get("patent"));
+// json.put("description", this.get("description"));
+// ElasticSearch.client().prepareIndex("posts", "post",String.valueOf(this.getId()))
+// .setSource(json)
+// .execute()
+// .actionGet();
+//
+// }
+
+ public static Post findByPost(int id_user, String patente){
+ return (findFirst("id_user = ? and patent = ?", id_user, patente));
+ }
+
+ public static Post createPost(User user, int id_address,String patente, String descripcion){
+ int user2 = user.getInteger("id_user");
+ Post post =create("id_user",user2,"id_address",id_address, "description",descripcion,"patent",patente);
+ post.saveIt();
+ return findByPost(user2,patente);
+ }
+
+ public static Boolean existPost(int id_user,String patente){
+ return (Post.first("id_user = ? and patent = ? ", id_user,patente) != null);
+ }
+
+ public static void deletePost(int id_post){
+ List q = Question.find("id_post =?",id_post);
+ for (int i = 0; i < q.size(); i++) {
+ Answer.deleteAnswer(q.get(i).getInteger("id_question"));
+
+ }
+
+ Question.delete("id_post = ?", id_post );
+
+ Post.delete("id_post = ? ",id_post);
+ }
+}
+
diff --git a/src/main/java/com/unrc/app/models/Question.java b/src/main/java/com/unrc/app/models/Question.java
new file mode 100644
index 0000000..e2c2a8b
--- /dev/null
+++ b/src/main/java/com/unrc/app/models/Question.java
@@ -0,0 +1,50 @@
+
+package com.unrc.app.models;
+import com.unrc.app.ElasticSearch;
+import java.util.HashMap;
+import java.util.Map;
+import org.javalite.activejdbc.Model;
+
+
+public class Question extends Model{
+ static {
+ validatePresenceOf("id_user","id_post","description");
+ }
+// @Override
+// public void afterCreate(){
+//
+// Map json = new HashMap<>();
+// json.put("id_user", this.get("id_user"));
+// json.put("id_post", this.get("id_post"));
+// json.put("description", this.get("description"));
+// ElasticSearch.client().prepareIndex("questions", "question",String.valueOf(this.getId()))
+// .setSource(json)
+// .execute()
+// .actionGet();
+//
+// }
+
+ public static Question findByQuestion(int id_user, int id_post){
+ return (findFirst("id_post = ? and id_user = ?", id_post, id_user));
+ }
+
+ public static Question createQuestion(User user, int id_post, String descripcion){
+ int user2 = user.getInteger("id_user");
+ Question question =create("id_user",user2, "description",descripcion,"id_post",id_post);
+ question.saveIt();
+ return findByQuestion(user2,id_post);
+ }
+
+ public static Boolean existQuestion(int id_user,int id_post){
+ return (Answer.first("id_user = ? and id_post = ? ", id_user,id_post) != null);
+ }
+
+ public static void deleteQuestion(int id_question,int id_user,int id_post){
+ if(existQuestion(id_user,id_post)){
+ Answer.delete("id_question = ?", id_question );
+ Question.delete("id_user = ? and id_post = ? ",id_user, id_post);
+ }
+ }
+
+
+}
diff --git a/src/main/java/com/unrc/app/models/User.java b/src/main/java/com/unrc/app/models/User.java
index 5441706..afa13be 100644
--- a/src/main/java/com/unrc/app/models/User.java
+++ b/src/main/java/com/unrc/app/models/User.java
@@ -1,9 +1,64 @@
package com.unrc.app.models;
+import com.unrc.app.ElasticSearch;
+import java.util.HashMap;
+import java.util.Map;
+import org.elasticsearch.action.get.GetResponse;
+import org.elasticsearch.action.search.SearchResponse;
import org.javalite.activejdbc.Model;
public class User extends Model {
- static {
- validatePresenceOf("first_name", "last_name");
- }
+ static {
+ validatePresenceOf("first_name", "last_name", "email", "contrasena");
+ }
+ @Override
+ public void afterCreate(){
+
+ Map json = new HashMap<>();
+ json.put("name", this.get("first_name"));
+ json.put("email", this.get("email"));
+ json.put("id", this.getId());
+ ElasticSearch.client().prepareIndex("users", "user",String.valueOf(this.getId()))
+ .setSource(json)
+ .execute()
+ .actionGet();
+ getUserElasticsearch();
+
+ }
+ public void getUserElasticsearch(){
+ System.out.println("---------------------------antesconsulta---------------------------------------------");
+
+ GetResponse response = ElasticSearch.client().prepareGet("users", "user", String.valueOf(this.getId()))
+ .execute()
+ .actionGet();
+ System.out.println("---------------------------consulta---------------------------------------------");
+
+ System.out.println("source---"+response.getSource());
+ }
+
+
+ public static User findByEmail(String email){
+ return (findFirst("email = ?", email));
+ }
+
+ public static Boolean existUser(int id_user){
+ return (User.first("id_user = ? ", id_user) != null);
+ }
+
+ public static User createUser(String nombre, String apellido, String email, String contrasena){
+ User user =create("first_name", nombre, "last_name", apellido, "email", email, "contrasena", contrasena);
+ user.saveIt();
+ return findByEmail(email);
+ }
+
+ public static void deleteUser(int id_user){
+ if(existUser(id_user)){
+ User.delete("id_user = ?", id_user);
+ Vehicle.delete("id_user=?", id_user);
+ Post.delete("id_user=?", id_user);
+ Answer.delete("id_user=?", id_user);
+ Question.delete("id_user=?", id_user);
+ }
+
+ }
}
diff --git a/src/main/java/com/unrc/app/models/UsersAddress.java b/src/main/java/com/unrc/app/models/UsersAddress.java
new file mode 100644
index 0000000..311ca1e
--- /dev/null
+++ b/src/main/java/com/unrc/app/models/UsersAddress.java
@@ -0,0 +1,47 @@
+package com.unrc.app.models;
+import com.unrc.app.ElasticSearch;
+import java.util.HashMap;
+import java.util.Map;
+import org.javalite.activejdbc.Model;
+import org.javalite.activejdbc.annotations.Table;
+
+public class UsersAddress extends Model {
+ static {
+ validatePresenceOf("id_user","id_address");
+ }
+// @Override
+// public void afterCreate(){
+//
+// Map json = new HashMap<>();
+// json.put("id_user", this.get("id_user"));
+// json.put("id_address", this.get("id_address"));
+// ElasticSearch.client().prepareIndex("usersAddress", "useraddress",String.valueOf(this.getId()))
+// .setSource(json)
+// .execute()
+// .actionGet();
+//
+// }
+
+ public static UsersAddress findByUsersAddress(int id_user, int id_address){
+ return (findFirst("id_user = ? and id_address= ?", id_user,id_address));
+ }
+
+ public static UsersAddress createUsersAddress(int id_user, int id_address){
+ UsersAddress answer =create("id_user",id_user, "id_address",id_address);
+ answer.saveIt();
+ return findByUsersAddress(id_user,id_address);
+ }
+
+ public static Boolean existUsersAddress(int id_user,int id_address){
+ return (UsersAddress.first("id_user = ? and id_address = ? ", id_user,id_address) != null);
+ }
+
+ public static void deleteUsersAddress(int id_user,int id_address){
+ if(existUsersAddress(id_user,id_address)){
+ UsersAddress.delete("id_user = ? and id_question = ? ",id_user, id_address);
+ }
+ }
+
+
+
+}
diff --git a/src/main/java/com/unrc/app/models/Vehicle.java b/src/main/java/com/unrc/app/models/Vehicle.java
new file mode 100644
index 0000000..4009dfd
--- /dev/null
+++ b/src/main/java/com/unrc/app/models/Vehicle.java
@@ -0,0 +1,68 @@
+
+package com.unrc.app.models;
+import com.unrc.app.ElasticSearch;
+import java.util.HashMap;
+import java.util.Map;
+import org.elasticsearch.action.get.GetResponse;
+import org.javalite.activejdbc.Model;
+import org.elasticsearch.client.Client;
+
+public class Vehicle extends Model{
+ static {
+ validatePresenceOf("mark", "model","patent","id_user", "color","tipo","cc","isCoupe","capacity");
+ }
+// @Override
+// public void afterCreate(){
+//
+// Map json = new HashMap<>();
+// json.put("mark", this.get("mark"));
+// json.put("model", this.get("model"));
+// json.put("patent", this.get("patent"));
+// json.put("id_user", this.get("id_user"));
+// json.put("color", this.get("color"));
+// json.put("tipo", this.get("tipo"));
+// json.put("cc", this.get("cc"));
+// json.put("isCoupe", this.get("isCoupe"));
+// json.put("capacity", this.get("capacity"));
+//
+// ElasticSearch.client().prepareIndex("vehicles", "vehicle",String.valueOf(this.getId()))
+// .setSource(json)
+// .execute()
+// .actionGet();
+//
+// }
+// public void getVehicleElasticsearch(){
+// System.out.println("---------------------------antesconsulta---------------------------------------------");
+// ElasticSearch.client().close();
+// GetResponse response = ElasticSearch.client().prepareGet("vehicles", "vehicle", "2")
+// .execute()
+// .actionGet();
+// System.out.println("---------------------------consulta---------------------------------------------");
+//
+// System.out.println("source---"+response.getSource());
+// System.out.println("source as string --- "+response.getSourceAsString());
+// }
+
+ public static Vehicle findByPatent(String patente){
+ return (findFirst("patent = ?", patente));
+ }
+
+ public static Boolean existVehicle(String patente){
+ return (Vehicle.first("patent = ? ", patente ) == null);
+ }
+
+ public static Vehicle createVehicle(String patente, String marca, String modelo,int usuario,String color,String tipo,int cc,String isCoupe,int capacity){
+ Vehicle vehiculo =create("patent", patente, "model", modelo, "mark", marca,"id_user",usuario,"color",color,"tipo",tipo,"cc",cc,"isCoupe",isCoupe,"Capacity",capacity);
+
+ vehiculo.saveIt();
+
+ return findByPatent(patente);
+ }
+
+ public static void deleteVehicle(String patente){
+ if(existVehicle(patente)){
+ Vehicle.delete("patent = ?", patente );
+ }
+ }
+
+}
diff --git a/src/main/java/com/unrc/app/users.mustache b/src/main/java/com/unrc/app/users.mustache
new file mode 100644
index 0000000..af4dbc0
--- /dev/null
+++ b/src/main/java/com/unrc/app/users.mustache
@@ -0,0 +1,12 @@
+ Users
+
+ Users count: {{users_count}}
+
+
+ {{#users}}
+
+ {{name}} |
+
+ {{/users}}
+
+
diff --git a/src/main/resources/users.mustache b/src/main/resources/users.mustache
new file mode 100644
index 0000000..af4dbc0
--- /dev/null
+++ b/src/main/resources/users.mustache
@@ -0,0 +1,12 @@
+ Users
+
+ Users count: {{users_count}}
+
+
+ {{#users}}
+
+ {{name}} |
+
+ {{/users}}
+
+
diff --git a/src/test/java/com/unrc/app/CityTest.java b/src/test/java/com/unrc/app/CityTest.java
new file mode 100644
index 0000000..812e783
--- /dev/null
+++ b/src/test/java/com/unrc/app/CityTest.java
@@ -0,0 +1,48 @@
+package com.unrc.app;
+
+import com.unrc.app.models.User;
+import com.unrc.app.models.Address;
+
+import org.javalite.activejdbc.Base;
+import org.junit.After;
+import org.junit.Before;
+import org.junit.Test;
+
+import static org.javalite.test.jspec.JSpec.the;
+import static org.junit.Assert.assertEquals;
+
+public class CityTest{
+ @Before
+ public void before(){
+ Base.open("com.mysql.jdbc.Driver", "jdbc:mysql://localhost/carsapp_development", "root", "");
+ System.out.println("UserTest setup");
+ Base.openTransaction();
+ }
+
+ @After
+ public void after(){
+ System.out.println("UserTest tearDown");
+ Base.rollbackTransaction();
+ Base.close();
+ }
+
+ @Test
+ public void shouldValidateMandatoryFields(){
+ User user = new User();
+ Address adress = new Address();
+ the(user).shouldNotBe("valid");
+ the(adress).shouldNotBe("valid");
+ the(adress.errors().get("street")).shouldBeEqual("value is missing");
+ the(adress.errors().get("city")).shouldBeEqual("value is missing");
+ the(adress.errors().get("province")).shouldBeEqual("value is missing");
+ the(adress.errors().get("postal_code")).shouldBeEqual("value is missing");
+ the(adress.errors().get("num")).shouldBeEqual("value is missing");
+
+ user.set("first_name", "John", "last_name", "Doe", "email", "example@email.com","contrasena","asd123");
+ adress.set("street","mitre","city","rio cuarto","province","cordoba","postal_code","5800","num","220");
+
+ // Everything is good:
+ the(user).shouldBe("valid");
+ the(adress).shouldBe("valid");
+ }
+}
diff --git a/src/test/java/com/unrc/app/PostTest.java b/src/test/java/com/unrc/app/PostTest.java
new file mode 100644
index 0000000..5000366
--- /dev/null
+++ b/src/test/java/com/unrc/app/PostTest.java
@@ -0,0 +1,53 @@
+package com.unrc.app;
+
+import com.unrc.app.models.User;
+import com.unrc.app.models.Post;
+import com.unrc.app.models.Vehicle;
+
+import org.javalite.activejdbc.Base;
+import org.junit.After;
+import org.junit.Before;
+import org.junit.Test;
+
+import static org.javalite.test.jspec.JSpec.the;
+import static org.junit.Assert.assertEquals;
+
+public class PostTest{
+ @Before
+ public void before(){
+ Base.open("com.mysql.jdbc.Driver", "jdbc:mysql://localhost/carsapp_development", "root", "");
+ System.out.println("UserTest setup");
+ Base.openTransaction();
+ }
+
+ @After
+ public void after(){
+ System.out.println("UserTest tearDown");
+ Base.rollbackTransaction();
+ Base.close();
+ }
+
+ @Test
+ public void shouldValidateMandatoryFields(){
+ User user = new User();
+ Vehicle vehicle = new Vehicle();
+ Post post = new Post();
+ the(user).shouldNotBe("valid");
+ the(post).shouldNotBe("valid");
+ the(vehicle).shouldNotBe("valid");
+ the(post.errors().get("id_user")).shouldBeEqual("value is missing");
+ the(post.errors().get("id_address")).shouldBeEqual("value is missing");
+ the(post.errors().get("patent")).shouldBeEqual("value is missing");
+ the(post.errors().get("description")).shouldBeEqual("value is missing");
+
+ post.set("id_user","1","id_address","1","patent","kff911","description","si es un buen coche");
+ user.set("first_name", "John", "last_name", "Doe", "email", "example@email.com","contrasena","asd123");
+ vehicle.set("id_vehicle","1","patent","kff911","mark","ferrari","model","enzo","id_user","1","color","rojo","tipo","auto","cc","0","isCoupe","si","capacity","2");
+
+
+ // Everything is good:
+ the(user).shouldBe("valid");
+ the(post).shouldBe("valid");
+ the(vehicle).shouldBe("valid");
+ }
+}
diff --git a/src/test/java/com/unrc/app/QuestionTest.java b/src/test/java/com/unrc/app/QuestionTest.java
new file mode 100644
index 0000000..55f78e2
--- /dev/null
+++ b/src/test/java/com/unrc/app/QuestionTest.java
@@ -0,0 +1,61 @@
+package com.unrc.app;
+
+import com.unrc.app.models.User;
+import com.unrc.app.models.Post;
+import com.unrc.app.models.Question;
+import com.unrc.app.models.Vehicle;
+
+import org.javalite.activejdbc.Base;
+import org.junit.After;
+import org.junit.Before;
+import org.junit.Test;
+
+import static org.javalite.test.jspec.JSpec.the;
+import static org.junit.Assert.assertEquals;
+
+public class QuestionTest{
+ @Before
+ public void before(){
+ Base.open("com.mysql.jdbc.Driver", "jdbc:mysql://localhost/carsapp_development", "root", "");
+ System.out.println("UserTest setup");
+ Base.openTransaction();
+ }
+
+ @After
+ public void after(){
+ System.out.println("UserTest tearDown");
+ Base.rollbackTransaction();
+ Base.close();
+ }
+
+ @Test
+ public void shouldValidateMandatoryFields(){
+ User user = new User();
+ User user2 = new User();
+ Vehicle vehicle = new Vehicle();
+ Post post = new Post();
+ Question question = new Question();
+ the(question).shouldNotBe("valid");
+ the(user).shouldNotBe("valid");
+ the(post).shouldNotBe("valid");
+ the(vehicle).shouldNotBe("valid");
+ the(post.errors().get("id_user")).shouldBeEqual("value is missing");
+ the(post.errors().get("id_address")).shouldBeEqual("value is missing");
+ the(post.errors().get("patent")).shouldBeEqual("value is missing");
+ the(post.errors().get("description")).shouldBeEqual("value is missing");
+
+ post.set("id_user","1","id_address","1","patent","kff911","description","es un gran coche");
+ user.set("first_name", "John", "last_name", "Doe", "email", "example@email.com","contrasena","asd123");
+ user2.set("first_name", "Juan", "last_name", "hulk", "email", "example2@email.com","contrasena","asd456");
+ vehicle.set("id_vehicle","1","patent","kff911","mark","ferrari","model","enzo","id_user","1","color","rojo","tipo","auto","cc","0","isCoupe","si","capacity","2");
+ question.set("id_user","2","id_post","1","description","cual es el precio");
+
+
+
+ // Everything is good:
+ the(user).shouldBe("valid");
+ the(post).shouldBe("valid");
+ the(vehicle).shouldBe("valid");
+ the(question).shouldBe("valid");
+ }
+}
diff --git a/src/test/java/com/unrc/app/UserAdressTest.java b/src/test/java/com/unrc/app/UserAdressTest.java
new file mode 100644
index 0000000..7052e74
--- /dev/null
+++ b/src/test/java/com/unrc/app/UserAdressTest.java
@@ -0,0 +1,54 @@
+package com.unrc.app;
+
+import com.unrc.app.models. *;
+
+
+import org.javalite.activejdbc.Base;
+import org.junit.After;
+import org.junit.Before;
+import org.junit.Test;
+
+import static org.javalite.test.jspec.JSpec.the;
+import static org.junit.Assert.assertEquals;
+
+public class UserAdressTest{
+ @Before
+ public void before(){
+ Base.open("com.mysql.jdbc.Driver", "jdbc:mysql://localhost/carsapp_development", "root", "");
+ System.out.println("UserTest setup");
+ Base.openTransaction();
+ }
+
+ @After
+ public void after(){
+ System.out.println("UserTest tearDown");
+ Base.rollbackTransaction();
+ Base.close();
+ }
+
+ @Test
+ public void shouldValidateMandatoryFields(){
+ User user = new User();
+ Post post = new Post();
+ UsersAddress address = new UsersAddress();
+ the(user).shouldNotBe("valid");
+ the(post).shouldNotBe("valid");
+ the(address).shouldNotBe("valid");
+ the(address.errors().get("id_user")).shouldBeEqual("value is missing");
+ the(address.errors().get("id_address")).shouldBeEqual("value is missing");
+
+ post.set("id_user","1","id_address","1","patent","kff911","description","es un gran coche");
+ user.set("first_name", "John", "last_name", "Doe", "email", "example@email.com","contrasena","asd123");
+ address.set("id_user","1","id_address","1");
+
+
+ // Everything is good:
+ the(user).shouldBe("valid");
+ the(post).shouldBe("valid");
+ the(address).shouldBe("valid");
+
+ }
+
+
+
+}
diff --git a/src/test/java/com/unrc/app/UserTest.java b/src/test/java/com/unrc/app/UserTest.java
index be59bd8..fae767c 100644
--- a/src/test/java/com/unrc/app/UserTest.java
+++ b/src/test/java/com/unrc/app/UserTest.java
@@ -13,7 +13,7 @@
public class UserTest{
@Before
public void before(){
- Base.open("com.mysql.jdbc.Driver", "jdbc:mysql://localhost/carsapp_test", "root", "");
+ Base.open("com.mysql.jdbc.Driver", "jdbc:mysql://localhost/carsapp_development", "root", "");
System.out.println("UserTest setup");
Base.openTransaction();
}
@@ -32,8 +32,10 @@ public void shouldValidateMandatoryFields(){
the(user).shouldNotBe("valid");
the(user.errors().get("first_name")).shouldBeEqual("value is missing");
the(user.errors().get("last_name")).shouldBeEqual("value is missing");
+ the(user.errors().get("email")).shouldBeEqual("value is missing");
+ the(user.errors().get("contrasena")).shouldBeEqual("value is missing");
- user.set("first_name", "John", "last_name", "Doe", "email", "example@email.com");
+ user.set("first_name", "John", "last_name", "Doe", "email", "example@email.com","contrasena","asd123");
// Everything is good:
the(user).shouldBe("valid");
diff --git a/src/test/java/com/unrc/app/VehicleTest.java b/src/test/java/com/unrc/app/VehicleTest.java
new file mode 100644
index 0000000..a17b22c
--- /dev/null
+++ b/src/test/java/com/unrc/app/VehicleTest.java
@@ -0,0 +1,52 @@
+package com.unrc.app;
+
+import com.unrc.app.models.User;
+import com.unrc.app.models.Vehicle;
+import org.javalite.activejdbc.Base;
+import org.junit.After;
+import org.junit.Before;
+import org.junit.Test;
+import static org.javalite.test.jspec.JSpec.the;
+import static org.junit.Assert.assertEquals;
+
+public class VehicleTest {
+
+ @Before
+ public void before() {
+ Base.open("com.mysql.jdbc.Driver", "jdbc:mysql://localhost/carsapp_development", "root", "");
+ System.out.println("UserTest setup");
+ Base.openTransaction();
+ }
+
+ @After
+ public void after() {
+ System.out.println("UserTest tearDown");
+ Base.close();
+ }
+
+ @Test
+ public void shouldValidateMandatoryFields() {
+ Vehicle vehicle = new Vehicle();
+ User user = new User();
+ the(user).shouldNotBe("valid");
+ the(vehicle).shouldNotBe("valid");
+ the(vehicle.errors().get("patent")).shouldBeEqual("value is missing");
+ the(vehicle.errors().get("mark")).shouldBeEqual("value is missing");
+ the(vehicle.errors().get("model")).shouldBeEqual("value is missing");
+ the(vehicle.errors().get("id_user")).shouldBeEqual("value is missing");
+ the(vehicle.errors().get("color")).shouldBeEqual("value is missing");
+ the(vehicle.errors().get("tipo")).shouldBeEqual("value is missing");
+ the(vehicle.errors().get("cc")).shouldBeEqual("value is missing");
+ the(vehicle.errors().get("isCoupe")).shouldBeEqual("value is missing");
+ the(vehicle.errors().get("capacity")).shouldBeEqual("value is missing");
+
+ user.set("first_name", "John", "last_name", "Doe", "email", "example@email.com","contrasena","asd123");
+ vehicle.set("patent","kff911","mark","ferrari","model","enzo","id_user","1","color","rojo","tipo","auto","cc","0","isCoupe","si","capacity","0");
+
+ // Everything is good:
+
+ the(user).shouldBe("valid");
+ the(vehicle).shouldBe("valid");
+
+ }
+}
diff --git a/users.mustache b/users.mustache
new file mode 100644
index 0000000..af4dbc0
--- /dev/null
+++ b/users.mustache
@@ -0,0 +1,12 @@
+ Users
+
+ Users count: {{users_count}}
+
+
+ {{#users}}
+
+ {{name}} |
+
+ {{/users}}
+
+