File size: 2,940 Bytes
9e798a1
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
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
from fastapi import APIRouter, HTTPException, status, Depends
from typing import List
from .Model import Plan
from .Schema import (
    CreatePlanRequest,
    UpdatePlanRequest,
    PlanResponse,
    PlanListResponse,
    BaseResponse,
)

plan_router = APIRouter(tags=["Plans"])


@plan_router.post("/plan/create", response_model=BaseResponse)
async def create_plan(request: CreatePlanRequest):
    # Check if a plan with the same name already exists
    existing_plan = await Plan.get_or_none(name=request.name)
    if existing_plan:
        raise HTTPException(
            status_code=status.HTTP_400_BAD_REQUEST,
            detail="A plan with this name already exists",
        )

    # Create a new plan
    plan = await Plan.create(
        name=request.name,
        amount=request.amount,
        duration=request.duration,
        download_speed=request.download_speed,
        upload_speed=request.upload_speed,
    )
    await plan.save()

    return BaseResponse(
        code=200, message="Plan created successfully", payload={"plan_id": str(plan.id)}
    )


@plan_router.put("/plan/{plan_id}/update", response_model=BaseResponse)
async def update_plan(plan_id: str, request: UpdatePlanRequest):
    # Find the plan by ID
    plan = await Plan.get_or_none(id=plan_id)
    if not plan:
        raise HTTPException(
            status_code=status.HTTP_404_NOT_FOUND, detail="Plan not found"
        )

    # Update the plan fields if provided
    if request.name is not None:
        plan.name = request.name
    if request.amount is not None:
        plan.amount = request.amount
    if request.duration is not None:
        plan.duration = request.duration
    if request.download_speed is not None:
        plan.download_speed = request.download_speed
    if request.upload_speed is not None:
        plan.upload_speed = request.upload_speed

    await plan.save()

    return BaseResponse(
        code=200, message="Plan updated successfully", payload={"plan_id": str(plan.id)}
    )


@plan_router.delete("/plan/{plan_id}/delete", response_model=BaseResponse)
async def delete_plan(plan_id: str):
    # Find the plan by ID
    plan = await Plan.get_or_none(id=plan_id)
    if not plan:
        raise HTTPException(
            status_code=status.HTTP_404_NOT_FOUND, detail="Plan not found"
        )

    await plan.delete()

    return BaseResponse(code=200, message="Plan deleted successfully")


@plan_router.get("/plans", response_model=PlanListResponse)
async def list_plans():
    plans = await Plan.all()
    total_count = await Plan.all().count()

    result = [
        PlanResponse(
            id=str(plan.id),
            name=plan.name,
            amount=plan.amount,
            duration=plan.duration,
            download_speed=plan.download_speed,
            upload_speed=plan.upload_speed,
        )
        for plan in plans
    ]

    return PlanListResponse(plans=result, total_count=total_count)