Skip to content
Projects
Groups
Snippets
Help
Loading...
Help
Submit feedback
Contribute to GitLab
Sign in
Toggle navigation
M
MongoDB-POC
Project
Project
Details
Activity
Releases
Cycle Analytics
Repository
Repository
Files
Commits
Branches
Tags
Contributors
Graph
Compare
Charts
Issues
0
Issues
0
List
Board
Labels
Milestones
Merge Requests
0
Merge Requests
0
CI / CD
CI / CD
Pipelines
Jobs
Schedules
Charts
Wiki
Wiki
Snippets
Snippets
Members
Members
Collapse sidebar
Close sidebar
Activity
Graph
Charts
Create a new issue
Jobs
Commits
Issue Boards
Open sidebar
Pavitra Mallemdoddi Papili
MongoDB-POC
Commits
6f25d67d
Commit
6f25d67d
authored
Mar 19, 2021
by
Pavitra Mallemdoddi Papili
1
Browse files
Options
Browse Files
Download
Email Patches
Plain Diff
Committing the changes
parent
aee845c4
Changes
10
Hide whitespace changes
Inline
Side-by-side
Showing
10 changed files
with
507 additions
and
507 deletions
+507
-507
EmployeeController.java
src/main/java/com/poc/controller/EmployeeController.java
+65
-65
Employee.java
src/main/java/com/poc/entity/Employee.java
+110
-110
EmployeeMapper.java
src/main/java/com/poc/mapper/EmployeeMapper.java
+45
-45
EntityMapper.java
src/main/java/com/poc/mapper/EntityMapper.java
+7
-7
Emp.java
src/main/java/com/poc/model/Emp.java
+90
-90
EmpMongoRepository.java
src/main/java/com/poc/repository/EmpMongoRepository.java
+27
-27
EmpRepository.java
src/main/java/com/poc/repository/EmpRepository.java
+13
-13
EmpRepositoryImpl.java
src/main/java/com/poc/repository/EmpRepositoryImpl.java
+55
-55
EmployeeService.java
src/main/java/com/poc/service/EmployeeService.java
+18
-18
EmployeeServiceImpl.java
src/main/java/com/poc/service/EmployeeServiceImpl.java
+77
-77
No files found.
src/main/java/com/poc/controller/EmployeeController.java
View file @
6f25d67d
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
.
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
);
}
}
src/main/java/com/poc/entity/Employee.java
View file @
6f25d67d
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
.
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
;
}
}
src/main/java/com/poc/mapper/EmployeeMapper.java
View file @
6f25d67d
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
;
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
;
}
}
src/main/java/com/poc/mapper/EntityMapper.java
View file @
6f25d67d
package
com
.
poc
.
mapper
;
public
interface
EntityMapper
{
public
Object
mapPojoToEntity
(
Object
pojo
);
public
Object
mapEntityToPojo
(
Object
entity
);
}
package
com
.
poc
.
mapper
;
public
interface
EntityMapper
{
public
Object
mapPojoToEntity
(
Object
pojo
);
public
Object
mapEntityToPojo
(
Object
entity
);
}
src/main/java/com/poc/model/Emp.java
View file @
6f25d67d
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
.
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
;
}
}
src/main/java/com/poc/repository/EmpMongoRepository.java
View file @
6f25d67d
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.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
);
}
src/main/java/com/poc/repository/EmpRepository.java
View file @
6f25d67d
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
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
);
}
src/main/java/com/poc/repository/EmpRepositoryImpl.java
View file @
6f25d67d
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
.
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
());
}
}
src/main/java/com/poc/service/EmployeeService.java
View file @
6f25d67d
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.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
);
}
src/main/java/com/poc/service/EmployeeServiceImpl.java
View file @
6f25d67d
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
);
}
}
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
);
}
}
Pavitra Mallemdoddi Papili
@ppapili
mentioned in commit
e9184f72
·
Mar 19, 2021
mentioned in commit
e9184f72
mentioned in commit e9184f723f083e5c13e6f6740d1aa7d4921ef56d
Toggle commit list
Write
Preview
Markdown
is supported
0%
Try again
or
attach a new file
Attach a file
Cancel
You are about to add
0
people
to the discussion. Proceed with caution.
Finish editing this message first!
Cancel
Please
register
or
sign in
to comment