Skip to content
This repository has been archived by the owner on Sep 13, 2022. It is now read-only.

Commit

Permalink
Topic adding
Browse files Browse the repository at this point in the history
  • Loading branch information
SkPhilipp committed Aug 21, 2012
1 parent 3ba348d commit d4c7645
Show file tree
Hide file tree
Showing 9 changed files with 113 additions and 33 deletions.
3 changes: 0 additions & 3 deletions src/khan/videos/models/Topic.java
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,6 @@
import java.util.Date;

import javax.persistence.Id;
import javax.persistence.Transient;

import com.googlecode.objectify.Key;

Expand All @@ -16,10 +15,8 @@ public class Topic implements Serializable {
@Id
private String name;
// Creation data
@Transient
private Key<AppUser> user;
private Date created;
@Transient
private Key<Topic> parent = null;

public Key<Topic> getParent() {
Expand Down
8 changes: 1 addition & 7 deletions src/khan/videos/models/Video.java
Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +19,6 @@ public enum Status {
@Id
private String youtubeId;
private Date submitted;
private String submitIPAddress;
private Key<AppUser> user;
private Key<Topic> topic;
private String title;
Expand All @@ -41,10 +40,6 @@ public Date getSubmitted() {
return this.submitted;
}

public String getSubmitIPAddress() {
return this.submitIPAddress;
}

public Key<AppUser> getUser() {
return this.user;
}
Expand All @@ -61,11 +56,10 @@ public void setTopic(Key<Topic> topic) {
this.topic = topic;
}

public Video(String youtubeId, String submitIPAddress, Key<AppUser> user, Key<Topic> topic, String title) {
public Video(String youtubeId, Key<AppUser> user, Key<Topic> topic, String title) {
this.topic = topic;
this.youtubeId = youtubeId;
this.submitted = Calendar.getInstance().getTime();
this.submitIPAddress = submitIPAddress;
this.user = user;
this.title = title;
this.status = Status.Voting;
Expand Down
45 changes: 45 additions & 0 deletions src/khan/videos/servlets/api/TopicServlet.java
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@
import javax.servlet.http.HttpServletResponse;

import khan.videos.DAO;
import khan.videos.models.AppUser;
import khan.videos.models.Topic;
import khan.videos.servlets.login.BaseUserServlet;

Expand All @@ -26,4 +27,48 @@ public void doGet(HttpServletRequest req, HttpServletResponse resp) throws IOExc
resp.getOutputStream().print(gson.toJson(topics));
}

@Override
public void doPost(HttpServletRequest req, HttpServletResponse resp, AppUser user) throws IOException {
// Check topic name
String topicName = req.getParameter("topicName");
String topicNameParent = req.getParameter("topic");
if (topicName == null || topicName.length() == 0) {
resp.sendError(HttpServletResponse.SC_BAD_REQUEST, "Voer een naam in voor het nieuwe onderdeel.");
return;
}
// Check < and > characters
if (topicName.contains("<") || topicName.contains(">")) {
resp.sendError(HttpServletResponse.SC_BAD_REQUEST,
"\"<\" en \">\" tekens zijn niet toegestaan in onderdeel namen.");
return;
}
// Check for max length
if (topicName.length() > 60) {
resp.sendError(HttpServletResponse.SC_BAD_REQUEST,
"De maximale lengte voor namen van onderdelen is 60 karakters.");
return;
}
// Check for double entry
DAO dao = DAO.get();
Topic existing = dao.ofy().find(Topic.class, topicName);
if (existing != null) {
resp.sendError(HttpServletResponse.SC_BAD_REQUEST, "Er bestaat al een onderdeel met deze naam.");
return;
}
// Check if parent topic; if parent topic exists
Topic parent = null;
if (topicNameParent != null && topicNameParent.length() > 0) {
parent = dao.ofy().find(Topic.class, topicNameParent);
if (parent == null) {
resp.sendError(HttpServletResponse.SC_BAD_REQUEST,
"Het onderdeel waar je je nieuwe onderdeel aan wilt toevoegen bestaat niet (meer).");
return;
}
}
// Success
Topic topic = new Topic(topicName, user, parent);
dao.addTopic(topic);
resp.setStatus(HttpServletResponse.SC_OK);
}

}
4 changes: 2 additions & 2 deletions src/khan/videos/servlets/api/VideoServlet.java
Original file line number Diff line number Diff line change
Expand Up @@ -76,8 +76,8 @@ public void doPost(HttpServletRequest req, HttpServletResponse resp, AppUser use
return;
}
// Success
Video video = new Video(youtubeId, req.getRemoteAddr(), new Key<AppUser>(AppUser.class, user.getId()),
new Key<Topic>(Topic.class, topicname), title);
Video video = new Video(youtubeId, new Key<AppUser>(AppUser.class, user.getId()), new Key<Topic>(Topic.class,
topicname), title);
dao.addVideo(video);
resp.setStatus(HttpServletResponse.SC_OK);
resp.getOutputStream().print("/vote?id=" + youtubeId);
Expand Down
8 changes: 4 additions & 4 deletions src/tests/DAOTest.java
Original file line number Diff line number Diff line change
Expand Up @@ -43,11 +43,11 @@ public void testTopics() {
@Test
public void testVideos() {
DAO dao = DAO.get();
dao.addVideo(new Video("YOUTUBEID0", "127.0.0.1", null, null, "Title"));
dao.addVideo(new Video("YOUTUBEID1", "127.0.0.1", null, null, "Title"));
dao.addVideo(new Video("YOUTUBEID2", "127.0.0.1", null, null, "Title"));
dao.addVideo(new Video("YOUTUBEID0", null, null, "Title"));
dao.addVideo(new Video("YOUTUBEID1", null, null, "Title"));
dao.addVideo(new Video("YOUTUBEID2", null, null, "Title"));
// Add the same video again
dao.addVideo(new Video("YOUTUBEID2", "127.0.0.1", null, null, "Title"));
dao.addVideo(new Video("YOUTUBEID2", null, null, "Title"));
// Videos: Root
{
List<Video> videos = dao.getTopicVideos(null);
Expand Down
2 changes: 1 addition & 1 deletion war/WEB-INF/appengine-web.xml
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
<?xml version="1.0" encoding="utf-8"?>
<appengine-web-app xmlns="http://appengine.google.com/ns/1.0">
<application>videotribunal</application>
<version>2</version>
<version>3</version>
<threadsafe>true</threadsafe>
<sessions-enabled>true</sessions-enabled>
<system-properties>
Expand Down
53 changes: 40 additions & 13 deletions war/js/treeview.js
Original file line number Diff line number Diff line change
Expand Up @@ -4,22 +4,27 @@ controller('TreeController', ['$scope', function($scope){
$scope.stack.splice(depth, $scope.stack.length - depth);
$scope.setTopic(obj);
};
$scope.setTopicDisabled = false;
$scope.setTopic = function(obj){
$scope.stack.push(obj);
var topic = obj == undefined ? undefined : obj.name;
$scope.current = topic;
var urlTopic = '/api/topic' + ( topic == undefined ? "" : '?topic=' + topic);
$.getJSON(urlTopic, function(cbd){
$scope.$apply(function(){
$scope.topics = cbd;
if($scope.setTopicDisabled == false){
$scope.setTopicDisabled = true;
$scope.stack.push(obj);
var topic = obj == undefined ? undefined : obj.name;
$scope.current = topic;
var urlTopic = '/api/topic' + ( topic == undefined ? "" : '?topic=' + topic);
$.getJSON(urlTopic, function(cbd){
$scope.$apply(function(){
$scope.topics = cbd;
$scope.setTopicDisabled = false;
});
});
});
var urlVideo = '/api/video' + ( topic == undefined ? "" : '?topic=' + topic);
$.getJSON(urlVideo, function(cbd){
$scope.$apply(function(){
$scope.videos = cbd;
var urlVideo = '/api/video' + ( topic == undefined ? "" : '?topic=' + topic);
$.getJSON(urlVideo, function(cbd){
$scope.$apply(function(){
$scope.videos = cbd;
});
});
});
}
};
$scope.addVideo = function(youtubeId){
$scope.addVideoStatus = 'loading';
Expand All @@ -44,6 +49,28 @@ controller('TreeController', ['$scope', function($scope){
}
});
};
$scope.addTopic = function(topicName){
$scope.addTopicStatus = 'loading';
$.ajax({
type: 'POST',
url: '/api/topic',
data: {
topicName: topicName,
topic: $scope.current
},
success: function(responseText){
$scope.$apply(function(){
$scope.addTopicStatus = 'success';
});
},
error: function(data){
$scope.$apply(function(){
$scope.addTopicMessage = data.statusText;
$scope.addTopicStatus = 'error';
});
}
});
};
$scope.stack = new Array();
$scope.setTopic();
}]);
22 changes: 20 additions & 2 deletions war/templates/home.html
Original file line number Diff line number Diff line change
Expand Up @@ -51,7 +51,7 @@ <h4 class="alert-heading">JavaScript staat uit!</h4>
{% if defaults.session %}
<li class="pull-right">
<a data-toggle="modal" href="#videoModal"class="btn" rel="tooltip" title="Een nieuwe video toevoegen aan het huidige onderdeel"><i class="icon-plus"></i> Video</a>
<!--<a data-toggle="modal" href="#topicModal" class="btn" rel="tooltip" title="Een nieuw onderdeel toevoegen onder het huidige onderdeel"><i class="icon-plus"></i> Topic</a>-->
<a data-toggle="modal" href="#topicModal" class="btn" rel="tooltip" title="Een nieuw onderdeel toevoegen onder het huidige onderdeel"><i class="icon-plus"></i> Topic</a>
</li>
{% endif %}
</ul>
Expand Down Expand Up @@ -124,9 +124,27 @@ <h3>Video Toevoegen</h3>
<h3>Onderdeel Toevoegen</h3>
</div>
<div class="modal-body">
<div class="control-group">
<label class="control-label">Onderdeel</label>
<div class="controls">
<input type="text" class="input-xlarge" disabled="disabled" value="{\{ current }}">
</div>
</div>
<div class="control-group">
<label class="control-label">Onderdeel Naam</label>
<div class="controls">
<input type="text" class="input-xlarge" ng-model="addTopicName" placeholder="Nieuw onderdeel naam">
</div>
</div>
</div>
<div class="modal-footer">
<a href="#" class="btn btn-primary">Verzenden</a>
<a ng-disabled="addTopicStatus == 'loading'" ng-click="addTopic(addTopicName)" class="btn btn-primary">Verzenden</a>
<div style="margin-top:5px;" class="alert alert-error" ng-show="addTopicStatus == 'error'">
{\{ addTopicMessage }}
</div>
<div style="margin-top:5px;" class="alert alert-success" ng-show="addTopicStatus == 'success'">
Je nieuwe topic is toegevoegd aan het onderdeel
</div>
</div>
</div>

Expand Down
1 change: 0 additions & 1 deletion war/templates/vote.html
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,6 @@
{% for vote in votes %}
{% if vote.comment.length %}
<div class="alert alert-info">
{{ vote.accepted }}
{{ vote.comment }}
</div>
{% else %}
Expand Down

0 comments on commit d4c7645

Please sign in to comment.