添加收藏相关接口

This commit is contained in:
zk
2026-03-20 19:29:47 +08:00
parent e2fb308655
commit 1d2d17d867
2 changed files with 52 additions and 1 deletions
@@ -41,4 +41,22 @@ public class JobController {
Long userId = UserSecurityTool.getUserId();
return jobService.getJobDetail(jobId, userId);
}
/**
* 收藏岗位
*/
@PostMapping("/{jobId}/favorite")
public void favoriteJob(@PathVariable Long jobId) {
Long userId = UserSecurityTool.getUserId();
jobService.favoriteJob(jobId, userId);
}
/**
* 取消收藏岗位
*/
@DeleteMapping("/{jobId}/favorite")
public void unfavoriteJob(@PathVariable Long jobId) {
Long userId = UserSecurityTool.getUserId();
jobService.unfavoriteJob(jobId, userId);
}
}
@@ -15,12 +15,13 @@ import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import java.time.Instant;
import java.util.*;
import java.util.stream.Collectors;
/**
* 岗位服务
* <p>主要功能:岗位列表查询、岗位详情查询、匹配度计算</p>
* <p>主要功能:岗位列表查询、岗位详情查询、岗位收藏管理、匹配度计算</p>
* <p>依赖服务:JobMatchService(匹配度计算)</p>
* <p>使用的表:bg_job、bg_company、bg_user_job_dislike、bg_user_job_favorite、bg_china_regions_code、bg_job_category、bg_industry</p>
*
@@ -261,4 +262,36 @@ public class JobService {
return dto;
}
/**
* 收藏岗位
* <p>方法逻辑流程:</p>
* <p>1. 检查岗位是否存在</p>
* <p>2. 检查是否已收藏(避免重复)</p>
* <p>3. 插入收藏记录</p>
*/
public void favoriteJob(Long jobId, Long userId) {
// 1. 检查岗位是否存在
Job job = jobMapper.selectOne(new LambdaQueryWrapper<Job>().eq(Job::getId, jobId).eq(Job::getStatus, 0));
if (job == null) throw new RuntimeException("岗位不存在或已下架");
// 2. 检查是否已收藏
Long count = userJobFavoriteMapper.selectCount(new LambdaQueryWrapper<UserJobFavorite>().eq(UserJobFavorite::getUserId, userId).eq(UserJobFavorite::getJobId, jobId));
if (count > 0) throw new RuntimeException("已收藏该岗位");
// 3. 插入收藏记录
UserJobFavorite favorite = new UserJobFavorite();
favorite.setUserId(userId);
favorite.setJobId(jobId);
favorite.setCreateTime(Instant.now());
userJobFavoriteMapper.insert(favorite);
}
/**
* 取消收藏岗位
* <p>方法逻辑流程:</p>
* <p>1. 删除收藏记录</p>
*/
public void unfavoriteJob(Long jobId, Long userId) {
userJobFavoriteMapper.delete(new LambdaQueryWrapper<UserJobFavorite>().eq(UserJobFavorite::getUserId, userId).eq(UserJobFavorite::getJobId, jobId));
}
}