Permalink
Browse files

initial commit. needs edit commands and ideally better nonexistant us…

…er/game/data handling
  • Loading branch information...
0 parents commit f10b11356e746d5738615684b57bff1bc8b91aa5 @clockfort committed Feb 27, 2012
Showing with 49 additions and 0 deletions.
  1. +49 −0 achieve-rest.hs
@@ -0,0 +1,49 @@
+-- For both scotty and MYSql-Simple
+{-# LANGUAGE OverloadedStrings #-}
+
+-- For Scotty
+import Web.Scotty
+import Data.Monoid (mconcat)
+
+-- MySQL
+import Control.Monad
+import Database.HDBC
+import Database.HDBC.MySQL
+import Control.Monad.IO.Class
+import qualified Control.Exception as E
+
+main = scotty 3000 $ do
+ get "/v1/users/:user" $ do
+ user <- param "user"
+ json user
+ get "/v1/users/:user/score" $ do
+ user <- param "user"
+ score <- liftIO $ (fetchTotalScore user)
+ json score
+ get "/v1/apps/:app/description" $ do
+ app <- param "app"
+ appDesc <- liftIO $ fetchAppDesc app
+ json appDesc
+
+cshAchievementsDB = defaultMySQLConnectInfo { mysqlHost = "db.csh.rit.edu", mysqlUser= "achievements_api", mysqlPassword = "", mysqlDatabase = "achievements"}
+
+fetchTotalScore username =
+ do
+ conn <- connectMySQL cshAchievementsDB
+ rows <- quickQuery' conn "SELECT SUM( t1.score ) FROM achievements AS t1 INNER JOIN achievement_progress AS t2 ON t2.achievement_id = t1.id WHERE t1.progress_max = t2.progress AND t2.user_id = ( SELECT id FROM users WHERE username = ? ) " [toSql username]
+ disconnect conn
+ return (fromSql (rows !! 0 !! 0) :: Integer)
+
+fetchUserID username =
+ do
+ conn <- connectMySQL cshAchievementsDB
+ rows <- quickQuery' conn "SELECT id from users where username = ?" [toSql username]
+ disconnect conn
+ return (fromSql (rows !! 0 !! 0) :: Integer)
+
+fetchAppDesc appName =
+ do
+ conn <- connectMySQL cshAchievementsDB
+ rows <- quickQuery' conn "SELECT description from apps where name = ?" [toSql appName]
+ disconnect conn
+ return (fromSql (rows !! 0 !! 0) :: String)

0 comments on commit f10b113

Please sign in to comment.