1- from datetime import datetime , timezone
21from uuid import UUID
32
43from fastapi import APIRouter , Depends
5- from fastapi .responses import JSONResponse
6- from sqlmodel import Session , select
4+ from fastapi .exceptions import RequestValidationError
5+ from sqlmodel import Session
76
87from app .api .deps import get_db
9- from app .api .schemas .enums import InputStatus , InputType
8+ from app .api .schemas .enums import InputStatus
109from app .api .schemas .input import InputRecordResponse , TextInputRequest , TextInputResponse
1110from app .core .config import INPUT_POLL_INTERVAL_SECONDS
1211from app .core .errors .base import AppError
13- from app .models import Input
12+ from app .db .repositories import create_input , get_input as repo_get_input
13+ from app .services .input import InputService
1414
1515router = APIRouter (prefix = "/input" , tags = ["input" ])
1616
1717
1818@router .post ("/text" , response_model = TextInputResponse , status_code = 201 )
1919def submit_text_input (body : TextInputRequest , db : Session = Depends (get_db )):
20- narrative = body .narrative
21-
22- if len (narrative ) > 50_000 :
20+ if len (body .narrative ) > 50_000 :
2321 raise AppError (
2422 "Narrative exceeds maximum length of 50,000 characters" ,
2523 status_code = 413 ,
2624 error_code = "NARRATIVE_TOO_LONG" ,
27- detail = {"max_characters" : 50_000 , "received_characters" : len (narrative )},
25+ detail = {"max_characters" : 50_000 , "received_characters" : len (body . narrative )},
2826 )
2927
30- words = narrative .split ()
31- if len (words ) < 10 :
32- return JSONResponse (
33- status_code = 422 ,
34- content = {
35- "error_code" : "VALIDATION_ERROR" ,
36- "message" : "Request validation failed" ,
37- "validation_errors" : [
38- {
39- "field" : "narrative" ,
40- "issue" : "Must contain at least 10 words" ,
41- "value" : narrative ,
42- }
43- ],
44- },
28+ svc = InputService ()
29+ try :
30+ record = svc .build_text_input (
31+ narrative = body .narrative ,
32+ station_id = body .station_id ,
33+ responder_badge = body .responder_badge ,
34+ incident_date_hint = body .incident_date_hint ,
35+ )
36+ except ValueError as exc :
37+ raise RequestValidationError (
38+ errors = [
39+ {
40+ "loc" : ("body" , "narrative" ),
41+ "msg" : str (exc ),
42+ "input" : body .narrative ,
43+ "type" : "value_error" ,
44+ }
45+ ]
4546 )
4647
47- now = datetime .now (timezone .utc )
48- record = Input (
49- input_type = InputType .text ,
50- status = InputStatus .ready ,
51- transcript = narrative ,
52- character_count = len (narrative ),
53- word_count = len (words ),
54- station_id = body .station_id ,
55- responder_badge = body .responder_badge ,
56- incident_date_hint = body .incident_date_hint ,
57- created_at = now ,
58- updated_at = now ,
59- )
60- db .add (record )
61- db .commit ()
62- db .refresh (record )
48+ record = create_input (db , record )
6349
6450 return TextInputResponse (
6551 input_id = record .input_id ,
@@ -73,7 +59,7 @@ def submit_text_input(body: TextInputRequest, db: Session = Depends(get_db)):
7359
7460@router .get ("/{input_id}" , response_model = InputRecordResponse )
7561def get_input (input_id : UUID , db : Session = Depends (get_db )):
76- record = db . exec ( select ( Input ). where ( Input . input_id == input_id )). first ( )
62+ record = repo_get_input ( db , input_id )
7763 if record is None :
7864 raise AppError (
7965 f"Input with ID { input_id } not found" ,
0 commit comments