Skip to content

Commit

Permalink
snake case json model. AppKey in header
Browse files Browse the repository at this point in the history
  • Loading branch information
sazid1462 committed Jun 25, 2019
1 parent f87ec91 commit a3c73a7
Show file tree
Hide file tree
Showing 15 changed files with 207 additions and 127 deletions.
36 changes: 21 additions & 15 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,7 +45,6 @@ public ResponseEntity<CompanyResponseModel> companySignUp(@Valid @RequestBody Co
if (isInvalid(secretKey) && !secretKey.equals(this.secretKey))
throw new MissingHeaderException(secretKey + " is missing or invalid.");

company.setCid(UUID.randomUUID().toString());
company = companyRepository.saveAndFlush(company);
// Retrieve the saved company
// company = companyRepository.getCompanyByCid(company.getCid());
Expand All @@ -57,13 +56,16 @@ public ResponseEntity<CompanyResponseModel> companySignUp(@Valid @RequestBody Co
admin.setAppKey(Base64.getEncoder().encodeToString(generator.generateKey().getEncoded()));
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!");
Expand All @@ -75,19 +77,23 @@ public ResponseEntity<User> userSignUp(@Valid @RequestBody SignUpRequestModel si
}

@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.getCompanyId());
model.setSubordinates(userRepository.findUsersByCompanyIdAndRole(admin.getCompanyId(), 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
3 changes: 3 additions & 0 deletions src/main/java/com/rls/ids/entities/BaseEntity.java
Original file line number Diff line number Diff line change
@@ -1,5 +1,7 @@
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.jpa.domain.support.AuditingEntityListener;
Expand All @@ -12,6 +14,7 @@

@MappedSuperclass
@EntityListeners({AuditingEntityListener.class})
@JsonNaming(PropertyNamingStrategy.SnakeCaseStrategy.class)
abstract class BaseEntity {
@CreationTimestamp
@Temporal(TemporalType.TIMESTAMP)
Expand Down
13 changes: 3 additions & 10 deletions src/main/java/com/rls/ids/entities/Company.java
Original file line number Diff line number Diff line change
@@ -1,5 +1,7 @@
package com.rls.ids.entities;

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

Expand All @@ -9,14 +11,13 @@

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

private String cid;

@Column(unique = true, nullable = false)
@NotBlank
private String domain;
Expand Down Expand Up @@ -50,14 +51,6 @@ public void setDeletedAt(Date deletedAt) {
this.deletedAt = deletedAt;
}

public String getCid() {
return cid;
}

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

public int getId() {
return id;
}
Expand Down
4 changes: 4 additions & 0 deletions src/main/java/com/rls/ids/entities/User.java
Original file line number Diff line number Diff line change
@@ -1,12 +1,15 @@
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.*;
import javax.validation.constraints.NotNull;

@Entity // This tells Hibernate to make a table out of this class
@Validated
@JsonNaming(PropertyNamingStrategy.SnakeCaseStrategy.class)
public class User extends BaseEntity{
@Id
@GeneratedValue(strategy = GenerationType.IDENTITY)
Expand All @@ -23,6 +26,7 @@ public class User extends BaseEntity{

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

public User() {}
Expand Down
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;
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -42,4 +42,9 @@ public ResponseEntity<Object> exception(UnsupportedPayloadTypeException exceptio
public ResponseEntity<Object> exception(UnsupportedSearchParameter exception) {
return new ResponseEntity<>(exception.getMessage(), HttpStatus.BAD_REQUEST);
}

@ExceptionHandler(value = InvalidAppKeyException.class)
public ResponseEntity<Object> exception(InvalidAppKeyException exception) {
return new ResponseEntity<>(exception.getMessage(), HttpStatus.BAD_REQUEST);
}
}
27 changes: 16 additions & 11 deletions src/main/java/com/rls/ids/models/CompanyResponseModel.java
Original file line number Diff line number Diff line change
@@ -1,28 +1,25 @@
package com.rls.ids.models;

import com.fasterxml.jackson.databind.PropertyNamingStrategy;
import com.fasterxml.jackson.databind.annotation.JsonNaming;
import com.rls.ids.entities.Company;
import com.rls.ids.entities.User;

@JsonNaming(PropertyNamingStrategy.SnakeCaseStrategy.class)
public class CompanyResponseModel {
private String domain;
private String companyId;
private String companyName;
private User admin;

public CompanyResponseModel() { }

public CompanyResponseModel(String domain, String companyId, User admin) {
this.domain = domain;
this.companyId = companyId;
public CompanyResponseModel(Company company, User admin) {
this.domain = company.getDomain();
this.companyName = company.getName();
this.admin = admin;
}

public String getCompanyId() {
return companyId;
}

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

public String getDomain() {
return domain;
}
Expand All @@ -38,4 +35,12 @@ public User getAdmin() {
public void setAdmin(User admin) {
this.admin = admin;
}

public String getCompanyName() {
return companyName;
}

public void setCompanyName(String companyName) {
this.companyName = companyName;
}
}
5 changes: 4 additions & 1 deletion src/main/java/com/rls/ids/models/SignUpRequestModel.java
Original file line number Diff line number Diff line change
@@ -1,9 +1,12 @@
package com.rls.ids.models;

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

import javax.validation.constraints.NotBlank;

@JsonNaming(PropertyNamingStrategy.SnakeCaseStrategy.class)
public class SignUpRequestModel {
@NotBlank
private String appKey;
@NotBlank
private String userId;
Expand Down
19 changes: 0 additions & 19 deletions src/main/java/com/rls/ids/models/SubordinateModel.java

This file was deleted.

93 changes: 93 additions & 0 deletions src/main/java/com/rls/ids/models/UserResolveResponseModel.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,93 @@
package com.rls.ids.models;

import com.fasterxml.jackson.databind.PropertyNamingStrategy;
import com.fasterxml.jackson.databind.annotation.JsonNaming;
import com.rls.ids.entities.User;

import java.util.LinkedList;
import java.util.List;
import java.util.Map;

@JsonNaming(PropertyNamingStrategy.SnakeCaseStrategy.class)
public class UserResolveResponseModel {
private String companyName;
private int companyId;
private String domain;
private String userId;
private String role;
private List<User> subordinates;

public UserResolveResponseModel(){}

public UserResolveResponseModel(List<java.util.Map.Entry> adminWithCompany) {
for (Map.Entry entry: adminWithCompany){
if ("company_name".equals(entry.getKey())) {
companyName = (String) entry.getValue();
} else if ("domain".equals(entry.getKey())) {
domain = (String) entry.getValue();
} else if ("user_id".equals(entry.getKey())) {
userId = (String) entry.getValue();
} else if ("role".equals(entry.getKey())) {
role = (String) entry.getValue();
} else if ("company_id".equals(entry.getKey())) {
companyId = (int) entry.getValue();
}
}
}

public UserResolveResponseModel(String userId, String role, String companyName, String domain) {
this.userId = userId;
this.role = role;
this.companyName = companyName;
this.domain = domain;
}

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 List<User> getSubordinates() {
return subordinates;
}

public void setSubordinates(List<User> subordinates) {
this.subordinates = new LinkedList<>();
this.subordinates.addAll(subordinates);
}

public String getCompanyName() {
return companyName;
}

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

public String getDomain() {
return domain;
}

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

public int getCompanyId() {
return companyId;
}

public void setCompanyId(int companyId) {
this.companyId = companyId;
}
}
Loading

0 comments on commit a3c73a7

Please sign in to comment.