Author

The Score Prophet - APIs & Data Model

29 November, 2022, by Madison Shaw

During the development of The Score Prophet one of our major concerns was data: what data do we need? How do we access it? And, how do we store it? This post will give an overview of our data model, our data storage methods, and the API backend of The Score Prophet application.

For a user to play The Score Prophet game, they need an account. To give them an account, we need some personal information: name, email, and password. Then, as they play the game, each user creates their own game-related data: their entries, score predictions, and number of points earned. Finally, we have the World Cup data: the dates and locations of the matches, the teams playing them, and the outcomes of the matches.

To keep track of the data we needed to collect, we put together a data model, shown below.

Schema definition

This shows the attributes of each data object we store, and the data type that we store them as.

With the data model defined, the next step was to choose a database to store it in. When selecting a database for The Score Prophet, there were several different factors we had to take into account. In particular, we focused on scalability and flexibility—not only did we have to consider our growing user base, but we also had to think about The Score Prophet’s future beyond the World Cup—in the near future, we intend to host games based on other competitions, and more competitions mean more data that we have to store. The team have also discussed possible additional features for The Score Prophet such as statistics about matches and players, or allowing users to upload a profile picture for their account. Both of these would require additional fields to be added to the data model, and so we needed a database that was flexible rather than having a rigid structure.

Bearing all of this in mind, we decided to go for a MongoDB database. MongoDB is a NoSQL document database—rather than storing data in rigidly structured tables, data is stored in collections of JSON-like documents. Instead of rows and columns, the documents contain fields which are easy to alter or add to. Its flexibility and ease of use, as well as its high compatibility with Node.js applications, made MongoDB the perfect choice for The Score Prophet’s data storage.

Sending user data to and from the database is made possible by our API backend, developed using Node.js and the Express framework. Data is input into a form by the user. It is then sent via HTTP request to the backend server. Upon hitting the server, the request is sent through the appropriate router. Depending on the request and the parameters provided, the router passes the request to a controller. For example, when the user router receives a POST request, it will direct the request to the “insert” function within the user controller. To keep data secure, the router checks for a valid access token before passing on the request.

  
    router.post('/',[
        AuthValidationMiddleware.validJWTNeeded,
        UserController.insert
    ]);
  

The controller then determines what happens to the data and sends back a response. In the case of creating a new user account, the controller reads in the data, checks that no account already exists for the email, then encrypts the password and adds the user’s information to the database.

Each user’s information is input manually by the user themselves. For the World Cup data, on the other hand, the sheer amount of information meant that entering the data manually was out of the question. Instead, we decided to import the match data from a third-party provider using their football API, which provided all the match information we needed directly to our database.

With the backend development and database setup complete, we were ready to deploy The Score Prophet to the Cloud. The Node.js application was containerised using Docker and then deployed to the cloud via IBM Cloud Code Engine.

NOTE: There is still time to play along, for free, at www.thescoreprophet.com. There are spot prizes still up for grabs - and we like to think it's a bit of fun.

© Copyright 2024 Madigan Solutions UK Limited
Madigan Solutions UK Limited is a company registered in Northern Ireland with Company Number NI675324. VAT Number 368 3929 47.

Home | Services | About | Blog | Contact

Terms & Conditions | Privacy Policy | Disclaimer