|
1 | 1 | package com.urunov.mongodb.controller;
|
2 | 2 |
|
| 3 | +import com.urunov.mongodb.exception.ResourceNotFoundException; |
3 | 4 | import com.urunov.mongodb.model.Employees;
|
4 | 5 | import com.urunov.mongodb.repository.MongoDB;
|
| 6 | +import com.urunov.mongodb.service.SequenceGeneratorService; |
5 | 7 | import org.springframework.beans.factory.annotation.Autowired;
|
6 |
| -import org.springframework.web.bind.annotation.GetMapping; |
7 |
| -import org.springframework.web.bind.annotation.RequestMapping; |
8 |
| -import org.springframework.web.bind.annotation.RestController; |
| 8 | +import org.springframework.http.ResponseEntity; |
| 9 | +import org.springframework.web.bind.annotation.*; |
9 | 10 |
|
| 11 | +import javax.validation.Valid; |
| 12 | +import java.util.HashMap; |
10 | 13 | import java.util.List;
|
| 14 | +import java.util.Map; |
| 15 | + |
11 | 16 |
|
12 | 17 | @RestController
|
13 | 18 | @RequestMapping("/api/v1")
|
@@ -16,9 +21,53 @@ public class EmployeeController {
|
16 | 21 | @Autowired
|
17 | 22 | private MongoDB mongoDB;
|
18 | 23 |
|
19 |
| -@GetMapping("/") |
| 24 | +@Autowired |
| 25 | +private SequenceGeneratorService sequenceGeneratorService; |
| 26 | + |
| 27 | +@GetMapping("/all") |
20 | 28 | public List<Employees> getAllEmployee(){
|
21 | 29 | return mongoDB.findAll();
|
22 | 30 | }
|
23 | 31 |
|
| 32 | + |
| 33 | +@GetMapping("/employee/{id}") |
| 34 | +public ResponseEntity <Employees> getEmployeeById(@PathVariable(value = "id") String employeeId) throws ResourceNotFoundException{ |
| 35 | +Employees employees = mongoDB.findById(employeeId) |
| 36 | +.orElseThrow(() -> new ResourceNotFoundException("Employee not found for this id::" + employeeId)); |
| 37 | +return ResponseEntity.ok().body(employees); |
| 38 | +} |
| 39 | + |
| 40 | +@PostMapping("/employees") |
| 41 | +public Employees createEmployees(@Valid @RequestBody Employees employees) { |
| 42 | +// |
| 43 | +employees.setId(sequenceGeneratorService.generateSequence(Employees.SEQUENCE_NAME)); |
| 44 | +return mongoDB.save(employees); |
| 45 | +} |
| 46 | + |
| 47 | +@PutMapping("/employees/{id}") |
| 48 | +public ResponseEntity <Employees> updateEmployee(@PathVariable(value = "id") String employeeId, @Valid @RequestBody Employees details) throws ResourceNotFoundException { |
| 49 | +// |
| 50 | +Employees employees = mongoDB.findById(employeeId) |
| 51 | +.orElseThrow(() -> new ResourceNotFoundException("Employee not found this id::" + employeeId)); |
| 52 | + |
| 53 | +employees.setName(details.getName()); |
| 54 | +employees.setPhone(details.getPhone()); |
| 55 | +employees.setAge(details.getAge()); |
| 56 | +employees.setPosition(details.getPosition()); |
| 57 | +final Employees updatedEmployee = mongoDB.save(employees); |
| 58 | +return ResponseEntity.ok(updatedEmployee); |
| 59 | +} |
| 60 | + |
| 61 | +@DeleteMapping("/employees/{id}") |
| 62 | +public Map<String, Boolean> deleteEmployee(@PathVariable(value = "id") String id) throws ResourceNotFoundException { |
| 63 | +// |
| 64 | +Employees employees = mongoDB.findById(id) |
| 65 | +.orElseThrow(() -> new ResourceNotFoundException("Employee not found for this id::" + id)); |
| 66 | + |
| 67 | +mongoDB.delete(employees); |
| 68 | +Map<String, Boolean> response = new HashMap<>(); |
| 69 | +response.put("deleted", Boolean.TRUE); |
| 70 | + |
| 71 | +return response; |
| 72 | +} |
24 | 73 | }
|
0 commit comments