-
Notifications
You must be signed in to change notification settings - Fork 26
Expand file tree
/
Copy pathProjectController.java
More file actions
60 lines (47 loc) · 2.01 KB
/
ProjectController.java
File metadata and controls
60 lines (47 loc) · 2.01 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
package com.accenture.codingtest.springbootcodingtest.controller;
import java.util.List;
import java.util.stream.Collectors;
import org.springframework.beans.factory.annotation.Autowired;
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.accenture.codingtest.springbootcodingtest.entity.Project;
import com.accenture.codingtest.springbootcodingtest.entity.exception.RecordNotFoundException;
import com.accenture.codingtest.springbootcodingtest.entity.model.ProjectDto;
import com.accenture.codingtest.springbootcodingtest.service.ProjectService;
@RestController
@RequestMapping(value = "/api/v1/projects")
public class ProjectController {
@Autowired
private ProjectService projectService;
@PostMapping("")
public ProjectDto create(@RequestBody ProjectDto projectDto) {
return this.convertProjectEntityToProjectDto(
projectService.save(this.convertProjectDtoToProjectEntity(projectDto)));
}
@GetMapping("")
public List<ProjectDto> findAll() {
return projectService.findAll().stream().map(this::convertProjectEntityToProjectDto)
.collect(Collectors.toList());
}
@GetMapping("/{id}")
public ProjectDto findById(@PathVariable String id) {
return projectService.findById(id).map(this::convertProjectEntityToProjectDto)
.orElseThrow(RecordNotFoundException::new);
}
private ProjectDto convertProjectEntityToProjectDto(Project project) {
ProjectDto projectDto = new ProjectDto();
projectDto.setId(project.getId());
projectDto.setName(projectDto.getName());
return projectDto;
}
private Project convertProjectDtoToProjectEntity(ProjectDto projectDto) {
Project project = new Project();
project.setId(projectDto.getId());
project.setName(projectDto.getName());
return project;
}
}