2025-10-10
项目收藏
This commit is contained in:
@@ -1,5 +1,5 @@
|
||||
import {Injectable} from "@nestjs/common";
|
||||
import {ProgramEntity, ProgramGroupEntity, UsersEntity} from "../entity";
|
||||
import {ProgramEntity, ProgramGroupEntity, UsersEntity, ProgramCollectEntity} from "../entity";
|
||||
import {Repository} from "typeorm";
|
||||
import {InjectRepository} from '@nestjs/typeorm';
|
||||
import {ResultData} from "../const/result";
|
||||
@@ -10,6 +10,7 @@ export class ProgramService {
|
||||
constructor(
|
||||
@InjectRepository(ProgramEntity) private readonly programRepository: Repository<ProgramEntity>,
|
||||
@InjectRepository(ProgramGroupEntity) private readonly groupRepository: Repository<ProgramGroupEntity>,
|
||||
@InjectRepository(ProgramCollectEntity) private readonly collectRepository: Repository<ProgramCollectEntity>,
|
||||
) {
|
||||
}
|
||||
|
||||
@@ -119,4 +120,132 @@ export class ProgramService {
|
||||
return ResultData.fail(500, err.message);
|
||||
}
|
||||
}
|
||||
|
||||
// 收藏项目
|
||||
async collect(collect: ProgramCollectEntity) {
|
||||
try {
|
||||
// 判断参数
|
||||
if (!collect.programId || !collect.userId) {
|
||||
throw new Error("参数错误");
|
||||
}
|
||||
|
||||
// 判断是否已收藏
|
||||
let collectSearchResult = await this.collectRepository.findOne({
|
||||
where: {
|
||||
programId: collect.programId,
|
||||
userId: collect.userId
|
||||
}
|
||||
})
|
||||
if (collectSearchResult != null) {
|
||||
throw new Error("该项目已收藏");
|
||||
}
|
||||
// 没有记录则进行添加收藏
|
||||
let result = await this.collectRepository.save(collect)
|
||||
return result != null ? ResultData.success() : ResultData.fail(500, "收藏失败");
|
||||
} catch (err) {
|
||||
return ResultData.fail(500, err.message);
|
||||
}
|
||||
}
|
||||
|
||||
// 取消收藏项目
|
||||
async uncollect(collect: ProgramCollectEntity) {
|
||||
try {
|
||||
// 判断参数
|
||||
if (!collect.programId || !collect.userId) {
|
||||
throw new Error("参数错误");
|
||||
}
|
||||
const result = await this.collectRepository.delete(collect)
|
||||
return (result.affected != null && result.affected > 0) ? ResultData.success() : ResultData.fail(500, "取消收藏失败");
|
||||
} catch (err) {
|
||||
return ResultData.fail(500, err.message);
|
||||
}
|
||||
}
|
||||
|
||||
// 收藏列表
|
||||
async collectList(collect: ProgramCollectEntity) {
|
||||
try {
|
||||
collect.page = collect.page || 1;
|
||||
collect.size = collect.size || 10;
|
||||
const offset = (collect.page - 1) * collect.size;
|
||||
const queryBuilder = this.programRepository.createQueryBuilder('collect');
|
||||
queryBuilder.leftJoinAndMapOne(
|
||||
'collect.program',
|
||||
ProgramEntity,
|
||||
'program',
|
||||
'program.id = collect.program_id'
|
||||
)
|
||||
queryBuilder.leftJoinAndMapOne(
|
||||
'collect.user',
|
||||
UsersEntity,
|
||||
'users',
|
||||
'users.id = collect.user_id'
|
||||
)
|
||||
queryBuilder.where('collect.user_id = :userId', {userId: collect.userId});
|
||||
|
||||
queryBuilder
|
||||
.orderBy('program.createDate', 'DESC')
|
||||
.take(collect.size || 10)
|
||||
.skip(offset || 0);
|
||||
|
||||
const [items, total] = await queryBuilder.getManyAndCount();
|
||||
|
||||
let data = {
|
||||
list: items,
|
||||
total,
|
||||
page: collect.page || 1,
|
||||
size: collect.size || 10,
|
||||
pages: Math.ceil(total / (collect.size || 10))
|
||||
}
|
||||
return ResultData.success(data);
|
||||
} catch (err) {
|
||||
return ResultData.fail(500, err.message);
|
||||
}
|
||||
}
|
||||
|
||||
// 是否已收藏
|
||||
async isCollect(collect: ProgramCollectEntity) {
|
||||
try {
|
||||
// 判断参数
|
||||
if (!collect.programId || !collect.userId) {
|
||||
throw new Error("参数错误");
|
||||
}
|
||||
// 是否存在数据
|
||||
let result = await this.collectRepository.findOne({
|
||||
where: {
|
||||
programId: collect.programId,
|
||||
userId: collect.userId
|
||||
}
|
||||
})
|
||||
return ResultData.success({isCollect: result != null}, "success")
|
||||
} catch (err) {
|
||||
return ResultData.fail(500, err.message);
|
||||
}
|
||||
}
|
||||
|
||||
async random(collect: ProgramCollectEntity) {
|
||||
try {
|
||||
if (!collect.userId || !collect.programId){
|
||||
throw new Error("参数错误");
|
||||
}
|
||||
const queryBuilder = this.programRepository.createQueryBuilder("program")
|
||||
const result = await queryBuilder
|
||||
.leftJoin(
|
||||
ProgramCollectEntity, // 收藏表实体名称
|
||||
"favorite",
|
||||
"favorite.program_id = program.id AND favorite.user_id = :userId",
|
||||
{ userId: collect.userId }
|
||||
)
|
||||
.where("favorite.id IS NULL") // 没有收藏记录
|
||||
.andWhere("program.id != :programId", { programId: collect.programId })
|
||||
.orderBy("RAND()")
|
||||
.select("program.id", "id") // 选择ID字段
|
||||
.getRawOne()
|
||||
if (result == null) {
|
||||
return ResultData.fail(500, "没有更多项目");
|
||||
}
|
||||
return ResultData.success(result);
|
||||
}catch (err){
|
||||
return ResultData.fail(500, err.message);
|
||||
}
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user