-
-
Notifications
You must be signed in to change notification settings - Fork 7.4k
Backend update #1542
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Closed
Closed
Backend update #1542
Changes from all commits
Commits
Show all changes
20 commits
Select commit
Hold shift + click to select a range
3aec0c8
Configuração de credenciais
marcelomizuno c08554b
First commit ticket-page-test ( conf. routes, SidebarItems and ticket…
nandacruz b295f29
Merge pull request #1 from marcelomizuno/feature/frontend-ticket-page
marcelomizuno 9a36735
Update test - Adicionando um <li> na pagina de tickets usando a branc…
nandacruz c929cc0
Added tickets
marcelomizuno bac2fde
Added middleware
marcelomizuno 80eb972
Middleware fix
marcelomizuno 0bd34d8
Revert "Middleware fix"
marcelomizuno 3f0ccb2
Revert "Added middleware"
marcelomizuno eddb24d
rota para checar CORS-Origins
marcelomizuno baf8766
Correção de relacionamento de classes
marcelomizuno 2c20829
Update Dockerfile
marcelomizuno 9b22480
Update Dockerfile
marcelomizuno dd39b9f
Revert "Update Dockerfile"
marcelomizuno 6c04c7f
backend/readme.md restored
marcelomizuno e94eb72
Update db.py
marcelomizuno 7f5a3d4
Update db.py
marcelomizuno b02a77f
Update db.py
marcelomizuno ad1f58b
Migration fix
marcelomizuno 84659d2
Add category to ticket table (bd)
marcelomizuno File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. At least the change with the I don't think this is something anybody wants on production that happens automatically. |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
24 changes: 24 additions & 0 deletions
24
backend/app/alembic/versions/b54d6e812a9c_add_category_to_ticket.py
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,24 @@ | ||
""" | ||
Add category to ticket table | ||
""" | ||
from alembic import op | ||
import sqlalchemy as sa | ||
import sqlmodel.sql.sqltypes | ||
|
||
|
||
# revision identifiers, used by Alembic. | ||
revision = 'b54d6e812a9c' | ||
down_revision = 'f23a9c45d178' | ||
branch_labels = None | ||
depends_on = None | ||
|
||
|
||
def upgrade(): | ||
# Add category column to ticket table | ||
op.add_column('ticket', sa.Column('category', sqlmodel.sql.sqltypes.AutoString(), nullable=False, | ||
server_default="Suporte")) # Default to "Suporte" for existing tickets | ||
|
||
|
||
def downgrade(): | ||
# Remove category column from ticket table | ||
op.drop_column('ticket', 'category') |
68 changes: 68 additions & 0 deletions
68
backend/app/alembic/versions/f23a9c45d178_add_ticket_and_comment.py
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,68 @@ | ||
""" | ||
Add ticket and comment tables | ||
""" | ||
from alembic import op | ||
import sqlalchemy as sa | ||
import sqlmodel.sql.sqltypes | ||
from sqlalchemy.dialects.postgresql import UUID | ||
from uuid import uuid4 | ||
|
||
|
||
# revision identifiers, used by Alembic. | ||
revision = 'f23a9c45d178' | ||
down_revision = '1a31ce608336' | ||
branch_labels = None | ||
depends_on = None | ||
|
||
|
||
def upgrade(): | ||
# Create ticket table | ||
op.create_table( | ||
'ticket', | ||
sa.Column("id", UUID(), nullable=False, server_default=sa.text("gen_random_uuid()")), | ||
sa.Column("title", sqlmodel.sql.sqltypes.AutoString(), nullable=False), | ||
sa.Column("description", sqlmodel.sql.sqltypes.AutoString(), nullable=True), | ||
sa.Column("status", sqlmodel.sql.sqltypes.AutoString(), nullable=False), | ||
sa.Column("priority", sqlmodel.sql.sqltypes.AutoString(), nullable=True), | ||
sa.Column("created_at", sa.DateTime(), nullable=False, server_default=sa.text("now()")), | ||
sa.Column("updated_at", sa.DateTime(), nullable=True), | ||
sa.Column("owner_id", UUID(), nullable=False), | ||
sa.PrimaryKeyConstraint("id"), | ||
sa.ForeignKeyConstraint( | ||
["owner_id"], ["user.id"], ondelete="CASCADE" | ||
), | ||
) | ||
|
||
# Create index for ticket lookup by owner | ||
op.create_index(op.f('ix_ticket_owner_id'), 'ticket', ['owner_id'], unique=False) | ||
|
||
# Create comment table | ||
op.create_table( | ||
'comment', | ||
sa.Column("id", UUID(), nullable=False, server_default=sa.text("gen_random_uuid()")), | ||
sa.Column("content", sqlmodel.sql.sqltypes.AutoString(), nullable=False), | ||
sa.Column("created_at", sa.DateTime(), nullable=False, server_default=sa.text("now()")), | ||
sa.Column("ticket_id", UUID(), nullable=False), | ||
sa.Column("user_id", UUID(), nullable=False), | ||
sa.PrimaryKeyConstraint("id"), | ||
sa.ForeignKeyConstraint( | ||
["ticket_id"], ["ticket.id"], ondelete="CASCADE" | ||
), | ||
sa.ForeignKeyConstraint( | ||
["user_id"], ["user.id"], ondelete="CASCADE" | ||
), | ||
) | ||
|
||
# Create indexes for faster comment lookups | ||
op.create_index(op.f('ix_comment_ticket_id'), 'comment', ['ticket_id'], unique=False) | ||
op.create_index(op.f('ix_comment_user_id'), 'comment', ['user_id'], unique=False) | ||
|
||
|
||
def downgrade(): | ||
# Drop tables in reverse order (comments first, then tickets) | ||
op.drop_index(op.f('ix_comment_user_id'), table_name='comment') | ||
op.drop_index(op.f('ix_comment_ticket_id'), table_name='comment') | ||
op.drop_table('comment') | ||
|
||
op.drop_index(op.f('ix_ticket_owner_id'), table_name='ticket') | ||
op.drop_table('ticket') |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,172 @@ | ||
import uuid | ||
from typing import Any | ||
|
||
from fastapi import APIRouter, HTTPException, Query | ||
from sqlmodel import func, select | ||
|
||
from app.api.deps import CurrentUser, SessionDep | ||
from app.models import ( | ||
Ticket, | ||
TicketCreate, | ||
TicketUpdate, | ||
TicketPublic, | ||
TicketsPublic, | ||
TicketDetailPublic, | ||
Comment, | ||
CommentCreate, | ||
CommentPublic, | ||
Message, | ||
TicketCategory, | ||
TicketPriority, | ||
TicketStatus | ||
) | ||
|
||
router = APIRouter(prefix="/tickets", tags=["tickets"]) | ||
|
||
|
||
@router.get("/", response_model=TicketsPublic) | ||
def read_tickets( | ||
session: SessionDep, | ||
current_user: CurrentUser, | ||
skip: int = 0, | ||
limit: int = 100, | ||
page: int = Query(1, ge=1), | ||
category: TicketCategory = None, | ||
priority: TicketPriority = None, | ||
status: TicketStatus = None | ||
) -> Any: | ||
""" | ||
Listar todos os tickets (com filtros e paginação). | ||
""" | ||
skip = (page - 1) * limit | ||
|
||
# Base query | ||
query = select(Ticket) | ||
count_query = select(func.count()).select_from(Ticket) | ||
|
||
# Apply filters | ||
if category: | ||
query = query.where(Ticket.category == category) | ||
count_query = count_query.where(Ticket.category == category) | ||
|
||
if priority: | ||
query = query.where(Ticket.priority == priority) | ||
count_query = count_query.where(Ticket.priority == priority) | ||
|
||
if status: | ||
query = query.where(Ticket.status == status) | ||
count_query = count_query.where(Ticket.status == status) | ||
|
||
# Apply user filter if not superuser | ||
if not current_user.is_superuser: | ||
query = query.where(Ticket.user_id == current_user.id) | ||
count_query = count_query.where(Ticket.user_id == current_user.id) | ||
|
||
# Get count and tickets | ||
count = session.exec(count_query).one() | ||
tickets = session.exec(query.offset(skip).limit(limit)).all() | ||
|
||
return TicketsPublic(data=tickets, count=count, page=page) | ||
|
||
|
||
@router.get("/{id}", response_model=TicketDetailPublic) | ||
def read_ticket(session: SessionDep, current_user: CurrentUser, id: uuid.UUID) -> Any: | ||
""" | ||
Retorna detalhes de um ticket. | ||
""" | ||
ticket = session.get(Ticket, id) | ||
if not ticket: | ||
raise HTTPException(status_code=404, detail="Ticket não encontrado") | ||
|
||
if not current_user.is_superuser and (ticket.user_id != current_user.id): | ||
raise HTTPException(status_code=400, detail="Permissões insuficientes") | ||
|
||
return ticket | ||
|
||
|
||
@router.post("/", response_model=TicketPublic) | ||
def create_ticket( | ||
*, session: SessionDep, current_user: CurrentUser, ticket_in: TicketCreate | ||
) -> Any: | ||
""" | ||
Criar um novo ticket. | ||
""" | ||
ticket = Ticket.model_validate(ticket_in, update={"user_id": current_user.id}) | ||
session.add(ticket) | ||
session.commit() | ||
session.refresh(ticket) | ||
return ticket | ||
|
||
|
||
@router.put("/{id}", response_model=TicketPublic) | ||
def update_ticket( | ||
*, | ||
session: SessionDep, | ||
current_user: CurrentUser, | ||
id: uuid.UUID, | ||
ticket_in: TicketUpdate, | ||
) -> Any: | ||
""" | ||
Atualizar um ticket existente. | ||
""" | ||
ticket = session.get(Ticket, id) | ||
if not ticket: | ||
raise HTTPException(status_code=404, detail="Ticket não encontrado") | ||
|
||
if not current_user.is_superuser and (ticket.user_id != current_user.id): | ||
raise HTTPException(status_code=400, detail="Permissões insuficientes") | ||
|
||
update_dict = ticket_in.model_dump(exclude_unset=True) | ||
ticket.sqlmodel_update(update_dict) | ||
ticket.updated_at = func.now() # Update the updated_at field | ||
|
||
session.add(ticket) | ||
session.commit() | ||
session.refresh(ticket) | ||
return ticket | ||
|
||
|
||
@router.delete("/{id}") | ||
def delete_ticket( | ||
session: SessionDep, current_user: CurrentUser, id: uuid.UUID | ||
) -> Message: | ||
""" | ||
Deletar um ticket. | ||
""" | ||
ticket = session.get(Ticket, id) | ||
if not ticket: | ||
raise HTTPException(status_code=404, detail="Ticket não encontrado") | ||
|
||
if not current_user.is_superuser and (ticket.user_id != current_user.id): | ||
raise HTTPException(status_code=400, detail="Permissões insuficientes") | ||
|
||
session.delete(ticket) | ||
session.commit() | ||
return Message(message="Ticket removido com sucesso") | ||
|
||
|
||
@router.post("/{id}/comments", response_model=CommentPublic) | ||
def create_comment( | ||
*, | ||
session: SessionDep, | ||
current_user: CurrentUser, | ||
id: uuid.UUID, | ||
comment_in: CommentCreate, | ||
) -> Any: | ||
""" | ||
Adicionar comentário a um ticket. | ||
""" | ||
ticket = session.get(Ticket, id) | ||
if not ticket: | ||
raise HTTPException(status_code=404, detail="Ticket não encontrado") | ||
|
||
comment = Comment( | ||
**comment_in.model_dump(), | ||
ticket_id=id, | ||
user_id=current_user.id | ||
) | ||
|
||
session.add(comment) | ||
session.commit() | ||
session.refresh(comment) | ||
return comment |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
This file change should be removed from the commit.