Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

#4 feat: 채용공고 지원하기 API 개발 #10

Open
wants to merge 4 commits into
base: main
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -4,8 +4,7 @@
import com.example.jehunonboarding.controller.response.CommonResponse;
import com.example.jehunonboarding.controller.response.JobPostingFindDetailResponse;
import com.example.jehunonboarding.controller.response.JobPostingsSearchResponse;
import com.example.jehunonboarding.domain.JobPosting;
import com.example.jehunonboarding.domain.JobPostingService;
import com.example.jehunonboarding.domain.*;
import jakarta.validation.Valid;
import lombok.RequiredArgsConstructor;
import org.springframework.data.domain.Pageable;
Expand All @@ -28,12 +27,14 @@ public ResponseEntity<CommonResponse> register(@Valid @RequestBody JobPostingReg
}

@PutMapping("/v1/job-postings/{jobPostingId}")
public ResponseEntity<CommonResponse> edit(@RequestBody JobPostingEditRequest request) {
public ResponseEntity<CommonResponse> edit(@PathVariable int jobPostingId, @Valid @RequestBody JobPostingEditInfo request) {
jobPostingService.edit(jobPostingId, request);
return new ResponseEntity(new CommonResponse(true), HttpStatus.OK);
}

@DeleteMapping("/v1/job-postings/{jobPostingId}")
public ResponseEntity<CommonResponse> remove(@PathVariable int jobPostingId) {
@DeleteMapping("/v1/job-postings/{jobPostingId}/{companyId}")
public ResponseEntity<CommonResponse> remove(@PathVariable int companyId, @PathVariable int jobPostingId) {
jobPostingService.remove(companyId, jobPostingId);
return new ResponseEntity(new CommonResponse(true), HttpStatus.OK);
}

Expand All @@ -45,12 +46,13 @@ public ResponseEntity<JobPostingsSearchResponse> search(String keyword, Pageable

@GetMapping("/v1/job-postings/{jobPostingId}")
public ResponseEntity<JobPostingFindDetailResponse> findDetail(@PathVariable int jobPostingId) {
return new ResponseEntity(new JobPostingFindDetailResponse(), HttpStatus.OK);
List<JobPosting> jobPostings = jobPostingService.findDetail(jobPostingId);
return new ResponseEntity(new JobPostingFindDetailResponse(jobPostings), HttpStatus.OK);
}

@PostMapping("/v1/job-postings/{jobPostingId}/apply")
public ResponseEntity<CommonResponse> apply(@RequestBody JobPostingApplyRequest request) {
public ResponseEntity<CommonResponse> apply(@PathVariable int jobPostingId, @Valid @RequestBody JobPostingApplyRequest request) {
jobPostingService.apply(jobPostingId, request);
return new ResponseEntity(new CommonResponse(true), HttpStatus.OK);
}

}
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,6 @@

@Data
public class JobPostingApplyRequest {
private int companyId;
private int jopPostingId;
private int userId;
}
Original file line number Diff line number Diff line change
@@ -1,14 +1,18 @@
package com.example.jehunonboarding.controller.request;

import jakarta.validation.constraints.NotNull;
import lombok.Data;

@Data
public class JobPostingEditRequest {
private String companyName;
private String nation;
private String region;
@NotNull
private int companyId;
@NotNull
private String jobPosition;
@NotNull
private long jobCompensation;
@NotNull
private String description;
@NotNull
private String skill;
}
Original file line number Diff line number Diff line change
@@ -1,16 +1,40 @@
package com.example.jehunonboarding.controller.response;

import com.example.jehunonboarding.domain.JobPosting;
import lombok.Data;

import java.util.List;
import java.util.stream.Collectors;

@Data
public class JobPostingFindDetailResponse {

private int companyId;
private String companyName;
private String nation;
private String region;
private String jobPosition;
private long jobCompensation;
private String description;
private String skill;
private List<JobPostingResponse> details;

public JobPostingFindDetailResponse(List<JobPosting> jobPostings) {
this.details = jobPostings.stream()
.map(JobPostingResponse::new)
.toList();
}

@Data
static class JobPostingResponse {
private int companyId;
private String companyName;
private String nation;
private String region;
private String jobPosition;
private long jobCompensation;
private String skill;

public JobPostingResponse(JobPosting jobPosting) {
this.companyId = jobPosting.getCompanyId();
this.companyName = jobPosting.getCompanyName();
this.nation = jobPosting.getNation();
this.region = jobPosting.getRegion();
this.jobPosition = jobPosting.getJobPosition();
this.jobCompensation = jobPosting.getJobCompensation();
this.skill = jobPosting.getSkill();
}
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,14 @@
package com.example.jehunonboarding.controller.response;

import lombok.Data;

@Data
public class JobPostingSearchResponse {
private int companyId;
private String companyName;
private String nation;
private String region;
private String jobPosition;
private long jobCompensation;
private String skill;
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,14 @@
package com.example.jehunonboarding.domain;

import lombok.AllArgsConstructor;
import lombok.Getter;

@Getter
@AllArgsConstructor
public class JobPostingEditInfo {
private int companyId;
private String jobPosition;
private long jobCompensation;
private String description;
private String skill;
}
Original file line number Diff line number Diff line change
@@ -1,8 +1,12 @@
package com.example.jehunonboarding.domain;

import jakarta.persistence.*;
import jakarta.validation.Valid;
import lombok.NoArgsConstructor;

import java.util.HashSet;
import java.util.Set;

@Entity
@Table(name = "job_posting")
@NoArgsConstructor
Expand All @@ -27,11 +31,34 @@ public class JobPostingEntity {
@Column(nullable = false)
private String skill;

// 채용공고 지원에서 필요하며 중복을 피하기 위한 HashSet
@ElementCollection
private Set<String> applicants = new HashSet<>();

public JobPostingEntity(int companyId, String jobPosition, long jobCompensation, String description, String skill) {
this.companyId = companyId;
this.jobPosition = jobPosition;
this.jobCompensation = jobCompensation;
this.description = description;
this.skill = skill;
}

public void editJobPosting(JobPostingEditInfo editInfo) {
this.jobPosition = editInfo.getJobPosition();
this.jobCompensation = editInfo.getJobCompensation();
this.description = editInfo.getDescription();
this.skill = editInfo.getSkill();
}

public boolean removeJobPosting(int companyId, int jobPostingId) {
return this.companyId == companyId && this.id == jobPostingId;
}

public boolean isApplicant(String userId) {
return applicants.contains(userId);
}

public void addApplicant(int userId) {
applicants.add(String.valueOf(userId));
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,10 @@
package com.example.jehunonboarding.domain;

import lombok.AllArgsConstructor;
import lombok.Getter;

@Getter
@AllArgsConstructor
public class JobPostingRemoveInfo {
private int companyId;
}
Original file line number Diff line number Diff line change
@@ -1,7 +1,10 @@
package com.example.jehunonboarding.domain;

import com.example.jehunonboarding.controller.request.JobPostingApplyRequest;
import com.example.jehunonboarding.repository.CompanyRepository;
import com.example.jehunonboarding.repository.JobPostingRepository;
import com.example.jehunonboarding.repository.UserRepository;
import jakarta.validation.Valid;
import lombok.RequiredArgsConstructor;
import org.springframework.data.domain.Pageable;
import org.springframework.stereotype.Service;
Expand Down Expand Up @@ -33,4 +36,48 @@ public void register(JobPostingRegisterInfo registerInfo) {
public List<JobPosting> search(Pageable pageable, String keyword) {
return jobPostingRepository.findByKeyword(pageable, keyword);
}

public void edit(int jobPostingId, @Valid JobPostingEditInfo editInfo) {
companyRepository.findById(editInfo.getCompanyId())
.orElseThrow(() -> new IllegalArgumentException("존재하지 않는 회사입니다."));

JobPostingEntity jobPostingEntity = jobPostingRepository.findById((long) jobPostingId)
.orElseThrow(() -> new IllegalArgumentException("존재하지 않는 채용공고입니다."));

jobPostingEntity.editJobPosting(editInfo);
jobPostingRepository.save(jobPostingEntity);
}

public void remove(int companyId, int jobPostingId) {
companyRepository.findById(companyId)
.orElseThrow(() -> new IllegalArgumentException("존재하지 않는 회사입니다."));

JobPostingEntity jobPostingEntity = jobPostingRepository.findById((long) jobPostingId)
.orElseThrow(() -> new IllegalArgumentException("존재하지 않는 채용공고입니다."));

if (jobPostingEntity.removeJobPosting(companyId, jobPostingId)) {
jobPostingRepository.delete(jobPostingEntity);
} else {
throw new IllegalArgumentException("채용공고의 회사와 삭제 요청의 회사가 일치하지 않습니다.");
}
}

public List<JobPosting> findDetail(int jobPostingId) {
return jobPostingRepository.detailPosting(jobPostingId);
}

public void apply(int jobPostingId, JobPostingApplyRequest request) {
JobPostingEntity jobPostingEntity = jobPostingRepository.findById((long) jobPostingId)
.orElseThrow(() -> new IllegalArgumentException("존재하지 않는 채용공고입니다."));

int userId = request.getUserId();

if (jobPostingEntity.isApplicant(String.valueOf(userId))) {
throw new IllegalArgumentException("이미 지원한 채용공고입니다.");
} else {
jobPostingEntity.addApplicant(userId);

jobPostingRepository.save(jobPostingEntity);
}
}
}
14 changes: 14 additions & 0 deletions src/main/java/com/example/jehunonboarding/domain/Users.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,14 @@
package com.example.jehunonboarding.domain;

import jakarta.persistence.Entity;
import jakarta.persistence.GeneratedValue;
import jakarta.persistence.Id;
import jakarta.persistence.Table;

@Entity
@Table(name = "users")
public class Users {
@Id
@GeneratedValue
private int id;
}
Original file line number Diff line number Diff line change
Expand Up @@ -12,4 +12,6 @@ public interface JobPostingRepository extends JpaRepository<JobPostingEntity, Lo

@Query("SELECT new com.example.jehunonboarding.domain.JobPosting(c.id, c.companyName, c.nation, c.region, jp.jobPosition, jp.jobCompensation, jp.description, jp.skill) FROM JobPostingEntity jp INNER JOIN Company c ON jp.companyId = c.id WHERE jp.jobPosition LIKE %:keyword% OR c.companyName LIKE %:keyword%")
List<JobPosting> findByKeyword(Pageable pageable, String keyword);
@Query("SELECT new com.example.jehunonboarding.domain.JobPosting(c.id, c.companyName, c.nation, c.region, jp.jobPosition, jp.jobCompensation, jp.description, jp.skill) FROM JobPostingEntity jp INNER JOIN Company c ON jp.companyId = c.id")
List<JobPosting> detailPosting(int jobPostingId);
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
package com.example.jehunonboarding.repository;

import com.example.jehunonboarding.domain.Users;
import org.springframework.data.jpa.repository.JpaRepository;

public interface UserRepository extends JpaRepository<Users, Integer> {
}
2 changes: 2 additions & 0 deletions src/main/resources/data.sql
Original file line number Diff line number Diff line change
@@ -1 +1,3 @@
INSERT INTO company (id, company_name, nation, region) VALUES (1, '제훈테크', '한국', '서울');

INSERT INTO users (id) VALUES (1);