important docker commands

important docker commands

Important Docker Commands from Beginner to Expert level

Important docker commands I will be going over basic and important definitions and commands that is essential while working with docker images and docker containers

What is docker

The official definition of docker can be found here. But in short, you can consider docker as tool to develop and deploy code using the concept of containers

What is container

Important docker commands might not make any sense if we don’t properly define what a container is:
Continue reading important docker commands

spring upload multiple file

spring upload multiple files

Spring upload multiple files with restful API

Spring upload multiple files with restful api is one of the most requires task. If you are working with spring, chances are high that you are working with file as well.

If you are doing registration, you might need to have multiple files to be uploaded to your server. So how to do spring upload multiple files is what I will be showing you.
Continue reading spring upload multiple files

random file from directory

random files from directory

Get Random Files From Directory Using Shell Script

Random files from directory is most sought after operation specially when working with files. You may be creating video from image, analyzing sample data and more.

Shell scripting has a lot to offer, not only random files from directory. The cool thing about shell script is, it is more native and fast.

I am working on generating videos from selected images and I needed to have ability to select random files from the given folder.
Continue reading random files from directory

postgres not null

psql select not working

PSQL Select not working – or other simple commands not working

Working with command line postgres tool psql and having an issue?

Why doesn’t the psql command show the simple select query?

PSQL select not working is a problem that can happen in either of the following scenarios:

  1. Are you on the right database?
    Chances are you might not be on the right database at all and the relation you are trying to find might not be there at all:Check if you are on the right database.
     
    Run the command \l to see the available databases.How to connect to the postgres database from the list?
    Use the command \c database_name
    Let’s say the database name is inventory then:\c inventory will connect you to the right database
  2. Continue reading psql select not working

postgres not null

postgres not null to null

How to remove NOT NULL constraint in postgres

A NOT NULL constraint is essential tool in postgres database to ensure validation.

But sometimes, you need to remove not null and allow column to be null.

Remove NOT NULL Postgres


ALTER TABLE table_name ALTER COLUMN column_name DROP NOT NULL
custom spring validator

custom spring validator

creating custom spring validator

Spring boot comes with lots of validation out of the box, Min, Max, NotNull. But, those are not enough as custom validations are important and needed.

When to use custom spring validator

When there is specific business logic that needs to be validated, custom validation will be to the rescue.

An example of creating a custom spring validator can be the following scenario. On your application there is a registration page.

Let’s say you have a survey sent out before registration and you want a user to fill out a survey using their email associated with it before registration.

When the user comes to your website for registration, one criteria as validated user is a completion of the survey. Here, you can create this validator and use it on more than one forms.

What do I need for custom spring validator

A basic knowledge of java and spring framework is good enough for this. Also make sure if it is worth creating spring custom validator. This is also the same if you are considering custom spring boot validator

Custom Spring validator example, spring validator tutorial

Let us extend the above example and work on it. We would like to have a unique email to be read from database as custom spring validator.

Let’s call it UniqueEmailValidator. This is will be checked mostly during registration and we can assume it can also used in a couple of other places.

First, we need to create a custom target interface


/**
 * Custom email validation
 * Checks if the email already exists or not.
 * 
 * @author Kaleb Woldearegay 
 *
 */
@Target({ElementType.METHOD, ElementType.FIELD})
@Retention(RetentionPolicy.RUNTIME)
@Constraint(validatedBy = UniqueEmailValidator.class)
public @interface UniqueEmail {
    String message() default "{Email already exists}";
    Class[] groups() default {};
    Class[] payload() default {};
}

You can find the full code on github here.

Then we will need a concrete class that implements the interface


/**
 * Custom email validation. Check if the provided email is unique or not.
 * @author Kaleb Woldearegay 
 *
 */
@Configurable
public class UniqueEmailValidator implements ConstraintValidator {

    private MemberService memberService; //this is injected to the class
    
    public UniqueEmailValidator() {}
    
    @Autowired
    public UniqueEmailValidator(MemberService memberService) {
        this.memberService = memberService;
    }
    
    public void initialize(UniqueEmail constraint) {
    }
 
    public boolean isValid(String email, ConstraintValidatorContext context) {
        if (email == null) {
            return false;
        }
        //actual logic of search for the member goes here..
        Optional member = this.memberService.findByEmail(email);
        return !member.isPresent();
    }

You can see the full class here

The important part of the implementation is the isValid one. That should return the boolean outcome.

That is it, now you have the a full-fledged custom spring validator in created.

You can use it using the annotation @UniqueEmailValidator in your controller or wherever you need to validate.

If you have any questions or comments, let me know.

facebook auto post using php

facebook page post from PHP

How to autopost to facebook page using php

Facebook auto post using php, hmm.., I have a post on this topic a while ago, this one.

This post answers your questions of how to post to facebook page using php.

Though some of the initial steps do still hold, that is no more the case today. In order to combat the fake news and other improper usage of the platform, facebook has come up with different approach.
Continue reading facebook page post from PHP

spring boot validation

spring boot validation

Spring boot validation and Custom Validations

Spring boot validation is probably one of the cool features spring has to provide. Not only spring provided validations but also, it allows custom validation as well.

Custom Spring boot validator will allow you to act like the native validator in your application. Not only that, once you create custom spring validator, you can reuse it.

What is spring validator

Spring validator allows validating the data of the request. The validator will handle basic logic of handling validation than re-inventing the wheel.
Continue reading spring boot validation

important vim commands

important VIM commands

Important VIM commands for daily usage

Important VIM commands is selected vim commands that every programmer shall have by heart.

By far important vim commands might be those we use in everyday programming life. But there more to it.

What is vim

Vim is a smart text editor for linux distros. It allows plugs ins for different usages. Basically, it pretty much knows most of programming languages you open with it.

Important vim commands will allow you to have a fast and productive usage of VIM while working on it.

Why should I use vim

You should use vim because it is cool. Really it is cool. Apart from being cool, it is light weight and almost available in most linux distributions.

Vim allows having third party plugins that will allow you to do multiple tasks from coloring to memory based copy and more.

Vim is programming language friendly. If you are working on java, it will highlight the keywords for you. Same for PHP or javascript or almost for lots of languages.

how it install vim

Usually VIM will come with most of the linux flavors:

If you are on Ubuntu installing vim on ubuntu will be
Continue reading important VIM commands

git sub directory

how to add subdirectories in git

Adding subdirectories in existing git repository

You have done git push but the new folders didn’t got pushed. What happened?

Having new folders and subfolders in the git repository, but the files under those not showing in the server?

Adding subdirectories in existing git repositories should have been direct right? But, sometimes it is not. I will show you How to add subdirectory in existing git repository

Let’s say you have existing git repository my-git-repo and you want to add new folder under it: new-folder/another-sub-folder

Step by steps to add git subdirectories in existing git repository

1. Create folder in the repository you are going to create new folders

mkdir -p new-folder/another-sub-folder

2. Now if you do git status you will see the new folder under new listing.

3. Create a file under another-sub-folder otherwise git won’t show nothing.

cd new-folder/another-sub-folder && touch some-file.xml

4. Go to another-sub-folder and issue git init

5. Now go back to the root of the folder and issue git add new-folder/

6. You are done, when you do git status you will see your new files being listed for commit.