DEV Community

GaneshMani
GaneshMani

Posted on • Originally published at cloudnweb.dev

Scenario-based Learning – MatchMaking Algorithm – Part 2

For regular updates, subscribe to weekly newsletter, Weekly Updates.

This is the second part of Scenario-based Learning. Scenario-based Learning – MatchMaking Algorithm - Node.js – Part 2

you can learn more about the context of the series from part one.

https://cloudnweb.dev/2019/07/scenario-based-learning-a-new-learning-perspective-node-js-part-1/

Firstly, In this article, we will see an interesting problem scenario which you might face in several business requirements.

That is to say, Have you ever wondered, How a Swiggy, Uber-Eats, and Restaurant Applications works?.

How do they show the restaurant according to our location?. Well, we will learn how to develop an application like that in this article

How is the Match Making Algorithm Works?

There are many numbers of applications like PUBG, Uber, Food Delivery Applications, Hotel Booking Application, Matrimonial sites uses the concept of Match Making.

Match Making is nothing but matching a Profile with another Profile with different criteria's or needs.

Example

For Example, Games like PUBG matches profiles based on player level, Applications like OYO, Swiggy, and Uber matches profiles based locations, previous history, and ratings, etc.

In this article, we will see a simple matchmaking algorithm which is Match Profiles based on location.

Problem Scenario/ User Story

Here is the problem scenario,

you have to develop an application where user registers with his details such email and location details.

On the other hand, a restaurant can able to register with their location and city details.

Further, In the user dashboard, you need to show all the nearby restaurants according to the user location.

Above all, To make it more interesting and challenging, develop the API's using GraphQL.

Note : If you are new to GraphQL, i recommend you to read this article graphql

https://academind.com/learn/node-js/graphql-with-node-react-full-app/

Solution

Demo

Complete Source Code : https://github.com/ganeshmani/foodmonster

Mainly, this demo source code contains only the API for the problem scenario.

if you want to build the front-end dashboard, you can do it and make a PR, that is much appreciated. :-)

Business Logic of Application

the magic happens in the MongoDB database query. MongoDB GeoSpatial Queries. we are storing the user location lat and long in the database.


const userSchema = new Mongoose.Schema({
    name : {
        type : String,
        required : true
    },
    email : {
        type : String,
        required : true
    },
    password : {
        type : String,
        required : true
    },
    salt : {
        type : String
    },
    location : {
        type : {
            type : String,
            enum : ['Point'],
            required : true
        },
        coordinates : {
            type : [Number],
            required : true
        }
    }
});

Mainly, location stores the latitude and longitude in the database with the type Point

Similarly, we are storing the latitude and longitude of the Restaurant in the database.

const restaurentSchema = new Mongoose.Schema({
    name : {
        type : String,
        required : true
    },
    city : {
        type : String,
        required : true
    },
    location : {
        type : {
            type : String,
            enum : ['Point'],
            required : true
        },
        coordinates : {
            type : [Number],
            required : true
        }
    }
});

while searching the nearby restaurant using the user latitude and longitude. we are making the MongoDB database with the query geoNear which gets all the restaurant details based on the latitude and longitude.

https://docs.mongodb.com/manual/reference/operator/aggregation/geoNear/

Finally, the search result would be,

Advanced Problem Scenario

Scenario #2

In addition to the location-based Search result, add the User favourites, Restaurant Ratings in the top of the search result.

Scenario #3

Mainly, Learning an algorithm behind the application is a crucial step in understanding the problem in the software industry.

learn more about Stable Marriage Algorithm and implement that in any programming language.

https://algorithmia.com/algorithms/matching/StableMarriageAlgorithm/docs

https://en.wikipedia.org/wiki/Stable_marriage_problem#/media/File:Gale-Shapley.gif

https://towardsdatascience.com/gale-shapley-algorithm-simply-explained-caa344e643c2

To learn more about Node.js and MongoDB, there are number of resources available on the internet.

we will see more problem scenarios in upcoming articles, share this article with others, so they can be able to learn by practicing the problems.

Note : If you have a Problem scenario that you want to share with other developers. it will be published through this series. Contact me for further details

Until then, Happy Coding :-)

Top comments (0)