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
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,8 @@

import com.employee_wise.assignment.entity.Employee;

// TODO this can be done via something called Server side templating engine
//
public class EmailConstants {

public static String getAdditionMessage(String manName, Employee emp) {
Expand Down
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
package com.employee_wise.assignment.constants;

// TODO this entire class is useless
public class EndPoint {

public static final String EMPLOYEE = "/employee";
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -14,8 +14,8 @@ public enum ErrorCodeEnum {
MAIL_NOT_SENT("10008","Failed Request, Mail could not be sent to the Reciepent");


private String errorCode;
private String errorMessage;
private final String errorCode;
private final String errorMessage;

private ErrorCodeEnum(String errorCode, String errorMessage) {
this.errorCode=errorCode;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -37,14 +37,14 @@ public class Controller {
public ResponseEntity<PostEmployeeResponse> addEmployee(@RequestBody Employee emp){

PostEmployeeResponse savedEmp = this.empSerImp.addEmployee(emp);
return new ResponseEntity<PostEmployeeResponse>(savedEmp,HttpStatus.CREATED);
return new ResponseEntity<>(savedEmp,HttpStatus.CREATED);
}

@GetMapping(EndPoint.GET_EMPLOYEE_BY_ID+ "/{id}")
public ResponseEntity<Employee> getEmployeeById(@PathVariable("id") String id){

Employee returnedEmp = this.empSerImp.getEmployeeById(id);
return new ResponseEntity<Employee>(returnedEmp,HttpStatus.FOUND);
return new ResponseEntity<>(returnedEmp,HttpStatus.FOUND);
}

@GetMapping(EndPoint.GET_ALL_EMPLOYEE)
Expand All @@ -55,13 +55,13 @@ public ResponseEntity<List<Employee>> getAllEmployee(
){

List<Employee> returnedEmp = this.empSerImp.getAllEmployee(pageNumber, pageSize, sortBy);
return new ResponseEntity<List<Employee>>(returnedEmp,HttpStatus.FOUND);
return new ResponseEntity<>(returnedEmp,HttpStatus.FOUND);
}

@PutMapping(EndPoint.UPDATE_EMPLOYEE_BY_ID + "/{id}")
public ResponseEntity<PostEmployeeResponse> updateEmployeeById(@PathVariable("id") String id, @RequestBody Employee emp){
PostEmployeeResponse updatedEmp = this.empSerImp.updateEmployeeById(id, emp);
return new ResponseEntity<PostEmployeeResponse>(updatedEmp,HttpStatus.OK);
return new ResponseEntity<>(updatedEmp,HttpStatus.OK);
}

@DeleteMapping(EndPoint.DELETE_EMPLOYEE_BY_ID+"/{id}")
Expand All @@ -75,21 +75,21 @@ public ResponseEntity<Employee> deleteEmployeeById(@PathVariable String id){
@GetMapping(EndPoint.GET_MANAGER_BY_NTH_LEVEL+ "/{id}" + "/{n}")
public ResponseEntity<PostEmployeeResponse> getNthManager(@PathVariable("id") String id, @PathVariable("n") Integer n){
PostEmployeeResponse Emp = this.empSerImp.getNthManager(id, n);
return new ResponseEntity<PostEmployeeResponse>(Emp,HttpStatus.OK);
return new ResponseEntity<>(Emp,HttpStatus.OK);
}

@PostMapping(EndPoint.SEND_SIMPLE_MAIL)
public ResponseEntity<EmailResponse> sendMail(@RequestBody EmailDetails details)
{
EmailResponse status = emailService.sendSimpleMail(details);
return new ResponseEntity<EmailResponse>(status,HttpStatus.OK);
return new ResponseEntity<>(status,HttpStatus.OK);
}

@PostMapping(EndPoint.SEND_MAIL_WITH_ATTACHMENT)
public ResponseEntity<EmailResponse> sendMailWithAttachment(@RequestBody EmailDetails details)
{
EmailResponse status = emailService.sendMailWithAttachment(details);
return new ResponseEntity<EmailResponse>(status,HttpStatus.OK);
return new ResponseEntity<>(status,HttpStatus.OK);
}

}
Original file line number Diff line number Diff line change
@@ -1,16 +1,8 @@
package com.employee_wise.assignment.entity;

import lombok.AllArgsConstructor;
import lombok.Data;
import lombok.NoArgsConstructor;

@Data
@AllArgsConstructor
@NoArgsConstructor
public class EmailDetails {

private String recipient;
private String msgBody;
private String subject;
private String attachment;
}
public record EmailDetails (
String recipient,
String msgBody,
String subject,
String attachment
) implements NonNullCopier<EmailDetails>{}
Original file line number Diff line number Diff line change
Expand Up @@ -12,14 +12,14 @@
import lombok.NoArgsConstructor;
import lombok.Setter;


// TODO migrate this to Record too - Java 17 Records
@Data
@NoArgsConstructor
@AllArgsConstructor
@Getter
@Setter
@Document
public class Employee {
public class Employee implements NonNullCopier<Employee> {

@Id
@Indexed(unique = true)
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,21 @@
package com.employee_wise.assignment.entity;

import java.lang.reflect.Field;
import java.util.Arrays;

public interface NonNullCopier<T> {

default void autoCopy(T src){
if ( getClass() != src.getClass() ) return;
Field[] fields = getClass().getDeclaredFields();
Arrays.stream(fields).forEach( f -> {
try {
Object val = f.get(src) ;
if ( val != null ){
f.set(this, val);
}
} catch (IllegalAccessException ignored) {
}
});
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@
import lombok.Getter;
import lombok.Setter;

// TODO migrate to record
@Getter
@Setter
@AllArgsConstructor
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,8 @@
import lombok.Data;
import lombok.NoArgsConstructor;

// TODO migrate to record

@Data
@Builder
@AllArgsConstructor
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,10 @@

import com.employee_wise.assignment.entity.Employee;

// TODO are we using MongoDB? If not, why we are using MongoRepo?
// import org.springframework.data.jpa.repository.JpaRepository;
// import org.springframework.stereotype.Repository;
// https://hevodata.com/learn/spring-boot-mysql/
@Repository
public interface EmployeeRepo extends MongoRepository<Employee , UUID>{

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,8 @@
import lombok.NoArgsConstructor;
import lombok.Setter;

// TODO migrate to Java 17 Record
// Also probably not needed
@Data
@Getter
@Setter
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,8 @@
import lombok.NoArgsConstructor;
import lombok.Setter;

// TODO migrate to Java 17 Record
// Not required,
@Data
@Getter
@Setter
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -10,10 +10,13 @@
@Service
public interface EmployeeService {

public PostEmployeeResponse addEmployee(Employee emp);
public List<Employee> getAllEmployee(Integer pageNumber, Integer pageSize, String sortBy);
public Employee getEmployeeById(String Id);
public PostEmployeeResponse updateEmployeeById(String id,Employee emp);
public Employee deleteEmployeeById(String id);
public PostEmployeeResponse getNthManager(String id, Integer n);
// TODO, refactor to return the new created employee id
PostEmployeeResponse addEmployee(Employee emp);
List<Employee> getAllEmployee(Integer pageNumber, Integer pageSize, String sortBy);
Employee getEmployeeById(String Id);
PostEmployeeResponse updateEmployeeById(String id,Employee emp);
Employee deleteEmployeeById(String id);

// TODO refactor to return the Employee, not this
PostEmployeeResponse getNthManager(String id, Integer n);
}
Original file line number Diff line number Diff line change
Expand Up @@ -34,12 +34,12 @@ public EmailResponse sendSimpleMail(EmailDetails details)
SimpleMailMessage mailMessage = new SimpleMailMessage();

mailMessage.setFrom(sender);
mailMessage.setTo(details.getRecipient());
mailMessage.setText(details.getMsgBody());
mailMessage.setSubject(details.getSubject());
mailMessage.setTo(details.recipient());
mailMessage.setText(details.msgBody());
mailMessage.setSubject(details.subject());

javaMailSender.send(mailMessage);
return new EmailResponse(details.getRecipient().toString(),"Email is Successfully sent to Recipent.");
return new EmailResponse(details.recipient(),"Email is Successfully sent to Recipent.");
}
catch (Exception e) {
throw new EmployeeException(HttpStatus.INTERNAL_SERVER_ERROR,
Expand All @@ -56,16 +56,16 @@ public EmailResponse sendMailWithAttachment(EmailDetails details)
try {
mimeMessageHelper = new MimeMessageHelper(mimeMessage, true);
mimeMessageHelper.setFrom(sender);
mimeMessageHelper.setTo(details.getRecipient());
mimeMessageHelper.setText(details.getMsgBody());
mimeMessageHelper.setSubject(details.getSubject());
mimeMessageHelper.setTo(details.recipient());
mimeMessageHelper.setText(details.msgBody());
mimeMessageHelper.setSubject(details.subject());

FileSystemResource file = new FileSystemResource(new File(details.getAttachment()));
FileSystemResource file = new FileSystemResource(new File(details.attachment()));

mimeMessageHelper.addAttachment(file.getFilename(), file);

javaMailSender.send(mimeMessage);
return new EmailResponse(details.getRecipient().toString(),"Email with provided Attachment is Successfully sent to Recipent.");
return new EmailResponse(details.recipient(),"Email with provided Attachment is Successfully sent to Recipent.");
}
catch (MessagingException e) {
throw new EmployeeException(HttpStatus.INTERNAL_SERVER_ERROR,
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -42,7 +42,7 @@ public class EmployeeServiceImpl implements EmployeeService{
@Override
public PostEmployeeResponse addEmployee(Employee emp) {
Employee saveEmp = this.repo.save(emp);
if(saveEmp == null) {
if(saveEmp == null) { // TODO refactor, this code is no go , will not get triggered - read the doc
throw new EmployeeException(HttpStatus.INTERNAL_SERVER_ERROR,
ErrorCodeEnum.EMPLOYEE_NOT_ADDED.getErrorCode(),
ErrorCodeEnum.EMPLOYEE_NOT_ADDED.getErrorMessage());
Expand Down Expand Up @@ -109,37 +109,24 @@ public Employee deleteEmployeeById(String id) {
@Override
public PostEmployeeResponse updateEmployeeById(String id, Employee emp) {
UUID uuid = UUID.fromString(id);
List<Employee> empl = this.repo.findById(uuid).stream().collect(Collectors.toList());
Employee employee = empl.get(0);
if(employee == null) {
// TODO this is not how you do it
Optional<Employee> opt = this.repo.findById(uuid);
if(opt.isEmpty()) {
throw new EmployeeException(HttpStatus.INTERNAL_SERVER_ERROR,
ErrorCodeEnum.EMPLOYEE_NOT_FOUND.getErrorCode(),
ErrorCodeEnum.EMPLOYEE_NOT_FOUND.getErrorMessage());
}
else {
if(emp.getEmployeeName() != null) {
employee.setEmployeeName(emp.getEmployeeName());
}
if(emp.getEmail() != null) {
employee.setEmail(emp.getEmail());
}
if(emp.getPhoneNumber() != null) {
employee.setPhoneNumber(emp.getPhoneNumber());
}
if(emp.getReportsTo() != null) {
employee.setReportsTo(emp.getReportsTo());
}
if(emp.getProfileImage() != null) {
employee.setProfileImage(emp.getProfileImage());
}
}
Employee employee = opt.get();
// TODO this is another lotta code, no need, can do much better
employee.autoCopy(emp);

Employee saveEmp = this.repo.save(employee);
if(saveEmp == null) {
if(saveEmp == null) { // TODO this never happens
throw new EmployeeException(HttpStatus.INTERNAL_SERVER_ERROR,
ErrorCodeEnum.EMPLOYEE_NOT_UPDATED.getErrorCode(),
ErrorCodeEnum.EMPLOYEE_NOT_UPDATED.getErrorMessage());
}
// This never happens
Employee mEmpl = getEmployeeById(saveEmp.getReportsTo().toString());
if(mEmpl == null) {
throw new EmployeeException(HttpStatus.INTERNAL_SERVER_ERROR,
Expand All @@ -150,7 +137,7 @@ public PostEmployeeResponse updateEmployeeById(String id, Employee emp) {
emailServ.sendSimpleMail(email);
return new PostEmployeeResponse(saveEmp.getId().toString(), "Employee is Updated");
}

@Override
public PostEmployeeResponse getNthManager(String id, Integer n) {
UUID uuid = UUID.fromString(id);
Expand Down Expand Up @@ -181,13 +168,15 @@ public PostEmployeeResponse getNthManager(String id, Integer n) {
}
}

// TODO this is a bad impl, create a write through cache here
// TODO Cache out the entire org in memory and return as fast as you can
private Employee findNthManager(Employee employee, int n) {
UUID managerId = employee.getReportsTo();

while (managerId != null && n > BASE_LEVEL) {
Optional<Employee> optionalManager = this.repo.findById(managerId).stream().findFirst();

if (!optionalManager.isPresent()) {
if (optionalManager.isEmpty()) {
throw new EmployeeException(HttpStatus.INTERNAL_SERVER_ERROR,
ErrorCodeEnum.MANAGER_NOT_FOUND.getErrorCode(),
ErrorCodeEnum.MANAGER_NOT_FOUND.getErrorMessage());
Expand Down