Skip to content

Commit

Permalink
Merge pull request #4 from Punit-Choudhary/main
Browse files Browse the repository at this point in the history
Upload REST API
  • Loading branch information
Manty-K authored Oct 24, 2021
2 parents 7796f24 + 5a56eb5 commit 279321a
Show file tree
Hide file tree
Showing 2 changed files with 190 additions and 1 deletion.
2 changes: 1 addition & 1 deletion README.md
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
# Feelings Hub

![Banner](https://github.com/Punit-Choudhary/FeelingsHub/blob/main/banner.png)
## 💡 Inspiration
Mental health is a major concern worldwide and India is not far behind in sharing this. About 14% of the global burden of disease is attributed to neuropsychiatric disorders and in India WHO estimates that the burden of mental health problems in India is 2443 disability-adjusted life years (DALYs) per 100 00 population and the suicide rate percentage due to mental health is 21.1 percent in the last few years and it had increased more due to spread of covid 19 virus. Mental health, loneliness, depression, etc are all serious issues in modern times but people are afraid to talk about them. Indian societies consider mental health as taboo due to which people are more afraid to talk about it and instead of getting support and help from society they suffer alone. People who suffer from mental illness needed a medium to talk, to express themselves, they needed support and motivation. By remarking this importance we are creating an app where people who suffer from mental health can talk openly, share their problems and difficulties and get motivated, and be guided properly so that they can enjoy their life to the fullest of its means. That's our sole purpose of making this app to bring happiness back to the life of people.

Expand Down
189 changes: 189 additions & 0 deletions rest_api.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,189 @@
import uuid
from flask import Flask
from flask.json import jsonify
from flask_restful import Api, Resource

# Create App
app = Flask(__name__)
app.url_map.strict_slashes = False

api = Api(app)


pairs = []

# Functions

# function to update data
def update_data(sad_data, happy_data):
with open("sad.txt", 'w') as sad:
for data in sad_data:
data += "\n"
sad.write(data)

with open("happy.txt", 'w') as happy:
for data in happy_data:
data += "\n"
happy.write(data)

# function to get uuid
def get_uuid():
return str(uuid.uuid4())

#function to open files
def get_file(which="all"):

if which == "all":
# opening file of sad people
with open("sad.txt") as file:
sad_data = file.readlines()
sad_list = [sad_data[i][:-1] for i in range(len(sad_data))]

# opening file of cheerful
with open("happy.txt") as file:
happy_data = file.readlines()
happy_list = [happy_data[i][:-1] for i in range(len(happy_data))]

return sad_list, happy_list
elif which == "sad":
# opening file of sad people
with open("sad.txt") as file:
sad_data = file.readlines()
sad_list = [sad_data[i][:-1] for i in range(len(sad_data))]

return sad_list
elif which == "happy":
# opening file of cheerful
with open("happy.txt") as file:
happy_data = file.readlines()
happy_list = [happy_data[i][:-1] for i in range(len(happy_data))]

return happy_list

# function to make pairs
def make_pairs() -> None:
sad_list, happy_list = get_file()

while sad_list and happy_list:
convcode = get_uuid()
sad = sad_list.pop(0)
happy = happy_list.pop(0)

pairs.append(
{
sad: convcode,
happy: convcode
}
)
print(pairs) # for debugging purpose
print("\n\n") # for debugging purpose

# Updating the data of sad & happy file
update_data(sad_list, happy_list)

# function to update paired lists
def update_pairs(id):
updated = False

for i in range(len(pairs)):
if id in pairs[i]:
pairs.pop(i)
updated = True

if updated:
break

class Home(Resource):
def get(self):
return """
Get Sad list: https://url/sad
Update Sad list: https://url/sad/<id>
Get Happy list: https://url/happy
Update Happy list: https://url/happy/<id>
Get Pair: https://url/pair/<id>
Remove Pair: https://url/remove/<id>
"""


class Depressed_Put(Resource):
def put(self, id):
with open("sad.txt", 'a') as sad:
id += "\n"
sad.write(id)
return jsonify("Added")


class Depressed_Get(Resource):
def get(self):
sad_list = get_file(which="sad")
return sad_list


class Happy_Put(Resource):
def put(self, id):
with open("happy.txt", 'a') as happy:
id += "\n"
happy.write(id)
return jsonify("Added")


class Happy_Get(Resource):
def get(self):
happy_list = get_file(which="happy")
return happy_list


class Get_Pair(Resource):
def get(self, id):
make_pairs()
sad_list, happy_list = get_file()
if id in sad_list or id in happy_list:
return "Not Paired Yet", 202
elif any([id in d for d in pairs]):
temp = ""
for pair in pairs:
temp = pair.get(id, "")
if temp != "":
break
return temp, 200
else:
return "Not Found!", 404


class Remove_Pair(Resource):
def get(self, id):
update_pairs(id)
return "Updated!", 200


class Remove_sad(Resource):
def get(self, id):
sad_list, happy_list = get_file()
sad_list.remove(id)
update_data(sad_list, happy_list)

return f"Removed {id}", 200


class Remove_happy(Resource):
def get(self, id):
sad_list, happy_list = get_file()
happy_list.remove(id)
update_data(sad_list, happy_list)

return f"Removed {id}", 200


api.add_resource(Home, '/')
api.add_resource(Depressed_Get, '/sad')
api.add_resource(Depressed_Put, '/sad/<id>')
api.add_resource(Happy_Get, '/happy')
api.add_resource(Happy_Put, '/happy/<id>')
api.add_resource(Get_Pair, '/pair/<id>')
api.add_resource(Remove_Pair, '/remove/<id>')
api.add_resource(Remove_sad, '/sad/remove/<id>')
api.add_resource(Remove_happy, '/happy/remove/<id>')

# driver function
if __name__ == '__main__':
app.run(host="0.0.0.0", port=6969)

0 comments on commit 279321a

Please sign in to comment.