def put(station_id): """ Update a station that is registered in the database. :param: code: Code of the origin station (i.e. FRADI) - Default is the station's one name: Code of the destination station (i.e. FRAFJ) - Default is the station's one :return: A JSON file of the station newly updated. """ # station parser station_parser = reqparse.RequestParser() station_parser.add_argument( name='code', type=str, help="The code of the station (i.e. FRAFJ)") station_parser.add_argument(name='name', type=str, help="The name of the station") station_args = station_parser.parse_args() station = dbStation.objects(id=station_id).first() # get default values if station_args['code'] is None: station_args['code'] = station.code if station_args['name'] is None: station_args['name'] = station.name dbStation.objects(id=station_id).update_one( set__code=station_args['code'], set__name=station_args['name']) return json.loads( dbStation.objects(id=station_id).first().to_json()), 200
def delete(station_id): """ Delete from the database a single station registered in the database. :param: request_id: Id of the station to delete :return: 204. """ dbStation.objects(id=station_id).delete() return "", 204
def get(station_id): """ Get a single station registered in the database. :param: station_id: Id of the station to get :return: A JSON file of the station. """ if dbStation.objects(id=station_id).first() is not None: return json.loads( dbStation.objects(id=station_id).first().to_json()), 200 else: return "Station not found at this id {}".format(station_id), 404
def post(): """ Add a NEW station that will be registered in the database. :param: Arguments of the POST request. code: Code of the station (i.e. FRAFJ) - Required name: Name of the station - Required :return: A JSON file of the station newly registered. """ # stations parser stations_parser = reqparse.RequestParser() stations_parser.add_argument( name='code', type=str, required=True, help="The code of the station (i.e. FRAFJ)") stations_parser.add_argument(name='name', type=str, required=True, help="The name of the station") stations_args = stations_parser.parse_args() # checks if the station already exists in the database station_exist = dbStation.objects( code=stations_args['code'], name=stations_args['name']).first() is not None if station_exist: station_id = dbStation.objects( code=stations_args['code'], name=stations_args['name']).first().id return "The station already exists at id {}".format( station_id), 208 else: station = dbStation(code=stations_args['code'], name=stations_args['name']) station.save() return json.loads(station.to_json()), 201