Code Review. How to modify an old commit.

Sometimes, when using Code review, you need to modify an old commit with the suggestions. I used to create a new commit for the modifications, but this is non clean, intuitive (if you wrap commits by task), nor backwards compatible.

There is a better way to this using git rebase, which basically allows you to change a commit and apply those changes to further commits.

In order to do that, we need to rebase the commit this way.
1º- Checkout the branch
2º- git rebase –interactive ‘xxxxxx^’
3º- In the editor, change ‘pitch’ to ‘edit’ on the commit we want to modify.
4º- Make the changes
​5º- git commit –all –amend –no-edit
6º- git rebase –continue
​7º- git push (–force) origin <branch_name>​
I hope it helps

From Java to Kotlin. Cheatsheet

I wanted to keep everything regarding the differences between Java and Kotlin on one place. That’s why I’ve created with Cheatsheet.
I hope it would be useful for you as well.

Printing

Java

Kotlin

Variables I

Java

Kotlin

Variables II

Java

Kotlin

Null I

Java

Kotlin

Null II

Java

Kotlin

Strings I

Java

Kotlin

Strings II

Java

Kotlin

Ternary Operator

Java

Kotlin

Bits Operations

Java

Kotlin

Is As In

Java

Kotlin

Smart Cast

Java

Kotlin

Switch / When

Java

Kotlin

For

Java

Kotlin

Collections

Java

Kotlin

Collections

Java

Kotlin

Collections

Java

Kotlin

Collections

Java

Kotlin

Collections

Java

Kotlin

 

How to create a REST API with Node.js and LoopBack

how to API REST tutorial

In this post, we are going to learn how to use LoopBack Node.js framework  to create a fully functional REST API over MongoDB.

We will follow these steps:

1- Define the models in the database

2- Define the relations between models

3- Define users access levels

PREREQUISITES

To follow this tutorial we will need NodeJS and MongoDB installed in our computer.

DATABASE SET UP

Install the LoopBack module.

Run Loopback and follow instructions.

Move to the new folder.

We are going to do an e-commerce API for the example. We want to to store all date in a persistent way into a MongoDB database that will have Clients, Products and Comments.

In order to do so, we need to specify the route of the database. As it is not created yet, will create a new folder and connect it to MongoDB.

Then, in another terminal, we run the Loopback commands to create the database

We’ll call it “ecommerce”

Once we generated the database, we can see the code generated by Loopback at the file model-config.json.

It is recommendable to change the attribute “dataSource”: “db” to the database we just created (“MongoDB”) at ACL,RoleMapping and Role.

1- DEFINE THE MODELS IN THE DATABASE

To create a new collection (what would be a table), we’ll use the following command.

The way to define the data will be as the following example:

*In this case is not needed to define more attributes because we are extending the “User” class that already have username, password, email and other needed attributes.

Now we are going to define the “Product” collection.

Now, the comments:

Let’s add some Comments properties now.

Now we have all the collections defined, it’s time to define the relation between them.

2- DEFINE THE RELATION BETWEEN MODELS

Let’s review what we have done up to now.

On the one hand, we have the e-commerce Users and on the other hand we have the Products.

We also want the Users to post Comments about our Products.

So these would be the relations:

1- A product can have several comments.

2- A product can have several users commenting about it.

3- A comment belongs to determined product.

therefore…

4- A user can post several comments.

and

5- A product can have several comments about it.

[insert schema here]

To define the relations between collections, we will use the following command:

1- A product can have several comments.

2- A product can have several users commenting about it.

3- A comment belongs to determined product.

4- A user can post several comments.

5- A product can have several comments about it.

With what we have done up to now

Up to this point, we already have the API and we can do some testing by this command.

or

Despite of this, we haven’t finished yet, as any user could use all the methods and CREATE and DELETE some data without any authentication, which drives us to the last step.

3- DEFINE USERS ACCESS LEVELS

The first thing that we are going to do in this section is to create the users “admin” and “kike” (although you can change the last one for your username) and we will give to the administrator privileges to the user “admin”.

Loopback allows us to include a script that will run whenever we start the service, so we will use that to introduce these two users into the database.

So, we create a file at <our project folder>/server/boot/script.js and copy the following code:

This will create two new users. A regular user (Kike) and an Admin user (obviously, admin). After that we create a Role admin and bind them together.

Now, let’s restrict the some accesses for Authenticated users:

We will use the Loopback Access Control List (ACL) by using this command:

So first, let’s deny all kind of accesses:

Once we have done this, let’s enable GET (READ) accesses for autheticated users:

And finally, allow Admins to perform all operations:

Once we have done this, we finished and already have a fully functional REST API with Node.js.

We can run it and start playing with it with he following command:

Cool, isn’t it? Now you can relax, take a cup of coffee and tell your coworkers how hard you have been working.

Cheers!

Improve your algorithms using the correct Data Structure

Algorithm complexity. Big O

 

We know that Object-oriented Programming can help us to design and build huge systems, but this is only the half history. Usually, we use computer programs because we need to manage huge amounts of data and do it within a reasonable amount of time.

Here is where algorithm design comes into play. The way we manage the data into the algorithm will determine the time that will take to solve the problem. (See above the Amount of data – Time correlation table)

Is this post, I want to talk about how using the correct data structure can help us to get a successful cost-effective algorithm.

When developing an algorithm, there are several things that we should take into account:

1- Amount of data

2- How we structure the data

3- How we manage the data

4- Time and space complexity

We assume that the main aim of our algorithm is to manage a big amount of data ( >1000 items ). The difference of time between two algorithms (let’s say O(x) vs O(x²)) for 10 elements is almost irrelevant.

Our work as a software engineers is to avoid unnecessary resource consumption and decrease the server costs.

The most important step when choosing a Data Structure is to know what kind of operations will be performed against that Data Structure.

Most common operations are Access, Search, Insertion and Deletion. Requirements may vary depending of each case but we base our research in those 4 as a first step.

Of course, the most valuable aspect of an algorithm is the programmer creativity. Let’s consider then, the following Data Structure table a cheatsheet for that.

 

AccessSearchInsertionDeletionSpace
ArrayO(1)O(n)O(n)O(n)O(n)
StackO(n)O(n)O(1)O(1)O(n)
QueueO(n)O(n)O(1)O(1)O(n)
List with PIO(n)O(n)O(1)O(1)O(n)
Hash tableO(1)/O(n)O(1)/O(n)O(1)/O(n)>=O(n)

*as long as we overdimension the hash table there will be less collisions and we will get the item easily.

 

My plan is to get more deeply into the listed Data Structures. If you find interesting any other Data Structure, please comment below. I will appreciate any suggestion.

 

Uso de cookies

Este sitio web utiliza cookies para que usted tenga la mejor experiencia de usuario. Si continúa navegando está dando su consentimiento para la aceptación de las mencionadas cookies y la aceptación de nuestra política de cookies, pinche el enlace para mayor información.

ACEPTAR
Aviso de cookies