Web API - Controller Design - javascript

I have a Web API project that currently has a ServicesController. I get the list of services, for a server, by making this call:
$http.get(rootWebApiUrl + '/api/services/' + server)
The ServicesController has this signature:
public IEnumerable<FirstSolarService> Get(string id)
Now I want to make two more calls.
Get the Windows folders within the path for the service itself.
After the user selects a folder in #1, show the DLLs in that folder.
I have a choice to make as far as my Web API controllers go. Do I put this all in the ServicesController, or should I create separate controllers for each type of object I'm returning? If the latter, then I would create these two controllers:
FoldersController
FilesController
But what's awkward about that (maybe) is that I'd call each of those by passing in something other than the ID of the folder or file. To get folders, I'd pass in the service name. To get files, I'd pass in the path of the service. Is that the way it is supposed to be done? I'd just like to do this the correct way.

I don't think you should split it up into multiple controllers, unless they each are going to each have considerable functionality, or if Folders and Files are real domain objects/repository entities.
As you may know, you are not limited to the Get/Post/Put/Delete convention for method names in WebAPIs. You can specify the action name if you want. For example, if you wanted a Folders method, you could add one:
[HttpGet]
public ReturnType Folders(string serviceName)
{
}
Your API URL for the above would be '/api/services/folders/' + server.
You don't need to create a custom route -- the default one incorporates an {action} component. Philosophically, I would ask yourself: are services composed of folders? Can folders exists without services? If folders depend on services, then I think it's ok to include that functionality in the services controller. If they don't, then split up the controllers based on functionality.

Related

How to connect Laravel 5.4 backend with Angular 5 Frontend

I am working on a project where I need to develop frontend in angular 5 and backend business logic in laravel 5.4 with mySql Database. I am a newbie to this technology and dont know how to develop data flow connection between these two framworks.
It will be helpful if you tell me the easiest and most generic way to solve this out.
As with any other technology, you'll need to work with an API.
In your Laravel project:
Your Laravel project should have models, controllers and a file for the route paths for each method in your controllers, right? If you don't know where the routes are, there's a dedicated folder called "routes" at the root of your project.
Inside this folder, you'll have two important files: api.php and web.php. These two work exactly the same way, but the api.php will automatically add '/api' to the beginning of your route.
A route looks like a link, such as: "yourdomain.com/api/person/name/jondoe". Every route has to be accessed through an HTTP method, usually GET, POST, PUT or DELETE.
Example: let's say you need your user to see a list of people. You'll probably have a Person model and a PersonController containing some function called displayAllPeople(), and this function will select the people from your database. In your api.php file you'll add something like:
Route::get('/people', 'PersonController#displayAllPeople');
You can test that in your browser. Try to access http://yourdomain.com/api/people. You should see a JSON output of your function's return data. In this example, a list of people. Now all we have to do is make your Angular project call this API route.
Source: https://laravel.com/docs/5.7/controllers
In your Angular project:
Remember that Person model you created on your backend? Angular will access that model too, so you'll have to create a Person.ts model here. While that's not required to work, it's a good practice to create this model with the same attributes as the backend model.
Now you'll need a service. The service is a file that will access that Laravel route for you. If you're using Angular CLI, just run the command:
ng generate service person
That will create a person.service.ts file for you. Inside this file, you'll write all the functions to select people, update, save, delete people, and so on. Your function will look something like this:
findAll(): Observable<Person[]> {
return this.http.get<Person[]>('yourdomain.com/api/people');
}
Whenever you call the findAll() function, you'll access the Laravel route, which will return the People from your database. You can do that with any operation you want. Just make sure you follow the HTTP standards.
That also works if you want to send some data from your frontend to the backend, like if you want to save some new Person in your database. You'll need a POST call sending your new Person model which will be received by your Laravel's Controller function, and then persist it in the database.
Source: https://angular.io/tutorial/toh-pt4

How to pass data from one Javascript controller file to another?

So, the situation I have gotten myself seems to be a little complicated but I'm hoping there is a simple solution to it.
I have two Javascript controller files, one called rootPageClient.js and another called findPollClient.js. There is also a file called ajax-functions.js which basically contains two functions to route the ajax calls to the right route. The routes and the get and post method functions are contained in the index.js file. The file structure is shown below
Root
Common
ajax-functions.js
Controllers
findPollClient.js
rootPageClient.js
routes
index.js
I have a specific piece of data in the rootPageClient.js file that I need passed through to the findPollClient.js file. All calls are routed through the ajax-functions.js file to the requested route in index.js.
I have figured out that I need to call the same route in the index.js file from both controller files in the order that I want to pass the files from and to. But, I'm not quite sure how to do this. Is there a simple way to do this?
Create a service and save the data inside the service. Have the service injected in both the controllers. Whenever you change some data in service in one of the controllers, it will retain when you look it up in other controller. This is because services and factories are singletons, so they are the right place to store application state and access it anywhere you want.

