Creating a movie fetching service in Angular

I'm a full-stack developer from South Africa 🇿🇦. I love writing about JavaScript, HTML and CSS.
Search for a command to run...

I'm a full-stack developer from South Africa 🇿🇦. I love writing about JavaScript, HTML and CSS.
No comments yet. Be the first to comment.
Most of you know me for my consistency, a golden arrow in my blog series. I've written 1000 articles in 1008 days! Almost an article a day, and my honeymoon was the only holiday I ever took. I'm super proud of this achievement; it has been a fantasti...

It's not the first time I'll be talking about community. I think it's an essential aspect of any successful tool. This shows in my previous explorations of Astro, Medusa, and now Vendure as well. All these products thrive in a super open, welcoming, ...

The cool part about Vendure is how easy it is to set up and how abstract each layer is. Basically, we get the following elements: External database Server Worker Admin UI Frontend While this is amazing, it also brings a bit of complexity when it co...

The previous article looked at customizing Vendure on a data and process level. In this article, we'll look at customizing emails, as they are often a big part of a webshop system. We'll be looking at two different layers of customization for customi...

Even though Vendure is a pretty significant project out of the box, in some cases, we might want to go in and modify some elements to work to our specific use case. In this article, I'll take a high-level look at some elements we can customize within...

We have been exploring Angular for a couple of days now, and let's make it awesome by getting some data from an API.
We will be learning how to do API calls and make a custom service to do all these calls from.
The end result will look like this.

To make any request, we will be using the HttpClient module.
We will be loading this module in our app.module.ts so the whole application will be able to leverage it.
Let's start by defining the import at the top of our file.
import { HttpClientModule } from "@angular/common/http";
Then we can place the actual import after the BrowserModule.
@NgModule({
declarations: [
// All declarations
],
imports: [
BrowserModule,
HttpClientModule,
// Other imports
],
providers: [],
bootstrap: [AppComponent]
})
Now we can go ahead and create a service to talk to our API.
Open your favorite terminal and run the following command in your project folder.
Let's first make our movie model so Typescript can leverage this.
ng generate class movie
Open the movie.ts file in your editor and make it as such:
export class Movie {
Poster: string;
Title: string;
Type: string;
Year: string;
imdbID: string;
}
Now let's generate the service
ng generate service movie
This will create a movie.service.ts file.
Open this file in your editor of choice and let's start by defining some basics.
import { HttpClient } from '@angular/common/http';
import { Injectable } from '@angular/core';
import { Movie } from './movie';
export type ApiResponse = {
Response: string;
Search: Movie[];
totalResults: string;
};
@Injectable({
providedIn: 'root'
})
export class MovieService {
apiURL: string = 'http://www.omdbapi.com/?apikey={key}';
constructor(private httpClient: HttpClient) {}
}
We will be using OMDBapi to get some movies. You can still get a free APIKey on their website.
For this example, we will just be placing this URL in the file, in a real-world example, use an env file for the key or a constant file for storing URLs.
Now we can create our method. It's going to be a search method where we will search for a specific movie.
searchMovie(name: string) {
return this.httpClient.get<any>(`${this.apiURL}&s=${name}`);
}
Now we of course want to call our service and show the movies we get.
Open the welcome.component.ts file and load our service in the import.
import { MovieService } from '../movie.service';
Now we need to inject it in the component, but adding it in the constructor.
constructor(private movieService: MovieService) { }
Let's also define our movies variable to be an array of our Movies class.
movies: Movie[];
On the ngOnInit we will be calling our service and asking it for Batman movies.
ngOnInit(): void {
this.movieService.searchMovie('Batman').subscribe(result => {
this.movies = result.Search;
});
}
We call the service and subscribe to the result. In turn, we set the movies variable to be the result.Search value.
Now let's adjust our HTML file to reflect our movies.
<div class="grid grid-cols-5">
<div class="max-w-sm rounded overflow-hidden shadow-lg" *ngFor="let movie of movies">
<img class="w-full" [src]="movie.Poster" [alt]="movie.Title">
<div class="px-6 py-4">
<div class="font-bold text-xl mb-2">{{ movie.Title }}</div>
</div>
</div>
</div>
There we go, we now have our initial movie search app!
Now it's up to you to get this code and make an input field where we can search or a specific movie.
And even click on a poster to see more information about this movie.
Let me know on Twitter what you created with this!
You can find my part of this project on GitHub.
Thank you for reading my blog. Feel free to subscribe to my email newsletter and connect on Facebook or Twitter