Вот обзор моего кода:
ProjectService.java
Код: Выделить всё
public void createProject(ProjectRequest request, Authentication auth) {
Project project = projectMapper.toProject(request);
Category category = categoryRepository.findById(request.categoryId()).orElseThrow();
project.setCustomer((User) auth.getPrincipal());
project.setCategory(category);
projectRepository.save(project);
}
Код: Выделить всё
@PostMapping
public ResponseEntity createProject(
Authentication auth,
@RequestBody @Valid ProjectRequest request
) {
service.createProject(request, auth);
return ResponseEntity.status(201).build();
}
Код: Выделить всё
public record ProjectRequest(
@NotNull(message = "Title cannot be null")
@NotEmpty(message = "Title cannot be empty")
@Size(min = 20, message = "Title must be at least 20 characters long")
String title,
@NotNull(message = "Description cannot be null")
@NotEmpty(message = "Description cannot be empty")
@Size(min = 1, message = "Description must be at least 200 characters long")
String description,
@NotNull(message = "Price cannot be null")
@Min(value = 50, message = "Price must be at least 20")
@Max(value = 10000, message = "Price must not exceed 10,000")
double price,
Long categoryId
) {
}
Код: Выделить всё
public Project toProject(ProjectRequest r) {
return Project.builder()
.price(r.price())
.title(r.title())
.description(r.description())
.build();
}
значения запроса
Сущность проекта непосредственно перед сохранением
объекта проекта
Я также обнаружил, что в этой строке выдается исключение:
Код: Выделить всё
projectRepository.save(project);Подробнее здесь: https://stackoverflow.com/questions/790 ... sscastexce
Мобильная версия