-
Notifications
You must be signed in to change notification settings - Fork 2
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Browse files
Browse the repository at this point in the history
Be/#261 채용공고 목록 조회 기능 구현
- Loading branch information
Showing
10 changed files
with
82 additions
and
204 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
35 changes: 0 additions & 35 deletions
35
backend/src/main/java/com/graphy/backend/domain/job/dto/JobDto.java
This file was deleted.
Oops, something went wrong.
40 changes: 40 additions & 0 deletions
40
backend/src/main/java/com/graphy/backend/domain/job/dto/response/GetJobResponse.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,40 @@ | ||
package com.graphy.backend.domain.job.dto.response; | ||
|
||
import com.graphy.backend.domain.job.domain.Job; | ||
import java.time.LocalDateTime; | ||
import lombok.AllArgsConstructor; | ||
import lombok.Builder; | ||
import lombok.Getter; | ||
import lombok.NoArgsConstructor; | ||
import org.springframework.data.domain.Page; | ||
|
||
@Getter | ||
@Builder | ||
@NoArgsConstructor | ||
@AllArgsConstructor | ||
public class GetJobResponse { | ||
|
||
private Long id; | ||
|
||
private String companyName; | ||
|
||
private String title; | ||
|
||
private String url; | ||
|
||
private LocalDateTime expirationDate; | ||
|
||
public static GetJobResponse from(Job job) { | ||
return GetJobResponse.builder() | ||
.id(job.getId()) | ||
.companyName(job.getCompanyName()) | ||
.title(job.getTitle()) | ||
.url(job.getUrl()) | ||
.expirationDate(job.getExpirationDate()) | ||
.build(); | ||
} | ||
|
||
public static Page<GetJobResponse> listOf(Page<Job> jobs) { | ||
return jobs.map(GetJobResponse::from); | ||
} | ||
} |
12 changes: 5 additions & 7 deletions
12
backend/src/main/java/com/graphy/backend/domain/job/repository/JobRepository.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,14 +1,12 @@ | ||
package com.graphy.backend.domain.job.repository; | ||
|
||
import com.graphy.backend.domain.job.domain.Job; | ||
import org.springframework.data.domain.Page; | ||
import org.springframework.data.domain.Pageable; | ||
import org.springframework.data.jpa.repository.JpaRepository; | ||
import org.springframework.data.jpa.repository.Modifying; | ||
import org.springframework.data.jpa.repository.Query; | ||
|
||
import java.time.LocalDateTime; | ||
|
||
public interface JobRepository extends JpaRepository<Job, Long> { | ||
@Modifying | ||
@Query("DELETE FROM Job j WHERE j.expirationDate < :today") | ||
void deleteAllExpiredSince(LocalDateTime today); | ||
|
||
@Override | ||
Page<Job> findAll(Pageable pageable); | ||
} |
97 changes: 7 additions & 90 deletions
97
backend/src/main/java/com/graphy/backend/domain/job/service/JobService.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,106 +1,23 @@ | ||
package com.graphy.backend.domain.job.service; | ||
|
||
import com.graphy.backend.domain.job.domain.Job; | ||
import com.graphy.backend.domain.job.dto.JobDto; | ||
import com.graphy.backend.domain.job.dto.response.GetJobResponse; | ||
import com.graphy.backend.domain.job.repository.JobRepository; | ||
import java.util.List; | ||
import lombok.RequiredArgsConstructor; | ||
import org.springframework.beans.factory.annotation.Value; | ||
import org.springframework.data.domain.Page; | ||
import org.springframework.data.domain.Pageable; | ||
import org.springframework.stereotype.Service; | ||
import org.json.JSONObject; | ||
import org.json.JSONArray; | ||
import org.springframework.transaction.annotation.Transactional; | ||
|
||
import java.io.BufferedReader; | ||
import java.io.InputStreamReader; | ||
import java.net.HttpURLConnection; | ||
import java.net.URL; | ||
import java.net.URLEncoder; | ||
import java.time.Instant; | ||
import java.time.LocalDateTime; | ||
import java.time.ZoneId; | ||
|
||
|
||
@Service | ||
@Transactional | ||
@RequiredArgsConstructor | ||
public class JobService { | ||
@Value("${job.key}") | ||
String accessKey; | ||
private final JobRepository jobRepository; | ||
|
||
|
||
public void save() { | ||
StringBuffer response = new StringBuffer(); | ||
|
||
try { | ||
String text = URLEncoder.encode("", "UTF-8"); | ||
String apiURL = "https://oapi.saramin.co.kr/job-search?access-key=" + accessKey + "&bbs_gb=0&job_type=&edu_lv=&job_mid_cd=2"; | ||
|
||
URL url = new URL(apiURL); | ||
HttpURLConnection con = (HttpURLConnection) url.openConnection(); | ||
con.setRequestMethod("GET"); | ||
con.setRequestProperty("Accept", "application/json"); | ||
|
||
int responseCode = con.getResponseCode(); | ||
BufferedReader br; | ||
if (responseCode == 200) { // 정상 호출 | ||
br = new BufferedReader(new InputStreamReader(con.getInputStream())); | ||
} else { // 에러 발생 | ||
br = new BufferedReader(new InputStreamReader(con.getErrorStream())); | ||
} | ||
|
||
String inputLine; | ||
while ((inputLine = br.readLine()) != null) { | ||
response.append(inputLine); | ||
} | ||
br.close(); | ||
} catch (Exception e) { | ||
System.out.println(e); | ||
} | ||
|
||
saveJobInfo(response.toString()); | ||
System.out.println(response); | ||
} | ||
|
||
private void saveJobInfo(String response) { | ||
|
||
JSONObject jsonObject = new JSONObject(response); | ||
|
||
JSONArray jobsArray = jsonObject.getJSONObject("jobs").getJSONArray("job"); | ||
|
||
for (int i = 0; i < jobsArray.length(); i++) { | ||
JSONObject jobObject = jobsArray.getJSONObject(i); | ||
|
||
// 공고 ID | ||
Long jobId = jobObject.getLong("id"); | ||
|
||
// 회사 이름 | ||
String companyName = jobObject.getJSONObject("company") | ||
.getJSONObject("detail") | ||
.getString("name"); | ||
|
||
// 공고 제목 | ||
String jobTitle = jobObject.getJSONObject("position") | ||
.getString("title"); | ||
|
||
// URL | ||
String companyInfoURL = jobObject.getJSONObject("company") | ||
.getJSONObject("detail") | ||
.getString("href"); | ||
|
||
// 만료일 | ||
long expirationTimestampLong = jobObject.getLong("expiration-timestamp"); | ||
LocalDateTime expirationTimestamp = LocalDateTime.ofInstant( | ||
Instant.ofEpochSecond(expirationTimestampLong), | ||
ZoneId.systemDefault()); | ||
|
||
JobDto.CreateJobInfoRequest dto = new JobDto.CreateJobInfoRequest(jobId, companyName, jobTitle, companyInfoURL, expirationTimestamp); | ||
jobRepository.save(dto.toEntity()); | ||
} | ||
} | ||
|
||
|
||
public void deleteExpiredJobs() { | ||
jobRepository.deleteAllExpiredSince(LocalDateTime.now()); | ||
public List<GetJobResponse> findJobList(Pageable pageable) { | ||
Page<Job> jobs = jobRepository.findAll(pageable); | ||
return GetJobResponse.listOf(jobs).getContent(); | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters