Spaces:
Sleeping
Sleeping
AdeshRathod commited on
Commit ·
272199f
1
Parent(s): 0596d0a
Add initial project structure with FastAPI backend and React frontend
Browse files- Implemented backend with FastAPI, including user and project management.
- Added JWT authentication for user login and registration.
- Created Dockerfile and docker-compose for containerization.
- Included .gitignore for Python and Node.js environments.
- Added README for project setup and usage instructions.
- Defined data models using Pydantic for validation.
- Established MongoDB connection using Motor for asynchronous operations.
- .gitignore +34 -0
- CODEREADME.md +107 -0
- Dockerfile +18 -0
- LICENSE +19 -0
- arclen_ssh +8 -0
- arclen_ssh.pub +1 -0
- docker-compose.yml +19 -0
- package-lock.json +77 -0
- package.json +6 -0
- requirements.txt +8 -0
- src/dao/project_dao.py +118 -0
- src/dao/user_dao.py +25 -0
- src/db.py +25 -0
- src/main.py +25 -0
- src/models/project.py +32 -0
- src/routes/auth.py +54 -0
- src/routes/projects.py +125 -0
- src/utils/helpers.py +2 -0
.gitignore
ADDED
|
@@ -0,0 +1,34 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
# Ignore frontend build and node modules
|
| 2 |
+
frontend/
|
| 3 |
+
frontend/node_modules/
|
| 4 |
+
/node_modules/
|
| 5 |
+
|
| 6 |
+
# Python virtualenv
|
| 7 |
+
.venv/
|
| 8 |
+
venv/
|
| 9 |
+
|
| 10 |
+
# Byte-compiled / optimized / DLL files
|
| 11 |
+
__pycache__/
|
| 12 |
+
*.py[cod]
|
| 13 |
+
*$py.class
|
| 14 |
+
|
| 15 |
+
# Distribution / packaging
|
| 16 |
+
build/
|
| 17 |
+
dist/
|
| 18 |
+
*.egg-info/
|
| 19 |
+
|
| 20 |
+
# Local env files
|
| 21 |
+
# .env
|
| 22 |
+
# env.local
|
| 23 |
+
|
| 24 |
+
# VSCode
|
| 25 |
+
.vscode/
|
| 26 |
+
|
| 27 |
+
# Mac
|
| 28 |
+
.DS_Store
|
| 29 |
+
.venv/
|
| 30 |
+
venv/
|
| 31 |
+
__pycache__/
|
| 32 |
+
.pytest_cache/
|
| 33 |
+
*.pyc
|
| 34 |
+
.env
|
CODEREADME.md
ADDED
|
@@ -0,0 +1,107 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
# Test Project
|
| 2 |
+
|
| 3 |
+
Simple FastAPI example project scaffold.
|
| 4 |
+
|
| 5 |
+
Quick start
|
| 6 |
+
|
| 7 |
+
1. Create a virtualenv and activate it.
|
| 8 |
+
|
| 9 |
+
On Windows (PowerShell):
|
| 10 |
+
|
| 11 |
+
```powershell
|
| 12 |
+
python -m venv .venv; .\.venv\Scripts\Activate.ps1
|
| 13 |
+
pip install -r requirements.txt
|
| 14 |
+
```
|
| 15 |
+
|
| 16 |
+
2. Run the app:
|
| 17 |
+
|
| 18 |
+
```powershell
|
| 19 |
+
python -m src.main
|
| 20 |
+
# or
|
| 21 |
+
uvicorn src.main:app --reload --host 0.0.0.0 --port 8000
|
| 22 |
+
```
|
| 23 |
+
|
| 24 |
+
3. API endpoints
|
| 25 |
+
|
| 26 |
+
Run tests:
|
| 27 |
+
|
| 28 |
+
```powershell
|
| 29 |
+
pytest -q
|
| 30 |
+
```
|
| 31 |
+
|
| 32 |
+
## UC Projects (Backend + Frontend)
|
| 33 |
+
|
| 34 |
+
This repository contains a FastAPI backend and a React + Vite frontend.
|
| 35 |
+
|
| 36 |
+
Prerequisites
|
| 37 |
+
|
| 38 |
+
- Python 3.11
|
| 39 |
+
- Node 18+ (for frontend)
|
| 40 |
+
- Docker (optional, for docker-compose)
|
| 41 |
+
- A MongoDB instance and connection URI
|
| 42 |
+
|
| 43 |
+
Set up local environment
|
| 44 |
+
|
| 45 |
+
1. Copy `.env.example` to `.env` and set `MONGODB_URI`.
|
| 46 |
+
|
| 47 |
+
2. Install backend dependencies and run (PowerShell):
|
| 48 |
+
|
| 49 |
+
```powershell
|
| 50 |
+
python -m venv .venv; .\.venv\Scripts\Activate.ps1
|
| 51 |
+
pip install -r requirements.txt
|
| 52 |
+
uvicorn src.main:app --reload --host 0.0.0.0 --port 8000
|
| 53 |
+
```
|
| 54 |
+
|
| 55 |
+
3. Run frontend (in another shell):
|
| 56 |
+
|
| 57 |
+
```powershell
|
| 58 |
+
cd frontend
|
| 59 |
+
npm install
|
| 60 |
+
npm run dev
|
| 61 |
+
```
|
| 62 |
+
|
| 63 |
+
Or with Docker Compose (builds backend and frontend):
|
| 64 |
+
|
| 65 |
+
```powershell
|
| 66 |
+
# from repo root
|
| 67 |
+
$env:MONGODB_URI = "your-mongo-uri-here"; docker-compose up --build
|
| 68 |
+
```
|
| 69 |
+
|
| 70 |
+
API endpoints (summary)
|
| 71 |
+
|
| 72 |
+
- `POST /api/register` - register a user (not used by frontend)
|
| 73 |
+
- `POST /api/login` - login with `number` and `mpin` (used by frontend)
|
| 74 |
+
- `GET /api/projects?user_id=<id>` - list projects
|
| 75 |
+
- `POST /api/projects` - create project {name, description, created_by}
|
| 76 |
+
- `GET /api/projects/{project_id}` - get project with contacts and credentials
|
| 77 |
+
- `POST /api/projects/{id}/contacts` - add contact
|
| 78 |
+
- `DELETE /api/projects/{id}/contacts/{contact_id}` - delete contact
|
| 79 |
+
- `POST /api/projects/{id}/credentials` - add credential
|
| 80 |
+
- `DELETE /api/projects/{id}/credentials/{credential_id}` - delete credential
|
| 81 |
+
|
| 82 |
+
Notes
|
| 83 |
+
|
| 84 |
+
- The frontend expects the API to be available at `/api/*` (same host during local dev via proxy or when both are deployed).
|
| 85 |
+
- Keep secrets like `MONGODB_URI` out of the repo and use environment variables or secret stores.
|
| 86 |
+
|
| 87 |
+
python --version
|
| 88 |
+
py --version
|
| 89 |
+
|
| 90 |
+
# create venv with the Windows py launcher
|
| 91 |
+
|
| 92 |
+
py -3 -m venv .venv
|
| 93 |
+
|
| 94 |
+
# allow running local activation scripts (if not already allowed)
|
| 95 |
+
|
| 96 |
+
Set-ExecutionPolicy -ExecutionPolicy RemoteSigned -Scope CurrentUser
|
| 97 |
+
|
| 98 |
+
# activate the venv in PowerShell
|
| 99 |
+
|
| 100 |
+
.\.venv\Scripts\Activate.ps1
|
| 101 |
+
|
| 102 |
+
# then install dependencies
|
| 103 |
+
|
| 104 |
+
pip install -r requirements.txt
|
| 105 |
+
|
| 106 |
+
# run
|
| 107 |
+
uvicorn src.main:app --reload --host 0.0.0.0 --port 8000
|
Dockerfile
ADDED
|
@@ -0,0 +1,18 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
FROM python:3.11-slim
|
| 2 |
+
|
| 3 |
+
WORKDIR /app
|
| 4 |
+
|
| 5 |
+
# Install build dependencies for some Python packages
|
| 6 |
+
RUN apt-get update && apt-get install -y build-essential gcc git && rm -rf /var/lib/apt/lists/*
|
| 7 |
+
|
| 8 |
+
COPY requirements.txt ./
|
| 9 |
+
RUN pip install --no-cache-dir -U pip
|
| 10 |
+
RUN pip install --no-cache-dir -r requirements.txt
|
| 11 |
+
|
| 12 |
+
COPY . /app
|
| 13 |
+
|
| 14 |
+
# Use port 7860 which is commonly routed by hosting platforms like Hugging Face Spaces
|
| 15 |
+
EXPOSE 7860
|
| 16 |
+
|
| 17 |
+
# Start the app on port 7860
|
| 18 |
+
CMD ["uvicorn", "src.main:app", "--host", "0.0.0.0", "--port", "7860", "--loop", "asyncio", "--http", "h11"]
|
LICENSE
ADDED
|
@@ -0,0 +1,19 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
MIT License
|
| 2 |
+
|
| 3 |
+
Permission is hereby granted, free of charge, to any person obtaining a copy
|
| 4 |
+
of this software and associated documentation files (the "Software"), to deal
|
| 5 |
+
in the Software without restriction, including without limitation the rights
|
| 6 |
+
to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
|
| 7 |
+
copies of the Software, and to permit persons to whom the Software is
|
| 8 |
+
furnished to do so, subject to the following conditions:
|
| 9 |
+
|
| 10 |
+
The above copyright notice and this permission notice shall be included in all
|
| 11 |
+
copies or substantial portions of the Software.
|
| 12 |
+
|
| 13 |
+
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
|
| 14 |
+
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
|
| 15 |
+
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
|
| 16 |
+
AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
|
| 17 |
+
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
|
| 18 |
+
OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
|
| 19 |
+
SOFTWARE.
|
arclen_ssh
ADDED
|
@@ -0,0 +1,8 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
-----BEGIN OPENSSH PRIVATE KEY-----
|
| 2 |
+
b3BlbnNzaC1rZXktdjEAAAAACmFlczI1Ni1jdHIAAAAGYmNyeXB0AAAAGAAAABDALvAReN
|
| 3 |
+
zRaUqAoPktUFwWAAAAGAAAAAEAAAAzAAAAC3NzaC1lZDI1NTE5AAAAINPdCakRLDCXwOmb
|
| 4 |
+
g+fcZyadCqLuTWlqOOpmG7Rt25mUAAAAoB6uWV/MBchot6/cGKljfUz4YzGyhLhbZEgs0u
|
| 5 |
+
u3V6oSqDNmuMjxH5J8lrvj4CtpAjxB3LInEMHCk+syDWQaIl9QiOcMFevl9Mi0UKLFY8KW
|
| 6 |
+
gku7yB8gQArZIdYJUzrYXrm2ImDRneUKtLDWXpP4b+qB3asIZt9HWZHThgyAHP2la4yYp/
|
| 7 |
+
DdvC2tCAgjiDqCD7Tbtm5EUBmzh0mdGxzwD5c=
|
| 8 |
+
-----END OPENSSH PRIVATE KEY-----
|
arclen_ssh.pub
ADDED
|
@@ -0,0 +1 @@
|
|
|
|
|
|
|
| 1 |
+
ssh-ed25519 AAAAC3NzaC1lZDI1NTE5AAAAINPdCakRLDCXwOmbg+fcZyadCqLuTWlqOOpmG7Rt25mU arclen.technolgies@gmail.com
|
docker-compose.yml
ADDED
|
@@ -0,0 +1,19 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
version: '3.8'
|
| 2 |
+
services:
|
| 3 |
+
backend:
|
| 4 |
+
build:
|
| 5 |
+
context: .
|
| 6 |
+
dockerfile: Dockerfile
|
| 7 |
+
ports:
|
| 8 |
+
- "8000:8000"
|
| 9 |
+
environment:
|
| 10 |
+
- MONGODB_URI=${MONGODB_URI}
|
| 11 |
+
|
| 12 |
+
frontend:
|
| 13 |
+
build:
|
| 14 |
+
context: ./frontend
|
| 15 |
+
dockerfile: Dockerfile
|
| 16 |
+
ports:
|
| 17 |
+
- "3000:80"
|
| 18 |
+
depends_on:
|
| 19 |
+
- backend
|
package-lock.json
ADDED
|
@@ -0,0 +1,77 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
{
|
| 2 |
+
"name": "ucprojects_python",
|
| 3 |
+
"lockfileVersion": 3,
|
| 4 |
+
"requires": true,
|
| 5 |
+
"packages": {
|
| 6 |
+
"": {
|
| 7 |
+
"dependencies": {
|
| 8 |
+
"lucide-react": "^0.560.0",
|
| 9 |
+
"react-hot-toast": "^2.6.0"
|
| 10 |
+
}
|
| 11 |
+
},
|
| 12 |
+
"node_modules/csstype": {
|
| 13 |
+
"version": "3.2.3",
|
| 14 |
+
"resolved": "https://registry.npmjs.org/csstype/-/csstype-3.2.3.tgz",
|
| 15 |
+
"integrity": "sha512-z1HGKcYy2xA8AGQfwrn0PAy+PB7X/GSj3UVJW9qKyn43xWa+gl5nXmU4qqLMRzWVLFC8KusUX8T/0kCiOYpAIQ=="
|
| 16 |
+
},
|
| 17 |
+
"node_modules/goober": {
|
| 18 |
+
"version": "2.1.18",
|
| 19 |
+
"resolved": "https://registry.npmjs.org/goober/-/goober-2.1.18.tgz",
|
| 20 |
+
"integrity": "sha512-2vFqsaDVIT9Gz7N6kAL++pLpp41l3PfDuusHcjnGLfR6+huZkl6ziX+zgVC3ZxpqWhzH6pyDdGrCeDhMIvwaxw==",
|
| 21 |
+
"peerDependencies": {
|
| 22 |
+
"csstype": "^3.0.10"
|
| 23 |
+
}
|
| 24 |
+
},
|
| 25 |
+
"node_modules/lucide-react": {
|
| 26 |
+
"version": "0.560.0",
|
| 27 |
+
"resolved": "https://registry.npmjs.org/lucide-react/-/lucide-react-0.560.0.tgz",
|
| 28 |
+
"integrity": "sha512-NwKoUA/aBShsdL8WE5lukV2F/tjHzQRlonQs7fkNGI1sCT0Ay4a9Ap3ST2clUUkcY+9eQ0pBe2hybTQd2fmyDA==",
|
| 29 |
+
"peerDependencies": {
|
| 30 |
+
"react": "^16.5.1 || ^17.0.0 || ^18.0.0 || ^19.0.0"
|
| 31 |
+
}
|
| 32 |
+
},
|
| 33 |
+
"node_modules/react": {
|
| 34 |
+
"version": "19.2.1",
|
| 35 |
+
"resolved": "https://registry.npmjs.org/react/-/react-19.2.1.tgz",
|
| 36 |
+
"integrity": "sha512-DGrYcCWK7tvYMnWh79yrPHt+vdx9tY+1gPZa7nJQtO/p8bLTDaHp4dzwEhQB7pZ4Xe3ok4XKuEPrVuc+wlpkmw==",
|
| 37 |
+
"peer": true,
|
| 38 |
+
"engines": {
|
| 39 |
+
"node": ">=0.10.0"
|
| 40 |
+
}
|
| 41 |
+
},
|
| 42 |
+
"node_modules/react-dom": {
|
| 43 |
+
"version": "19.2.1",
|
| 44 |
+
"resolved": "https://registry.npmjs.org/react-dom/-/react-dom-19.2.1.tgz",
|
| 45 |
+
"integrity": "sha512-ibrK8llX2a4eOskq1mXKu/TGZj9qzomO+sNfO98M6d9zIPOEhlBkMkBUBLd1vgS0gQsLDBzA+8jJBVXDnfHmJg==",
|
| 46 |
+
"peer": true,
|
| 47 |
+
"dependencies": {
|
| 48 |
+
"scheduler": "^0.27.0"
|
| 49 |
+
},
|
| 50 |
+
"peerDependencies": {
|
| 51 |
+
"react": "^19.2.1"
|
| 52 |
+
}
|
| 53 |
+
},
|
| 54 |
+
"node_modules/react-hot-toast": {
|
| 55 |
+
"version": "2.6.0",
|
| 56 |
+
"resolved": "https://registry.npmjs.org/react-hot-toast/-/react-hot-toast-2.6.0.tgz",
|
| 57 |
+
"integrity": "sha512-bH+2EBMZ4sdyou/DPrfgIouFpcRLCJ+HoCA32UoAYHn6T3Ur5yfcDCeSr5mwldl6pFOsiocmrXMuoCJ1vV8bWg==",
|
| 58 |
+
"dependencies": {
|
| 59 |
+
"csstype": "^3.1.3",
|
| 60 |
+
"goober": "^2.1.16"
|
| 61 |
+
},
|
| 62 |
+
"engines": {
|
| 63 |
+
"node": ">=10"
|
| 64 |
+
},
|
| 65 |
+
"peerDependencies": {
|
| 66 |
+
"react": ">=16",
|
| 67 |
+
"react-dom": ">=16"
|
| 68 |
+
}
|
| 69 |
+
},
|
| 70 |
+
"node_modules/scheduler": {
|
| 71 |
+
"version": "0.27.0",
|
| 72 |
+
"resolved": "https://registry.npmjs.org/scheduler/-/scheduler-0.27.0.tgz",
|
| 73 |
+
"integrity": "sha512-eNv+WrVbKu1f3vbYJT/xtiF5syA5HPIMtf9IgY/nKg0sWqzAUEvqY/xm7OcZc/qafLx/iO9FgOmeSAp4v5ti/Q==",
|
| 74 |
+
"peer": true
|
| 75 |
+
}
|
| 76 |
+
}
|
| 77 |
+
}
|
package.json
ADDED
|
@@ -0,0 +1,6 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
{
|
| 2 |
+
"dependencies": {
|
| 3 |
+
"lucide-react": "^0.560.0",
|
| 4 |
+
"react-hot-toast": "^2.6.0"
|
| 5 |
+
}
|
| 6 |
+
}
|
requirements.txt
ADDED
|
@@ -0,0 +1,8 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
fastapi>=0.95
|
| 2 |
+
uvicorn[standard]>=0.22
|
| 3 |
+
pydantic>=1.10
|
| 4 |
+
pytest>=7.0
|
| 5 |
+
requests>=2.28
|
| 6 |
+
motor>=3.1
|
| 7 |
+
python-dotenv>=1.0
|
| 8 |
+
PyJWT>=2.0
|
src/dao/project_dao.py
ADDED
|
@@ -0,0 +1,118 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
from typing import List, Optional
|
| 2 |
+
from src.db import get_db
|
| 3 |
+
from bson import ObjectId
|
| 4 |
+
from uuid import uuid4
|
| 5 |
+
|
| 6 |
+
|
| 7 |
+
class ProjectDAO:
|
| 8 |
+
COLLECTION = "projects"
|
| 9 |
+
|
| 10 |
+
@staticmethod
|
| 11 |
+
async def create_project(name: str, description: Optional[str], created_by: Optional[str] = None, client: Optional[str] = None) -> dict:
|
| 12 |
+
db = get_db()
|
| 13 |
+
doc = {"name": name, "description": description, "created_by": created_by, "contacts": [], "credentials": []}
|
| 14 |
+
if client:
|
| 15 |
+
doc["client"] = client
|
| 16 |
+
res = await db[ProjectDAO.COLLECTION].insert_one(doc)
|
| 17 |
+
return {"id": str(res.inserted_id), "name": name}
|
| 18 |
+
|
| 19 |
+
@staticmethod
|
| 20 |
+
async def list_projects(user_id: Optional[str] = None) -> dict:
|
| 21 |
+
db = get_db()
|
| 22 |
+
query = {}
|
| 23 |
+
if user_id:
|
| 24 |
+
query["created_by"] = user_id
|
| 25 |
+
cursor = db[ProjectDAO.COLLECTION].find(query, {"contacts": 0, "credentials": 0})
|
| 26 |
+
items = []
|
| 27 |
+
async for doc in cursor:
|
| 28 |
+
items.append({
|
| 29 |
+
"id": str(doc.get("_id")),
|
| 30 |
+
"name": doc.get("name"),
|
| 31 |
+
"description": doc.get("description"),
|
| 32 |
+
"info": doc.get("description"),
|
| 33 |
+
"client": doc.get("client"),
|
| 34 |
+
"created_by": doc.get("created_by"),
|
| 35 |
+
})
|
| 36 |
+
return {"count": len(items), "projects": items}
|
| 37 |
+
|
| 38 |
+
@staticmethod
|
| 39 |
+
async def get_project(project_id: str) -> Optional[dict]:
|
| 40 |
+
db = get_db()
|
| 41 |
+
doc = await db[ProjectDAO.COLLECTION].find_one({"_id": ObjectId(project_id)})
|
| 42 |
+
if not doc:
|
| 43 |
+
return None
|
| 44 |
+
return {
|
| 45 |
+
"id": str(doc.get("_id")),
|
| 46 |
+
"name": doc.get("name"),
|
| 47 |
+
"description": doc.get("description"),
|
| 48 |
+
"info": doc.get("description"),
|
| 49 |
+
"client": doc.get("client"),
|
| 50 |
+
"created_by": doc.get("created_by"),
|
| 51 |
+
"contacts": doc.get("contacts", []),
|
| 52 |
+
"credentials": doc.get("credentials", []),
|
| 53 |
+
}
|
| 54 |
+
|
| 55 |
+
@staticmethod
|
| 56 |
+
async def update_project(project_id: str, updates: dict) -> bool:
|
| 57 |
+
db = get_db()
|
| 58 |
+
if not updates:
|
| 59 |
+
return True
|
| 60 |
+
res = await db[ProjectDAO.COLLECTION].update_one({"_id": ObjectId(project_id)}, {"$set": updates})
|
| 61 |
+
return res.matched_count > 0
|
| 62 |
+
|
| 63 |
+
@staticmethod
|
| 64 |
+
async def add_contact(project_id: str, payload: dict) -> dict:
|
| 65 |
+
db = get_db()
|
| 66 |
+
contact = {"id": str(uuid4()), **payload}
|
| 67 |
+
res = await db[ProjectDAO.COLLECTION].update_one({"_id": ObjectId(project_id)}, {"$push": {"contacts": contact}})
|
| 68 |
+
if res.matched_count == 0:
|
| 69 |
+
return None
|
| 70 |
+
return contact
|
| 71 |
+
|
| 72 |
+
@staticmethod
|
| 73 |
+
async def delete_contact(project_id: str, contact_id: str) -> bool:
|
| 74 |
+
db = get_db()
|
| 75 |
+
res = await db[ProjectDAO.COLLECTION].update_one({"_id": ObjectId(project_id)}, {"$pull": {"contacts": {"id": contact_id}}})
|
| 76 |
+
return res.matched_count > 0
|
| 77 |
+
|
| 78 |
+
@staticmethod
|
| 79 |
+
async def add_credential(project_id: str, payload: dict) -> dict:
|
| 80 |
+
db = get_db()
|
| 81 |
+
# normalize payload: accept both `notes` and `note` from clients and store as `note`
|
| 82 |
+
p = dict(payload)
|
| 83 |
+
if "notes" in p and "note" not in p:
|
| 84 |
+
p["note"] = p.pop("notes")
|
| 85 |
+
credential = {"id": str(uuid4()), **p}
|
| 86 |
+
res = await db[ProjectDAO.COLLECTION].update_one({"_id": ObjectId(project_id)}, {"$push": {"credentials": credential}})
|
| 87 |
+
if res.matched_count == 0:
|
| 88 |
+
return None
|
| 89 |
+
return credential
|
| 90 |
+
|
| 91 |
+
@staticmethod
|
| 92 |
+
async def delete_credential(project_id: str, credential_id: str) -> bool:
|
| 93 |
+
db = get_db()
|
| 94 |
+
res = await db[ProjectDAO.COLLECTION].update_one({"_id": ObjectId(project_id)}, {"$pull": {"credentials": {"id": credential_id}}})
|
| 95 |
+
return res.matched_count > 0
|
| 96 |
+
|
| 97 |
+
@staticmethod
|
| 98 |
+
async def update_credential(project_id: str, credential_id: str, updates: dict) -> bool:
|
| 99 |
+
db = get_db()
|
| 100 |
+
if not updates:
|
| 101 |
+
return True
|
| 102 |
+
# map notes->note if provided
|
| 103 |
+
if "notes" in updates and "note" not in updates:
|
| 104 |
+
updates["note"] = updates.pop("notes")
|
| 105 |
+
# build $set for nested credential fields
|
| 106 |
+
set_ops = {f"credentials.$[elem].{k}": v for k, v in updates.items()}
|
| 107 |
+
res = await db[ProjectDAO.COLLECTION].update_one(
|
| 108 |
+
{"_id": ObjectId(project_id)},
|
| 109 |
+
{"$set": set_ops},
|
| 110 |
+
array_filters=[{"elem.id": credential_id}],
|
| 111 |
+
)
|
| 112 |
+
return res.matched_count > 0
|
| 113 |
+
|
| 114 |
+
@staticmethod
|
| 115 |
+
async def delete_project(project_id: str) -> bool:
|
| 116 |
+
db = get_db()
|
| 117 |
+
res = await db[ProjectDAO.COLLECTION].delete_one({"_id": ObjectId(project_id)})
|
| 118 |
+
return res.deleted_count > 0
|
src/dao/user_dao.py
ADDED
|
@@ -0,0 +1,25 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
from typing import Optional
|
| 2 |
+
from src.db import get_db
|
| 3 |
+
from bson import ObjectId
|
| 4 |
+
|
| 5 |
+
|
| 6 |
+
class UserDAO:
|
| 7 |
+
COLLECTION = "users"
|
| 8 |
+
|
| 9 |
+
@staticmethod
|
| 10 |
+
async def create_user(number: str, mpin: str, name: Optional[str] = None) -> dict:
|
| 11 |
+
db = get_db()
|
| 12 |
+
doc = {"number": number, "mpin": mpin, "name": name}
|
| 13 |
+
res = await db[UserDAO.COLLECTION].insert_one(doc)
|
| 14 |
+
doc["id"] = str(res.inserted_id)
|
| 15 |
+
return doc
|
| 16 |
+
|
| 17 |
+
@staticmethod
|
| 18 |
+
async def find_by_number(number: str) -> Optional[dict]:
|
| 19 |
+
db = get_db()
|
| 20 |
+
return await db[UserDAO.COLLECTION].find_one({"number": number})
|
| 21 |
+
|
| 22 |
+
@staticmethod
|
| 23 |
+
async def find_by_number_and_mpin(number: str, mpin: str) -> Optional[dict]:
|
| 24 |
+
db = get_db()
|
| 25 |
+
return await db[UserDAO.COLLECTION].find_one({"number": number, "mpin": mpin})
|
src/db.py
ADDED
|
@@ -0,0 +1,25 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
from motor.motor_asyncio import AsyncIOMotorClient
|
| 2 |
+
import os
|
| 3 |
+
from dotenv import load_dotenv
|
| 4 |
+
|
| 5 |
+
# Load environment from .env if present
|
| 6 |
+
load_dotenv()
|
| 7 |
+
|
| 8 |
+
MONGODB_URI = os.getenv("MONGODB_URI", "mongodb://localhost:27017/")
|
| 9 |
+
# Optional database name (useful for MongoDB SRV URIs that don't include a DB)
|
| 10 |
+
MONGODB_DB = os.getenv("MONGODB_DB", "ucprojects")
|
| 11 |
+
|
| 12 |
+
client: AsyncIOMotorClient | None = None
|
| 13 |
+
|
| 14 |
+
|
| 15 |
+
def get_client() -> AsyncIOMotorClient:
|
| 16 |
+
global client
|
| 17 |
+
if client is None:
|
| 18 |
+
client = AsyncIOMotorClient(MONGODB_URI)
|
| 19 |
+
return client
|
| 20 |
+
|
| 21 |
+
|
| 22 |
+
def get_db():
|
| 23 |
+
# Return a named database. If the URI already contains a default DB, this
|
| 24 |
+
# will still work because get_database accepts the explicit name.
|
| 25 |
+
return get_client().get_database(MONGODB_DB)
|
src/main.py
ADDED
|
@@ -0,0 +1,25 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
from fastapi import FastAPI
|
| 2 |
+
from fastapi.middleware.cors import CORSMiddleware
|
| 3 |
+
|
| 4 |
+
from src.routes.auth import router as auth_router
|
| 5 |
+
from src.routes.projects import router as projects_router
|
| 6 |
+
|
| 7 |
+
app = FastAPI(title="UC Projects API")
|
| 8 |
+
|
| 9 |
+
# Allow CORS for local frontend dev and deployment origins (adjust as needed)
|
| 10 |
+
app.add_middleware(
|
| 11 |
+
CORSMiddleware,
|
| 12 |
+
allow_origins=["*"],
|
| 13 |
+
allow_credentials=True,
|
| 14 |
+
allow_methods=["*"],
|
| 15 |
+
allow_headers=["*"],
|
| 16 |
+
)
|
| 17 |
+
|
| 18 |
+
app.include_router(auth_router, prefix="/api")
|
| 19 |
+
app.include_router(projects_router, prefix="/api")
|
| 20 |
+
|
| 21 |
+
|
| 22 |
+
if __name__ == "__main__":
|
| 23 |
+
import uvicorn
|
| 24 |
+
|
| 25 |
+
uvicorn.run("src.main:app", host="0.0.0.0", port=8000, reload=True)
|
src/models/project.py
ADDED
|
@@ -0,0 +1,32 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
from pydantic import BaseModel, Field
|
| 2 |
+
from typing import List, Optional
|
| 3 |
+
|
| 4 |
+
|
| 5 |
+
class Contact(BaseModel):
|
| 6 |
+
id: str
|
| 7 |
+
name: Optional[str]
|
| 8 |
+
phone: Optional[str]
|
| 9 |
+
email: Optional[str]
|
| 10 |
+
|
| 11 |
+
|
| 12 |
+
class Credential(BaseModel):
|
| 13 |
+
id: str
|
| 14 |
+
title: str
|
| 15 |
+
username: Optional[str]
|
| 16 |
+
password: Optional[str]
|
| 17 |
+
notes: Optional[str]
|
| 18 |
+
|
| 19 |
+
|
| 20 |
+
class ProjectCreate(BaseModel):
|
| 21 |
+
name: str = Field(..., min_length=1)
|
| 22 |
+
description: Optional[str]
|
| 23 |
+
created_by: str
|
| 24 |
+
|
| 25 |
+
|
| 26 |
+
class ProjectInfo(BaseModel):
|
| 27 |
+
id: str
|
| 28 |
+
name: str
|
| 29 |
+
description: Optional[str]
|
| 30 |
+
created_by: str
|
| 31 |
+
contacts: List[Contact] = []
|
| 32 |
+
credentials: List[Credential] = []
|
src/routes/auth.py
ADDED
|
@@ -0,0 +1,54 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
from fastapi import APIRouter, HTTPException
|
| 2 |
+
from pydantic import BaseModel, Field
|
| 3 |
+
from typing import Optional
|
| 4 |
+
|
| 5 |
+
from src.dao.user_dao import UserDAO
|
| 6 |
+
import os
|
| 7 |
+
import jwt
|
| 8 |
+
from datetime import datetime, timedelta
|
| 9 |
+
|
| 10 |
+
# NOTE: Set JWT_SECRET in environment for production
|
| 11 |
+
JWT_SECRET = os.environ.get("JWT_SECRET", "dev-secret-change-this")
|
| 12 |
+
JWT_ALGORITHM = "HS256"
|
| 13 |
+
JWT_EXP_DAYS = int(os.environ.get("JWT_EXP_DAYS", "7"))
|
| 14 |
+
|
| 15 |
+
router = APIRouter(tags=["auth"])
|
| 16 |
+
|
| 17 |
+
|
| 18 |
+
class RegisterModel(BaseModel):
|
| 19 |
+
number: str = Field(...)
|
| 20 |
+
mpin: str = Field(...)
|
| 21 |
+
name: Optional[str]
|
| 22 |
+
|
| 23 |
+
|
| 24 |
+
class LoginModel(BaseModel):
|
| 25 |
+
number: str = Field(...)
|
| 26 |
+
mpin: str = Field(...)
|
| 27 |
+
|
| 28 |
+
|
| 29 |
+
@router.post("/register")
|
| 30 |
+
async def register(payload: RegisterModel):
|
| 31 |
+
existing = await UserDAO.find_by_number(payload.number)
|
| 32 |
+
if existing:
|
| 33 |
+
raise HTTPException(status_code=400, detail="User already exists")
|
| 34 |
+
doc = await UserDAO.create_user(payload.number, payload.mpin, payload.name)
|
| 35 |
+
return {"id": doc.get("id"), "number": doc.get("number"), "name": doc.get("name")}
|
| 36 |
+
|
| 37 |
+
|
| 38 |
+
@router.post("/login")
|
| 39 |
+
async def login(payload: LoginModel):
|
| 40 |
+
user = await UserDAO.find_by_number_and_mpin(payload.number, payload.mpin)
|
| 41 |
+
if not user:
|
| 42 |
+
raise HTTPException(status_code=401, detail="Invalid credentials")
|
| 43 |
+
# create JWT token
|
| 44 |
+
now = datetime.utcnow()
|
| 45 |
+
exp = now + timedelta(days=JWT_EXP_DAYS)
|
| 46 |
+
token_payload = {
|
| 47 |
+
"sub": str(user.get("_id")),
|
| 48 |
+
"number": user.get("number"),
|
| 49 |
+
"name": user.get("name"),
|
| 50 |
+
"iat": int(now.timestamp()),
|
| 51 |
+
"exp": int(exp.timestamp()),
|
| 52 |
+
}
|
| 53 |
+
token = jwt.encode(token_payload, JWT_SECRET, algorithm=JWT_ALGORITHM)
|
| 54 |
+
return {"id": str(user.get("_id")), "number": user.get("number"), "name": user.get("name"), "token": token}
|
src/routes/projects.py
ADDED
|
@@ -0,0 +1,125 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
from fastapi import APIRouter, HTTPException
|
| 2 |
+
from pydantic import BaseModel, Field
|
| 3 |
+
from typing import Optional
|
| 4 |
+
|
| 5 |
+
from src.dao.project_dao import ProjectDAO
|
| 6 |
+
|
| 7 |
+
router = APIRouter(tags=["projects"])
|
| 8 |
+
|
| 9 |
+
|
| 10 |
+
class ProjectCreate(BaseModel):
|
| 11 |
+
name: str = Field(...)
|
| 12 |
+
description: Optional[str] = None
|
| 13 |
+
info: Optional[str] = None
|
| 14 |
+
created_by: Optional[str] = None
|
| 15 |
+
client: Optional[str] = None
|
| 16 |
+
|
| 17 |
+
|
| 18 |
+
class ContactModel(BaseModel):
|
| 19 |
+
name: Optional[str] = None
|
| 20 |
+
phone: Optional[str] = None
|
| 21 |
+
email: Optional[str] = None
|
| 22 |
+
designation: Optional[str] = None
|
| 23 |
+
|
| 24 |
+
|
| 25 |
+
class CredentialModel(BaseModel):
|
| 26 |
+
title: Optional[str] = None
|
| 27 |
+
username: Optional[str] = None
|
| 28 |
+
password: Optional[str] = None
|
| 29 |
+
notes: Optional[str] = None
|
| 30 |
+
note: Optional[str] = None
|
| 31 |
+
|
| 32 |
+
|
| 33 |
+
class CredentialUpdate(BaseModel):
|
| 34 |
+
title: Optional[str] = None
|
| 35 |
+
username: Optional[str] = None
|
| 36 |
+
password: Optional[str] = None
|
| 37 |
+
notes: Optional[str] = None
|
| 38 |
+
note: Optional[str] = None
|
| 39 |
+
|
| 40 |
+
|
| 41 |
+
class ProjectUpdate(BaseModel):
|
| 42 |
+
name: Optional[str]
|
| 43 |
+
description: Optional[str] = None
|
| 44 |
+
info: Optional[str] = None
|
| 45 |
+
client: Optional[str] = None
|
| 46 |
+
|
| 47 |
+
|
| 48 |
+
@router.get("/projects")
|
| 49 |
+
async def list_projects(user_id: Optional[str] = None):
|
| 50 |
+
return await ProjectDAO.list_projects(user_id)
|
| 51 |
+
|
| 52 |
+
|
| 53 |
+
@router.post("/projects")
|
| 54 |
+
async def create_project(payload: ProjectCreate):
|
| 55 |
+
# support either `description` or `info` coming from frontend
|
| 56 |
+
desc = payload.description if payload.description is not None else payload.info
|
| 57 |
+
return await ProjectDAO.create_project(payload.name, desc, payload.created_by, payload.client)
|
| 58 |
+
|
| 59 |
+
|
| 60 |
+
@router.get("/projects/{project_id}")
|
| 61 |
+
async def get_project(project_id: str):
|
| 62 |
+
doc = await ProjectDAO.get_project(project_id)
|
| 63 |
+
if not doc:
|
| 64 |
+
raise HTTPException(status_code=404, detail="Project not found")
|
| 65 |
+
return doc
|
| 66 |
+
|
| 67 |
+
|
| 68 |
+
@router.post("/projects/{project_id}/contacts")
|
| 69 |
+
async def add_contact(project_id: str, payload: ContactModel):
|
| 70 |
+
contact = await ProjectDAO.add_contact(project_id, payload.dict())
|
| 71 |
+
if not contact:
|
| 72 |
+
raise HTTPException(status_code=404, detail="Project not found")
|
| 73 |
+
return contact
|
| 74 |
+
|
| 75 |
+
|
| 76 |
+
@router.delete("/projects/{project_id}/contacts/{contact_id}")
|
| 77 |
+
async def delete_contact(project_id: str, contact_id: str):
|
| 78 |
+
ok = await ProjectDAO.delete_contact(project_id, contact_id)
|
| 79 |
+
if not ok:
|
| 80 |
+
raise HTTPException(status_code=404, detail="Project not found")
|
| 81 |
+
return {"ok": True}
|
| 82 |
+
|
| 83 |
+
|
| 84 |
+
@router.post("/projects/{project_id}/credentials")
|
| 85 |
+
async def add_credential(project_id: str, payload: CredentialModel):
|
| 86 |
+
credential = await ProjectDAO.add_credential(project_id, payload.dict())
|
| 87 |
+
if not credential:
|
| 88 |
+
raise HTTPException(status_code=404, detail="Project not found")
|
| 89 |
+
return credential
|
| 90 |
+
|
| 91 |
+
|
| 92 |
+
@router.delete("/projects/{project_id}/credentials/{credential_id}")
|
| 93 |
+
async def delete_credential(project_id: str, credential_id: str):
|
| 94 |
+
ok = await ProjectDAO.delete_credential(project_id, credential_id)
|
| 95 |
+
if not ok:
|
| 96 |
+
raise HTTPException(status_code=404, detail="Project not found")
|
| 97 |
+
return {"ok": True}
|
| 98 |
+
|
| 99 |
+
|
| 100 |
+
@router.put("/projects/{project_id}/credentials/{credential_id}")
|
| 101 |
+
async def update_credential(project_id: str, credential_id: str, payload: CredentialUpdate):
|
| 102 |
+
ok = await ProjectDAO.update_credential(project_id, credential_id, payload.dict(exclude_none=True))
|
| 103 |
+
if not ok:
|
| 104 |
+
raise HTTPException(status_code=404, detail="Project or credential not found")
|
| 105 |
+
return {"ok": True}
|
| 106 |
+
|
| 107 |
+
|
| 108 |
+
@router.put("/projects/{project_id}")
|
| 109 |
+
async def update_project(project_id: str, payload: ProjectUpdate):
|
| 110 |
+
data = payload.dict(exclude_none=True)
|
| 111 |
+
# allow frontend to send `info` — map it to `description` in DB
|
| 112 |
+
if "info" in data:
|
| 113 |
+
data["description"] = data.pop("info")
|
| 114 |
+
ok = await ProjectDAO.update_project(project_id, data)
|
| 115 |
+
if not ok:
|
| 116 |
+
raise HTTPException(status_code=404, detail="Project not found")
|
| 117 |
+
return {"ok": True}
|
| 118 |
+
|
| 119 |
+
|
| 120 |
+
@router.delete("/projects/{project_id}")
|
| 121 |
+
async def delete_project(project_id: str):
|
| 122 |
+
ok = await ProjectDAO.delete_project(project_id)
|
| 123 |
+
if not ok:
|
| 124 |
+
raise HTTPException(status_code=404, detail="Project not found")
|
| 125 |
+
return {"ok": True}
|
src/utils/helpers.py
ADDED
|
@@ -0,0 +1,2 @@
|
|
|
|
|
|
|
|
|
|
| 1 |
+
def normalize_name(name: str) -> str:
|
| 2 |
+
return " ".join(part.capitalize() for part in name.strip().split())
|