Skip to content

Commit

Permalink
Merge pull request #19 from goormthon-Univ/feature/15-postRecruitment
Browse files Browse the repository at this point in the history
[feature/15-postRecruitment] 모집글 등록 API
  • Loading branch information
JoongHyun-Kim committed Mar 21, 2024
2 parents 5e6ea61 + 76c0eaa commit de74ca3
Show file tree
Hide file tree
Showing 9 changed files with 121 additions and 2 deletions.
2 changes: 1 addition & 1 deletion src/main/java/com/hatcher/haemo/common/BaseEntity.java
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,7 @@
@EntityListeners(AuditingEntityListener.class)
public class BaseEntity {

@Column(columnDefinition = "varchar(10) default 'recruiting'")
@Column(columnDefinition = "varchar(10) default 'active'")
@Setter
private String status;

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -2,4 +2,5 @@

public class RequestURI {
public final static String user = "/users";
public final static String recruitment = "/recruitments";
}
Original file line number Diff line number Diff line change
Expand Up @@ -33,6 +33,7 @@ public enum BaseResponseStatus {
DUPLICATED_LOGIN_ID(false, HttpStatus.CONFLICT, "중복된 로그인 아이디입니다."),

// recruitment(2100-2199)
WRONG_RECRUIT_TYPE(false, HttpStatus.NOT_FOUND, "해당 Recruit type을 찾을 수 없습니다."),

// comment(2200-2299)

Expand Down
24 changes: 23 additions & 1 deletion src/main/java/com/hatcher/haemo/common/enums/RecruitType.java
Original file line number Diff line number Diff line change
@@ -1,5 +1,27 @@
package com.hatcher.haemo.common.enums;

import com.hatcher.haemo.common.BaseException;

import java.util.Arrays;

import static com.hatcher.haemo.common.enums.BaseResponseStatus.WRONG_RECRUIT_TYPE;

public enum RecruitType {
EXHIBITION
RESTAURANT("맛집"),
CAFE("카페"),
MOVIE("영화"),
EXHIBITION("전시회"),
BOARD_GAME("보드게임"),
ESCAPE_GAME("방탈출"),
COIN_KARAOKE("코인노래방"),
DELIVERY_FOOD("배달음식");

RecruitType(String name) {}

public static RecruitType getEnumByName(String name) throws BaseException {
return Arrays.stream(RecruitType.values())
.filter(contents -> contents.name().equalsIgnoreCase(name))
.findFirst()
.orElseThrow(() -> new BaseException(WRONG_RECRUIT_TYPE));
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,43 @@
package com.hatcher.haemo.recruitment.application;

import com.hatcher.haemo.common.BaseException;
import com.hatcher.haemo.common.enums.RecruitType;
import com.hatcher.haemo.recruitment.domain.Recruitment;
import com.hatcher.haemo.recruitment.dto.RecruitmentPostRequest;
import com.hatcher.haemo.recruitment.repository.RecruitmentRepository;
import com.hatcher.haemo.user.application.UserService;
import com.hatcher.haemo.user.domain.User;
import com.hatcher.haemo.user.repository.UserRepository;
import lombok.RequiredArgsConstructor;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import static com.hatcher.haemo.common.constants.Constant.Recruitment.RECRUITING;
import static com.hatcher.haemo.common.enums.BaseResponseStatus.INTERNAL_SERVER_ERROR;
import static com.hatcher.haemo.common.enums.BaseResponseStatus.INVALID_USER_IDX;

@Service
@RequiredArgsConstructor
public class RecruitmentService {

private final UserService userService;
private final UserRepository userRepository;
private final RecruitmentRepository recruitmentRepository;

// 모집글 등록
@Transactional(rollbackFor = Exception.class)
public void postRecruitment(RecruitmentPostRequest recruitmentPostRequest) throws BaseException {
try {
User leader = userRepository.findById(userService.getUserIdxWithValidation()).orElseThrow(() -> new BaseException(INVALID_USER_IDX));

Recruitment recruitment = new Recruitment(recruitmentPostRequest.name(), leader, RecruitType.getEnumByName(recruitmentPostRequest.type()),
recruitmentPostRequest.participantLimit(), recruitmentPostRequest.contactUrl(), recruitmentPostRequest.description());
recruitment.setStatus(RECRUITING);
recruitmentRepository.save(recruitment);
} catch (BaseException e) {
throw e;
} catch (Exception e) {
throw new BaseException(INTERNAL_SERVER_ERROR);
}
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
package com.hatcher.haemo.recruitment.dto;

public record RecruitmentPostRequest(String name,
String type,
Integer participantLimit,
String contactUrl,
String description) {}
Original file line number Diff line number Diff line change
@@ -0,0 +1,32 @@
package com.hatcher.haemo.recruitment.presentation;

import com.hatcher.haemo.common.BaseException;
import com.hatcher.haemo.common.BaseResponse;
import com.hatcher.haemo.recruitment.application.RecruitmentService;
import com.hatcher.haemo.recruitment.dto.RecruitmentPostRequest;
import lombok.RequiredArgsConstructor;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import static com.hatcher.haemo.common.constants.RequestURI.recruitment;

@RestController
@RequiredArgsConstructor
@RequestMapping(recruitment)
public class RecruitmentController {

private final RecruitmentService recruitmentService;

// 모집글 등록
@PostMapping("")
public BaseResponse<?> postRecruitment(@RequestBody RecruitmentPostRequest recruitmentPostRequest) {
try {
recruitmentService.postRecruitment(recruitmentPostRequest);
return BaseResponse.success();
} catch (BaseException e) {
return BaseResponse.failure(e.getStatus());
}
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
package com.hatcher.haemo.recruitment.repository;

import com.hatcher.haemo.recruitment.domain.Recruitment;
import org.springframework.data.jpa.repository.JpaRepository;

public interface RecruitmentRepository extends JpaRepository<Recruitment, Long> {
}
Original file line number Diff line number Diff line change
Expand Up @@ -71,4 +71,10 @@ public User getUserByUserIdx(Long userIdx) {
return user.orElse(null);
}
}

public Long getUserIdxWithValidation() throws BaseException {
Long userIdx = authService.getUserIdx();
if (userIdx == null) throw new BaseException(NULL_ACCESS_TOKEN);
return userIdx;
}
}

0 comments on commit de74ca3

Please sign in to comment.