Express using a patternless dynamic base URL to render different pages

I am interested in having a route that could respond to a request with a file eg express's res.sendFile() based on the URL's base parameter i.e. www.example.com/:parameter. The problem is that the URLs are completely user generated and completely dynamic. Similar to that of Github, www.github.com/username could render a user's profile or www.github.com/project could render a project—but they are both strings that don't have a pattern and the machine has no way of knowing that www.github.com/username refers to a user view unless it does some type of check.
app.all('/*', function(req, res) {
res.sendfile('index.html', { root: config.server.distFolder });
Github responds to server requests with different views based on the parameter, even though they have no predefined pattern.
i.e. it would be easy to know that www.github.com/user/username is a user route and the server can respond with a user view (the pattern to match would be www.github.com/user/:user But when the string is completely dynamic it becomes more difficult. (How does express know if it should respond with a user or a project view with a url like example.com/cococola)?
I believe you would somehow be able to check the URL parameter, understand that it refers to (in this case) either a project or a user's page, and then render that view. How do you do this without making a synchronous call and forcing the user to wait for the server to check what view-type the parameter string refers to before responding?
I'm using angular, are there other ways to respond to server requests with different pages based on URL's that have no predeterminable matching pattern? The reason being that, I would like to separate my site into many different apps. www.example.com/username might require a user's profile SPA, whereas www.example.com/projectname might require a user's project SPA—but, as these are user defined, there is no way to respond based on the parameter's matching pattern. Would like to keep the URL as minimal as possible :-)
Any help is appreciated. Thanks :-)
Just use a database / some kind of key value store where the key is the url parameter and the value is the view type. Then you just need to do a simple lookup.

How to pass data to one module to another module in angularJS

In my project I have two ng-app's one is for login page and another for home page.
Once login is successful for the user, in ajax success callback I got the some response related to that user and in that callback I am used the window.location="home.html".
As of now I used session storage feature to pass the data to home page.
The best way to do it is by creating a service. The service can be injected into your controllers and other services as necessary.
Create a service and use getter and setters.When you get a response in your first module set the object using setter and get the data using getter in another module.
The best way to communicate between two controllers or say two modules is using services.
According To angularjs examples only one module stands for one project though outof the box you can you can use more than one
but To Achieve this .This Is how i Normally model myproject
**MyProjectModule** (only **one** module)
---> loginController.js (initial page)
(If LOGIN IS Authorised Then Redirect To Desktop->desktopController.js)
---> desktopController.js
---> customerController.js
---> salesController.js

Web Service call in JavaScript in ASP.NET : Is there a security issue with my implementation? Is there a better way?

I wrote a very simplistic asmx web service. I setup a script manager in the aspx page and call the web service via JavaScript. Everything is working fine:
function CallMyWebService(sender,args){
MyClass.WebService1.HelloWorld(args._value.toString(),1,OnSuccess,OnFail,sender._id.toString();
}
function OnSuccess(returnStringFromWebService,userContext){
alert(returnStringFromWebService);
}
function OnFail(returnString){
alert(returnString);
}
...
public class WebService1: System.Web.Services.WebService
{
[WebMethod()]
public string HelloWorld(string prefixText, int count)
{
return prefixText;
}
}
The problem is that the class name, asmx file name and method name are all visible via "view source". I have considered a RESTful web service but it seems if you put a .svc file in your project (instead of the .asmx file) then the calls are similar and you can again see in the project the class name and such.
Is there a security issue here? Is there some way I can call a web service via JavaScript in ASP.NET that wouldn't expose so much? If RESTful is the answer? I would like the web service (asmx or svc) in the same solution as the aspx files.
Thank you in advance.
Don't worry to much about exposing your API. Sniffers will be able to sniff your calls and call it themselves. Just make sure that if they call it themselves they can only do what their user/session allows them to do.
Don't worry about exposing the URLs of your services. That's fine.
If you're concerned about exposing some sensitive data to people who aren't authenticated, you can always change your decorator from:
[WebMethod]
to:
[WebMethod(true)]
This will allow you to reference the Session variable inside your WebMethod calls. Then, you can make sure that the request is coming from someone who's logged in successfully and has the access level required to perform whatever action takes place inside that particular WebMethod.
At the very least, you could grab the current user's username and record it somewhere (a database, logfile, event log, etc.) so that you'll know who's trying to access what. This is useful for debugging problems in a WebMethod, especially if you have users with different permission levels operating on the same page.

Categories

Resources