diff --git a/triggers/HttpCreateChat/__init__.py b/triggers/HttpCreateChat/__init__.py new file mode 100644 index 0000000000000000000000000000000000000000..d14fb00e0c30571a2e08d8bfd97214f93d4081b4 --- /dev/null +++ b/triggers/HttpCreateChat/__init__.py @@ -0,0 +1,61 @@ +import logging +import json +import azure.functions as func +from azure.cosmos.exceptions import CosmosHttpResponseError +import azure.cosmos.cosmos_client as cosmos_client +import os +import config + +HOST = config.settings['host'] +MASTER_KEY = config.settings['master_key'] +DATABASE_ID = config.settings['database_id'] +CONTAINER_ID = config.settings['chat_info_container_id'] + +client = cosmos_client.CosmosClient(HOST,credential=MASTER_KEY) +database = client.get_database_client(DATABASE_ID) +container = database.get_container_client(CONTAINER_ID) +#need to check type of inputs + + +#input = name: String, members: userID(s), admins: userID(s) +#fields = name, memberIDs, admins +#admin must be member +#return Id of created chat + +def main(req: func.HttpRequest,chatinfo: func.Out[func.Document]) -> func.HttpResponse: + logging.info('Python HTTP trigger function processed a request.') + + json_given = req.get_json() + if 'members' not in json_given or ('admins' not in json_given) or ('name' not in json_given): + return func.HttpResponse("Missing JSON key") + + max_id = 0 + prompts_query = "SELECT TOP 1 * FROM ChatInfo ORDER BY ChatInfo.ChatID DESC" + for item in container.query_items(query=prompts_query,enable_cross_partition_query=True): + logging.info(item["ChatID"]) + if item["ChatID"] > max_id: + max_id = item["ChatID"] + + + + new_id = max_id + 1 + + dict_out = {} + dict_out['ChatID'] = new_id + dict_out['name'] = json_given['name'] + dict_out['members'] = json_given['members'] + dict_out['admins'] = json_given['admins'] + + + try: + chatinfo.set(func.Document.from_dict(dict_out)) + json_out = { + 'result' : True, + 'chatId' : new_id + } + return func.HttpResponse(json.dumps(json_out)) + except CosmosHttpResponseError: + return func.HttpResponse("An error has occured") + + + \ No newline at end of file diff --git a/triggers/HttpCreateChat/function.json b/triggers/HttpCreateChat/function.json new file mode 100644 index 0000000000000000000000000000000000000000..cf8cb81fb395f212ddb02de001e3d996ae295320 --- /dev/null +++ b/triggers/HttpCreateChat/function.json @@ -0,0 +1,28 @@ +{ + "scriptFile": "__init__.py", + "bindings": [ + { + "authLevel": "anonymous", + "type": "httpTrigger", + "direction": "in", + "name": "req", + "methods": [ + "post" + ] + }, + { + "type": "http", + "direction": "out", + "name": "$return" + }, + { + "type": "cosmosDB", + "direction": "out", + "name": "chatinfo", + "databaseName": "UsersDB", + "collectionName": "ChatInfo", + "createIfNotExists": "false", + "connectionStringSetting": "usersdb" + } + ] +} diff --git a/triggers/HttpCreateChat/sample.dat b/triggers/HttpCreateChat/sample.dat new file mode 100644 index 0000000000000000000000000000000000000000..26aac46f1807186ce38f96e9eef5722b5c15edca --- /dev/null +++ b/triggers/HttpCreateChat/sample.dat @@ -0,0 +1,3 @@ +{ + "name": "Azure" +} \ No newline at end of file diff --git a/triggers/HttpGetChatsForUserID/__init__.py b/triggers/HttpGetChatsForUserID/__init__.py new file mode 100644 index 0000000000000000000000000000000000000000..fd6e1b457569a2dba0fbc755290ebbce3e57e65c --- /dev/null +++ b/triggers/HttpGetChatsForUserID/__init__.py @@ -0,0 +1,28 @@ +import logging +import json +import azure.functions as func +from azure.cosmos.exceptions import CosmosHttpResponseError + + +#takes in memberID +#returns chatID of chats the member is in + + +def main(req: func.HttpRequest,documents: func.DocumentList) -> func.HttpResponse: + logging.info('Python HTTP trigger function processed a request.') + + json_given = req.get_json() + if 'memberID' not in json_given: + return func.HttpResponse("Missing JSON key") + + memID = json_given['memberID'] + + chatIDs = [] + #go through each chat and find memberID + for document in documents: + if(memID in document["members"]): + chatIDs.append(document["ChatID"]) + + + return func.HttpResponse(json.dumps(chatIDs)) + diff --git a/triggers/HttpGetChatsForUserID/function.json b/triggers/HttpGetChatsForUserID/function.json new file mode 100644 index 0000000000000000000000000000000000000000..1a5ebb645ccfba162e950c9d147984a483e7e17a --- /dev/null +++ b/triggers/HttpGetChatsForUserID/function.json @@ -0,0 +1,27 @@ +{ + "scriptFile": "__init__.py", + "bindings": [ + { + "authLevel": "anonymous", + "type": "httpTrigger", + "direction": "in", + "name": "req", + "methods": [ + "get" + ] + },{ + "type": "cosmosDB", + "direction": "in", + "name": "documents", + "databaseName": "UsersDB", + "collectionName": "ChatInfo", + "createIfNotExists": "false", + "connectionStringSetting": "usersdb" + } , + { + "type": "http", + "direction": "out", + "name": "$return" + } + ] +} diff --git a/triggers/HttpGetChatsForUserID/sample.dat b/triggers/HttpGetChatsForUserID/sample.dat new file mode 100644 index 0000000000000000000000000000000000000000..26aac46f1807186ce38f96e9eef5722b5c15edca --- /dev/null +++ b/triggers/HttpGetChatsForUserID/sample.dat @@ -0,0 +1,3 @@ +{ + "name": "Azure" +} \ No newline at end of file diff --git a/triggers/HttpGetLastChatMessage/__init__.py b/triggers/HttpGetLastChatMessage/__init__.py new file mode 100644 index 0000000000000000000000000000000000000000..386d5db4b7eea1578aeb044cb0f30af74a895e4d --- /dev/null +++ b/triggers/HttpGetLastChatMessage/__init__.py @@ -0,0 +1,45 @@ +import logging +import config + +import azure.functions as func +import json +import azure.cosmos.cosmos_client as cosmos_client +import os + +HOST = config.settings['host'] +MASTER_KEY = config.settings['master_key'] +DATABASE_ID = config.settings['database_id'] +CONTAINER_ID = config.settings['chat_messages_container_id'] + +client = cosmos_client.CosmosClient(HOST,credential=MASTER_KEY) +database = client.get_database_client(DATABASE_ID) +container = database.get_container_client(CONTAINER_ID) + +#input ChatID, return last message + +def main(req: func.HttpRequest) -> func.HttpResponse: + logging.info('Python HTTP trigger function processed a request.') + + json_given = req.get_json() + if 'ChatID' not in json_given: + return func.HttpResponse("Missing JSON key") + ChatID = json_given['ChatID'] + + prompt_query = "SELECT TOP 1 ChatMessages.username, ChatMessages.message, ChatMessages.date," + prompt_query+= "ChatMessages.time FROM ChatMessages WHERE ChatMessages.ChatID = " + str(ChatID) + prompt_query+= " ORDER BY ChatMessages.date ASC,ChatMessages.time ASC" + items_out = [] + for item in container.query_items(query=prompt_query,enable_cross_partition_query=True): + new_dict = { + "username":item['username'], + "message":item['message'], + "date":item['date'], + "time":item['time'] + } + items_out.append(new_dict) + + if(len(items_out) == 1): + return func.HttpResponse(json.dumps(items_out[0])) + else: + return func.HttpResponse("error") + diff --git a/triggers/HttpGetLastChatMessage/function.json b/triggers/HttpGetLastChatMessage/function.json new file mode 100644 index 0000000000000000000000000000000000000000..f24e4f1d299f872f0cec65f76c0d0e02eb4e8796 --- /dev/null +++ b/triggers/HttpGetLastChatMessage/function.json @@ -0,0 +1,19 @@ +{ + "scriptFile": "__init__.py", + "bindings": [ + { + "authLevel": "anonymous", + "type": "httpTrigger", + "direction": "in", + "name": "req", + "methods": [ + "get" + ] + }, + { + "type": "http", + "direction": "out", + "name": "$return" + } + ] +} diff --git a/triggers/HttpGetLastChatMessage/sample.dat b/triggers/HttpGetLastChatMessage/sample.dat new file mode 100644 index 0000000000000000000000000000000000000000..26aac46f1807186ce38f96e9eef5722b5c15edca --- /dev/null +++ b/triggers/HttpGetLastChatMessage/sample.dat @@ -0,0 +1,3 @@ +{ + "name": "Azure" +} \ No newline at end of file diff --git a/triggers/HttpRetrieveAllChatMessages/__init__.py b/triggers/HttpRetrieveAllChatMessages/__init__.py new file mode 100644 index 0000000000000000000000000000000000000000..2dc534a71addd99c2ae3a9e4842cd55da46096e0 --- /dev/null +++ b/triggers/HttpRetrieveAllChatMessages/__init__.py @@ -0,0 +1,38 @@ +import logging +import config + +import azure.functions as func +import json +import azure.cosmos.cosmos_client as cosmos_client +import os + +HOST = config.settings['host'] +MASTER_KEY = config.settings['master_key'] +DATABASE_ID = config.settings['database_id'] +CONTAINER_ID = config.settings['chat_messages_container_id'] + +client = cosmos_client.CosmosClient(HOST,credential=MASTER_KEY) +database = client.get_database_client(DATABASE_ID) +container = database.get_container_client(CONTAINER_ID) + + +def main(req: func.HttpRequest) -> func.HttpResponse: + logging.info('Python HTTP trigger function processed a request.') + + json_given = req.get_json() + if 'ChatID' not in json_given: + return func.HttpResponse("Missing JSON key") + ChatID = json_given['ChatID'] + + prompt_query = "SELECT * FROM ChatMessages WHERE ChatMessages.ChatID = " + str(ChatID) + prompt_query+= " ORDER BY ChatMessages.date ASC, ChatMessages.time ASC" + items_out = [] + for item in container.query_items(query=prompt_query,enable_cross_partition_query=True): + new_dict = { + "username":item['username'], + "message":item['message'], + "date":item['date'], + "time":item['time'] + } + items_out.append(new_dict) + return func.HttpResponse(json.dumps(items_out)) \ No newline at end of file diff --git a/triggers/HttpRetrieveAllChatMessages/function.json b/triggers/HttpRetrieveAllChatMessages/function.json new file mode 100644 index 0000000000000000000000000000000000000000..f24e4f1d299f872f0cec65f76c0d0e02eb4e8796 --- /dev/null +++ b/triggers/HttpRetrieveAllChatMessages/function.json @@ -0,0 +1,19 @@ +{ + "scriptFile": "__init__.py", + "bindings": [ + { + "authLevel": "anonymous", + "type": "httpTrigger", + "direction": "in", + "name": "req", + "methods": [ + "get" + ] + }, + { + "type": "http", + "direction": "out", + "name": "$return" + } + ] +} diff --git a/triggers/HttpRetrieveAllChatMessages/sample.dat b/triggers/HttpRetrieveAllChatMessages/sample.dat new file mode 100644 index 0000000000000000000000000000000000000000..26aac46f1807186ce38f96e9eef5722b5c15edca --- /dev/null +++ b/triggers/HttpRetrieveAllChatMessages/sample.dat @@ -0,0 +1,3 @@ +{ + "name": "Azure" +} \ No newline at end of file diff --git a/triggers/HttpStoreChatMessage/__init__.py b/triggers/HttpStoreChatMessage/__init__.py new file mode 100644 index 0000000000000000000000000000000000000000..357d8ccc3e868d12b264ced80665274f471425d6 --- /dev/null +++ b/triggers/HttpStoreChatMessage/__init__.py @@ -0,0 +1,46 @@ +import logging +import json +import azure.functions as func +from azure.cosmos.exceptions import CosmosHttpResponseError +from datetime import datetime + +#could check if user is in chat + +#atm it takes in just a username and message then stores this in the database +def main(req: func.HttpRequest,messages: func.Out[func.Document]) -> func.HttpResponse: + logging.info('Python HTTP trigger function processed a request.') + + input_json = req.get_json() + #or date or time + if 'username' not in input_json or ('message' not in input_json) or ('ChatID' not in input_json): + return func.HttpResponse("Missing JSON key") + + func.HttpResponse.mimetype = 'application/json' + func.HttpResponse.charset = 'utf-8' + + + #get username and password from json + username = input_json['username'] + message = input_json['message'] + date = input_json['date'] + time = input_json['time'] + ChatID = input_json['ChatID'] + + new_dict = {} + new_dict['username'] = username + new_dict['message'] = message + new_dict['ChatID'] = ChatID + new_dict['date'] = date + new_dict['time'] = time + try: + messages.set(func.Document.from_dict(new_dict)) + json_out = { + 'result' : True, + 'msg':'OK' + } + return func.HttpResponse(json.dumps(json_out)) + except CosmosHttpResponseError: + return func.HttpResponse("An error has occured") + + + diff --git a/triggers/HttpStoreChatMessage/function.json b/triggers/HttpStoreChatMessage/function.json new file mode 100644 index 0000000000000000000000000000000000000000..99cfd7fa2809912c5b33cdb02a7436355caa0954 --- /dev/null +++ b/triggers/HttpStoreChatMessage/function.json @@ -0,0 +1,28 @@ +{ + "scriptFile": "__init__.py", + "bindings": [ + { + "authLevel": "anonymous", + "type": "httpTrigger", + "direction": "in", + "name": "req", + "methods": [ + "post" + ] + }, + { + "type": "cosmosDB", + "direction": "out", + "name": "messages", + "databaseName": "UsersDB", + "collectionName": "ChatMessages", + "createIfNotExists": "false", + "connectionStringSetting": "usersdb" + }, + { + "type": "http", + "direction": "out", + "name": "$return" + } + ] +} diff --git a/triggers/HttpStoreChatMessage/sample.dat b/triggers/HttpStoreChatMessage/sample.dat new file mode 100644 index 0000000000000000000000000000000000000000..26aac46f1807186ce38f96e9eef5722b5c15edca --- /dev/null +++ b/triggers/HttpStoreChatMessage/sample.dat @@ -0,0 +1,3 @@ +{ + "name": "Azure" +} \ No newline at end of file diff --git a/triggers/HttpUserLogin/__init__.py b/triggers/HttpUserLogin/__init__.py new file mode 100644 index 0000000000000000000000000000000000000000..89de111510aacf694870a4afadd301bb55bf59c8 --- /dev/null +++ b/triggers/HttpUserLogin/__init__.py @@ -0,0 +1,43 @@ +import config +import json +import azure.functions as func +import azure.cosmos.cosmos_client as cosmos_client + +HOST = config.settings['host'] +MASTER_KEY = config.settings['master_key'] +DATABASE_ID = config.settings['database_id'] +CONTAINER_ID = config.settings['users_container_id'] + +client = cosmos_client.CosmosClient(HOST,credential=MASTER_KEY) +database = client.get_database_client(DATABASE_ID) +container = database.get_container_client(CONTAINER_ID) + +def main(req: func.HttpRequest) -> func.HttpResponse: + data = req.get_json() + if 'username' not in data or ('password' not in data): + return func.HttpResponse("Missing JSON key") + + user_to_authenticate = data['username'] + + user = None + for item in container.query_items( + query='SELECT * FROM users WHERE users.username = "' + user_to_authenticate + '"', + enable_cross_partition_query=True): + user = item + + func.HttpResponse.mimetype = 'application/json' + func.HttpResponse.charset = 'utf-8' + + # Check if password mateches with the one stored in the db or if user exists + if user is None or (data['password'] != user['password']): + json_out = { + 'result': False, + 'msg': 'Username or password incorrect' + } + return func.HttpResponse(json.dumps(json_out)) + else: + json_out = { + 'result': True, + 'msg': 'OK' + } + return func.HttpResponse(json.dumps(json_out)) \ No newline at end of file diff --git a/triggers/HttpUserLogin/function.json b/triggers/HttpUserLogin/function.json new file mode 100644 index 0000000000000000000000000000000000000000..2853aa81ac425cfe80b650c06b3a7d5f91218fca --- /dev/null +++ b/triggers/HttpUserLogin/function.json @@ -0,0 +1,19 @@ +{ + "scriptFile": "__init__.py", + "bindings": [ + { + "authLevel": "anonymous", + "type": "httpTrigger", + "direction": "in", + "name": "req", + "methods": [ + "get" + ] + }, + { + "type": "http", + "direction": "out", + "name": "$return" + } + ] +} diff --git a/triggers/HttpUserRegister/__init__.py b/triggers/HttpUserRegister/__init__.py new file mode 100644 index 0000000000000000000000000000000000000000..da3916f2af4941afc881cd96520e2e9a445c11c4 --- /dev/null +++ b/triggers/HttpUserRegister/__init__.py @@ -0,0 +1,59 @@ +import json +import azure.functions as func +from azure.cosmos.exceptions import CosmosHttpResponseError + +def main(req: func.HttpRequest, document: func.DocumentList, users: func.Out[func.Document]) -> func.HttpResponse: + user = req.get_json() + if 'username' not in user or ('password' not in user): + return func.HttpResponse("Missing JSON key") + + all_users = [] + for p in document: + all_users.append(p['username']) + + func.HttpResponse.mimetype = 'application/json' + func.HttpResponse.charset = 'utf-8' + + if len(user['username']) < 4: + json_out = { + 'result': False, + 'msg': 'Username less than 4 characters' + } + return func.HttpResponse(json.dumps(json_out)) + elif len(user['username']) > 16: + json_out = { + 'result': False, + 'msg': 'Username more than 16 characters' + } + return func.HttpResponse(json.dumps(json_out)) + elif user['username'] in all_users: + json_out = { + 'result': False, + 'msg': 'Username already exists' + } + return func.HttpResponse(json.dumps(json_out)) + + if len(user['password']) < 8: + json_out = { + 'result': False, + 'msg': 'Password less than 8 characters' + } + return func.HttpResponse(json.dumps(json_out)) + elif len(user['password']) > 24: + json_out = { + 'result': False, + 'msg': 'Password more than 24 characters' + } + return func.HttpResponse(json.dumps(json_out)) + + try: + user['games_played'] = 0 + user['total_score'] = 0 + users.set(func.Document.from_dict(user)) + json_out = { + 'result': True, + 'msg': 'OK' + } + return func.HttpResponse(json.dumps(json_out)) + except CosmosHttpResponseError: + return func.HttpResponse("Cosmos DB error") \ No newline at end of file diff --git a/triggers/HttpUserRegister/function.json b/triggers/HttpUserRegister/function.json new file mode 100644 index 0000000000000000000000000000000000000000..ef8a610ba37e440c97356f591cac3b6f28510701 --- /dev/null +++ b/triggers/HttpUserRegister/function.json @@ -0,0 +1,37 @@ +{ + "scriptFile": "__init__.py", + "bindings": [ + { + "authLevel": "anonymous", + "type": "httpTrigger", + "direction": "in", + "name": "req", + "methods": [ + "post" + ] + }, + { + "type": "http", + "direction": "out", + "name": "$return" + }, + { + "type": "cosmosDB", + "direction": "out", + "name": "users", + "databaseName": "UsersDB", + "collectionName": "Users", + "createIfNotExists": "false", + "connectionStringSetting": "usersdb" + }, + { + "type": "cosmosDB", + "direction": "in", + "name": "document", + "databaseName": "UsersDB", + "collectionName": "Users", + "createIfNotExists": "false", + "connectionStringSetting": "usersdb" + } + ] +}