Committing the changes

parent 618e0225
HELP.md
target/
!.mvn/wrapper/maven-wrapper.jar
!**/src/main/**/target/
!**/src/test/**/target/
### STS ###
.apt_generated
.classpath
.factorypath
.project
.settings
.springBeans
.sts4-cache
### IntelliJ IDEA ###
.idea
*.iws
*.iml
*.ipr
### NetBeans ###
/nbproject/private/
/nbbuild/
/dist/
/nbdist/
/.nb-gradle/
build/
!**/src/main/**/build/
!**/src/test/**/build/
### VS Code ###
.vscode/
<?xml version="1.0" encoding="UTF-8"?>
<project xmlns="http://maven.apache.org/POM/4.0.0"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 https://maven.apache.org/xsd/maven-4.0.0.xsd">
<modelVersion>4.0.0</modelVersion>
<parent>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-parent</artifactId>
<version>2.4.3</version>
<relativePath /> <!-- lookup parent from repository -->
</parent>
<groupId>com.poc</groupId>
<artifactId>mongodb-poc</artifactId>
<version>0.0.1-SNAPSHOT</version>
<name>Mongo DB POC</name>
<description>Demo project for Spring Boot and Mongo with Kafka</description>
<properties>
<java.version>1.8</java.version>
</properties>
<dependencies>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-data-mongodb</artifactId>
</dependency>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-web</artifactId>
</dependency>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-test</artifactId>
<scope>test</scope>
</dependency>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-validation</artifactId>
<scope>compile</scope>
<exclusions>
<exclusion>
<artifactId>tomcat-embed-el</artifactId>
<groupId>org.apache.tomcat.embed</groupId>
</exclusion>
</exclusions>
</dependency>
</dependencies>
<build>
<plugins>
<plugin>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-maven-plugin</artifactId>
</plugin>
</plugins>
</build>
</project>
package com.poc;
import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;
@SpringBootApplication
public class SimplePocApplication {
public static void main(String[] args) {
SpringApplication.run(SimplePocApplication.class, args);
}
}
package com.poc.controller;
import java.util.List;
import javax.validation.Valid;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.http.HttpStatus;
import org.springframework.http.ResponseEntity;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.DeleteMapping;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;
import com.poc.entity.Employee;
import com.poc.model.Emp;
import com.poc.service.EmployeeService;
@RestController
@RequestMapping(path = "/employees")
@Validated
public class EmployeeController {
@Autowired
private EmployeeService employeeService;
@GetMapping(produces = "application/json", consumes = "application/json", path = "/empId/{empId}")
public ResponseEntity<Emp> findByEmpId(@Valid @PathVariable Long empId) {
return new ResponseEntity<>(employeeService.findByEmpId(empId), HttpStatus.OK);
}
@GetMapping(produces = "application/json", consumes = "application/json", path = "/")
public ResponseEntity<List<Emp>> findAll() {
return new ResponseEntity<>(employeeService.findAll(), HttpStatus.OK);
}
@GetMapping(produces = "application/json", consumes = "application/json", path = "/id/{id}")
public ResponseEntity<Emp> findById(@Valid @PathVariable String id) {
return new ResponseEntity<>(employeeService.findById(id), HttpStatus.OK);
}
@GetMapping(produces = "application/json", consumes = "application/json", path = "/sal/{sal}")
public ResponseEntity<List<Emp>> findBySal(@Valid @PathVariable Double sal) {
return new ResponseEntity<>(employeeService.findBySalary(sal), HttpStatus.OK);
}
@PostMapping(produces = "application/json", consumes = "application/json", path = "/upsert")
public ResponseEntity<Emp> saveOrUpdateEmployee(@Valid @RequestBody Emp emp) {
employeeService.saveOrUpdate(emp);
return new ResponseEntity<>(HttpStatus.OK);
}
@PostMapping(produces = "application/json", consumes = "application/json", path = "/save")
public ResponseEntity<Emp> saveEmployee(@Valid @RequestBody Emp emp) {
return new ResponseEntity<>(employeeService.save(emp), HttpStatus.OK);
}
@DeleteMapping(produces = "application/json", consumes = "application/json", path = "/delete/{empId}")
public ResponseEntity<Long> deleteByEmpId(@PathVariable Long empId) {
return new ResponseEntity<>(employeeService.deleteByEmpId(empId), HttpStatus.OK);
}
}
package com.poc.entity;
import org.springframework.data.annotation.Id;
import org.springframework.data.mongodb.core.mapping.Document;
import org.springframework.data.mongodb.core.mapping.Field;
@Document(collection = "employee")
public class Employee {
@Id
private String _id;
@Field(name = "emp_id")
private Long empId;
@Field(name = "emp_name")
private String empName;
@Field(name = "emp_sal")
private Double empSal;
@Field(name = "dept_id")
private String deptId;
@Field(name = "dept_name")
private String deptName;
public Employee() {
super();
}
/**
* @return the _id
*/
public String get_id() {
return _id;
}
/**
* @param _id the _id to set
*/
public void set_id(String _id) {
this._id = _id;
}
/**
* @return the empId
*/
public Long getEmpId() {
return empId;
}
/**
* @param empId the empId to set
*/
public void setEmpId(Long empId) {
this.empId = empId;
}
/**
* @return the empName
*/
public String getEmpName() {
return empName;
}
/**
* @param empName the empName to set
*/
public void setEmpName(String empName) {
this.empName = empName;
}
/**
* @return the empSal
*/
public Double getEmpSal() {
return empSal;
}
/**
* @param empSal the empSal to set
*/
public void setEmpSal(Double empSal) {
this.empSal = empSal;
}
/**
* @return the deptId
*/
public String getDeptId() {
return deptId;
}
/**
* @param deptId the deptId to set
*/
public void setDeptId(String deptId) {
this.deptId = deptId;
}
/**
* @return the deptName
*/
public String getDeptName() {
return deptName;
}
/**
* @param deptName the deptName to set
*/
public void setDeptName(String deptName) {
this.deptName = deptName;
}
}
package com.poc.mapper;
import java.util.Optional;
import org.springframework.stereotype.Component;
import com.poc.entity.Employee;
import com.poc.model.Emp;
@Component
public class EmployeeMapper implements EntityMapper {
@Override
public Object mapPojoToEntity(Object pojo) {
Optional<Object> obj=Optional.ofNullable(pojo);
if(obj.isPresent()) {
Emp emp=(Emp)pojo;
Employee employee=new Employee();
employee.setDeptId(emp.getDeptId());
employee.setDeptName(emp.getDeptName());
employee.setEmpId(emp.getEmpId());
employee.setEmpName(emp.getEmpName());
employee.setEmpSal(emp.getEmpSal());
return employee;
}
return null;
}
@Override
public Object mapEntityToPojo(Object entity) {
Optional<Object> obj=Optional.ofNullable(entity);
if(obj.isPresent()) {
Employee employee=(Employee)entity;
Emp emp=new Emp();
emp.setDeptId(employee.getDeptId());
emp.setDeptName(employee.getDeptName());
emp.setEmpId(employee.getEmpId());
emp.setEmpName(employee.getEmpName());
emp.setEmpSal(employee.getEmpSal());
return emp;
}
return null;
}
}
package com.poc.mapper;
public interface EntityMapper {
public Object mapPojoToEntity(Object pojo);
public Object mapEntityToPojo(Object entity);
}
package com.poc.model;
import javax.validation.constraints.NotNull;
public class Emp {
@NotNull(message = "empID should not be null")
private Long empId;
@NotNull(message = "empName should not be null")
private String empName;
@NotNull(message = "empSal should not be null")
private Double empSal;
@NotNull(message = "deptId should not be null")
private String deptId;
@NotNull(message = "deptName should not be null")
private String deptName;
public Emp() {
super();
}
/**
* @return the empId
*/
public Long getEmpId() {
return empId;
}
/**
* @param empId the empId to set
*/
public void setEmpId(Long empId) {
this.empId = empId;
}
/**
* @return the empName
*/
public String getEmpName() {
return empName;
}
/**
* @param empName the empName to set
*/
public void setEmpName(String empName) {
this.empName = empName;
}
/**
* @return the empSal
*/
public Double getEmpSal() {
return empSal;
}
/**
* @param empSal the empSal to set
*/
public void setEmpSal(Double empSal) {
this.empSal = empSal;
}
/**
* @return the deptId
*/
public String getDeptId() {
return deptId;
}
/**
* @param deptId the deptId to set
*/
public void setDeptId(String deptId) {
this.deptId = deptId;
}
/**
* @return the deptName
*/
public String getDeptName() {
return deptName;
}
/**
* @param deptName the deptName to set
*/
public void setDeptName(String deptName) {
this.deptName = deptName;
}
}
package com.poc.repository;
import java.util.stream.Stream;
import org.springframework.data.mongodb.repository.MongoRepository;
import org.springframework.data.mongodb.repository.Query;
import org.springframework.stereotype.Repository;
import com.poc.entity.Employee;
@Repository
public interface EmpMongoRepository extends MongoRepository<Employee, String> {
@Query(value="{emp_id : ?0}",sort = "{emp_id : 1}")
public Employee findByEmpId(Long empId);
/**
* -1 -decending
* 1 ascending
* @param salary
* @return
*/
@Query(value="{emp_sal : {$gte: ?0}}",sort = "{emp_sal : -1}")
public Stream<Employee> findBySalary(Double salary);
@Query(value = "{emp_id : ?0}", delete = true)
public Long deleteByEmployeeId(Long empId);
}
package com.poc.repository;
import java.util.List;
import com.poc.entity.Employee;
public interface EmpRepository {
public List<Employee> findAll();
public Employee findById(String id);
public Employee save(Employee e);
public void saveOrUpdate(Employee e);
}
package com.poc.repository;
import java.util.List;
import org.bson.Document;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.mongodb.core.MongoTemplate;
import org.springframework.data.mongodb.core.query.Criteria;
import org.springframework.data.mongodb.core.query.Query;
import org.springframework.data.mongodb.core.query.Update;
import org.springframework.stereotype.Repository;
import com.mongodb.client.result.UpdateResult;
import com.poc.entity.Employee;
@Repository
public class EmpRepositoryImpl implements EmpRepository {
private static final Logger logger = LoggerFactory.getLogger(EmpRepositoryImpl.class);
@Autowired
private MongoTemplate mongoTemplate;
@Override
public List<Employee> findAll() {
return mongoTemplate.findAll(Employee.class);
}
@Override
public Employee findById(String id) {
Query query = new Query();
query.addCriteria(Criteria.where("emp_id").is(id));
return mongoTemplate.findOne(query, Employee.class);
}
@Override
public Employee save(Employee e) {
return mongoTemplate.save(e);
}
@Override
public void saveOrUpdate(Employee e) {
Query query = new Query();
query.addCriteria(Criteria.where("emp_id").is(e.getEmpId()));
Document doc = new Document();
mongoTemplate.getConverter().write(e, doc);
Update update = Update.fromDocument(doc);
UpdateResult result=mongoTemplate.upsert(query, update, Employee.class);
result.getMatchedCount();
logger.info("Matched Count:{},{}",result.getMatchedCount(),e.getEmpId());
}
}
package com.poc.service;
import java.util.List;
import com.poc.entity.Employee;
import com.poc.model.Emp;
public interface EmployeeService {
public Emp findByEmpId(Long empId);
public Emp findById(String id);
public List<Emp> findAll();
public Emp save(Emp emp);
public void saveOrUpdate(Emp emp);
public List<Emp> findBySalary(Double sal);
public Long deleteByEmpId(Long empId);
}
package com.poc.service;
import java.awt.print.Book;
import java.util.List;
import java.util.stream.Collectors;
import java.util.stream.Stream;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import com.poc.entity.Employee;
import com.poc.mapper.EmployeeMapper;
import com.poc.model.Emp;
import com.poc.repository.EmpMongoRepository;
import com.poc.repository.EmpRepository;
@Service
public class EmployeeServiceImpl implements EmployeeService {
@Autowired
private EmpMongoRepository empMongoRepository;
@Autowired
private EmployeeMapper employeeMapper;
@Autowired
private EmpRepository empRepository;
@Override
public Emp findByEmpId(Long empId) {
return (Emp)employeeMapper.mapEntityToPojo(empMongoRepository.findByEmpId(empId));
}
@Override
public Emp findById(String id) {
return (Emp)employeeMapper.mapEntityToPojo(empMongoRepository.findById(id).orElse(new Employee()));
}
@Override
public List<Emp> findAll() {
List<Employee> list= empMongoRepository.findAll();
List<Emp> empList= list.stream()
.map(e->employeeMapper.mapEntityToPojo(e))
.map(e->(Emp)e)
.collect(Collectors.toList());
return empList;
}
@Override
public Emp save(Emp emp) {
Employee employee = (Employee) employeeMapper.mapPojoToEntity(emp);
return (Emp)employeeMapper.mapEntityToPojo(empMongoRepository.save(employee));
}
@Override
public void saveOrUpdate(Emp emp) {
Employee employee = (Employee) employeeMapper.mapPojoToEntity(emp);
empRepository.saveOrUpdate(employee);
}
@Override
public List<Emp> findBySalary(Double sal) {
List<Emp> empList= null;
try (Stream<Employee> stream = empMongoRepository.findBySalary(sal)) {
List<Employee> list = stream.collect(Collectors.toList());
empList= list.stream()
.map(e->(Emp)employeeMapper.mapEntityToPojo(e))
.collect(Collectors.toList());
}
return empList;
}
@Override
public Long deleteByEmpId(Long empId) {
return empMongoRepository.deleteByEmployeeId(empId);
}
}
spring.data.mongodb.host=localhost
spring.data.mongodb.port=27017
spring.data.mongodb.database=ownDatabase
server.servlet.context-path=/simple-poc
logging.level.org.springframework.data.mongodb.core.MongoTemplate=DEBUG
logging.level.org.springframework=INFO
logging.level.root=INFO
server.port=8081
package com.poc;
import org.junit.jupiter.api.Test;
import org.springframework.boot.test.context.SpringBootTest;
@SpringBootTest
class SimplePocApplicationTests {
@Test
void contextLoads() {
}
}
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment