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
16 changes: 16 additions & 0 deletions build.gradle
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@ plugins {
id 'org.springframework.boot' version '2.2.3.RELEASE'
id 'io.spring.dependency-management' version '1.0.8.RELEASE'
id 'java'
id 'org.flywaydb.flyway' version '6.2.3'
}

sourceCompatibility = '11'
Expand All @@ -17,6 +18,21 @@ dependencies {
implementation 'org.springframework.boot:spring-boot-starter-web'
implementation 'org.springframework.boot:spring-boot-starter-jdbc'

implementation 'org.flywaydb:flyway-core'
runtimeOnly 'com.h2database:h2:1.4.200'

testCompile 'org.assertj:assertj-core'
testImplementation('org.springframework.boot:spring-boot-starter-test')
testImplementation('org.junit.jupiter:junit-jupiter:5.5.1')
}

test {
useJUnitPlatform()
}

flyway {
url = 'jdbc:h2:mem:'
locations = [
'classpath:db/migration'
]
}
10 changes: 10 additions & 0 deletions src/main/java/com/afli/persistence/user/dao/AfliEmployeeDao.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,10 @@
package com.afli.persistence.user.dao;

import com.afli.persistence.user.entity.AfliEmployee;

import java.util.List;

public interface AfliEmployeeDao {

List<AfliEmployee> findAllEmployees();
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,39 @@
package com.afli.persistence.user.dao.impl;

import com.afli.persistence.user.dao.AfliEmployeeDao;
import com.afli.persistence.user.entity.AfliEmployee;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.jdbc.core.JdbcTemplate;
import org.springframework.stereotype.Repository;

import java.util.List;

@Repository
public class AfliJdbcEmployeeDao implements AfliEmployeeDao {

private static final String FIND_ALL_EMPLOYEES = "" +
"SELECT u.id u_id, u.username u_username, u.first_name u_first_name, u.last_name u_last_name, e.title e_title, c.name c_name " +
"FROM users u " +
"JOIN employees e ON u.id = e.user_id " +
"JOIN companies c ON c.id = e.company_id";

private final JdbcTemplate jdbcTemplate;

@Autowired
public AfliJdbcEmployeeDao(JdbcTemplate jdbcTemplate) {
this.jdbcTemplate = jdbcTemplate;
}

@Override
public List<AfliEmployee> findAllEmployees() {
return jdbcTemplate
.query(FIND_ALL_EMPLOYEES, (rs, rowNum) -> new AfliEmployee()
.setId(rs.getLong("u_id"))
.setUsername(rs.getString("u_username"))
.setFirstName(rs.getString("u_first_name"))
.setLastName(rs.getString("u_last_name"))
.setTitle(rs.getString("e_title"))
.setCompany(rs.getString("c_name"))
);
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,33 @@
package com.afli.persistence.user.dao.impl;

import com.afli.persistence.user.dao.AfliUserDao;
import com.afli.persistence.user.entity.AfliUser;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Qualifier;
import org.springframework.jdbc.core.JdbcTemplate;
import org.springframework.stereotype.Repository;

import java.util.List;

@Repository
@Qualifier("afliJdbcUserDao")
public class AfliJdbcUserDao implements AfliUserDao {

private final JdbcTemplate jdbcTemplate;

@Autowired
public AfliJdbcUserDao(JdbcTemplate jdbcTemplate) {
this.jdbcTemplate = jdbcTemplate;
}

@Override
public List<AfliUser> findAllUsers() {
return jdbcTemplate
.query("SELECT id, username, first_name, last_name FROM users", (rs, rowNum) -> new AfliUser()
.setId(rs.getLong("id"))
.setUsername(rs.getString("username"))
.setFirstName(rs.getString("first_name"))
.setLastName(rs.getString("last_name"))
);
}
}
38 changes: 38 additions & 0 deletions src/main/java/com/afli/persistence/user/entity/AfliEmployee.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,38 @@
package com.afli.persistence.user.entity;

import lombok.Getter;
import lombok.Setter;
import lombok.experimental.Accessors;

@Getter
@Setter
@Accessors(chain = true)
public class AfliEmployee extends AfliUser {

private String title;
private String company;

@Override
public AfliEmployee setId(Long id) {
super.setId(id);
return this;
}

@Override
public AfliEmployee setUsername(String username) {
super.setUsername(username);
return this;
}

@Override
public AfliEmployee setFirstName(String firstName) {
super.setFirstName(firstName);
return this;
}

@Override
public AfliEmployee setLastName(String lastName) {
super.setLastName(lastName);
return this;
}
}
Original file line number Diff line number Diff line change
@@ -1,8 +1,11 @@
package com.afli.presentation.user.controller;

import com.afli.persistence.user.dao.AfliEmployeeDao;
import com.afli.persistence.user.dao.AfliUserDao;
import com.afli.persistence.user.entity.AfliEmployee;
import com.afli.persistence.user.entity.AfliUser;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Qualifier;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RestController;

Expand All @@ -14,15 +17,24 @@
public class AfliUserController {

private final AfliUserDao afliUserDao;
private final AfliEmployeeDao afliEmployeeDao;

@Autowired
public AfliUserController(AfliUserDao afliUserDao) {
public AfliUserController(@Qualifier("afliJdbcUserDao") AfliUserDao afliUserDao,
AfliEmployeeDao afliEmployeeDao) {
this.afliUserDao = afliUserDao;
this.afliEmployeeDao = afliEmployeeDao;
}

@GetMapping("/api/v1/users")
public Map<String, List<AfliUser>> getAllUsers() {
List<AfliUser> users = afliUserDao.findAllUsers();
return Collections.singletonMap("users", users);
}

@GetMapping("/api/v1/employees")
public Map<String, List<AfliEmployee>> getAllEmployees() {
List<AfliEmployee> employees = afliEmployeeDao.findAllEmployees();
return Collections.singletonMap("employees", employees);
}
}
1 change: 1 addition & 0 deletions src/main/resources/application.properties
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
logging.level.org.flywaydb=debug
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
DROP TABLE users;
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
DROP TABLE employees;
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
DROP TABLE companies;
6 changes: 6 additions & 0 deletions src/main/resources/db/migration/V1__init_users_table.sql
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
CREATE TABLE users(
id INT AUTO_INCREMENT PRIMARY KEY,
username VARCHAR(255) NOT NULL UNIQUE,
first_name VARCHAR(255) NOT NULL,
last_name VARCHAR(255) NOT NULL
);
8 changes: 8 additions & 0 deletions src/main/resources/db/migration/V2__insert_users.sql
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
INSERT INTO users
(username, first_name, last_name)
VALUES
('john.doe@gmail.com', 'John', 'Doe'),
('spider.man@gmail.com', 'Peter', 'Parker'),
('il.gusevskiy@gmail.com', 'Afli', 'Legend'),
('bot_1@gmail.com', 'Dummy_firstname', 'Dummy_lastname'),
('bot_2@gmail.com', 'Dummier_firstname', 'Dummier_lastname');
Original file line number Diff line number Diff line change
@@ -0,0 +1,4 @@
CREATE TABLE companies(
id INT AUTO_INCREMENT PRIMARY KEY,
name VARCHAR(255) NOT NULL UNIQUE
);
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
CREATE TABLE employees(
title VARCHAR(255) NOT NULL,
user_id INT NOT NULL UNIQUE,
company_id INT NOT NULL,
FOREIGN KEY (user_id) REFERENCES users(id),
FOREIGN KEY (company_id) REFERENCES companies(id)
);
5 changes: 5 additions & 0 deletions src/main/resources/db/migration/V3.3__insert_companies.sql
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
INSERT INTO companies
(name)
VALUES
('google'),
('e-corp');
8 changes: 8 additions & 0 deletions src/main/resources/db/migration/V3.4__insert_employees.sql
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
INSERT INTO employees
(title, user_id, company_id)
VALUES
('software engineer', (SELECT id FROM users WHERE username = 'john.doe@gmail.com'), (SELECT id FROM companies WHERE name = 'e-corp')),
('courier', (SELECT id FROM users WHERE username = 'spider.man@gmail.com'), (SELECT id FROM companies WHERE name = 'e-corp')),
('CEO', (SELECT id FROM users WHERE username = 'il.gusevskiy@gmail.com'), (SELECT id FROM companies WHERE name = 'e-corp')),
('junior software engineer', (SELECT id FROM users WHERE username = 'bot_1@gmail.com'), (SELECT id FROM companies WHERE name = 'google')),
('junior software engineer', (SELECT id FROM users WHERE username = 'bot_2@gmail.com'), (SELECT id FROM companies WHERE name = 'google'));
Original file line number Diff line number Diff line change
@@ -0,0 +1,69 @@
package com.afli.database.migration;

import com.afli.persistence.user.entity.AfliEmployee;
import org.assertj.core.api.Assertions;
import org.assertj.core.groups.Tuple;
import org.junit.jupiter.api.Test;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.boot.test.autoconfigure.orm.jpa.DataJpaTest;
import org.springframework.jdbc.core.JdbcTemplate;

import java.util.List;

@DataJpaTest
public class AfliFlywayEmployeeTest {

private static final String FIND_ALL_COMPANIES = "" +
"SELECT name " +
"FROM companies";

private static final String FIND_ALL_EMPLOYEES = "" +
"SELECT u.id u_id, u.username u_username, u.first_name u_first_name, u.last_name u_last_name, e.title e_title, c.name c_name " +
"FROM users u " +
"JOIN employees e ON u.id = e.user_id " +
"JOIN companies c ON c.id = e.company_id";

@Autowired
private JdbcTemplate jdbcTemplate;

@Test
void companiesTableIsInitializedProperly() {
// when
List<String> companies =
jdbcTemplate.query(FIND_ALL_COMPANIES, (rs, rowNum) -> rs.getString("name"));

// then
Assertions.assertThat(companies)
.containsExactlyInAnyOrder(
"google",
"e-corp"
);
}

@Test
void employeesTableIsInitializedProperly() {
// when
List<AfliEmployee> employees =
jdbcTemplate.query(FIND_ALL_EMPLOYEES, (rs, rowNum) ->
new AfliEmployee()
.setUsername(rs.getString("u_username"))
.setFirstName(rs.getString("u_first_name"))
.setLastName(rs.getString("u_last_name"))
.setTitle(rs.getString("e_title"))
.setCompany(rs.getString("c_name"))
);

// then
Assertions.assertThat(employees)
.extracting(
AfliEmployee::getUsername, AfliEmployee::getFirstName, AfliEmployee::getLastName,
AfliEmployee::getTitle, AfliEmployee::getCompany)
.containsExactlyInAnyOrder(
Tuple.tuple("john.doe@gmail.com", "John", "Doe", "software engineer", "e-corp"),
Tuple.tuple("spider.man@gmail.com", "Peter", "Parker", "courier", "e-corp"),
Tuple.tuple("il.gusevskiy@gmail.com", "Afli", "Legend", "CEO", "e-corp"),
Tuple.tuple("bot_1@gmail.com", "Dummy_firstname", "Dummy_lastname", "junior software engineer", "google"),
Tuple.tuple("bot_2@gmail.com", "Dummier_firstname", "Dummier_lastname", "junior software engineer", "google")
);
}
}