Skip to content

Commit

Permalink
[FEAT]#28 채용공고 수정 기능 구현
Browse files Browse the repository at this point in the history
DryRains committed Aug 30, 2023
1 parent f3e99f9 commit 2020da5
Showing 7 changed files with 100 additions and 3 deletions.
Original file line number Diff line number Diff line change
@@ -104,4 +104,70 @@ public List<PostResDTO> retrieveRecruitmentPostsOfCurrentCompany(String token) {
}


@Transactional
public PostResDTO updateRecruitmentPostById(String token, PostReqDTO postReqDTO, Long postId) {
String email = JwtUtil.getUserEmail(token, secretKey);
Optional<User> optionalUser = userRepository.findByEmail(email);
if (!optionalUser.isPresent()) {
//사용자를 찾을 수 없음
return null;//null->Exception 변경 예정
}

User user = optionalUser.get();
Optional<Post> optionalPost = postRepository.findById(postId);
if (!optionalPost.isPresent()) {
//게시물을 찾을 수 없음
return null;
}

Post post = optionalPost.get();
if (!user.getId().equals(post.getUser().getId())) {
//올바르지 않은 사용자
return null;
}
//Update
if (postReqDTO.getTitle() != null) {
post.setTitle(postReqDTO.getTitle());
}

if (postReqDTO.getCompany_photo_url() != null) {
post.setCompany_photo_url(postReqDTO.getCompany_photo_url());
}

if (postReqDTO.getCompensation_recommender() > 0) {
post.setCompensation_recommender(postReqDTO.getCompensation_recommender());
}

if (postReqDTO.getCompensation_applicant() > 0) {
post.setCompensation_applicant(postReqDTO.getCompensation_applicant());
}

if (postReqDTO.getDueDate() != null) {
post.setDueDate(postReqDTO.getDueDate());
}

if (postReqDTO.getContent() != null) {
post.setContent(postReqDTO.getContent());
}

if (postReqDTO.getWorking_address() != null) {
post.setWorking_address(postReqDTO.getWorking_address());
}

if (postReqDTO.getFields() != null && !postReqDTO.getFields().isEmpty()) {
//기존 field 삭제
for (Field field : post.getFields()) {
fieldRepository.delete(field);
}
post.getFields().clear();
//새로운 field 설정
for(FieldDTO fieldDTO : postReqDTO.getFields()){
Field field = new Field();
field.setName(fieldDTO.getName());
post.addField(field);
}
}
Post updatedPost = postRepository.save(post);
return modelMapper.map(updatedPost,PostResDTO.class);
}
}
Original file line number Diff line number Diff line change
@@ -21,7 +21,7 @@ public class Field {
@GeneratedValue(strategy = GenerationType.IDENTITY)
private Long id;

@ManyToOne
@ManyToOne()
@JoinColumn(name="post_id")
@JsonIgnore //양방향 관계 무한순환참조 방지
private Post post;
Original file line number Diff line number Diff line change
@@ -13,4 +13,5 @@
@Repository
public interface PostRepository extends JpaRepository<Post,Long> {
List<Post> findByUser_Id(Long userId);

}
Original file line number Diff line number Diff line change
@@ -16,6 +16,9 @@ public class PostResDTO { //반환용 DTO
@NotBlank
private Long userId;

@NotBlank
private String userEmail;

@NotBlank
private String title;

Original file line number Diff line number Diff line change
@@ -5,6 +5,7 @@
import com.example.rcp1.domain.recruitment.domain.Post;
import com.example.rcp1.domain.recruitment.dto.PostReqDTO;
import com.example.rcp1.domain.recruitment.dto.PostResDTO;
import com.example.rcp1.domain.user.dto.UpdateProfileReq;
import com.example.rcp1.global.BaseResponse;
import com.example.rcp1.global.ErrorCode;
import com.example.rcp1.global.SuccessCode;
@@ -28,7 +29,12 @@ public ResponseEntity<BaseResponse<Post>> createRecruitmentPost(@RequestHeader("
try {
String token = Authorization.substring(7);
Post post = recruitmentService.createRecruitmentPost(token, postDTO);
return ResponseEntity.ok(BaseResponse.success(SuccessCode.POST_CREATED_SUCCESS, post));
if (post != null) {
return ResponseEntity.ok(BaseResponse.success(SuccessCode.POST_CREATED_SUCCESS, post));
} else {
return ResponseEntity.status(HttpStatus.FORBIDDEN)
.body(BaseResponse.error(ErrorCode.FORBIDDEN, "채용공고를 작성할 권한이 없습니다."));
}
} catch (Exception e) {
return ResponseEntity.status(HttpStatus.BAD_REQUEST)
.body(BaseResponse.error(ErrorCode.REQUEST_VALIDATION_EXCEPTION, "채용공고 게시에 실패했습니다."));
@@ -77,6 +83,25 @@ public ResponseEntity<BaseResponse<List<PostResDTO>>> retrieveRecruitmentPostsOf
}
}

//특정 채용공고 수정
@PatchMapping("/posts/{postId}")
public ResponseEntity<BaseResponse<PostResDTO>> updateRecruitmentPostById(@RequestHeader("Authorization") String Authorization,
@Valid @RequestBody PostReqDTO postReqDTO,@PathVariable Long postId){
try {
String token = Authorization.substring(7);
PostResDTO post = recruitmentService.updateRecruitmentPostById(token,postReqDTO,postId);
if (post != null) {
return ResponseEntity.ok(BaseResponse.success(SuccessCode.POST_UPDATED_SUCCESS, post));
} else {
return ResponseEntity.status(HttpStatus.FORBIDDEN)
.body(BaseResponse.error(ErrorCode.FORBIDDEN, "채용공고를 수정할 권한이 없습니다."));
}
} catch (Exception e) {
return ResponseEntity.status(HttpStatus.BAD_REQUEST)
.body(BaseResponse.error(ErrorCode.REQUEST_VALIDATION_EXCEPTION, "채용공고 수정에 실패했습니다."));
}
}




3 changes: 2 additions & 1 deletion src/main/java/com/example/rcp1/global/ErrorCode.java
Original file line number Diff line number Diff line change
@@ -12,7 +12,8 @@ public enum ErrorCode {
// api 만들고 수정하기
REQUEST_VALIDATION_EXCEPTION(BAD_REQUEST, "잘못된 요청입니니다."),
EXPIRED_TOKEN(UNAUTHORIZED, "만료된 토큰입니다."),
NOT_FOUND(HttpStatus.NOT_FOUND, "요청한 리소스를 찾을 수 없습니다.");
NOT_FOUND(HttpStatus.NOT_FOUND, "요청한 리소스를 찾을 수 없습니다."),
FORBIDDEN(HttpStatus.FORBIDDEN, "권한이 없습니다.");

private final HttpStatus httpStatus;
private final String message;
1 change: 1 addition & 0 deletions src/main/java/com/example/rcp1/global/SuccessCode.java
Original file line number Diff line number Diff line change
@@ -19,6 +19,7 @@ public enum SuccessCode {
LOGICAL_DELETE_SUCCESS(OK, "논리적으로 삭제 되었습니다."),
POST_CREATED_SUCCESS(CREATED, "채용공고 생성에 성공했습니다."),
POST_RETRIEVAL_SUCCESS(OK,"채용공고 조회에 성공했습니다."),
POST_UPDATED_SUCCESS(OK, "채용공고 수정에 성공했습니다."),
CREATE_APPLY_SUCCESS(OK, "채용 공고에 지원서를 제출했습니다.");

private final HttpStatus httpStatus;

0 comments on commit 2020da5

Please sign in to comment.