@RestController
@RequiredArgsConstructor
public class ProjectController {
private final ProjectService projectService;
@PostMapping("/projects")
public ResponseEntity register (@RequestBody Project project){
CustomResponseBody<Project> responseBody = new CustomResponseBody<>("프로젝트 등록 성공");
try {
projectService.register(project);
} catch (RuntimeException re){
responseBody.setResultCode(-1);
responseBody.setResultMsg(re.getMessage());
return ResponseEntity.badRequest().body(responseBody);
} catch (Exception e){
responseBody.setResultCode(-2);
responseBody.setResultMsg(e.getMessage());
return ResponseEntity.status(HttpStatus.INTERNAL_SERVER_ERROR).body(responseBody);
}
return ResponseEntity.ok().body(responseBody);
}
}
프로젝트 등록 시 requestbody를 받으면 각각 project, project_skill, recruit_info db에 나뉘어 저장이 되어야했다.
하여 ProjectCreateDTO 라는 클래스를 만들어 주어 저장될 여러 파라미터들(모델들)을 해당 클래스에 넣어줌
@Getter @Setter
public class ProjectCreateDTO {
private Project project;
private ProjectSkill projectSkill;
}
@RestController
@RequiredArgsConstructor
public class ProjectController {
private final ProjectService projectService;
private final ProjectSkillService projectSkillService;
@PostMapping("/projects")
public ResponseEntity register (@RequestBody ProjectCreateDTO model){
CustomResponseBody<ProjectCreateDTO> responseBody = new CustomResponseBody<>("프로젝트 등록 성공2");
try {
projectService.register(model.getProject());
projectSkillService.register(model.getProjectSkill());
} catch (RuntimeException re){
responseBody.setResultCode(-1);
responseBody.setResultMsg(re.getMessage());
return ResponseEntity.badRequest().body(responseBody);
} catch (Exception e){
responseBody.setResultCode(-2);
responseBody.setResultMsg(e.getMessage());
return ResponseEntity.status(HttpStatus.INTERNAL_SERVER_ERROR).body(responseBody);
}
return ResponseEntity.ok().body(responseBody);
}
}