Quellcode durchsuchen

Show message if word is already in database

Thomas Dy vor 10 Jahren
Ursprung
Commit
aedc5b8811

+ 7 - 0
app/controllers/Cards.scala

@@ -26,6 +26,13 @@ object Cards extends Controller {
     )
   }
 
+  def exists(word: String) = Action {
+    Ok(Json.obj(
+      "word" -> word,
+      "exists" -> Card.exists(word)
+    ))
+  }
+
   def dump = Action {
     Ok(Json.toJson(Card.list()))
   }

+ 7 - 0
app/models/Card.scala

@@ -18,6 +18,13 @@ object Card {
     SQL("select count(*) from words").single(long("count"))
   }
 
+  def exists(word: String) = DB.withConnection { implicit c =>
+    SQL("select * from words where lower(word) = lower({word})")
+      .on('word -> word)
+      .list(str("word"))
+      .nonEmpty
+  }
+
   def add(card: Card) = DB.withTransaction { implicit c =>
     val id = SQL("insert into words values (default, {word})")
       .on('word -> card.word)

+ 1 - 0
conf/routes

@@ -11,6 +11,7 @@ GET     /stats                           controllers.Application.stats
 GET     /cards                           controllers.Cards.dump
 POST    /cards                           controllers.Cards.add
 GET     /cards/random                    controllers.Cards.random
+GET     /cards/exists                    controllers.Cards.exists(word: String)
 
 # Map static resources from the /public folder to the /assets URL path
 GET     /assets/*file                    controllers.Assets.at(path="/public", file)

+ 17 - 0
public/javascripts/main.js

@@ -55,6 +55,7 @@ function GameCtrl($scope, Taboo) {
 
 function ContributeCtrl($scope, $http, $timeout) {
   $scope.submitting = false;
+  $scope.exists = false;
 
   function init() {
     $scope.card = {
@@ -63,6 +64,22 @@ function ContributeCtrl($scope, $http, $timeout) {
     };
   }
 
+  function check() {
+    $http.get(jsRoutes.controllers.Cards.exists($scope.card.word).url)
+      .then(function(data) {
+        $scope.exists = data.data.exists;
+      });
+  }
+
+  var promise = null;
+
+  $scope.check = function() {
+    if(promise != null) {
+      $timeout.cancel(promise);
+    }
+    promise = $timeout(check, 200);
+  }
+
   $scope.submit = function() {
     if($scope.submitting) return;
     $scope.submitting = true;

+ 2 - 1
public/partials/contribute.html

@@ -10,7 +10,8 @@
         original work only. Don't just blindly copy a card from any of the Taboo games.
       </p>
       <form ng-submit="submit()">
-        <input type="text" ng-model="card.word" id="inputWord" placeholder="Word" required>
+        <input type="text" ng-model="card.word" id="inputWord" placeholder="Word" ng-change="check()" required>
+        <span ng-show="exists">We already have this word</span>
         <br>
         <input type="text" ng-model="card.taboos[0]" placeholder="Taboo Word" required>
         <br>