Skip to content

Commit

Permalink
Merge pull request #130 from ral-facilities/remove-userinvestigations…
Browse files Browse the repository at this point in the history
…-query-#126

Remove unused users investigation endpoints
  • Loading branch information
MRichards99 authored Jun 11, 2020
2 parents 05ad009 + 215c0dc commit 948b4dd
Show file tree
Hide file tree
Showing 4 changed files with 2,446 additions and 2,653 deletions.
46 changes: 0 additions & 46 deletions common/database_helpers.py
Original file line number Diff line number Diff line change
Expand Up @@ -508,52 +508,6 @@ def patch_entities(table, json_list):
return results


class UserInvestigationsQuery(ReadQuery):
"""
The query class used for the /users/<:id>/investigations endpoint
"""

def __init__(self, user_id):
super().__init__(INVESTIGATION)
self.base_query = self.base_query.join(INVESTIGATIONUSER).filter(INVESTIGATIONUSER.USER_ID == user_id)


def get_investigations_for_user(user_id, filters):
"""
Given a user id and a list of filters, return a filtered list of all investigations that belong to that user
:param user_id: The id of the user
:param filters: The list of filters
:return: A list of dictionary representations of the investigation entities
"""
with UserInvestigationsQuery(user_id) as query:
filter_handler = FilterOrderHandler()
return get_filtered_read_query_results(filter_handler, filters, query)


class UserInvestigationsCountQuery(CountQuery):
"""
The query class used for /users/<:id>/investigations/count
"""

def __init__(self, user_id):
super().__init__(INVESTIGATION)
self.base_query = self.base_query.join(INVESTIGATIONUSER).filter(INVESTIGATIONUSER.USER_ID == user_id)


def get_investigations_for_user_count(user_id, filters):
"""
Given a user id and a list of filters, return the count of all investigations that belong to that user
:param user_id: The id of the user
:param filters: The list of filters
:return: The count
"""
with UserInvestigationsCountQuery(user_id) as count_query:
filter_handler = FilterOrderHandler()
filter_handler.add_filters(filters)
filter_handler.apply_filters(count_query)
return count_query.get_count()


class InstrumentFacilityCyclesQuery(ReadQuery):
def __init__(self, instrument_id):
super().__init__(FACILITYCYCLE)
Expand Down
10 changes: 2 additions & 8 deletions src/main.py
Original file line number Diff line number Diff line change
Expand Up @@ -9,9 +9,8 @@
get_find_one_endpoint
from src.resources.entities.entity_map import endpoints
from src.resources.non_entities.sessions_endpoints import *
from src.resources.table_endpoints.table_endpoints import UsersInvestigations, UsersInvestigationsCount, \
InstrumentsFacilityCycles, InstrumentsFacilityCyclesCount, InstrumentsFacilityCyclesInvestigations, \
InstrumentsFacilityCyclesInvestigationsCount
from src.resources.table_endpoints.table_endpoints import InstrumentsFacilityCycles, InstrumentsFacilityCyclesCount, \
InstrumentsFacilityCyclesInvestigations, InstrumentsFacilityCyclesInvestigationsCount

from apispec import APISpec
from pathlib import Path
Expand Down Expand Up @@ -71,11 +70,6 @@
spec.path(resource=Sessions, api=api)

# Table specific endpoints
api.add_resource(UsersInvestigations, "/users/<int:id>/investigations")
spec.path(resource=UsersInvestigations, api=api)
api.add_resource(UsersInvestigationsCount,
"/users/<int:id>/investigations/count")
spec.path(resource=UsersInvestigationsCount, api=api)
api.add_resource(InstrumentsFacilityCycles,
"/instruments/<int:id>/facilitycycles")
spec.path(resource=InstrumentsFacilityCycles, api=api)
Expand Down
85 changes: 1 addition & 84 deletions src/resources/table_endpoints/table_endpoints.py
Original file line number Diff line number Diff line change
@@ -1,93 +1,10 @@
from flask_restful import Resource

from common.database_helpers import get_investigations_for_user, get_investigations_for_user_count, \
get_facility_cycles_for_instrument, get_facility_cycles_for_instrument_count, \
from common.database_helpers import get_facility_cycles_for_instrument, get_facility_cycles_for_instrument_count, \
get_investigations_for_instrument_in_facility_cycle, get_investigations_for_instrument_in_facility_cycle_count
from common.helpers import requires_session_id, queries_records, get_filters_from_query_string


class UsersInvestigations(Resource):
@requires_session_id
@queries_records
def get(self, id):
"""
---
summary: Get a user's Investigations
description: Retrieve the investigations that a user of a given ID is an InvestigationUser on, subject to filters.
tags:
- Investigations
parameters:
- in: path
required: true
name: ID
description: The id of the user to retrieve the investigations of
schema:
type: integer
- WHERE_FILTER
- ORDER_FILTER
- LIMIT_FILTER
- SKIP_FILTER
- DISTINCT_FILTER
- INCLUDE_FILTER
responses:
200:
description: Success - returns a list of the user's investigations that satisfy the filters
content:
application/json:
schema:
type: array
items:
$ref: '#/components/schemas/INVESTIGATION'
400:
description: Bad request - Something was wrong with the request
401:
description: Unauthorized - No session ID was found in the HTTP Authorization header
403:
description: Forbidden - The session ID provided is invalid
404:
description: No such record - Unable to find a record in the database
"""
return get_investigations_for_user(id, get_filters_from_query_string()), 200


class UsersInvestigationsCount(Resource):
@requires_session_id
@queries_records
def get(self, id):
"""
---
summary: Count a user's Investigations
description: Return the count of the Investigations that belong to a given user that would be retrieved given the filters provided
tags:
- Investigations
parameters:
- in: path
required: true
name: ID
description: The id of the user to count the investigations of
schema:
type: integer
- WHERE_FILTER
- DISTINCT_FILTER
responses:
200:
description: Success - The count of the user's investigations that satisfy the filters
content:
application/json:
schema:
type: integer
400:
description: Bad request - Something was wrong with the request
401:
description: Unauthorized - No session ID was found in the HTTP Authorization header
403:
description: Forbidden - The session ID provided is invalid
404:
description: No such record - Unable to find a record in the database
"""
return get_investigations_for_user_count(id, get_filters_from_query_string()), 200


class InstrumentsFacilityCycles(Resource):
@requires_session_id
@queries_records
Expand Down
Loading

0 comments on commit 948b4dd

Please sign in to comment.