transform query string with ngresource for ransack query - angularjs

I am using ransack in my Rails backend to make research.
I use Angularjs for my app and I would like to call my Rails controller with ransack formated query params, something like :
/conversations?q[subject_cont]=toto&q[status_eq]=1
I can't find any solution to transform any object to this query :/
I have tried to play with http interceptors but did not get any luck.
I am using ngresource to call my API.
Any idea?

in your controller, you could have something like:
var params = {
'q[subject_cont]': $scope.subjectCont',
'q[status_eq]': $scope. statusEq'
}
$scope.conversions = Conversation.query(params);

Related

Passing a list from AngularJS to Spring Controller

I am calling a Spring API from AngularJS, where I am trying to pass a list of model object as the param.
data.append('updatedItems', $scope.QuoteList);
$http.post('updateQuotes', data).success(function(response) {
console.log(response);
});
Then in Spring controller, I am trying to receive as below,
#RequestMapping(value = "/updateQuotes", method = RequestMethod.POST)
public ArrayList<ConsumerPrice> updateQuotes(#RequestBody List<ConsumerPrice> updatedItems, ConsumerPrice consumerPrice) {
System.out.println(updatedItems);
return null;
}
At this point, I get an exception as mentioned below. I am not sure if this is the correct approach to pass a list from Angular to Spring controller.
Invalid mime type "application/json;charset=utf-8, multipart/form-data;
I found few guidance in the internet and tried like, providing the consumes header but nothing helped. Any guidance will be great
I tried providing #PathVariable at the spring controller but didnt help
Finally I was able to resolve this. There is nothing in the spring controller but i changed the way we pass the Javascript array from Angular controller.
The first 2 lines down below is the difference on how I add the list to formdata array. That helped me to fetch the values in Spring Controller.
$scope.formData = [];
$scope.formData = $scope.cQuoteList;
$http.post('updateQuotes', $scope.formData).success(function(response) {
$scope.userQuotes();
});

JHipster : when to use $http and when to use $resource

I'm exploring how jhipster manipulates data. I have found $http.get() in getProfileInfo method in ProfileService Service whitch interacting restful api :
function getProfileInfo() {
if (!angular.isDefined(dataPromise)) {
dataPromise = $http.get('api/profile-info').then(function(result) {
if (result.data.activeProfiles) {
var response = {};
response.activeProfiles = result.data.activeProfiles;
response.ribbonEnv = result.data.ribbonEnv;
response.inProduction = result.data.activeProfiles.indexOf("prod") !== -1;
response.swaggerDisabled = result.data.activeProfiles.indexOf("no-swagger") !== -1;
return response;
}
});
}
return dataPromise;
}
and some where i have found $resouce() manipulating GET method. for example in BankAccount factory :
var resourceUrl = 'api/bank-accounts/:id';
I searched for when to use $http and when to use $resource and i found this :
AngularJS $http and $resource
why hipster is not following consistent way of interacting API and manipulating data!!?
so jhipster, when to use $http and when to use $resource in services??
We use $resource when requesting a RESTful endpoint, for example for an entity. $resource provides basic REST operations easily whereas $http is more specific.
For profile we only need to GET /profile-infos so it's useless to use $resource because we'll never need to call POST or DELETE on that URL.
$http will fetch you the entire page or complete set of data from a given URL whereas $resouce uses http but will help you to fetch a specific object or set of data.
$resource is fast and we use it when we need to increase the speed of our transaction.
$http is used when we are concerned with the time.

Ruby 2 intercepting request with Typhoeus

I want to change query params before request is hit using typhoeus. What should be right approach? Do we have interceptors in Ruby as that of Java, should I be using something like before_filter as that in Rails or meta-programming in Ruby?
I just checked there is Typhoeus::Request::Before module to hook request. Can anyone help me out how can I implement it?
I have implemented typhoeus before to change query params just before request gets executed as below. Hope it helps someone.
require 'uri'
Typhoeus.before { |request|
uri = URI.parse(request.base_url)
uri.query = [uri.query, "param1=value1"].compact.join('&')
request.base_url = uri.to_s
request
}

confused between Api Controller and oData Controller to use with angular's ngresource

i have an angular application that calls web api . it was working all fine with web api controller and ngresource until i required to get count of results.
to get just the count of a result i need to use $inlinecount which only seems to be working with oDataController . with oDataControlelr in place my promises dont work.
$scope.totalCount = storeCommandResource.query({ $inlinecount: "allpages", $top: 0 });
i get
Error: [$resource:badcfg] query
please guide.
its look like i can use oDatacontroller with a model builder setup like this
ODataConventionModelBuilder builder = new ODataConventionModelBuilder();
builder.EntitySet<StoreCommand>("StoreCommandsRest");
config.Routes.MapODataServiceRoute(routeName: "OData", routePrefix: "api", model: builder.GetEdmModel());
config.AddODataQueryFilter();
and all i needed to change was query to get
$scope.totalCount = storeCommandResource.get({ $inlinecount: "allpages", $top: 0 });
then i am reading my returned count like
response["odata.count"]

How can I build a get request with url that contains parameters

I am trying to do a get request to recover data from the server using the following url:
url = /api/projects/:projectId/scenarios
How can I do that using $http.get in AdngularJS?.
you might want to take a look at Restangular, I like its notation more than standard angularjs $http or $resource.
Restangular.setBaseURL('/api/');
Restangular.one('projects', projectId).all('scenarios').getList().then(function(data) {
myVariable = data;
});
Restangular

Resources