Skip to content

Commit

Permalink
added google provided fields to user
Browse files Browse the repository at this point in the history
  • Loading branch information
feijooso committed Feb 20, 2024
1 parent d9463b1 commit 5cbf59e
Show file tree
Hide file tree
Showing 5 changed files with 44 additions and 15 deletions.
8 changes: 6 additions & 2 deletions app/docker/tablas.sql
Original file line number Diff line number Diff line change
@@ -1,11 +1,15 @@
CREATE SCHEMA IF NOT EXISTS dev;

CREATE TABLE IF NOT EXISTS dev.users (
id SERIAL PRIMARY KEY, name VARCHAR(255) NOT NULL, mail VARCHAR(255) NOT NULL
id SERIAL PRIMARY KEY,
name VARCHAR(255),
email VARCHAR(255) UNIQUE NOT NULL,
genre VARCHAR(20),
photo VARCHAR(255)
);

INSERT INTO
dev.users (name, mail)
dev.users (name, email)
VALUES ('Agus', 'agus@fi.uba.ar'),
('Pach', 'pach@fi.uba.ar'),
('Sofi', 'sofi@fi.uba.ar'),
Expand Down
6 changes: 4 additions & 2 deletions app/models/users.py
Original file line number Diff line number Diff line change
Expand Up @@ -6,5 +6,7 @@ class User(Base):
__tablename__ = "users"
__table_args__ = {'schema': 'dev'}
id = Column(Integer, primary_key=True, index=True, autoincrement=True)
name = Column(String, nullable=False)
mail = Column(String, nullable=False)
name = Column(String, nullable=True)
email = Column(String, nullable=False)
genre = Column(String, nullable=True)
photo = Column(String, nullable=True)
21 changes: 17 additions & 4 deletions app/repository/Users.py
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
from sqlalchemy import create_engine, engine
from sqlalchemy.orm import Session
import os
from typing import Optional
from models.users import User


Expand Down Expand Up @@ -31,16 +32,28 @@ def get_user(self, user_id: int):
user = self.session.query(User).filter_by(id=user_id).first()
return user.__dict__ if user else None

def get_user_by_name(self, name: str):
user = self.session.query(User).filter_by(name=name).first()
def get_user_by_email(self, email: str):
user = self.session.query(User).filter_by(email=email).first()
return user.__dict__ if user else None

def get_all_users(self):
users = self.session.query(User).all()
return self.__parse_result(users)

def create_user(self, name: str, mail: str):
new_user = User(name=name, mail=mail)
def create_user(self, email: str,
name: Optional[str] = None,
genre: Optional[str] = None,
photo: Optional[str] = None):
user_data = {'email': email}

if name is not None:
user_data['name'] = name
if genre is not None:
user_data['genre'] = genre
if photo is not None:
user_data['photo'] = photo

new_user = User(**user_data)
self.session.add(new_user)
self.session.commit()
return new_user
Expand Down
6 changes: 4 additions & 2 deletions app/schemas/Schemas.py
Original file line number Diff line number Diff line change
Expand Up @@ -4,11 +4,13 @@
class UserSchema(BaseModel):
id: int
name: str
email: str
genre: str
photo: str


class CreateUserSchema(BaseModel):
name: str
mail: str
email: str


class LoginRequest(BaseModel):
Expand Down
18 changes: 13 additions & 5 deletions app/service/Users.py
Original file line number Diff line number Diff line change
Expand Up @@ -45,16 +45,24 @@ def get_all_users(self):
return self.user_repository.get_all_users()

def create_user(self, user_data: dict):
name = user_data.get("name")
mail = user_data.get("mail")
return self.user_repository.create_user(name, mail)
email = user_data.get("email")
return self.user_repository.create_user(email)

def login(self, auth_code: str):
access_token = get_access_token(auth_code)
user_info = get_user_info(access_token)

user = self.user_repository.get_user_by_name(user_info["email"])
user = self.user_repository.get_user_by_email(user_info.get("email"))

if user is None:
user = self.user_repository.create_user(user_info["email"])
user_data = {'email': user_info.get("email")}
if user_info.get("genre") is not None:
user_data['genre'] = user_info.get("genre")
if user_info.get("name") is not None:
user_data['name'] = user_info.get("name")
if user_info.get("picture") is not None:
user_data['photo'] = user_info.get("picture")

user = self.user_repository.create_user(**user_data)

return user

0 comments on commit 5cbf59e

Please sign in to comment.