11import logging
2- from enum import Enum
32from typing import List , Optional
43
54from analyticq .repository .scan_repository import (
6- AnalyticQSASTIssueModel , AnalyticQSASTScanResultModel ,
7- AnalyticQScanResultRepository )
8- from analyticq .schemas .scan_dto import ScanCreateRequest
5+ AnalyticQSASTScanResultModel , AnalyticQScanResultRepository )
6+ from analyticq . schemas . error_dto import ErrorResponse
7+ from analyticq .schemas .scan_dto import PaginatedScanResponse
98from analyticq .service import AnalyticQScanService
109from fastapi import APIRouter , Depends , Query , status
11- from pydantic import BaseModel
1210
1311logger = logging .getLogger (__name__ )
1412
1513scan_router = APIRouter (prefix = "/scans" , tags = ["Scan Management" ])
1614
1715
18- class ErrorResponse (BaseModel ):
19- detail : str
20-
21-
22- class Severity (str , Enum ):
23- CRITICAL = "CRITICAL"
24- HIGH = "HIGH"
25- MEDIUM = "MEDIUM"
26- LOW = "LOW"
27- UNKNOWN = "UNKNOWN"
28-
29-
30- class Confidence (str , Enum ):
31- CRITICAL = "CRITICAL"
32- HIGH = "HIGH"
33- MEDIUM = "MEDIUM"
34- LOW = "LOW"
35- UNKNOWN = "UNKNOWN"
36-
37-
3816def get_scan_service (
3917 repo : AnalyticQScanResultRepository = Depends (AnalyticQScanResultRepository )
4018) -> AnalyticQScanService :
4119 """Dependency provider for scan service."""
4220 return AnalyticQScanService (repo )
4321
4422
45- @scan_router .post (
23+ @scan_router .get (
4624 "/" ,
47- response_model = AnalyticQSASTScanResultModel ,
48- status_code = status . HTTP_201_CREATED ,
25+ summary = "Get all scans paginated" ,
26+ response_model = PaginatedScanResponse ,
4927 responses = {
50- status .HTTP_201_CREATED : {"description" : "Scan created successfully" },
51- status .HTTP_400_BAD_REQUEST : {"model" : ErrorResponse },
52- status .HTTP_500_INTERNAL_SERVER_ERROR : {"model" : ErrorResponse },
28+ status .HTTP_500_INTERNAL_SERVER_ERROR : {"model" : ErrorResponse , "description" : "Internal server error" },
5329 },
5430)
55- async def create_scan (
56- scan_data : ScanCreateRequest ,
31+ async def get_all_scans_paginated (
32+ page : int = Query (1 , ge = 1 , description = "Page number" ),
33+ page_size : Optional [int ] = Query (10 , ge = 1 , le = 100 , description = "Number of items per page, set to 0 for all items" ),
5734 service : AnalyticQScanService = Depends (get_scan_service )
5835):
59- """
60- Create a new scan record in the system.
61- """
62- return await service .create_scan (scan_data )
36+ if page_size == 0 :
37+ scans = await service .get_all_scan ()
38+ return {
39+ "items" : scans ,
40+ "total" : len (scans ),
41+ "page" : 1 ,
42+ "page_size" : len (scans ),
43+ "has_more" : False
44+ }
45+ else :
46+ # Get scans with pagination
47+ scans , total = await service .get_all_scans_paginated (page , page_size )
48+ return {
49+ "items" : scans ,
50+ "total" : total ,
51+ "page" : page ,
52+ "page_size" : page_size ,
53+ "has_more" : (page * page_size ) < total
54+ }
6355
6456
6557@scan_router .get (
66- "/{scan_id}/issues" ,
67- response_model = List [AnalyticQSASTIssueModel ],
58+ "/{scan_id}" ,
59+ response_model = AnalyticQSASTScanResultModel ,
60+ summary = "Get a scans by ID" ,
6861 responses = {
69- status .HTTP_200_OK : {"description" : "List of filtered issues" },
7062 status .HTTP_404_NOT_FOUND : {"model" : ErrorResponse },
7163 status .HTTP_500_INTERNAL_SERVER_ERROR : {"model" : ErrorResponse },
7264 },
7365)
74- async def get_filtered_scan_issues (
75- scan_id : str ,
76- severity : Optional [Severity ] = Query (None , description = "Filter issues by severity" ),
77- confidence : Optional [Confidence ] = Query (None , description = "Filter issues by confidence" ),
66+ async def get_scan_by_id (
67+ scan_id : int ,
7868 service : AnalyticQScanService = Depends (get_scan_service )
7969):
8070 """
8171 Retrieve a list of issues for a specific scan, optionally filtered by severity and confidence.
8272 """
83- return await service .filter_scan_issues (scan_id , severity , confidence )
73+ return await service .get_scan_by_id (scan_id )
8474
8575
8676@scan_router .get (
@@ -98,25 +88,7 @@ async def get_scans_by_tool_name(
9888 """
9989 Retrieve a list of scan results for a specific SAST tool.
10090 """
101- return await service .get_scans_by_tool_name (tool_name )
102-
103-
104- @scan_router .get (
105- "/tool/{tool_name}/issues" ,
106- response_model = List [AnalyticQSASTIssueModel ],
107- responses = {
108- status .HTTP_200_OK : {"description" : "List of issues for the specified tool" },
109- status .HTTP_500_INTERNAL_SERVER_ERROR : {"model" : ErrorResponse },
110- },
111- )
112- async def get_scan_issues_by_tool_name (
113- tool_name : str ,
114- service : AnalyticQScanService = Depends (get_scan_service )
115- ):
116- """
117- Retrieve all issues found by a specific security analysis tool.
118- """
119- return await service .get_issues_by_tool_name (tool_name )
91+ return await service .get_scans_by_tool_name (tool_name .lower ())
12092
12193
12294@scan_router .delete (
0 commit comments