Skip to content

Commit

Permalink
Merge branch 'develop' of https://github.com/shoeone96/wanted into fe…
Browse files Browse the repository at this point in the history
…ature/#32-likes-API
  • Loading branch information
WinsomeJoo committed Sep 2, 2023
2 parents 54fc6a3 + 230bc8a commit 1739bb4
Show file tree
Hide file tree
Showing 21 changed files with 312 additions and 19 deletions.
2 changes: 2 additions & 0 deletions src/main/java/com/clone/wanted/BaseEntity.java
Original file line number Diff line number Diff line change
Expand Up @@ -3,12 +3,14 @@
import jakarta.persistence.Column;
import jakarta.persistence.EntityListeners;
import jakarta.persistence.MappedSuperclass;
import lombok.Getter;
import org.springframework.data.annotation.CreatedDate;
import org.springframework.data.annotation.LastModifiedDate;
import org.springframework.data.jpa.domain.support.AuditingEntityListener;

import java.time.LocalDateTime;

@Getter
@MappedSuperclass
@EntityListeners(AuditingEntityListener.class)
public class BaseEntity {
Expand Down
4 changes: 4 additions & 0 deletions src/main/java/com/clone/wanted/Company/CompanyRepository.java
Original file line number Diff line number Diff line change
@@ -1,8 +1,12 @@
package com.clone.wanted.Company;

import com.clone.wanted.User.User;
import org.springframework.data.jpa.repository.JpaRepository;
import org.springframework.stereotype.Repository;

import java.util.Optional;

@Repository
public interface CompanyRepository extends JpaRepository<Company,Long> {
Optional<Company> findByUser(User user);
}
12 changes: 12 additions & 0 deletions src/main/java/com/clone/wanted/User/UserRepository.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,12 @@
package com.clone.wanted.User;

import org.springframework.data.jpa.repository.JpaRepository;
import org.springframework.stereotype.Repository;

import java.util.Optional;

@Repository
public interface UserRepository extends JpaRepository<User, Long> {

Optional<User> findByEmail(String email);
}
17 changes: 16 additions & 1 deletion src/main/java/com/clone/wanted/application/Application.java
Original file line number Diff line number Diff line change
Expand Up @@ -4,13 +4,14 @@
import com.clone.wanted.User.User;
import com.clone.wanted.employment.Employment;
import jakarta.persistence.*;
import lombok.AccessLevel;
import lombok.Getter;
import lombok.NoArgsConstructor;


@Entity
@Getter
@NoArgsConstructor
@NoArgsConstructor(access = AccessLevel.PROTECTED)
public class Application extends BaseEntity {

@Id @GeneratedValue(strategy = GenerationType.IDENTITY)
Expand All @@ -25,4 +26,18 @@ public class Application extends BaseEntity {
@Enumerated(EnumType.STRING)
private ApplicationStatus applicationStatus;

private Application (User user, Employment employment){
this.user = user;
this.employment = employment;
this.applicationStatus = ApplicationStatus.ONGOING;
}

public void updateStatus(ApplicationStatus status){
this.applicationStatus = status;
}

public static Application newEnrollment(User user, Employment employment){
return new Application(user, employment);
}

}
Original file line number Diff line number Diff line change
@@ -0,0 +1,49 @@
package com.clone.wanted.application;

import com.clone.wanted.application.requestDto.EstimateRequestDto;
import com.clone.wanted.application.responseDto.CompanyApplicationResponseDto;
import com.clone.wanted.application.responseDto.UserApplicationResponseDto;
import com.clone.wanted.config.BaseResponse;
import lombok.RequiredArgsConstructor;
import org.springframework.web.bind.annotation.*;

import java.util.List;

@RestController
@RequiredArgsConstructor
public class ApplicationController {

private final ApplicationService applicationService;
/**
* @param email
* @return BaseResponse
* TODO: spring security 단 이후 email 을 Authentication 으로 변경
*/
@PostMapping("api/v1/applications/{employmentId}")
public BaseResponse<Void> enroll(@RequestBody String email, @PathVariable Long employmentId) {
applicationService.enroll(email, employmentId);
return BaseResponse.success();
}

@PatchMapping("api/v1/applications/{employmentId}/estimate")
public BaseResponse<Void> estimate(@RequestBody String email, @RequestBody EstimateRequestDto requestDto, @PathVariable Long employmentId){
applicationService.estimate(email, requestDto, employmentId);
return BaseResponse.success();
}

@PatchMapping("api/v1/applications/{employmentId}/cancel")
public BaseResponse<Void> cancel(@RequestBody String email, @PathVariable Long employmentId){
applicationService.cancel(email, employmentId);
return BaseResponse.success();
}

@GetMapping("api/v1/applications")
public BaseResponse<List<UserApplicationResponseDto>> getUserApplicationList(@RequestBody String email){
return BaseResponse.success(applicationService.getUserApplications(email));
}

@GetMapping("api/v1/applications/{employmentId}")
public BaseResponse<List<CompanyApplicationResponseDto>> getCompanyApplicationList(@RequestBody String email, @PathVariable Long employmentId){
return BaseResponse.success(applicationService.getCompanyApplications(email, employmentId));
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,17 @@
package com.clone.wanted.application;

import com.clone.wanted.User.User;
import com.clone.wanted.employment.Employment;
import org.springframework.data.jpa.repository.JpaRepository;
import org.springframework.stereotype.Repository;

import java.util.List;
import java.util.Optional;

@Repository
interface ApplicationRepository extends JpaRepository<Application, Long> {

Optional<Application> findByUserAndEmployment(User suer, Employment employment);
List<Application> findAllByUser(User user);
List<Application> findAllByEmployment(Employment employment);
}
88 changes: 88 additions & 0 deletions src/main/java/com/clone/wanted/application/ApplicationService.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,88 @@
package com.clone.wanted.application;

import com.clone.wanted.Company.Company;
import com.clone.wanted.Company.CompanyRepository;
import com.clone.wanted.User.User;
import com.clone.wanted.User.UserRepository;
import com.clone.wanted.User.UserType;
import com.clone.wanted.application.requestDto.EstimateRequestDto;
import com.clone.wanted.application.responseDto.CompanyApplicationResponseDto;
import com.clone.wanted.application.responseDto.UserApplicationResponseDto;
import com.clone.wanted.config.BaseException;
import com.clone.wanted.config.BaseResponseStatus;
import com.clone.wanted.employment.Employment;
import com.clone.wanted.employment.EmploymentRepository;
import lombok.RequiredArgsConstructor;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.util.List;

@Service
@RequiredArgsConstructor
@Transactional
public class ApplicationService {

private final UserRepository userRepository;
private final EmploymentRepository employmentRepository;
private final ApplicationRepository applicationRepository;
private final CompanyRepository companyRepository;
public void enroll(String email, Long employmentId) {
User user = userRepository.findByEmail(email)
.orElseThrow(() -> new BaseException(BaseResponseStatus.USER_NOT_FOUND));
Employment employment = employmentRepository.findById(employmentId)
.orElseThrow(() -> new BaseException(BaseResponseStatus.EMPLOYMENT_NOT_FOUND));
applicationRepository.findByUserAndEmployment(user, employment)
.ifPresent((application -> {
if(application.getApplicationStatus() == ApplicationStatus.CANCEL) {
application.updateStatus(ApplicationStatus.ONGOING);
} else throw new BaseException(BaseResponseStatus.APPLICATION_ALREADY_EXIST);
}));
applicationRepository.save(Application.newEnrollment(user, employment));
}

public void estimate(String email, EstimateRequestDto requestDto, Long employmentId) {
User user = userRepository.findByEmail(email)
.orElseThrow(() -> new BaseException(BaseResponseStatus.USER_NOT_FOUND));
if(user.getUserType() == UserType.CORPORATE_USER) throw new BaseException(BaseResponseStatus.REQUEST_NOT_ALLOWED);
Employment employment = employmentRepository.findById(employmentId)
.orElseThrow(() -> new BaseException(BaseResponseStatus.EMPLOYMENT_NOT_FOUND));
Application application = applicationRepository.findByUserAndEmployment(user, employment)
.orElseThrow(() -> new BaseException(BaseResponseStatus.EMPLOYMENT_NOT_FOUND));
application.updateStatus(ApplicationStatus.returnStatus(requestDto.getApplicationStatus()));
}

public void cancel(String email, Long employmentId) {
User user = userRepository.findByEmail(email)
.orElseThrow(() -> new BaseException(BaseResponseStatus.USER_NOT_FOUND));
Employment employment = employmentRepository.findById(employmentId)
.orElseThrow(() -> new BaseException(BaseResponseStatus.EMPLOYMENT_NOT_FOUND));
Application application = applicationRepository.findByUserAndEmployment(user, employment)
.orElseThrow(() -> new BaseException(BaseResponseStatus.EMPLOYMENT_NOT_FOUND));
application.updateStatus(ApplicationStatus.CANCEL);
}

public List<UserApplicationResponseDto> getUserApplications(String email) {
User user = userRepository.findByEmail(email)
.orElseThrow(() -> new BaseException(BaseResponseStatus.USER_NOT_FOUND));
return applicationRepository.findAllByUser(user)
.stream()
.map(UserApplicationResponseDto::of)
.toList();
}

public List<CompanyApplicationResponseDto> getCompanyApplications(String email, Long employmentId) {
User user = userRepository.findByEmail(email)
.orElseThrow(() -> new BaseException(BaseResponseStatus.USER_NOT_FOUND));
if(user.getUserType() == UserType.CORPORATE_USER) throw new BaseException(BaseResponseStatus.REQUEST_NOT_ALLOWED);
Company company = companyRepository.findByUser(user)
.orElseThrow(() -> new BaseException(BaseResponseStatus.COMPANY_NOT_FOUND));
if(company.getUser() != user) throw new BaseException(BaseResponseStatus.REQUEST_NOT_ALLOWED);
Employment employment = employmentRepository.findById(employmentId)
.orElseThrow(() -> new BaseException(BaseResponseStatus.EMPLOYMENT_NOT_FOUND));
return applicationRepository.findAllByEmployment(employment)
.stream()
.map(CompanyApplicationResponseDto::of)
.toList();
}
}
Original file line number Diff line number Diff line change
@@ -1,5 +1,11 @@
package com.clone.wanted.application;

public enum ApplicationStatus {
ONGOING, CANCEL, FAIL, PASS
ONGOING, CANCEL, FAIL, PASS;
public static ApplicationStatus returnStatus(String type){
if(type.equals("ONGOING")) return ApplicationStatus.ONGOING;
if(type.equals("CANCEL")) return ApplicationStatus.CANCEL;
if(type.equals("FAIL")) return ApplicationStatus.FAIL;
return ApplicationStatus.PASS;
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,10 @@
package com.clone.wanted.application.requestDto;

import lombok.Getter;
import lombok.NoArgsConstructor;

@NoArgsConstructor
@Getter
public class EstimateRequestDto {
public String applicationStatus;
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,29 @@
package com.clone.wanted.application.responseDto;

import com.clone.wanted.application.Application;
import lombok.AllArgsConstructor;
import lombok.Getter;
import lombok.NoArgsConstructor;

import java.time.LocalDateTime;

@Getter
@NoArgsConstructor
@AllArgsConstructor
public class CompanyApplicationResponseDto {

private Long employmentId;
private String username;
private String position;
private LocalDateTime registerDate;
private String applicationStatus;

public static CompanyApplicationResponseDto of(Application application){
return new CompanyApplicationResponseDto(
application.getEmployment().getId(),
application.getUser().getUsername(),
application.getEmployment().getEmploymentTitle(),
application.getCreatedAt(),
application.getApplicationStatus().name());
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,29 @@
package com.clone.wanted.application.responseDto;

import com.clone.wanted.application.Application;
import lombok.AllArgsConstructor;
import lombok.Getter;
import lombok.NoArgsConstructor;

import java.time.LocalDate;
import java.time.LocalDateTime;

@Getter
@NoArgsConstructor
@AllArgsConstructor
public class UserApplicationResponseDto {
private Long employmentId;
private String companyName;
private String position;
private LocalDateTime registerDate;
private String applicationStatus;

public static UserApplicationResponseDto of(Application application){
return new UserApplicationResponseDto(
application.getEmployment().getId(),
application.getEmployment().getCompany().getCompanyName(),
application.getEmployment().getEmploymentTitle(),
application.getCreatedAt(),
application.getApplicationStatus().name());
}
}
2 changes: 1 addition & 1 deletion src/main/java/com/clone/wanted/config/BaseException.java
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,7 @@
@NoArgsConstructor

@ResponseStatus(code= HttpStatus.NOT_FOUND)
public class BaseException extends Exception {
public class BaseException extends RuntimeException {
private BaseResponseStatus status; //BaseResoinseStatus 객체에 매핑

}
Expand Down
3 changes: 3 additions & 0 deletions src/main/java/com/clone/wanted/config/BaseResponse.java
Original file line number Diff line number Diff line change
Expand Up @@ -40,6 +40,9 @@ public BaseResponse() {
public static BaseResponse<Void> success(){
return new BaseResponse<>();
}
public static <T> BaseResponse<T> success(T result){
return new BaseResponse<>(result);
}

// 요청에 실패한 경우
public BaseResponse(BaseResponseStatus status){
Expand Down
23 changes: 22 additions & 1 deletion src/main/java/com/clone/wanted/config/BaseResponseStatus.java
Original file line number Diff line number Diff line change
Expand Up @@ -36,11 +36,32 @@ public enum BaseResponseStatus {
* 4000 : Database, Server 오류
*/
DATABASE_ERROR(false, 4000, "데이터베이스 연결에 실패하였습니다."),
SERVER_ERROR(false, 4001, "서버와의 연결에 실패하였습니다.");
SERVER_ERROR(false, 4001, "서버와의 연결에 실패하였습니다."),


/*TODO 필요한 에러 메세지 추가해주세요- 하비 */
/**
* 5000 : User Exception
*/
USER_NOT_FOUND(false, 5000, "해당 유저를 찾을 수 없습니다."),
REQUEST_NOT_ALLOWED(false, 5001, "해당 기능을 사용할 권한이 없습니다."),


/**
* 6000 : Employment Exception
*/
EMPLOYMENT_NOT_FOUND(false, 6000, "해당 공고를 찾을 수 없습니다."),

/**
* 7000 : Employment Exception
*/
APPLICATION_ALREADY_EXIST(false, 7000, "이미 지원한 공고입니다."),
APPLICATION_NOT_EXIST(false, 7001, "지원 내역이 존재하지 않습니다."),

/**
* 8000 : Employment Exception
*/
COMPANY_NOT_FOUND(false, 8000, "해당 유저가 관리하는 회사 정보가 없습니다.");


private boolean isSuccess;
Expand Down
8 changes: 2 additions & 6 deletions src/main/java/com/clone/wanted/employment/Employment.java
Original file line number Diff line number Diff line change
Expand Up @@ -3,14 +3,10 @@

import com.clone.wanted.BaseEntity;
import com.clone.wanted.Company.Company;
import com.clone.wanted.employment.requestDto.EmploymentReqDto;
import jakarta.persistence.*;
import lombok.Getter;
import lombok.NoArgsConstructor;
import org.hibernate.annotations.DynamicInsert;
import org.hibernate.annotations.DynamicUpdate;

import java.time.DateTimeException;
import java.time.LocalDateTime;

@Getter
@NoArgsConstructor
Expand All @@ -35,7 +31,7 @@ public class Employment extends BaseEntity {
private int applicantReward;


public Employment(Company company,EmploymentReqDto employmentReqDto) {
public Employment(Company company, EmploymentReqDto employmentReqDto) {
this.company = company;
this.employmentTitle = employmentReqDto.getEmploymentTitle();
this.employmentContents = employmentReqDto.getEmploymentTitle();
Expand Down
Loading

0 comments on commit 1739bb4

Please sign in to comment.