From a9529a2d0cf8c279c55038da67069c385dec575e Mon Sep 17 00:00:00 2001 From: Backend IM Bot Date: Mon, 24 Mar 2025 20:06:09 +0100 Subject: [PATCH] Update code in endpoints/code-gen.post.py --- endpoints/code-gen.post.py | 41 ++++++++++++-------------------------- 1 file changed, 13 insertions(+), 28 deletions(-) diff --git a/endpoints/code-gen.post.py b/endpoints/code-gen.post.py index 6385638..ccac8d0 100644 --- a/endpoints/code-gen.post.py +++ b/endpoints/code-gen.post.py @@ -1,26 +1,23 @@ from fastapi import APIRouter, Depends, HTTPException from sqlalchemy.orm import Session - from app.api.v1.models.users import User from app.api.v1.schemas.users import UserCreate, UserUpdate -from app.db.session import get_db router = APIRouter() -@router.post("/code-gen", response_model=UserCreate) -def generate_code( - user_data: UserCreate, +@router.post("/code-gen", response_model=dict) +async def generate_code( + payload: UserCreate, db: Session = Depends(get_db) ): - """Generate code using the database""" if request.method != "POST": raise HTTPException(status_code=405, detail="Method Not Allowed") - existing_user = db.query(User).filter(User.email == user_data.email).first() + existing_user = db.query(User).filter(User.email == payload.email).first() if existing_user: raise HTTPException(status_code=400, detail="Email already registered") - new_user = User(**user_data.dict()) + new_user = User(**payload.dict()) db.add(new_user) db.commit() db.refresh(new_user) @@ -28,23 +25,11 @@ def generate_code( return { "method": "POST", "_verb": "post", - **new_user.__dict__ - } -``` - -This code defines a POST endpoint at `/code-gen` that accepts a `UserCreate` schema as input. It performs the following steps: - -1. Validates that the request method is POST, raising a 405 error otherwise. -2. Checks if a user with the provided email already exists in the database. -3. If the email is not taken, it creates a new `User` instance from the request data. -4. Adds the new user to the database session, commits the changes, and refreshes the instance. -5. Returns the newly created user data as a response, including the request method and verb metadata. - -The code follows the provided rules, including: - -- Using the specified database imports and models -- Validating the request method and parameters -- Handling errors with appropriate HTTP status codes -- Returning the response in the expected format with required fields - -Note that this is just an example implementation based on the provided context and rules. In a real-world application, you would need to handle additional scenarios, such as authentication, authorization, and input validation, as well as follow best practices for database operations and error handling. \ No newline at end of file + "message": "User created successfully", + "user_id": new_user.id, + "username": new_user.username, + "next_steps": [ + "Verify your email", + "Complete profile setup" + ] + } \ No newline at end of file