- Recruitment Entity
코드 중복을 피하기 위해 @Builder와 of()를 사용했다
@Table
@Getter
@Entity
@NoArgsConstructor(access = AccessLevel.PROTECTED)
public class Recruitment {
@Id
@GeneratedValue(strategy = GenerationType.IDENTITY)
private Long company_id;
@Setter
private String position;
@Setter
private Integer reward;
@Setter
private String contents;
@Setter
private String techInfo;
@Builder
public Recruitment(String position, Integer reward, String contents, String techInfo) {
this.position = position;
this.reward = reward;
this.contents = contents;
this.techInfo = techInfo;
}
public static Recruitment of(String position, Integer reward, String contents, String techInfo) {
Recruitment recruitment = Recruitment.builder()
.position(position)
.reward(reward)
.contents(contents)
.techInfo(techInfo)
.build();
return recruitment;
}
}
2. 등록 API 작성
- RecruitmentRepository
위치 : /domain/recruitment
public interface RecruitmentRepository extends JpaRepository<Recruitment, Long> {
}
- saveRecruit()
위치 : /service/recruitment/RecruimentService
public SaveRecruitResponse saveRecruit(SaveRecruitRequest request) {
Recruitment recruitment = Recruitment.builder()
.position(request.getPosition())
.reward(request.getReward())
.contents(request.getContents())
.techInfo(request.getTechInfo())
.build();
Recruitment created = recruitmentRepository.save(recruitment);
SaveRecruitResponse response = SaveRecruitResponse.of(created);
return response;
}
- saveRecruit
위치 : /web/recruitment/RecruitmentController
@PostMapping("/save")
public ApiResponse<SaveRecruitResponse> saveRecruit(@RequestBody SaveRecruitRequest request) {
SaveRecruitResponse response = recruitmentService.saveRecruit(request);
return ApiResponse.success(response);
}
- SaveRecruitRequest
위치 : /web/recruitment/request
@Getter
public class SaveRecruitRequest {
private String position;
private Integer reward;
private String contents;
private String techInfo;
}
- SaveRecruitResponse
위치 : /web/recruitment/response
@ToString
@Getter
@NoArgsConstructor(access = AccessLevel.PROTECTED)
public class SaveRecruitResponse {
private Long id;
private String position;
private Integer reward;
private String contents;
private String techInfo;
@Builder
public SaveRecruitResponse(Long id, String position, Integer reward, String contents, String techInfo) {
this.id = id;
this.position = position;
this.reward = reward;
this.contents = contents;
this.techInfo = techInfo;
}
public static SaveRecruitResponse of(Recruitment recruitment) {
SaveRecruitResponse response = SaveRecruitResponse.builder()
.id(recruitment.getCompany_id())
.position(recruitment.getPosition())
.reward(recruitment.getReward())
.contents(recruitment.getContents())
.techInfo(recruitment.getTechInfo())
.build();
return response;
}
}
- Postman 테스트
Request Body
Post http://localhost:8080/recruitment/save
- application/json
{
"position" : "Backend Junior Position",
"reward" : 1500000,
"contents" : "wanted now hiring backend junior position",
"techInfo" : "Python"
}
Response Body
{
"data": {
"id": 1,
"position": "Backend Junior Position",
"reward": 1500000,
"contents": "wanted now hiring backend junior position",
"techInfo": "Python"
}
}
'개인프로젝트 > 과제' 카테고리의 다른 글
5-2. 채용공고 검색 API (가산점 요소) (0) | 2022.08.25 |
---|---|
5-1. 채용공고 목록 API (0) | 2022.08.24 |
4. 삭제 API (0) | 2022.08.23 |
3. 수정 API (0) | 2022.08.23 |
원티드 백엔드 프리온보딩 과제 연습 1. 시작(git init, mysql 연동, Api Response 설정) (0) | 2022.08.23 |