Http interceptor in AngularJS : adding tokens and loading image to every http request

While creating AngularJS applications,we might need to send multiple  http requests to backend apis to fetch the data that need to be populated within our HTML.In most cases, these backend apis might be protected using token .Also we might need to add a loading image to indicate the user that the data is being fetched. HttpInterceptor helps us to a display the loader image before and hide the loader image after completing the request  and also add token to every request header.This tutorial helps us to create an HttpInterceptor in AngulaJS. We create an  app using the following code

Now we create a factory which will intercept http requests.Inside the interceptor we use a variable httpRequestNumber,which tracks the number of requests.When a new request comes up we increment it httpRequestNumber++ and on response or error we decrement it httpRequestNumber– .We also inject $q and $rootScope.$q is service that helps you run functions asynchronously, and use their return values (or exceptions) when they are done processing.$rootScope is the root scope .every app has single root scope. All other scopes are descendant scopes of the root scope.We also add a token to the request header using the following code.

The interceptor factory will look like this….

Join two tables and get count in mysql:Example with hasMany relations in sequelize

Consider we have two tables ‘users‘ and ‘comments‘.Each user can have multiple comments so that ‘users‘ and ‘comments‘  have ‘hasMany‘ relation. The foreign key is user_id in the comments table. The structure of both tables are as follows Users id    | name |    email 1    userone    user1@gmail.com 2    usertwo    user2@gmail.com Comments id    | comment        | user_id 1      commentone      1 2      commenttwo      1 3      commentthree    2 4      commentfour     2 5      commentfive      2 6      commentfive      2 As you can see, the user with id 1 has two comments and the user with id 2 has  4 comments. To get all users with their respective number of comments, we can use the following query.

The result will be 1 userone user1@gmail.com 2 2 usertwo user2@gmail.com 4 Sequelize Example   To learn more about sequelize, see the following article https://truetocode.com/orm-using-sequelize-orm-with-express-node-js/418/ For our example, we need to create two model files usersModel.js

  commentsModel.js

  hasMany relation Now we need to define hasMany relations from users to comments, in your db.js file….

Angular JS 1.6 – server side pagination with sorting using Angular UI Bootstrap

Angular 1.6 SPA server side pagination of data with sort. To create a SPA which lists user data with pagination  and sorting we need the following CDN links – Angular JS 1.6.9 js CDN. – Angular JS UI bootstrap -Bootstrap components written in pure AngularJS . – Bootstrap CSS file. STEP 1 – Create file index.html and add the relevant CDN links using the following codes.

STEP 2 – CREATING ANGULAR MODULE and DEFINE THE APP create, register and retrieve Angular JS module using the following code.The name of the module is ‘userLog’.

and in the HTML.

STEP 3 – DEFINE THE CONTROLLER AngularJS applications are controlled by controllers. The ng-controller directive defines the application controller.The name of the controller here is ‘listController’.In HTML , add the following code

STEP -4 CREATING SERVICES AND INJECTING THEM In this example we will create two services one for getting the total number of users and other for getting the data each page.The name of the first service is ‘count’ and name of the second service is ‘getloglist’

Services in both cases send HTTP requests to the APIs.For this we inject $http service.The $http service get takes a single argument which…

How to accurately check date range to prevent overlapping:mysql
Requests , Snippets / July 29, 2018

Q:I have a table with events , and each event has an auditorium id,start_date and end_date.In need to insert a new event in to the table without any overlapping on date range in an auditorium   select IF(count(*)>0, true, false) from events where ‘YOUR_START_DATE‘ <= end_date and ‘YOUR_END_DATE‘ >= start_date; OR select IF(count(*)>0, true, false) from events where ‘YOUR_START_DATE‘  BETWEEN start_date AND end_date or ‘YOUR_END_DATE‘ BETWEEN start_date and end_date and end_date or (‘YOUR_START_DATE‘ < start_date and ‘YOUR_END_DATE‘ > end_date); If true do not proceed with insert if false continue

 

Creating a Cron job in Laravel 5.2 :Task Scheduling
Beginner , PHP , Snippets / July 23, 2018

To create a cron job in Laravel 5.2 ,move to the app/console/ folder.The console folder contains all the relevant configuration files and folders for you to create a custom artisan command in Laravel 5.6 .  After creating the custom artisan command ,we can schedule the command to be run in specific intervals Step 1 Run

  to create new custom artisan command,This will generate a class called app/Console/Commands/CronJob.php.The newly generated file will have the following structure.You have to fill in name and description variables in the file.You can use the DB and MODEL files from CronJob.php. The file extends the Command Class   app/Console/Commands/CronJob.php

Here we select a set of users based on some custom criteria from the users table and do something to them periodically with the cron job.The action for the should be written in the CronJob.php – handle() function.The class defines our command.To run this need to use the following php artisan cronjob Step 2 Scheduling the job To schedule the when to run the command we have created open the kernel.php file in the app/console/ folder app/console/kernel.php

The kernel class extends ConsoleKernel .To add the commands

and to schedule the command

  This runs…

Sending email with SendGrid and Node.js – Using HTML templates:Step by step tutorial
Javascript and Node.js , Snippets / July 15, 2018

SendGrid is a communication platform that helps you manage communications with your clients via e-mail.SendGrid helps you manage email sending,design templates using code editor/design editor,run marketing campaign etc.To use send grid with your Node.js application,you need to integrate @sendgrid/mail package to your application.Follow the steps below to integrate and configure mail sending in Node.js with SendGrid   STEP 1 Create a SendGrid account using your email,provide the necessary information and confirm your account by clicking the confirmation mail from sendgrid   STEP 2 Sign in to your SendGrid account and go to dashboard.In the left side settings you will find an option called Api keys. Click on Api keys and generate a new api key for your sendgrid account.   STEP 3 – Integrating #sendgrid/mail To install sendgrid/mail for node run the following in the terminal.

In your node application,use the following code to import @sendgrid/mail

  STEP – 3 Creating a template in sendgrid To create aSendGrid template, go to the template section in the left side of your dashboard.Create a template  and then click the action button. From the action button choose the option New Version which will give you the following screen with design editor and code editor.Choose…

How to display user – display name,avatar and logout button in WordPress : Example
Beginner , PHP , Snippets , Wordpress / July 8, 2018

To display user displayname , avatar and provide a logout button for a user in your wordpress site,we can use the following  five worpress functions. is_user_logged_in () –  Check whether used in. wp_get_current_user(id/email,null)  –  Get current logged in user get_avatar_url(userid) – Get avatar url for current user using userid wp_logout_url(url) – Logout and redirect URL home_url() – Get your home URL  

  Check below links to see documentation in full https://developer.wordpress.org/reference/functions/is_user_logged_in/ https://developer.wordpress.org/reference/functions/wp_get_current_user/ https://developer.wordpress.org/reference/functions/get_user_by/ https://developer.wordpress.org/reference/functions/wp_logout_url/        

Share this page in social media platforms