Revize a2c335e8
Přidáno uživatelem Petr Urban před více než 1 rok
src/main/java/cz/zcu/fav/kiv/antipatterndetectionapp/v2/controller/management/CategoryController.java | ||
---|---|---|
12 | 12 |
* This class contains all endpoints connected to Categories |
13 | 13 |
*/ |
14 | 14 |
@Controller |
15 |
@RequiredArgsConstructor |
|
15 | 16 |
@RequestMapping("v2/management") |
16 | 17 |
public class CategoryController { |
17 | 18 |
|
19 |
private final ICategories categories; |
|
20 |
|
|
18 | 21 |
@GetMapping("/getCategories") |
19 | 22 |
public ResponseEntity<String> getCategories() { |
20 | 23 |
//TODO |
src/main/java/cz/zcu/fav/kiv/antipatterndetectionapp/v2/model/CategoryDto.java | ||
---|---|---|
1 |
package cz.zcu.fav.kiv.antipatterndetectionapp.v2.model; |
|
2 |
|
|
3 |
import lombok.Builder; |
|
4 |
import lombok.Data; |
|
5 |
|
|
6 |
@Data |
|
7 |
@Builder |
|
8 |
public class CategoryDto { |
|
9 |
|
|
10 |
private Long id; |
|
11 |
|
|
12 |
private String name; |
|
13 |
|
|
14 |
private String description; |
|
15 |
|
|
16 |
public static CategoryDtoBuilder builder() { return new CategoryDtoBuilder(); } |
|
17 |
|
|
18 |
} |
src/main/java/cz/zcu/fav/kiv/antipatterndetectionapp/v2/model/GeneralResponseDto.java | ||
---|---|---|
1 |
package cz.zcu.fav.kiv.antipatterndetectionapp.v2.model; |
|
2 |
|
|
3 |
import lombok.Builder; |
|
4 |
import lombok.Data; |
|
5 |
|
|
6 |
import java.util.List; |
|
7 |
|
|
8 |
@Data |
|
9 |
@Builder |
|
10 |
public class GeneralResponseDto<T> { |
|
11 |
private String message; |
|
12 |
private List<T> categories; |
|
13 |
public static <T> GeneralResponseDtoBuilder<T> builder() { return new GeneralResponseDtoBuilder<T>(); } |
|
14 |
|
|
15 |
} |
src/main/java/cz/zcu/fav/kiv/antipatterndetectionapp/v2/service/management/category/ICategories.java | ||
---|---|---|
1 |
package cz.zcu.fav.kiv.antipatterndetectionapp.v2.service.management.category; |
|
2 |
|
|
3 |
import cz.zcu.fav.kiv.antipatterndetectionapp.model.Project; |
|
4 |
import cz.zcu.fav.kiv.antipatterndetectionapp.v2.model.CategoryDto; |
|
5 |
import cz.zcu.fav.kiv.antipatterndetectionapp.v2.model.GeneralResponseDto; |
|
6 |
import org.springframework.http.ResponseEntity; |
|
7 |
|
|
8 |
import java.util.List; |
|
9 |
import java.util.Optional; |
|
10 |
|
|
11 |
public interface ICategories { |
|
12 |
|
|
13 |
ResponseEntity<GeneralResponseDto<CategoryDto>> getResponse(String projectId); |
|
14 |
|
|
15 |
} |
src/main/java/cz/zcu/fav/kiv/antipatterndetectionapp/v2/service/management/category/impl/CategoriesImpl.java | ||
---|---|---|
1 |
package cz.zcu.fav.kiv.antipatterndetectionapp.v2.service.management.category.impl; |
|
2 |
|
|
3 |
import cz.zcu.fav.kiv.antipatterndetectionapp.model.Project; |
|
4 |
import cz.zcu.fav.kiv.antipatterndetectionapp.model.management.Category; |
|
5 |
import cz.zcu.fav.kiv.antipatterndetectionapp.model.management.ProjectInstance; |
|
6 |
import cz.zcu.fav.kiv.antipatterndetectionapp.service.ProjectService; |
|
7 |
import cz.zcu.fav.kiv.antipatterndetectionapp.v2.model.CategoryDto; |
|
8 |
import cz.zcu.fav.kiv.antipatterndetectionapp.v2.model.GeneralResponseDto; |
|
9 |
import cz.zcu.fav.kiv.antipatterndetectionapp.v2.service.management.category.ICategories; |
|
10 |
import cz.zcu.fav.kiv.antipatterndetectionapp.v2.utils.converters.CategoryToDto; |
|
11 |
import lombok.NonNull; |
|
12 |
import lombok.RequiredArgsConstructor; |
|
13 |
import org.springframework.http.HttpStatus; |
|
14 |
import org.springframework.http.ResponseEntity; |
|
15 |
import org.springframework.lang.Nullable; |
|
16 |
import org.springframework.stereotype.Service; |
|
17 |
|
|
18 |
import java.util.*; |
|
19 |
|
|
20 |
@Service |
|
21 |
@RequiredArgsConstructor |
|
22 |
public class CategoriesImpl implements ICategories { |
|
23 |
|
|
24 |
private final ProjectService projectService; |
|
25 |
private CategoryToDto categoryMapper = new CategoryToDto(); |
|
26 |
private final String PROJECT_NOT_FOUND_MESSAGE = "Project not found!"; |
|
27 |
private final String CATEGORIES_NOT_FOUND_MESSAGE = "Categories not found for the project!"; |
|
28 |
private final String INVALID_PROJECT_ID = "Provided project ID is in invalid format!"; |
|
29 |
|
|
30 |
private Project getProjectById(Long id) { |
|
31 |
return projectService.getProjectById(id); |
|
32 |
} |
|
33 |
|
|
34 |
private List<CategoryDto> getCategories(Project project) { |
|
35 |
|
|
36 |
if (project == null) { |
|
37 |
return null; |
|
38 |
} |
|
39 |
|
|
40 |
List<Category> categories = new ArrayList<>(); |
|
41 |
for (ProjectInstance projectInstance : project.getProjectInstances()) { |
|
42 |
categories.addAll(projectInstance.getCategories()); |
|
43 |
} |
|
44 |
|
|
45 |
List<CategoryDto> categoriesDto = categoryMapper.convert(categories); |
|
46 |
categoriesDto.sort(Comparator.comparing(CategoryDto::getName, String.CASE_INSENSITIVE_ORDER)); |
|
47 |
|
|
48 |
return categoriesDto; |
|
49 |
} |
|
50 |
|
|
51 |
@Override |
|
52 |
public ResponseEntity<GeneralResponseDto<CategoryDto>> getResponse(String projectId) { |
|
53 |
// --- The value from the request body is String, so there must be a validation if the string is number or not. |
|
54 |
final Long id = tryParse(projectId); |
|
55 |
if (id == Long.MIN_VALUE) |
|
56 |
return createResponseEntity(HttpStatus.BAD_REQUEST, INVALID_PROJECT_ID, null); |
|
57 |
|
|
58 |
// --- get project by ID |
|
59 |
final Project project = getProjectById(id); |
|
60 |
|
|
61 |
if (project == null) |
|
62 |
return createResponseEntity(HttpStatus.NOT_FOUND, PROJECT_NOT_FOUND_MESSAGE, null); |
|
63 |
|
|
64 |
// --- Get all categories for the specific project |
|
65 |
final List<CategoryDto> categories = getCategories(project); |
|
66 |
|
|
67 |
if (categories == null) |
|
68 |
return createResponseEntity(HttpStatus.NOT_FOUND, CATEGORIES_NOT_FOUND_MESSAGE, null); |
|
69 |
|
|
70 |
return createResponseEntity(HttpStatus.OK, null, categories); |
|
71 |
} |
|
72 |
|
|
73 |
private ResponseEntity<GeneralResponseDto<CategoryDto>> createResponseEntity(@NonNull HttpStatus status, |
|
74 |
@Nullable String message, |
|
75 |
@Nullable List<CategoryDto> categories) { |
|
76 |
|
|
77 |
|
|
78 |
return new ResponseEntity<>( |
|
79 |
GeneralResponseDto |
|
80 |
.<CategoryDto>builder() |
|
81 |
.message(message) |
|
82 |
.categories(categories) |
|
83 |
.build(), status); |
|
84 |
} |
|
85 |
|
|
86 |
private Long tryParse(String valueToParse) { |
|
87 |
try { |
|
88 |
return Long.parseLong(valueToParse); |
|
89 |
} catch (NumberFormatException e) { |
|
90 |
return Long.MIN_VALUE; |
|
91 |
} |
|
92 |
} |
|
93 |
|
|
94 |
} |
src/main/java/cz/zcu/fav/kiv/antipatterndetectionapp/v2/utils/converters/CategoryToDto.java | ||
---|---|---|
1 |
package cz.zcu.fav.kiv.antipatterndetectionapp.v2.utils.converters; |
|
2 |
|
|
3 |
import cz.zcu.fav.kiv.antipatterndetectionapp.model.management.Category; |
|
4 |
import cz.zcu.fav.kiv.antipatterndetectionapp.v2.model.CategoryDto; |
|
5 |
|
|
6 |
import java.util.ArrayList; |
|
7 |
import java.util.List; |
|
8 |
|
|
9 |
public class CategoryToDto implements ClassToDto <Category, CategoryDto> { |
|
10 |
@Override |
|
11 |
public CategoryDto convert(Category source) { |
|
12 |
return CategoryDto |
|
13 |
.builder() |
|
14 |
.id(source.getId()) |
|
15 |
.name(source.getName()) |
|
16 |
.description(source.getDescription()) |
|
17 |
.build(); |
|
18 |
} |
|
19 |
|
|
20 |
@Override |
|
21 |
public List<CategoryDto> convert(List<Category> source) { |
|
22 |
List<CategoryDto> categories = new ArrayList<>(); |
|
23 |
source.stream().forEach(e -> categories.add(convert(e))); |
|
24 |
return categories; |
|
25 |
} |
|
26 |
} |
Také k dispozici: Unified diff
https://kivprogrammers.atlassian.net/browse/TSP2-58