Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
4 changes: 4 additions & 0 deletions .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -21,3 +21,7 @@

# virtual machine crash logs, see http://www.java.com/en/download/help/error_hotspot.xml
hs_err_pid*

.idea/

target/
4 changes: 4 additions & 0 deletions Dockerfile
Original file line number Diff line number Diff line change
@@ -0,0 +1,4 @@
FROM postgres
ENV POSTGRES_PASSWORD admin
ENV POSTGRES_DB postgres
COPY sqlscripts/docker_init.sql /docker-entrypoint-initdb.d/
28 changes: 28 additions & 0 deletions developer_guide/forecasting_api_doc.txt
Original file line number Diff line number Diff line change
@@ -0,0 +1,28 @@
1. created a simple spring boot web application using spring intializer website.
2. using postgres as a datasource hence creating a db to provide as spring datasource. For now
only one db is created in the name of the tenant.
//TODO In a larger picture we need to serve the api for multiple clients.
// TODO Need to discuss with Sebin on how to design services that can serve multiple clients.

The following is the command is spawn a docker container with container name postgresdb
that runs postgres server with user postgres and password admin

docker container run --name postgresdb -e POSTGRES_PASSWORD=admin -d -p 5432:5432 postgres
use the command to check the running containers
docker container ls

3. create a sql script that creates a database and a user for the database.
4. copy the script to the docker container
docker cp sqlscripts/forecaster.sql postgresdb:/
5. enter into the docker container bash
docker container exec -it postgresdb bash
6. run the following command to execute the copied sql
psql -U postgres --file forecaster.sql
7. Spring offers multiple ways to configure datasource beans. one way is to set in the application.properties
another way is using java config beans.
// TODO Need to discuss with sebin how they configure multiple datasources in their company and in general.
for now going with application.properties
8. set the following properties in application.properties.(Remember that properties file doesn't allow spaces)
spring.datasource.url=jdbc:postgresql://localhost:5432/walmartdb
spring.datasource.username=walmart
spring.datasource.password=walmart@123
5 changes: 5 additions & 0 deletions init.sh
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
docker build -t postgres_forecasting .

docker run -it --name postgresdb -d -p 5432:5432 postgres_forecasting

docker container exec -it postgresdb bash
92 changes: 92 additions & 0 deletions pom.xml
Original file line number Diff line number Diff line change
@@ -0,0 +1,92 @@
<?xml version="1.0" encoding="UTF-8"?>
<project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 https://maven.apache.org/xsd/maven-4.0.0.xsd">
<modelVersion>4.0.0</modelVersion>
<parent>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-parent</artifactId>
<version>2.3.4.RELEASE</version>
<relativePath/> <!-- lookup parent from repository -->
</parent>
<groupId>com.example</groupId>
<artifactId>forecasting</artifactId>
<version>0.0.1-SNAPSHOT</version>
<name>forecasting</name>
<description>Forecasting API</description>

<properties>
<java.version>1.8</java.version>
</properties>

<dependencies>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-actuator</artifactId>
</dependency>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-jdbc</artifactId>
</dependency>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-web</artifactId>
</dependency>

<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-validation</artifactId>
<version>2.3.4.RELEASE</version>
</dependency>


<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-devtools</artifactId>
<scope>runtime</scope>
<optional>true</optional>
</dependency>
<dependency>
<groupId>org.postgresql</groupId>
<artifactId>postgresql</artifactId>
<scope>runtime</scope>
</dependency>
<dependency>
<groupId>org.projectlombok</groupId>
<artifactId>lombok</artifactId>
<optional>true</optional>
</dependency>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-test</artifactId>
<scope>test</scope>
<exclusions>
<exclusion>
<groupId>org.junit.vintage</groupId>
<artifactId>junit-vintage-engine</artifactId>
</exclusion>
</exclusions>
</dependency>
<dependency>
<groupId>org.mindrot</groupId>
<artifactId>jbcrypt</artifactId>
<version>0.4</version>
</dependency>

<!-- <dependency>-->
<!-- <groupId>org.flywaydb</groupId>-->
<!-- <artifactId>flyway-core</artifactId>-->
<!-- <version>5.2.4</version>-->
<!-- </dependency>-->

</dependencies>

<build>
<plugins>
<plugin>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-maven-plugin</artifactId>
</plugin>
</plugins>
</build>

</project>
25 changes: 25 additions & 0 deletions sqlscripts/docker_init.sql
Original file line number Diff line number Diff line change
@@ -0,0 +1,25 @@
create user walmart with password 'walmart@123';
create database walmartdb with template=template0 owner=walmart;
\connect walmartdb;
alter default privileges grant all on tables to walmart;
alter default privileges grant all on sequences to walmart;

create extension "uuid-ossp";

create table orgs_enabled(
org_id integer primary key not null,
org_name varchar(30) not null,
is_enabled boolean not null default true
);

create table users (
user_id UUID primary key not null,
first_name varchar(20) not null,
last_name varchar(20),
email varchar(30) not null,
password text not null,
org_id integer not null
);

alter table users add constraint org_id_fk foreign key (org_id) references orgs_enabled(org_id);
create sequence org_id_seq increment 1 start 1;
28 changes: 28 additions & 0 deletions sqlscripts/forecaster.sql
Original file line number Diff line number Diff line change
@@ -0,0 +1,28 @@
drop database walmartdb;
drop user walmart;
drop sequence user_seq;
create user walmart with password 'walmart@123';
create database walmartdb with template=template0 owner=walmart;
\connect walmartdb;
alter default privileges grant all on tables to walmart;
alter default privileges grant all on sequences to walmart;

create table orgs_enabled(
org_id integer primary key not null,
org_name varchar(30) not null,
is_enabled boolean not null default true
);

create table users (
user_id UUID primary key not null,
first_name varchar(20) not null,
last_name varchar(20) not null,
email varchar(30) not null,
password text not null,
org_id integer not null
);

alter table users add constraint org_id_fk foreign key (org_id) references orgs_enabled(org_id);

create sequence user_seq increment 1 start 1;
create sequence org_id increment 1 start 1;
13 changes: 13 additions & 0 deletions src/main/java/com/example/forecasting/ForecastingApplication.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,13 @@
package com.example.forecasting;

import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;

@SpringBootApplication
public class ForecastingApplication {

public static void main(String[] args) {
SpringApplication.run(ForecastingApplication.class, args);
}

}
Original file line number Diff line number Diff line change
@@ -0,0 +1,43 @@
package com.example.forecasting.datasource;

import com.zaxxer.hikari.HikariDataSource;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.boot.autoconfigure.couchbase.CouchbaseProperties;
import org.springframework.boot.context.properties.ConfigurationProperties;
import org.springframework.boot.jdbc.DataSourceBuilder;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import org.springframework.core.env.Environment;
import org.springframework.jdbc.core.JdbcTemplate;

import javax.sql.DataSource;
import javax.validation.constraints.NotNull;

@Configuration
public class PostgresDataSource {

@Autowired
private Environment environment;

@Bean
@ConfigurationProperties("app.postgresource")
public HikariDataSource hikariDataSource(){
System.out.println("My Own Spring Bean of Data Source is Created ");
HikariDataSource hds = DataSourceBuilder
.create()
.type(HikariDataSource.class)
.build();

Integer maxPoolSize = Integer.parseInt(environment.getProperty("app.postgresource.maxPoolSize"));
hds.setJdbcUrl(environment.getProperty("app.postgresource.jdbcUrl"));
hds.setUsername(environment.getProperty("app.postgresource.username"));
hds.setPassword(environment.getProperty("app.postgresource.password"));
hds.setMaximumPoolSize(maxPoolSize);
return hds;
}

@Bean("walmart-postgres")
public JdbcTemplate jdbcTemplate(){
return new JdbcTemplate(hikariDataSource());
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,26 @@
package com.example.forecasting.datasource;

import com.zaxxer.hikari.HikariDataSource;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.boot.ApplicationArguments;
import org.springframework.boot.ApplicationRunner;
import org.springframework.stereotype.Component;

import java.sql.Connection;
import java.sql.SQLException;

@Component
public class PostgresDataSourceLoader implements ApplicationRunner {
private final HikariDataSource hikariDataSource;

@Autowired
public PostgresDataSourceLoader(HikariDataSource hikariDataSource) {
this.hikariDataSource = hikariDataSource;
}

@Override
public void run(ApplicationArguments args) throws SQLException {
Connection conn = hikariDataSource.getConnection();
Copy link
Copy Markdown
Owner Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This will currently not allow spring boot application to boot up if the DB is not running. instead catch and throw a warning maybe.

conn.close();
}
}
20 changes: 20 additions & 0 deletions src/main/java/com/example/forecasting/entities/Org.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,20 @@
package com.example.forecasting.entities;


import com.fasterxml.jackson.annotation.JsonProperty;

import javax.validation.constraints.NotBlank;

public class Org {

@NotBlank(message = "org name cannot be blank")
private String orgName;

public Org(@JsonProperty(value = "org_name", required = true) String orgName){
this.orgName = orgName;
}

public String getOrgName(){
return orgName;
}
}
72 changes: 72 additions & 0 deletions src/main/java/com/example/forecasting/entities/User.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,72 @@
package com.example.forecasting.entities;

import com.fasterxml.jackson.annotation.JsonIgnoreProperties;
import com.fasterxml.jackson.annotation.JsonProperty;
import org.springframework.lang.Nullable;
import javax.validation.constraints.NotBlank;
import java.util.UUID;

@JsonIgnoreProperties(value = {"id"})
public class User {

private final UUID id;
@NotBlank(message = "First Name cannot be blank.")
private final String firstName;
// TODO implement a custom validator
@Nullable
private final String lastName;
@NotBlank(message = "email cannot be blank.")
private final String email;
@NotBlank(message = "password cannot be blank.")
private final String password;
// TODO implement a custom validator
private final String orgName;


public User(UUID id,
@JsonProperty(value = "first_name", required = true) String firstName,
@Nullable @JsonProperty("last_name") String lastName,
@JsonProperty(value = "email", required = true) String email,
@JsonProperty(value = "password",
access = JsonProperty.Access.WRITE_ONLY,
required = true) String password,
@JsonProperty(value = "org_name", required = true) String orgName)
{

System.out.println("id is " + id);
System.out.println("first name is " + firstName);
System.out.println("last name is " + lastName);
System.out.println("email is " + email);
System.out.println("password is " + password);
System.out.println("org name is " + orgName);

this.id = id;
this.firstName = firstName;
this.lastName = lastName;
this.email = email;
this.password = password;
this.orgName = orgName;
}

public String getOrgName() {
return orgName;
}

public String getFirstName() {
return firstName;
}

@Nullable
public String getLastName() {
return lastName;
}

public String getEmail() {
return email;
}

public String getPassword() {
return password;
}

}
Loading