async def _get_ticket_or_404(ticket_id: str): _id = validate_object_id(ticket_id) ticket = await database.ticketDB.find_one({'_id': _id}) if ticket: return convert_id(ticket) else: raise HTTPException(status_code=404, detail='Ticket not found')
async def update_ticket(ticket_id: str, ticket_data: TicketPutModel): ticket_data = ticket_data.dict() ticket_data = {k: v for k, v in ticket_data.items() if v is not None} ticket_op = await database.ticketDB.update_one( {'_id': validate_object_id(ticket_id)}, {'$set': ticket_data}) if ticket_op.modified_count: return await _get_ticket_or_404(ticket_id) else: raise HTTPException(status_code=304)
async def create_ticket(ticket_id: str): ticket = await _get_ticket_or_404(ticket_id) if ticket['started_at']: raise HTTPException(status_code=304, detail='Ticket already started.') ticket_data = {'started_at': datetime.now()} ticket_op = await database.ticketDB.update_one( {'_id': validate_object_id(ticket_id)}, {'$set': ticket_data}) if ticket_op.modified_count: return await _get_ticket_or_404(ticket_id) else: raise HTTPException(status_code=304)
async def delete_ticket(ticket_id: str): await _get_ticket_or_404(ticket_id) ticket_op = await database.ticketDB.delete_one( {'_id': validate_object_id(ticket_id)}) if ticket_op.deleted_count: return {'status': f'deleted count: {ticket_op.deleted_count}'}