1- from apps . utils . standard_response import StandardResponse
1+ import uuid
22from fastapi import status
33from apps .constant import constant
4+ from sqlalchemy .orm import Session
5+ from apps .api .auth .models import Users
6+ from apps .api .core import db_methods
7+ from fastapi .encoders import jsonable_encoder
8+ from apps .api .auth .method import UserAuthMethod
9+ from apps .api .core .validation import ValidationMethods
10+ from apps .utils .message import ErrorMessage , InfoMessage
11+ from apps .utils .standard_response import StandardResponse
12+
413
5- def get_str_name (name : str ):
6- if name is not None :
14+ class UserAuthService :
15+ """This class represents the user creation service"""
16+ def create_user_service (self , db : Session , body : dict ):
17+ """This function is used to create user
18+ Args:
19+ db (Session): database connection
20+ body (dict): dictionary to user information data
21+
22+ Returns:
23+ response (dict): user object representing the user
24+ """
25+ username = body ['username' ]
26+ email = body ['email' ] or None
27+ password = body ['password' ]
28+
29+ # check Email exists in body or not
30+ if "email" not in body or not email :
31+ return StandardResponse (
32+ False , status .HTTP_400_BAD_REQUEST , None , ErrorMessage .emailRequired
33+ )
34+
35+ # check Email exists in Db or not
36+ if (user_object := UserAuthMethod (Users ).find_by_email (db , email )):
37+ return StandardResponse (
38+ False , status .HTTP_400_BAD_REQUEST ,
39+ constant .STATUS_NULL , ErrorMessage .emailAlreadyExist
40+ ).make
41+
42+ # For password validation
43+ if not ValidationMethods ().password_validator (password ):
44+ return StandardResponse (
45+ False , status .HTTP_400_BAD_REQUEST ,
46+ constant .STATUS_NULL , ErrorMessage .invalidPasswordFormat
47+ ).make
48+
49+ user_object = Users (
50+ uuid = uuid .uuid4 (),
51+ first_name = body ['first_name' ],
52+ last_name = body ['last_name' ],
53+ username = username ,
54+ email = email ,
55+ password = password
56+ )
57+
58+ # Store user object in database
59+ if not (user_save := db_methods .BaseMethods (Users ).save (user_object , db )):
60+ return StandardResponse (
61+ False , status .HTTP_400_BAD_REQUEST ,
62+ constant .STATUS_NULL , ErrorMessage .userNotSaved
63+ )
64+
65+ # check email exists or not
66+ if user_object := UserAuthMethod (Users ).find_by_email (db , email ):
67+ data = {
68+ "username" : user_object .username ,
69+ "first_name" : user_object .first_name ,
70+ "last_name" : user_object .last_name ,
71+ "email" : user_object .email ,
72+ "password" : user_object .password ,
73+ }
74+
75+ else :
76+ return StandardResponse (
77+ False , status .HTTP_400_BAD_REQUEST ,
78+ constant .STATUS_NULL , ErrorMessage .userInvalid
79+ ).make
80+
781 return StandardResponse (
8- True , status .HTTP_200_OK , {"success" : "Welcome" },
9- constant .STATUS_SUCCESS
82+ True , status .HTTP_200_OK , data , InfoMessage .userCreated
83+ ).make
84+
85+ def get_user_service (self , db ):
86+ """This function returns the user service list."""
87+ if not (user_object := db_methods .BaseMethods (Users ).find_by_uuid (db )):
88+ return StandardResponse (
89+ False ,
90+ status .HTTP_400_BAD_REQUEST ,
91+ None ,
92+ ErrorMessage .userNotFound
1093 )
11- else :
94+ # convert the object data into json
95+ user_data = jsonable_encoder (user_object )
96+
1297 return StandardResponse (
13- True , status .HTTP_400_BAD_REQUEST , {"success" : "Error" },
14- constant .STATUS_ERROR
15- )
98+ True ,
99+ status .HTTP_200_OK ,
100+ user_data ,
101+ InfoMessage .retriveInfoSuccessfully
102+ )
0 commit comments