Permalink
Browse files

Finished invite/kick participants features; Added Users.ajaxGet metho…

…d for future usage;
  • Loading branch information...
1 parent 45e4a12 commit 8a3b04d060252cc5ac51144b38bf1c95988b30d1 @fallenlord committed Feb 18, 2011
View
@@ -23,6 +23,7 @@
* 可以定时自动更新新的Story
* 游戏页面可以直接编辑(点击标题就编辑标题)
* 牌的排列需要适应小屏幕
+* 关闭的游戏不允许增删人员和修改标题等操作
技术:
* 考虑重构游戏列表页面将初始显示也改为ajax的
View
@@ -36,6 +36,54 @@ public static void show(@Required Long gameId) {
render(game);
}
+ public static void ajaxInvite(@Required Long gameId, @Required String username) {
+ Game game = Game.findById(gameId);
+ if (game == null) {
+ notFound(Messages.get("game.message.not-exists", gameId));
+ }
+ User user = User.find("byUsername", username).first();
+ if (user == null) {
+ notFound(Messages.get("user.message.not-exists", username));
+ }
+ if (game.hasMember(user)) {
+ error(409, Messages.get("participant.message.already-exists", user.username, game.name));
+ }
+ User connectedUser = Security.connectedUser();
+ if (game.type != Game.Type.BAZAAR && !game.master.equals(connectedUser)) {
+ forbidden(Messages.get("participant.message.forbidden", game.name));
+ }
+
+ game.addParticipant(user);
+ game.save();
+
+ request.format = "json";
+ render(user);
+ }
+
+ public static void ajaxKick(@Required Long gameId, @Required String username) {
+ Game game = Game.findById(gameId);
+ if (game == null) {
+ notFound(Messages.get("game.message.not-exists", gameId));
+ }
+ User user = User.find("byUsername", username).first();
+ if (user == null) {
+ notFound(Messages.get("user.message.not-exists", username));
+ }
+ if (!game.participants.contains(user)) {
+ notFound(Messages.get("participant.message.not-exists", username));
+ }
+ User connectedUser = Security.connectedUser();
+ if (game.type != Game.Type.BAZAAR && !game.master.equals(connectedUser)) {
+ forbidden(Messages.get("participant.message.forbidden", game.name));
+ }
+
+ game.participants.remove(user);
+ game.save();
+
+ request.format = "json";
+ render(user);
+ }
+
public static void ajaxCreate(@Required Game game) {
User user = Security.connectedUser();
game.type = Game.Type.TALKSHOW; // TODO This default type is temp
View
@@ -1,8 +1,15 @@
package controllers;
+import java.util.Collections;
+import java.util.List;
+
import models.User;
+import play.data.validation.Required;
+import play.i18n.Messages;
import play.mvc.Controller;
+import com.mchange.v2.sql.SqlUtils;
+
/**
* Users controller.
*
@@ -11,10 +18,40 @@
*/
public class Users extends Controller {
- public static void show(String username) {
+ private static final int DEFAULT_SUGGESTION_SIZE = 10;
+
+ public static void show(String username) {
User user = User.find("byUsername", username).first();
notFoundIfNull(user);
render(user);
}
+
+ public static void ajaxGet(@Required String username) {
+ User user = User.find("byUsername", username).first();
+ if (user == null) {
+ notFound(Messages.get("user.message.not-exists", username));
+ }
+ request.format = "json";
+ render(user);
+ }
+
+ public static void ajaxSuggest(@Required String term) {
+ if (term.length() == 0) {
+ renderJSON(Collections.emptyList());
+ }
+
+ // Retrive users
+ List<User> users;
+ String likeString = "%" + SqlUtils.escapeBadSqlPatternChars(term) + "%";
+ if (!likeString.contains("@")) {
+ users = User.find("(username like ? or name like ? or email like ?) order by username",
+ likeString, likeString, likeString + "@%").fetch(DEFAULT_SUGGESTION_SIZE);
+ } else {
+ users = User.find("email like ? order by username", likeString).fetch(DEFAULT_SUGGESTION_SIZE);
+ }
+
+ request.format = "json";
+ render(users);
+ }
}
@@ -0,0 +1 @@
+#{json.user user /}
@@ -0,0 +1 @@
+#{json.user user /}
View
@@ -2,6 +2,7 @@
#{set "title"}${game.name}#{/set}
#{set "moreScripts"}
<script src="@{'/public/javascripts/views/stories.js'}" type="text/javascript" charset="utf-8"></script>
+<script src="@{'/public/javascripts/views/participants.js'}" type="text/javascript" charset="utf-8"></script>
#{/set}
<div class="sidebar">
@@ -19,14 +20,9 @@ <h3>${game.name}</h3>
<div class="done">&{"game.time.end", game.endTime.formatInterval()}</div>
#{/elseif}
</div>
- <div class="participants">
+ <div id="participants" class="participants">
<h3>&{"game.participants"}</h3>
<ul>
- #{list items:game.participants, as:"participant"}
- <li class="user">
- <a href="@{Users.show(participant.id)}">${participant.name}</a>
- </li>
- #{/list}
</ul>
</div>
<div class="share">
@@ -104,12 +100,13 @@ <h3>${game.estimatingStory.name}</h3>
<script type="text/javascript">
$(function() {
+ // Stories
var getAllStoriesAction = #{jsAction @Stories.ajaxGetAll(":gameId") /}
var createStoryAction = #{jsAction @Stories.ajaxCreate(":gameId") /}
var updateStoryAction = #{jsAction @Stories.ajaxUpdate(":gameId", ":storyId") /}
var removeStoryAction = #{jsAction @Stories.ajaxDelete(":gameId", ":storyId") /}
- var show = new PP.views.Stories({
+ new PP.views.Stories({
game: #{json.game game /},
actions: {
getAllStories: getAllStoriesAction,
@@ -118,5 +115,26 @@ <h3>${game.estimatingStory.name}</h3>
removeStory: removeStoryAction
}
});
+
+ // Participants
+ var showUserAction = #{jsAction @Users.show(":username") /}
+ var suggestUserAction = #{jsAction @Users.ajaxSuggest() /}
+ var inviteUserAction = #{jsAction @Games.ajaxInvite(":gameId", ":username") /}
+ var kickUserAction = #{jsAction @Games.ajaxKick(":gameId", ":username") /}
+ new PP.views.Participants({
+ game: #{json.game game /},
+ participants: [
+ #{list items:game.participants, as:"participant"}
+ #{json.user participant /}
+ ${ participant_isLast ? "" : "," }
+ #{/list}
+ ],
+ actions: {
+ showUser: showUserAction,
+ suggestUser: suggestUserAction,
+ inviteUser: inviteUserAction,
+ kickUser: kickUserAction
+ }
+ });
})
</script>
@@ -0,0 +1 @@
+#{json.user user /}
@@ -0,0 +1,8 @@
+[
+#{list items:users, as:"user"}
+ {
+ "label": "${user.name}(${user.username}) - ${user.email}",
+ "value": "${user.username}"
+ }${ user_isLast ? "" : "," }
+#{/list}
+]
View
@@ -79,6 +79,10 @@ story.message.not-exists=Story %s not exists.
story.message.forbidden=You have no permission no story %s.
story.message.save-failure=Cannot save story %s.
+participant.invite=Invite people...
+participant.message.already-exists=User %s already joined in game %s
+participant.message.forbidden=You have no permission to join people to game %s
+
user.message.not-exists=User %s not exists.
secure.username=Username:
View
@@ -14,8 +14,12 @@ GET /games/{<\d+>gameId} Games
POST /ajax/games/? Games.ajaxCreate
PUT /ajax/games/{<\d+>gameId} Games.ajaxUpdate
DELETE /ajax/games/{<\d+>gameId} Games.ajaxDelete
+PUT /ajax/games/{<\d+>gameId}/participants/{username} Games.ajaxInvite
+DELETE /ajax/games/{<\d+>gameId}/participants/{username} Games.ajaxKick
GET /users/{username} Users.show
+GET /ajax/users/{username} Users.ajaxGet
+GET /ajax/suggestions/users Users.ajaxSuggest
POST /ajax/games/{<\d+>gameId}/stories/? Stories.ajaxCreate
GET /ajax/games/{<\d+>gameId}/stories/? Stories.ajaxGetAll
Oops, something went wrong.

0 comments on commit 8a3b04d

Please sign in to comment.