Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
6 changed files
with
132 additions
and
14 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -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<Tweet> 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<Tweet> 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)); | ||
} | ||
|
||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,8 @@ | ||
package responses; | ||
|
||
public class ApiResponse { | ||
public String status; | ||
public String message; | ||
public Object result; | ||
|
||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -3,3 +3,4 @@ | |
require: | ||
- play | ||
- play -> secure | ||
- net.sf.flexjson -> flexjson 2.1 |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters