UserService.icl 1.67 KB
Newer Older
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22
implementation module UserService

import Http, TSt
import HtmlUtil
import UserDB

derive JSONEncode UserDetails, Password

userService :: !String !Bool ![String] !HTTPRequest *TSt -> (!HTTPResponse, !*TSt)
userService url html path req tst
	# (mbSessionErr,tst)	= initSession sessionParam tst
	# (session,tst)			= getCurrentSession tst
	= case path of
		//List users
		[]
			| isJust mbSessionErr
				= (serviceResponse html "users" url params (jsonSessionErr mbSessionErr), tst)	
			
			# (users,tst)	= getUsers tst
			# items			= [details \\ RegisteredUser details <- users]
			# json			= JSONObject [("success",JSONBool True),("users",toJSON items)]
			= (serviceResponse html "users" url params json, tst)
23 24 25 26 27 28 29
		//List usernames
		["names"]
			| isJust mbSessionErr
				= (serviceResponse html "users names" url params (jsonSessionErr mbSessionErr), tst)	
			# (users,tst)	= getUsers tst
			# json			= JSONObject [("success",JSONBool True),("users",toJSON [toString u \\ u <- users])]
			= (serviceResponse html "users names" url params json, tst)
30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48
		//Show user details
		[userId]
			| isJust mbSessionErr
				= (serviceResponse html "user details" url params (jsonSessionErr mbSessionErr), tst)
			# (mbUser,tst)	= getUser userId tst
			= case mbUser of
				Just (RegisteredUser details)
					# json	= JSONObject [("success",JSONBool True),("user",toJSON details)]
					= (serviceResponse html "user details" url params json, tst)
				_
					= (notFoundResponse req,tst)
		_
			= (notFoundResponse req,tst)
where
	sessionParam	= paramValue "_session" req
	params			= [("_session",sessionParam,True)]
	
	jsonSessionErr (Just error)
					= JSONObject [("success",JSONBool False),("error", JSONString error)]