File size: 3,513 Bytes
b39c0ba
d57efd6
 
 
 
 
 
 
9002555
 
 
d57efd6
 
9002555
 
d57efd6
 
 
 
 
 
 
 
 
 
 
 
 
 
9002555
d57efd6
 
 
 
9002555
d57efd6
9002555
d57efd6
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
9002555
d57efd6
9002555
d57efd6
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
from fastapi import APIRouter, Depends
from fastapi.responses import JSONResponse
from api.router.user import user_dependency
from typing import Annotated
from sqlalchemy.orm import Session
from db.database import get_db
from db.models import Role, User
from service.dto import RoleCreate, RoleUpdate


router = APIRouter(tags=["Roles"])
db_dependency = Annotated[Session, Depends(get_db)]


@router.get("/roles")
async def get_data_roles(user: user_dependency, db: db_dependency):
    # Periksa apakah user valid dan memiliki role_id = 1
    if user is None or user.get("role_id") != 1:
        return JSONResponse(status_code=401, content="Authentication Failed")

    try:
        # Query data role
        roles = db.query(Role).all()
        
        # Jika tidak ada role ditemukan, kembalikan 404
        if not roles:
            return JSONResponse(status_code=404, content="No roles found")
        
        return roles

    except Exception as e:
        # Menangkap kesalahan untuk debug
        print(f"Error fetching roles: {str(e)}")
        return JSONResponse(status_code=500, content="Internal Server Error")

# POST: Add a new role
@router.post("/roles")
async def add_data_roles(
    role_data: RoleCreate, user: user_dependency, db: db_dependency
):
    if user is None or user.get("role_id") != 1:
        return JSONResponse(status_code=401, content="Authentication Failed")

    new_role = Role(name=role_data.name)
    db.add(new_role)
    db.commit()
    db.refresh(new_role)

    return {"message": "Role added successfully", "role": new_role}


@router.put("/roles/{role_id}")
async def update_data_roles(
    role_id: int, role_data: RoleUpdate, user: user_dependency, db: db_dependency
):
    if user is None or user.get("role_id") != 1:
        return JSONResponse(status_code=401, content="Authentication Failed")

    role = db.query(Role).filter(Role.id == id).first()
    if role is None:
        return JSONResponse(status_code=404, content="Role not found")

    role.name = role_data.name
    db.commit()
    db.refresh(role)

    return {"message": "Role updated successfully", "role": role}


# DELETE: Remove a role
@router.delete("/roles/{id}")
async def remove_data_roles(id: int, user: user_dependency, db: db_dependency):
    if user is None or user.get("role_id") != 1:
        return JSONResponse(status_code=401, content="Authentication Failed")

    role = db.query(Role).filter(Role.id == id).first()
    if role is None:
        return JSONResponse(status_code=404, content="Role not found")

    db.delete(role)
    db.commit()

    return {"message": "Role removed successfully"}


@router.put("/role_user/{id}")
async def update_user_role(
    user: user_dependency, db: db_dependency, user_id: int, role_data: RoleUpdate
):
    # Check if the current user is authenticated and has an admin role (role_id == 1)
    if user is None or user.get("role_id") != 1:
        return JSONResponse(status_code=401, content="Authentication Failed")

    # Fetch the user to be updated
    user_to_update = db.query(User).filter(User.id == user_id).first()
    if user_to_update is None:
        return JSONResponse(status_code=404, content="User not found")

    # Update the user's role
    user_to_update.role_id = (
        role_data.role_id
    )  # Assuming role_data contains the new role_id

    # Commit the changes to the database
    db.commit()
    db.refresh(user_to_update)

    return {"message": "User role updated successfully", "user": user_to_update}