Skip to content

Commit

Permalink
Merge pull request #9 from Evegen55/rdbms
Browse files Browse the repository at this point in the history
REST API for users implemented and ready to use (without secure)
  • Loading branch information
Evegen55 committed Mar 2, 2018
2 parents 2e5d2a2 + 388457c commit 802dee1
Show file tree
Hide file tree
Showing 19 changed files with 782 additions and 6 deletions.
1 change: 1 addition & 0 deletions .gitignore
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
.idea/
*.iml
.gradle/
*.bak
7 changes: 7 additions & 0 deletions build.gradle
Original file line number Diff line number Diff line change
Expand Up @@ -23,9 +23,16 @@ repositories {
dependencies {

compile("org.springframework.boot:spring-boot-starter-web:1.5.10.RELEASE")
compile("org.springframework.boot:spring-boot-starter-jdbc")
compile("org.springframework.boot:spring-boot-starter-data-jpa")
compile ("mysql:mysql-connector-java:5.1.45")

compile 'org.hibernate:hibernate-core:5.2.2.Final'
compile 'org.hibernate:hibernate-entitymanager:5.2.2.Final'

testCompile group: 'junit', name: 'junit', version: '4.12'
testCompile('org.springframework.boot:spring-boot-starter-test')
//compile('com.h2database:h2:1.4.193')
}

task wrapper(type: Wrapper) {
Expand Down
7 changes: 7 additions & 0 deletions rdbms_flow/init/examples_queries/select_from_tree_tables.sql
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
SELECT * FROM remote_banking.person
join (
SELECT * FROM remote_banking.emails
left join remote_banking.google_accounts
on emails.google_accounts_idgoogle_accounts = google_accounts.idgoogle_accounts
) as joined_emails_and_googles
where joined_emails_and_googles.person_idperson = person.idperson and person.idperson = 3;
3 changes: 3 additions & 0 deletions rdbms_flow/init/examples_queries/select_from_two_tables.sql
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
SELECT first_name, last_name, date_of_birth, email FROM remote_banking.person
inner join remote_banking.emails
where emails.person_idperson = person.idperson and person.idperson = 3;
48 changes: 48 additions & 0 deletions rdbms_flow/init/init.sql
Original file line number Diff line number Diff line change
@@ -0,0 +1,48 @@
CREATE DATABASE IF NOT EXISTS remote_banking;
USE remote_banking;

-- PERSON table
DROP TABLE IF EXISTS person;
CREATE TABLE person (
idperson int(11) NOT NULL AUTO_INCREMENT,
first_name varchar(45) NOT NULL,
last_name varchar(45) NOT NULL,
date_of_birth date NOT NULL,
gender varchar(45) NOT NULL,
PRIMARY KEY (idperson)
);
INSERT INTO remote_banking.person
(first_name, last_name, date_of_birth, gender)
VALUES
('John', 'Doe', '2000-01-1', 'man'),
('Jane', 'Doe', '2000-01-1', 'woman'),
('Jack', 'Doe', '2000-01-1', 'man');

-- GOOGLE table (empty at its initial state
DROP TABLE IF EXISTS google_accounts;
CREATE TABLE google_accounts (
idgoogle_accounts int(11) NOT NULL,
family_name varchar(45) DEFAULT NULL,
PRIMARY KEY (idgoogle_accounts)
);

-- EMAILS table
DROP TABLE IF EXISTS emails;
CREATE TABLE emails (
idemails int(11) NOT NULL AUTO_INCREMENT,
email varchar(45) NOT NULL,
person_idperson int(11) NOT NULL,
google_accounts_idgoogle_accounts int(11) DEFAULT NULL COMMENT 'email can be not a google',
PRIMARY KEY (idemails),
UNIQUE KEY email_UNIQUE (email),
KEY fk_emails_person_idx (person_idperson),
UNIQUE KEY fk_emails_google_accounts_idx (google_accounts_idgoogle_accounts),
CONSTRAINT fk_emails_google_accounts FOREIGN KEY (google_accounts_idgoogle_accounts) REFERENCES google_accounts (idgoogle_accounts) ON DELETE SET NULL ON UPDATE SET NULL,
CONSTRAINT fk_emails_person FOREIGN KEY (person_idperson) REFERENCES person (idperson) ON DELETE NO ACTION ON UPDATE NO ACTION
);
INSERT INTO remote_banking.emails
(email, person_idperson)
VALUES
('John@Doe.com', 1),
('Jane@Doe.com', 2),
('Jack@Doe.com', 3);
118 changes: 118 additions & 0 deletions rdbms_flow/init/mysql/initial_state_dump.sql
Original file line number Diff line number Diff line change
@@ -0,0 +1,118 @@
CREATE DATABASE IF NOT EXISTS `remote_banking` /*!40100 DEFAULT CHARACTER SET utf8 */;
USE `remote_banking`;
-- MySQL dump 10.13 Distrib 5.7.17, for Win64 (x86_64)
--
-- Host: localhost Database: remote_banking
-- ------------------------------------------------------
-- Server version 5.7.14-log

/*!40101 SET @OLD_CHARACTER_SET_CLIENT=@@CHARACTER_SET_CLIENT */;
/*!40101 SET @OLD_CHARACTER_SET_RESULTS=@@CHARACTER_SET_RESULTS */;
/*!40101 SET @OLD_COLLATION_CONNECTION=@@COLLATION_CONNECTION */;
/*!40101 SET NAMES utf8 */;
/*!40103 SET @OLD_TIME_ZONE=@@TIME_ZONE */;
/*!40103 SET TIME_ZONE='+00:00' */;
/*!40014 SET @OLD_UNIQUE_CHECKS=@@UNIQUE_CHECKS, UNIQUE_CHECKS=0 */;
/*!40014 SET @OLD_FOREIGN_KEY_CHECKS=@@FOREIGN_KEY_CHECKS, FOREIGN_KEY_CHECKS=0 */;
/*!40101 SET @OLD_SQL_MODE=@@SQL_MODE, SQL_MODE='NO_AUTO_VALUE_ON_ZERO' */;
/*!40111 SET @OLD_SQL_NOTES=@@SQL_NOTES, SQL_NOTES=0 */;

--
-- Table structure for table `emails`
--

DROP TABLE IF EXISTS `emails`;
/*!40101 SET @saved_cs_client = @@character_set_client */;
/*!40101 SET character_set_client = utf8 */;
CREATE TABLE `emails` (
`idemails` int(11) NOT NULL AUTO_INCREMENT,
`email` varchar(45) NOT NULL,
`person_idperson` int(11) NOT NULL,
`google_accounts_idgoogle_accounts` int(11) DEFAULT NULL COMMENT 'email can be not a google',
PRIMARY KEY (`idemails`),
UNIQUE KEY `email_UNIQUE` (`email`),
UNIQUE KEY `fk_emails_google_accounts_idx` (`google_accounts_idgoogle_accounts`),
KEY `fk_emails_person_idx` (`person_idperson`),
CONSTRAINT `fk_emails_google_accounts` FOREIGN KEY (`google_accounts_idgoogle_accounts`) REFERENCES `google_accounts` (`idgoogle_accounts`) ON DELETE SET NULL ON UPDATE SET NULL,
CONSTRAINT `fk_emails_person` FOREIGN KEY (`person_idperson`) REFERENCES `person` (`idperson`) ON DELETE NO ACTION ON UPDATE NO ACTION
) ENGINE=InnoDB AUTO_INCREMENT=4 DEFAULT CHARSET=utf8;
/*!40101 SET character_set_client = @saved_cs_client */;

--
-- Dumping data for table `emails`
--

LOCK TABLES `emails` WRITE;
/*!40000 ALTER TABLE `emails` DISABLE KEYS */;
INSERT INTO `emails` VALUES (1,'John@Doe.com',1,NULL),(2,'Jane@Doe.com',2,NULL),(3,'Jack@Doe.com',3,NULL);
/*!40000 ALTER TABLE `emails` ENABLE KEYS */;
UNLOCK TABLES;

--
-- Table structure for table `google_accounts`
--

DROP TABLE IF EXISTS `google_accounts`;
/*!40101 SET @saved_cs_client = @@character_set_client */;
/*!40101 SET character_set_client = utf8 */;
CREATE TABLE `google_accounts` (
`idgoogle_accounts` int(11) NOT NULL,
`family_name` varchar(45) DEFAULT NULL,
PRIMARY KEY (`idgoogle_accounts`)
) ENGINE=InnoDB DEFAULT CHARSET=utf8;
/*!40101 SET character_set_client = @saved_cs_client */;

--
-- Dumping data for table `google_accounts`
--

LOCK TABLES `google_accounts` WRITE;
/*!40000 ALTER TABLE `google_accounts` DISABLE KEYS */;
/*!40000 ALTER TABLE `google_accounts` ENABLE KEYS */;
UNLOCK TABLES;

--
-- Table structure for table `person`
--

DROP TABLE IF EXISTS `person`;
/*!40101 SET @saved_cs_client = @@character_set_client */;
/*!40101 SET character_set_client = utf8 */;
CREATE TABLE `person` (
`idperson` int(11) NOT NULL AUTO_INCREMENT,
`first_name` varchar(45) NOT NULL,
`last_name` varchar(45) NOT NULL,
`date_of_birth` date NOT NULL,
`gender` varchar(45) NOT NULL,
PRIMARY KEY (`idperson`)
) ENGINE=InnoDB AUTO_INCREMENT=4 DEFAULT CHARSET=utf8;
/*!40101 SET character_set_client = @saved_cs_client */;

--
-- Dumping data for table `person`
--

LOCK TABLES `person` WRITE;
/*!40000 ALTER TABLE `person` DISABLE KEYS */;
INSERT INTO `person` VALUES (1,'John','Doe','2000-01-01','man'),(2,'Jane','Doe','2000-01-01','woman'),(3,'Jack','Doe','2000-01-01','man');
/*!40000 ALTER TABLE `person` ENABLE KEYS */;
UNLOCK TABLES;

--
-- Dumping events for database 'remote_banking'
--

--
-- Dumping routines for database 'remote_banking'
--
/*!40103 SET TIME_ZONE=@OLD_TIME_ZONE */;

/*!40101 SET SQL_MODE=@OLD_SQL_MODE */;
/*!40014 SET FOREIGN_KEY_CHECKS=@OLD_FOREIGN_KEY_CHECKS */;
/*!40014 SET UNIQUE_CHECKS=@OLD_UNIQUE_CHECKS */;
/*!40101 SET CHARACTER_SET_CLIENT=@OLD_CHARACTER_SET_CLIENT */;
/*!40101 SET CHARACTER_SET_RESULTS=@OLD_CHARACTER_SET_RESULTS */;
/*!40101 SET COLLATION_CONNECTION=@OLD_COLLATION_CONNECTION */;
/*!40111 SET SQL_NOTES=@OLD_SQL_NOTES */;

-- Dump completed on 2018-03-01 20:20:21
Binary file added rdbms_flow/init/mysql/mysql_scheme.mwb
Binary file not shown.
Original file line number Diff line number Diff line change
Expand Up @@ -2,15 +2,14 @@

import org.springframework.boot.Banner;
import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.EnableAutoConfiguration;
import org.springframework.boot.autoconfigure.SpringBootApplication;
import org.springframework.boot.web.support.SpringBootServletInitializer;
import org.springframework.context.annotation.ComponentScan;

@EnableAutoConfiguration
@ComponentScan("com.remote.banking")
@SpringBootApplication(scanBasePackages = {"com.remote.banking"})
public class SpringBootRestApplication extends SpringBootServletInitializer {
public static void main(String[] args) {
SpringApplication application = new SpringApplication(SpringBootRestApplication.class);

public static void main(final String[] args) {
final SpringApplication application = new SpringApplication(SpringBootRestApplication.class);
application.setBannerMode(Banner.Mode.CONSOLE);
application.run(args);
}
Expand Down
69 changes: 69 additions & 0 deletions src/main/java/com/remote/banking/configuration/DBConfig.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,69 @@
package com.remote.banking.configuration;

import org.springframework.beans.factory.annotation.Qualifier;
import org.springframework.boot.autoconfigure.jdbc.DataSourceBuilder;
import org.springframework.boot.orm.jpa.EntityManagerFactoryBuilder;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import org.springframework.context.annotation.Primary;
import org.springframework.orm.jpa.JpaTransactionManager;
import org.springframework.orm.jpa.LocalContainerEntityManagerFactoryBean;
import org.springframework.orm.jpa.vendor.HibernateJpaVendorAdapter;
import org.springframework.transaction.PlatformTransactionManager;

import javax.annotation.Resource;
import javax.persistence.EntityManagerFactory;
import javax.sql.DataSource;
import java.util.Properties;

@Configuration
public class DBConfig {

@Resource(name = "db_properties")
private Properties globalProperties;
@Resource(name = "jpaProperties")
private Properties jpaProperties;

@Primary
@Bean(name = "dataSource")
public DataSource dataSource() {
final String datasource_url = globalProperties.getProperty("datasource.url");
final String driver_class_name = globalProperties.getProperty("datasource.driver-class-name");
final String datasource_username = globalProperties.getProperty("datasource.username");
final String datasource_password = globalProperties.getProperty("datasource.password");

final DataSourceBuilder dataSourceBuilder = DataSourceBuilder.create()
.url(datasource_url)
.driverClassName(driver_class_name)
.password(datasource_password)
.username(datasource_username);

return dataSourceBuilder.build();
}

@Primary
@Bean(name = "entityManagerFactory")
public LocalContainerEntityManagerFactoryBean entityManagerFactory(
EntityManagerFactoryBuilder builder,
@Qualifier("dataSource") DataSource dataSource) {

LocalContainerEntityManagerFactoryBean localContainerEntityManagerFactoryBean = builder
.dataSource(dataSource)
.packages("com.remote.banking.models.for_rdbms")
.persistenceUnit("remote_banking_db_JPAUnit")
.build();
localContainerEntityManagerFactoryBean.setJpaVendorAdapter(new HibernateJpaVendorAdapter());
localContainerEntityManagerFactoryBean.setJpaProperties(jpaProperties);

return localContainerEntityManagerFactoryBean;
}

@Primary
@Bean(name = "transactionManager")
public PlatformTransactionManager transactionManager(
@Qualifier("entityManagerFactory") EntityManagerFactory
entityManagerFactory) {
return new JpaTransactionManager(entityManagerFactory);
}

}
39 changes: 39 additions & 0 deletions src/main/java/com/remote/banking/configuration/PropertyConfig.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,39 @@
package com.remote.banking.configuration;

import org.springframework.beans.factory.config.PropertiesFactoryBean;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import org.springframework.core.io.ClassPathResource;
import org.springframework.validation.beanvalidation.LocalValidatorFactoryBean;

@Configuration
public class PropertyConfig {

@Bean(name = "global_properties")
//use it with @Resource(name = "global_properties") private Properties globalProperties;
public static PropertiesFactoryBean global() {
final PropertiesFactoryBean bean = new PropertiesFactoryBean();
bean.setLocation(new ClassPathResource("application.properties"));
return bean;
}

@Bean(name = "db_properties")
public static PropertiesFactoryBean dbconnections() {
final PropertiesFactoryBean bean = new PropertiesFactoryBean();
bean.setLocation(new ClassPathResource("db_connections.properties"));
return bean;
}

@Bean(name = "jpaProperties")
public static PropertiesFactoryBean jpaProperties() {
final PropertiesFactoryBean bean = new PropertiesFactoryBean();
bean.setLocation(new ClassPathResource("jpa.properties"));
return bean;
}

@Bean(name="validator")
public static LocalValidatorFactoryBean validatorFactoryBean() {
return new LocalValidatorFactoryBean();
}

}
45 changes: 45 additions & 0 deletions src/main/java/com/remote/banking/controllers/UserController.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,45 @@
package com.remote.banking.controllers;

import com.remote.banking.models.dao.UserDAO;
import com.remote.banking.models.for_rdbms.Person;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.http.HttpHeaders;
import org.springframework.http.MediaType;
import org.springframework.http.ResponseEntity;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RestController;

import java.util.List;

@RestController
@RequestMapping("${api.root}")
public class UserController {

@Autowired
private UserDAO userDAO;

@RequestMapping(method = RequestMethod.GET, value = "/users")
public ResponseEntity<String> getUserInfo() {

final List<Person> all = userDAO.findAllPersons();

return ResponseEntity.ok()
.contentType(MediaType.TEXT_PLAIN)
.header(HttpHeaders.CONTENT_DISPOSITION)
.body(all.toString());
}


@RequestMapping(method = RequestMethod.GET, value = "/users/{idperson}")
public ResponseEntity<String> getUserInfo(@PathVariable int idperson) {

final Person byId = userDAO.findById(idperson);

return ResponseEntity.ok()
.contentType(MediaType.TEXT_PLAIN)
.header(HttpHeaders.CONTENT_DISPOSITION)
.body(byId.toString());
}
}
Loading

0 comments on commit 802dee1

Please sign in to comment.