1
- from flask import request , jsonify , current_app
1
+ from flask import request , jsonify
2
2
from extensions import neo4j_db
3
- from models import User , Project , Tag
4
- from sqlalchemy .orm import sessionmaker
5
- import os
6
3
7
4
def get_profile (username ):
8
5
logged_in_user = request .args .get ('logged_in_user' )
@@ -41,7 +38,6 @@ def get_profile(username):
41
38
profile_data ['projects' ] = projects
42
39
43
40
if logged_in_user :
44
- # Check if the logged-in user is friends with the profile user
45
41
friendship_query = """
46
42
MATCH (u1:User {username: $logged_in_user})-[:FRIEND]->(u2:User {username: $username})
47
43
RETURN COUNT(u1) > 0 AS isFriend
@@ -71,95 +67,3 @@ def update_profile(username):
71
67
return jsonify ({'message' : 'Profile updated successfully' }), 200
72
68
return jsonify ({'message' : 'User not found' }), 404
73
69
74
- def add_project (username ):
75
- data = request .get_json ()
76
-
77
- # Get project details from request
78
- title = data .get ('title' )
79
- description = data .get ('description' , '' )
80
- repo_link = data .get ('repo_link' , '' )
81
- tags = data .get ('tags' , '' )
82
-
83
- # Define a query to create a project without tags initially
84
- create_project_query = """
85
- MATCH (u:User {username: $username})
86
- CREATE (p:Project {title: $title, description: $description, repo_link: $repo_link, tags: $tags})
87
- CREATE (u)-[:OWNS]->(p)
88
- RETURN p
89
- """
90
-
91
- try :
92
- with neo4j_db .driver .session () as session :
93
- # Create the project
94
- result = session .run (create_project_query , username = username , title = title , description = description , repo_link = repo_link , tags = tags )
95
- project_record = result .single ()
96
-
97
- if project_record :
98
- project = project_record ["p" ]
99
-
100
- domain_tags = [tag for tag in tags .split (',' ) if tag .strip () in tags ]
101
-
102
- # Update project with tags
103
- update_project_query = """
104
- MATCH (p:Project {title: $title, description: $description, repo_link: $repo_link})
105
- WITH p, $tags AS tags
106
- UNWIND tags AS tagName
107
- MERGE (t:Tag {name: tagName})
108
- CREATE (p)-[:TAGGED_WITH]->(t)
109
- RETURN p
110
- """
111
-
112
- # Update the project with tags
113
- session .run (update_project_query , title = title , description = description , repo_link = repo_link , tags = domain_tags )
114
-
115
- return jsonify ({'message' : 'Project added and categorized successfully' , 'project' : {
116
- 'title' : project ["title" ],
117
- 'description' : project .get ("description" , "" ),
118
- 'repoLink' : project .get ("repo_link" , "" ),
119
- 'tags' : domain_tags
120
- }}), 201
121
- else :
122
- return jsonify ({'message' : 'User not found' }), 404
123
- except Exception as e :
124
- current_app .logger .error (f"Error adding project: { e } " )
125
- return jsonify ({'message' : 'An error occurred while adding the project' }), 500
126
-
127
-
128
-
129
- def update_project (username , project_id ):
130
- data = request .get_json ()
131
- title = data .get ('title' )
132
- description = data .get ('description' )
133
- repo_link = data .get ('repo_link' )
134
- tags = data .get ('tags' , '' )
135
-
136
- query = """
137
- MATCH (u:User {username: $username})-[:OWNS]->(p:Project {id: $project_id})
138
- SET p.title = $title, p.description = $description, p.repo_link = $repo_link
139
- WITH p
140
- OPTIONAL MATCH (p)-[r:TAGGED_WITH]->(t:Tag)
141
- DELETE r
142
- WITH p
143
- UNWIND $tags AS tagName
144
- MERGE (t:Tag {name: tagName})
145
- CREATE (p)-[:TAGGED_WITH]->(t)
146
- RETURN p
147
- """
148
- with neo4j_db .driver .session () as session :
149
- result = session .run (query , username = username , project_id = project_id , title = title , description = description , repo_link = repo_link , tags = tags .split (', ' ) if tags else [])
150
- if result .single ():
151
- return jsonify ({'message' : 'Project updated successfully' }), 200
152
- return jsonify ({'message' : 'Project or user not found' }), 404
153
-
154
- def delete_project (username , project_title ):
155
- query = """
156
- MATCH (u:User {username: $username})-[:OWNS]->(p:Project {title: $project_title})
157
- OPTIONAL MATCH (p)-[r]-()
158
- DELETE r, p
159
- RETURN u
160
- """
161
- with neo4j_db .driver .session () as session :
162
- result = session .run (query , username = username , project_title = project_title )
163
- if result .single ():
164
- return jsonify ({'message' : 'Project deleted successfully' }), 200
165
- return jsonify ({'message' : 'Project or user not found' }), 404
0 commit comments