-
Notifications
You must be signed in to change notification settings - Fork 0
User login registration #1
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Open
ramchandra94
wants to merge
2
commits into
main
Choose a base branch
from
user_login_registration
base: main
Could not load branches
Branch not found: {{ refName }}
Loading
Could not load tags
Nothing to show
Loading
Are you sure you want to change the base?
Some commits from the old base branch may be removed from the timeline,
and old review comments may become outdated.
Open
Changes from all commits
Commits
Show all changes
2 commits
Select commit
Hold shift + click to select a range
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| 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/ |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| 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 |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| 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 |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| 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> |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| 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; |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| 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
13
src/main/java/com/example/forecasting/ForecastingApplication.java
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| 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); | ||
| } | ||
|
|
||
| } |
43 changes: 43 additions & 0 deletions
43
src/main/java/com/example/forecasting/datasource/PostgresDataSource.java
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| 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()); | ||
| } | ||
| } |
26 changes: 26 additions & 0 deletions
26
src/main/java/com/example/forecasting/datasource/PostgresDataSourceLoader.java
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| 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(); | ||
| conn.close(); | ||
| } | ||
| } | ||
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| 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; | ||
| } | ||
| } |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| 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; | ||
| } | ||
|
|
||
| } |
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
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.