,

Adding Cars Model, Make, and Series to AngularJS Admin Panel using API Integration

Posted by


In this tutorial, we will demonstrate how to create an AngularJS admin panel that allows users to add cars model, make, and series using the API Magic service. API Magic is a platform that allows developers to easily create APIs for their web applications.

To get started, make sure you have Node.js and AngularJS installed on your machine. If not, you can download and install them from their respective official websites.

Step 1: Set up your AngularJS project
First, create a new AngularJS project using the Angular CLI. Open your terminal and run the following command:

ng new my-admin-panel

Navigate to your project directory by running:

cd my-admin-panel

Step 2: Install API Magic SDK
Next, install the API Magic SDK by running the following command in your terminal:

npm install api-magic

Step 3: Create a new service for managing cars
Create a new service in your AngularJS project to manage the cars data. Run the following command to generate a new service:

ng generate service car

Open the newly created service file (car.service.ts) and import the API Magic SDK:

import { Injectable } from '@angular/core';
import { APIMagic } from 'api-magic';

@Injectable({
  providedIn: 'root'
})
export class CarService {
  constructor(private apiMagic: APIMagic) {}
}

Step 4: Add methods for managing cars
Create methods in the car service for adding cars model, make, and series. Add the following code to the service:

addCarModel(model: string) {
  return this.apiMagic.post('cars/model', { model });
}

addCarMake(make: string) {
  return this.apiMagic.post('cars/make', { make });
}

addCarSeries(series: string) {
  return this.apiMagic.post('cars/series', { series });
}

Step 5: Create forms for adding cars
Create forms in your AngularJS admin panel for users to add cars model, make, and series. Add the following code to your HTML template:

<form (ngSubmit)="onSubmitModel()">
  <input type="text" name="model" ngModel placeholder="Enter car model">
  <button type="submit">Add Model</button>
</form>

<form (ngSubmit)="onSubmitMake()">
  <input type="text" name="make" ngModel placeholder="Enter car make">
  <button type="submit">Add Make</button>
</form>

<form (ngSubmit)="onSubmitSeries()">
  <input type="text" name="series" ngModel placeholder="Enter car series">
  <button type="submit">Add Series</button>
</form>

Step 6: Implement the form submission logic
Add the following code to your component file (e.g., car.component.ts) to handle the form submission logic:

onSubmitModel() {
  const model = this.modelForm.value.model;
  this.carService.addCarModel(model).subscribe(
    response => {
      console.log('Car model added successfully');
    },
    error => {
      console.error('An error occurred while adding car model');
    }
  );
}

onSubmitMake() {
  const make = this.makeForm.value.make;
  this.carService.addCarMake(make).subscribe(
    response => {
      console.log('Car make added successfully');
    },
    error => {
      console.error('An error occurred while adding car make');
    }
  );
}

onSubmitSeries() {
  const series = this.seriesForm.value.series;
  this.carService.addCarSeries(series).subscribe(
    response => {
      console.log('Car series added successfully');
    },
    error => {
      console.error('An error occurred while adding car series');
    }
  );
}

Step 7: Display success messages to users
After adding a car model, make, or series successfully, display success messages to users. Modify your HTML template to display success messages:

<div *ngIf="modelSuccess">Car model added successfully</div>
<div *ngIf="makeSuccess">Car make added successfully</div>
<div *ngIf="seriesSuccess">Car series added successfully</div>

Modify your component file to set the success flags when a car model, make, or series is added successfully:

modelSuccess: boolean = false;
makeSuccess: boolean = false;
seriesSuccess: boolean = false;

onSubmitModel() {
  const model = this.modelForm.value.model;
  this.carService.addCarModel(model).subscribe(
    response => {
      this.modelSuccess = true;
      console.log('Car model added successfully');
    },
    error => {
      console.error('An error occurred while adding car model');
    }
  );
}

Step 8: Test your admin panel
Run your AngularJS project by running the following command in your terminal:

ng serve

Visit http://localhost:4200 in your web browser to access your AngularJS admin panel. Test adding cars model, make, and series using the forms you created.

Congratulations! You have successfully created an AngularJS admin panel that allows users to add cars model, make, and series using the API Magic service. Feel free to explore more features and functionalities of API Magic to enhance your web application even further.