Repository: fossasia/open-event-server

Issue:
Refactor duplicate endpoints.
**I'm submitting a ...**  (check one with "x")
- [x] bug report
- [ ] feature request
- [ ] support request => Please do not submit support requests here, instead ask your query in out Gitter channel at https://gitter.im/fossasia/open-event-orga-server

**Current behavior:**
<!-- Describe how the bug manifests. -->
Right now there are some duplicate endpoints that have arisen. Need to check and refactor them to avoid same endpoints refering to different views.



Discussion:
@SaptakS @shubham-padia Lets do this after most of the basic APIs are done?
@magdalenesuo I agree with you because there might come up more refactoring issues. So better we do it after all APIs are done in a basic format.
Along with this, we also need to update the related endpoints. Generally they are using `id` as keys in endpoints while they should be in format `model_id` like `event_id`, `session_id`, etc. This needs to be fixed so that proper utilization of these can be done in different methods.

Resolving patch:
diff --git a/app/api/__init__.py b/app/api/__init__.py
--- a/app/api/__init__.py
+++ b/app/api/__init__.py
@@ -137,8 +137,8 @@
 api.route(TrackRelationship, 'track_event', '/tracks/<int:id>/relationships/event')
 
 # speakers_calls
-api.route(SpeakersCallList, 'speakers_call_list', '/events/<int:event_id>/speakers-calls',
-          '/events/<event_identifier>/speakers-calls')
+api.route(SpeakersCallList, 'speakers_call_list', '/events/<int:event_id>/speakers-call',
+          '/events/<event_identifier>/speakers-call')
 api.route(SpeakersCallDetail, 'speakers_call_detail', '/speakers-calls/<int:id>',
           '/events/<int:event_id>/speakers-call', '/events/<event_identifier>/speakers-call')
 api.route(SpeakersCallRelationship, 'speakers_call_event',
diff --git a/app/api/speakers_calls.py b/app/api/speakers_calls.py
--- a/app/api/speakers_calls.py
+++ b/app/api/speakers_calls.py
@@ -49,24 +49,25 @@ def validate_date(self, data):
 
 class SpeakersCallList(ResourceList):
     """
-    List and create Speakers Call
+    create Speakers Call
     """
-
-    def query(self, view_kwargs):
-        query_ = self.session.query(SpeakersCall)
-        if view_kwargs.get('event_id'):
-            query_ = query_.join(Event).filter(Event.id == view_kwargs['event_id'])
-        elif view_kwargs.get('event_identifier'):
-            query_ = query_.join(Event).filter(Event.identifier == view_kwargs['event_identifier'])
-        return query_
-
     def before_create_object(self, data, view_kwargs):
         if view_kwargs.get('event_id'):
-            event = self.session.query(Event).filter_by(id=view_kwargs['event_id']).one()
-            data['event_id'] = event.id
+            try:
+                event = self.session.query(Event).filter_by(id=view_kwargs['event_id']).one()
+            except NoResultFound:
+                raise ObjectNotFound({'parameter': 'event_id'},
+                                     "Event: {} not found".format(view_kwargs['event_id']))
+            else:
+                data['event_id'] = event.id
         elif view_kwargs.get('event_identifier'):
-            event = self.session.query(Event).filter_by(identifier=view_kwargs['event_identifier']).one()
-            data['event_id'] = event.id
+            try:
+                event = self.session.query(Event).filter_by(identifier=view_kwargs['event_identifier']).one()
+            except NoResultFound:
+                raise ObjectNotFound({'parameter': 'event_identifier'},
+                                     "Event: {} not found".format(view_kwargs['event_identifier']))
+            else:
+                data['event_id'] = event.id
 
     view_kwargs = True
     decorators = (jwt_required, )
@@ -75,7 +76,6 @@ def before_create_object(self, data, view_kwargs):
     data_layer = {'session': db.session,
                   'model': SpeakersCall,
                   'methods': {
-                      'query': query,
                       'before_create_object': before_create_object
                   }}
 


Test patch: