Skip to content

Commit

Permalink
Merge pull request #3 from Realtime-Location-Service/flyway
Browse files Browse the repository at this point in the history
flyway integration
  • Loading branch information
sazid1462 authored Jul 16, 2019
2 parents 571f1c9 + 44107a9 commit a098cad
Show file tree
Hide file tree
Showing 24 changed files with 381 additions and 199 deletions.
4 changes: 2 additions & 2 deletions .idea/modules/rls42.ID-Service.main.iml

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

3 changes: 2 additions & 1 deletion .idea/modules/rls42.ID-Service.test.iml

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

6 changes: 3 additions & 3 deletions build.gradle
Original file line number Diff line number Diff line change
Expand Up @@ -23,7 +23,7 @@ apply plugin: 'com.palantir.docker'

bootJar {
baseName = 'id-service'
version = '0.1.0'
version = '1.0.0'
}

repositories {
Expand Down Expand Up @@ -60,11 +60,11 @@ dependencies {
// Use MySQL Connector-J
compile 'mysql:mysql-connector-java'

compile "org.flywaydb:flyway-core:5.2.4"

implementation("org.springframework.data:spring-data-envers")
implementation("com.fasterxml.jackson.core:jackson-annotations:2.9.7")
implementation("com.fasterxml.jackson.datatype:jackson-datatype-hibernate5:2.9.7")

developmentOnly("org.springframework.boot:spring-boot-devtools")

testCompile("junit:junit")
testCompile("org.springframework.boot:spring-boot-starter-test")
Expand Down
19 changes: 17 additions & 2 deletions docker-compose.yml
Original file line number Diff line number Diff line change
Expand Up @@ -7,8 +7,23 @@ services:
- "./db_data:/var/lib/mysql"
environment:
- "MYSQL_ROOT_PASSWORD=root"
- "MYSQL_DATABASE=rlsstore"
- "MYSQL_USER=rls"
- "MYSQL_DATABASE=ids_db"
- "MYSQL_USER=ids_user"
- "MYSQL_PASSWORD=12345678"
ports:
- "3306:3306"

id_service:
container_name: id_service
image: rls42/id-service:1.0.0
volumes:
- "./initdb.sql:/app/initdb.sql"
- - "./logs:/app/lib/logs"
environment:
- "SERVER_PORT=7001"
- "SPRING_PROFILES_ACTIVE=dev"
- "SPRING_DATASOURCE_URL=jdbc:mysql://localhost:3306/ids_db"
- "SPRING_DATASOURCE_USERNAME=ids_user"
- "SPRING_DATASOURCE_PASSWORD=12345678"
ports:
- "7001:7001"
47 changes: 28 additions & 19 deletions src/main/java/com/rls/ids/controllers/AppController.java
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,7 @@
import com.rls.ids.exceptions.MissingRequestParameterException;
import com.rls.ids.models.CompanyResponseModel;
import com.rls.ids.models.SignUpRequestModel;
import com.rls.ids.models.UserSignUpResponseModel;
import com.rls.ids.models.UserResolveResponseModel;
import com.rls.ids.repositories.CompanyRepository;
import com.rls.ids.repositories.UserRepository;
import org.springframework.beans.factory.annotation.Autowired;
Expand Down Expand Up @@ -45,46 +45,55 @@ public ResponseEntity<CompanyResponseModel> companySignUp(@Valid @RequestBody Co
if (isInvalid(secretKey) && !secretKey.equals(this.secretKey))
throw new MissingHeaderException(secretKey + " is missing or invalid.");

companyRepository.save(company);
company = companyRepository.saveAndFlush(company);
// Retrieve the saved company
// company = companyRepository.getCompanyByCid(company.getCid());

KeyGenerator generator = KeyGenerator.getInstance("AES");
generator.init(128); // The AES key size in number of bits

User admin = new User(UUID.randomUUID().toString(), company.getCid(), Role.ADMIN);
User admin = new User(UUID.randomUUID().toString(), company.getId(), Role.ADMIN);
admin.setAppKey(Base64.getEncoder().encodeToString(generator.generateKey().getEncoded()));
userRepository.save(admin);
admin = userRepository.save(admin);

CompanyResponseModel companyResponseModel = new CompanyResponseModel(company.getDomain(), company.getCid(), admin);
CompanyResponseModel companyResponseModel = new CompanyResponseModel(company, admin);
return new ResponseEntity<>(companyResponseModel, HttpStatus.OK);
}

@RequestMapping(path="user/signup", method = RequestMethod.POST, consumes = MediaType.APPLICATION_JSON_VALUE) // Map ONLY POST Requests
public ResponseEntity<User> userSignUp(@Valid @RequestBody SignUpRequestModel signUpModel) {
User admin = userRepository.getUserByAppKey(signUpModel.getAppKey());
public ResponseEntity<User> userSignUp(@Valid @RequestBody SignUpRequestModel signUpModel, @RequestHeader("AppKey") String appKey) {
if (appKey.isBlank()) {
appKey = signUpModel.getAppKey();
}
User admin = userRepository.getUserByAppKey(appKey);

if (admin == null)
throw new InvalidAppKeyException(signUpModel.getAppKey()+" is invalid!");

User user = new User(signUpModel.getUserId(), admin.getCid(), Role.USER);
userRepository.save(user);
User user = new User(signUpModel.getUserId(), admin.getCompanyId(), Role.USER);
user = userRepository.saveAndFlush(user);

return new ResponseEntity<>(user, HttpStatus.OK);
}

@RequestMapping(path="user/resolve", method = RequestMethod.GET) // Map ONLY POST Requests
public ResponseEntity<UserSignUpResponseModel> resolveUser(@RequestParam String appKey) {
public ResponseEntity<UserResolveResponseModel> resolveUser(@RequestHeader("AppKey") String appKey) {
if (isInvalid(appKey))
throw new MissingRequestParameterException(appKey + " is missing or invalid.");

User admin = userRepository.getUserByAppKey(appKey);

if (admin == null)
throw new InvalidAppKeyException(appKey+" is invalid!");

UserSignUpResponseModel model = new UserSignUpResponseModel(admin.getUserId(), admin.getRole(),
admin.getAppKey(), admin.getCid());
model.setSubordinates(userRepository.findUsersByCidAndRole(admin.getCid(), Role.USER));
return new ResponseEntity<>(model, HttpStatus.OK);
try {
// List adminList = userRepository.getUserWithCompanyByAppKey(appKey);
// List<java.util.Map.Entry> admin = (List<java.util.Map.Entry>) adminList.get(0);
User admin = userRepository.getUserByAppKey(appKey);
Company company = companyRepository.getCompanyById(admin.getCompanyId());

UserResolveResponseModel model = new UserResolveResponseModel(admin.getUserId(), admin.getRole(),
company.getName(), company.getDomain());
model.setSubordinates(userRepository.findUsersByCompanyIdAndRole(company.getId(), Role.USER));
return new ResponseEntity<>(model, HttpStatus.OK);
} catch (Exception e) {
throw new InvalidAppKeyException(appKey+" is invalid!" + e.toString());
}
}

private boolean isInvalid(String value) {
Expand Down
14 changes: 10 additions & 4 deletions src/main/java/com/rls/ids/entities/BaseEntity.java
Original file line number Diff line number Diff line change
@@ -1,20 +1,26 @@
package com.rls.ids.entities;

import com.fasterxml.jackson.databind.PropertyNamingStrategy;
import com.fasterxml.jackson.databind.annotation.JsonNaming;
import org.hibernate.annotations.CreationTimestamp;
import org.hibernate.annotations.UpdateTimestamp;
import org.springframework.data.annotation.CreatedDate;
import org.springframework.data.annotation.LastModifiedDate;
import org.springframework.data.jpa.domain.support.AuditingEntityListener;

import javax.persistence.*;
import java.sql.Date;
import javax.persistence.EntityListeners;
import javax.persistence.MappedSuperclass;
import javax.persistence.Temporal;
import javax.persistence.TemporalType;
import java.util.Date;

@MappedSuperclass
@EntityListeners({AuditingEntityListener.class})
@JsonNaming(PropertyNamingStrategy.SnakeCaseStrategy.class)
abstract class BaseEntity {
@CreationTimestamp
@Temporal(TemporalType.TIMESTAMP)
private Date createdAt;
@UpdateTimestamp
@Temporal(TemporalType.TIMESTAMP)
private Date updatedAt;

public Date getCreatedAt() {
Expand Down
26 changes: 13 additions & 13 deletions src/main/java/com/rls/ids/entities/Company.java
Original file line number Diff line number Diff line change
@@ -1,30 +1,30 @@
package com.rls.ids.entities;

import com.fasterxml.jackson.databind.PropertyNamingStrategy;
import com.fasterxml.jackson.databind.annotation.JsonNaming;
import jdk.jfr.Timestamp;
import org.hibernate.annotations.GenericGenerator;
import org.springframework.validation.annotation.Validated;

import javax.persistence.Column;
import javax.persistence.Entity;
import javax.persistence.GeneratedValue;
import javax.persistence.Id;
import javax.persistence.*;
import javax.validation.constraints.NotBlank;
import java.sql.Date;
import java.util.Date;

@Entity
@Validated
@JsonNaming(PropertyNamingStrategy.SnakeCaseStrategy.class)
public class Company extends BaseEntity {
@Id
@GeneratedValue(generator = "uuid")
@GenericGenerator(name = "uuid", strategy = "uuid2")
private String cid;
@Column(name = "id")
@GeneratedValue(strategy = GenerationType.IDENTITY)
private int id;

@Column(unique = true, nullable = false)
@NotBlank
private String domain;

private String name;
@Timestamp
@Temporal(TemporalType.TIMESTAMP)
private Date deletedAt;

public String getDomain() {
Expand All @@ -51,11 +51,11 @@ public void setDeletedAt(Date deletedAt) {
this.deletedAt = deletedAt;
}

public String getCid() {
return cid;
public int getId() {
return id;
}

public void setCid(String cid) {
this.cid = cid;
public void setId(int id) {
this.id = id;
}
}
53 changes: 21 additions & 32 deletions src/main/java/com/rls/ids/entities/User.java
Original file line number Diff line number Diff line change
@@ -1,19 +1,20 @@
package com.rls.ids.entities;

import com.fasterxml.jackson.databind.PropertyNamingStrategy;
import com.fasterxml.jackson.databind.annotation.JsonNaming;
import org.springframework.validation.annotation.Validated;

import javax.persistence.Column;
import javax.persistence.Entity;
import javax.persistence.Id;
import javax.persistence.IdClass;
import javax.persistence.*;
import javax.validation.constraints.NotNull;
import java.io.Serializable;

@Entity // This tells Hibernate to make a table out of this class
@Validated
@IdClass(User.CompositeKey.class)
@JsonNaming(PropertyNamingStrategy.SnakeCaseStrategy.class)
public class User extends BaseEntity{
@Id
@GeneratedValue(strategy = GenerationType.IDENTITY)
private int id;

@NotNull
@Column(name = "user_id")
private String userId;
Expand All @@ -23,14 +24,15 @@ public class User extends BaseEntity{
@Column(name = "app_key")
private String appKey;

@Id
@NotNull
private String cid;
@Column(name = "company_id")
@MapKey
private int companyId;

public User() {}
public User(String userId, String cid, String role) {
public User(String userId, int cid, String role) {
this.userId = userId;
this.cid = cid;
this.companyId = cid;
this.role = role;
}

Expand Down Expand Up @@ -58,32 +60,19 @@ public void setAppKey(String appKey) {
this.appKey = appKey;
}

public String getCid() {
return cid;
public int getCompanyId() {
return companyId;
}

public void setCid(String cid) {
this.cid = cid;
public void setCompanyId(int cid) {
this.companyId = cid;
}

static class CompositeKey implements Serializable {
private String cid;
private String userId;

public String getCid() {
return cid;
}

public void setCid(String cid) {
this.cid = cid;
}

public String getUserId() {
return userId;
}
public int getId() {
return id;
}

public void setUserId(String userId) {
this.userId = userId;
}
public void setId(int id) {
this.id = id;
}
}
45 changes: 45 additions & 0 deletions src/main/java/com/rls/ids/entities/UserWithCompany.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,45 @@
package com.rls.ids.entities;

import com.fasterxml.jackson.databind.PropertyNamingStrategy;
import com.fasterxml.jackson.databind.annotation.JsonNaming;

@JsonNaming(PropertyNamingStrategy.SnakeCaseStrategy.class)
public class UserWithCompany {
private String userId;
private String role;

private String domain;
private String companyName;

public String getUserId() {
return userId;
}

public void setUserId(String userId) {
this.userId = userId;
}

public String getRole() {
return role;
}

public void setRole(String role) {
this.role = role;
}

public String getDomain() {
return domain;
}

public void setDomain(String domain) {
this.domain = domain;
}

public String getCompanyName() {
return companyName;
}

public void setCompanyName(String companyName) {
this.companyName = companyName;
}
}
Loading

0 comments on commit a098cad

Please sign in to comment.