From 4a9bc29137016576141c1237410f066d3d9728d4 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Axel=20Hern=C3=A1ndez?= Date: Sun, 18 Sep 2011 13:14:24 +0100 Subject: [PATCH] ajax : enviando formulario --- twitter/app/controllers/Api.java | 40 ++++++++++++ twitter/app/responses/ApiResponse.java | 8 +++ twitter/app/views/Timeline/index.html | 86 ++++++++++++++++++++++---- twitter/app/views/main.html | 8 ++- twitter/conf/dependencies.yml | 1 + twitter/conf/routes | 3 + 6 files changed, 132 insertions(+), 14 deletions(-) create mode 100644 twitter/app/controllers/Api.java create mode 100644 twitter/app/responses/ApiResponse.java diff --git a/twitter/app/controllers/Api.java b/twitter/app/controllers/Api.java new file mode 100644 index 0000000..d5cf927 --- /dev/null +++ b/twitter/app/controllers/Api.java @@ -0,0 +1,40 @@ +package controllers; + +import java.util.List; + +import flexjson.JSONSerializer; + +import models.Tweet; +import models.User; +import play.mvc.Controller; +import play.utils.Utils; +import responses.ApiResponse; + +public class Api extends Controller { + + public static void tweetsNew(String msg){ + Tweet t = new Tweet(msg, Security.userConnected()); + t.validateAndSave(); + ApiResponse resp = new ApiResponse(); + if(validation.hasErrors()){ + resp.status = "ERROR"; + resp.message = Utils.join(validation.errors(), ","); + }else{ + resp.status = "OK"; + resp.result = t; + } + JSONSerializer serializer = new JSONSerializer().include("status", "message", "result.msg", "result.date", "result.author.username").exclude("*"); + renderJSON(serializer.serialize(resp)); + } + + public static void tweetsAll(){ + List tweets = Tweet.find("order by date desc").fetch(); + renderJSON(new JSONSerializer().include("msg", "date", "author.username").exclude("*").serialize(tweets)); + } + + public static void tweetsFromUser(String username){ + List tweets = Tweet.find("select tweet from Tweet tweet where tweet.author.username = ? order by tweet.date desc", username).fetch(); + renderJSON(new JSONSerializer().include("msg", "date", "author.username").exclude("*").serialize(tweets)); + } + +} diff --git a/twitter/app/responses/ApiResponse.java b/twitter/app/responses/ApiResponse.java new file mode 100644 index 0000000..28d9681 --- /dev/null +++ b/twitter/app/responses/ApiResponse.java @@ -0,0 +1,8 @@ +package responses; + +public class ApiResponse { + public String status; + public String message; + public Object result; + +} diff --git a/twitter/app/views/Timeline/index.html b/twitter/app/views/Timeline/index.html index 28f7e89..98f3348 100644 --- a/twitter/app/views/Timeline/index.html +++ b/twitter/app/views/Timeline/index.html @@ -8,21 +8,81 @@ #{/set} -#{ifErrors} -
- #{errors} -

${error}

- #{/errors} -
-#{/ifErrors} -#{form @Timeline.create()} +
+ × +

+
+ + + +
- +
-#{/form} +
+ +
+ +
+ + -#{list items:tweets, as:'tweet'} - #{renderTweet tweet /} -#{/list} \ No newline at end of file + \ No newline at end of file diff --git a/twitter/app/views/main.html b/twitter/app/views/main.html index e3f9a39..93a2094 100644 --- a/twitter/app/views/main.html +++ b/twitter/app/views/main.html @@ -6,10 +6,16 @@ - + + + + + + + #{get 'moreStyles' /} #{get 'moreScripts' /} diff --git a/twitter/conf/dependencies.yml b/twitter/conf/dependencies.yml index 28005e5..ee21ff2 100644 --- a/twitter/conf/dependencies.yml +++ b/twitter/conf/dependencies.yml @@ -3,3 +3,4 @@ require: - play - play -> secure + - net.sf.flexjson -> flexjson 2.1 diff --git a/twitter/conf/routes b/twitter/conf/routes index 588d7d9..238bd38 100644 --- a/twitter/conf/routes +++ b/twitter/conf/routes @@ -9,6 +9,9 @@ GET / Timeline.index GET /stats Timeline.stats POST /create Timeline.create +GET /api/tweets/fromUser/{username} Api.tweetsFromUser +GET /api/tweets/all Api.tweetsAll +GET /api/tweets/new Api.tweetsNew # Ignore favicon requests GET /favicon.ico 404