78 lines
2.5 KiB
TypeScript
78 lines
2.5 KiB
TypeScript
import { HttpException, Injectable } from '@nestjs/common';
|
|
import { InjectRepository } from '@nestjs/typeorm';
|
|
import { Carousal } from '../entity/carousal.entity';
|
|
import { Repository } from 'typeorm';
|
|
import { CreateCarousalRequest } from '../dto/create-carousal.request';
|
|
import { SearchCarousalFilter } from '../dto/search-carousal.filter';
|
|
import { CarousalStatus } from '../enum/carousal-status.enum';
|
|
|
|
@Injectable()
|
|
export class CarousalService {
|
|
constructor(
|
|
@InjectRepository(Carousal)
|
|
private readonly carousalRepository: Repository<Carousal>,
|
|
) {}
|
|
|
|
create(createCarousalRequest: CreateCarousalRequest) {
|
|
const carousal = this.carousalRepository.create({
|
|
imageUrl: createCarousalRequest.imageUrl,
|
|
targetType: createCarousalRequest.targetType,
|
|
targetId: createCarousalRequest.targetId,
|
|
targetUrl: createCarousalRequest.targetUrl,
|
|
});
|
|
return this.carousalRepository.save(carousal);
|
|
}
|
|
|
|
async search(searchCarousalFilter: SearchCarousalFilter) {
|
|
return this.carousalRepository.findAndCount({
|
|
where: searchCarousalFilter.getConditions(),
|
|
skip: searchCarousalFilter.getSkip(),
|
|
take: searchCarousalFilter.getSize(),
|
|
order: searchCarousalFilter.getOrderBy(),
|
|
});
|
|
}
|
|
|
|
async get(id: string) {
|
|
const carousal = await this.carousalRepository.findOneBy({ id });
|
|
if (!carousal) {
|
|
throw new HttpException('Not Found', 404);
|
|
}
|
|
return carousal;
|
|
}
|
|
|
|
async update(id: string, updateCarousalRequest: CreateCarousalRequest) {
|
|
const carousal = await this.get(id);
|
|
carousal.imageUrl = updateCarousalRequest.imageUrl;
|
|
carousal.targetType = updateCarousalRequest.targetType;
|
|
carousal.targetId = updateCarousalRequest.targetId;
|
|
carousal.targetUrl = updateCarousalRequest.targetUrl;
|
|
return this.carousalRepository.save(carousal);
|
|
}
|
|
|
|
async active(id: string) {
|
|
const carousal = await this.get(id);
|
|
carousal.status = CarousalStatus.ACTIVE;
|
|
return this.carousalRepository.save(carousal);
|
|
}
|
|
|
|
async inactive(id: string) {
|
|
const carousal = await this.get(id);
|
|
carousal.status = CarousalStatus.INACTIVE;
|
|
return this.carousalRepository.save(carousal);
|
|
}
|
|
|
|
async delete(id: string) {
|
|
const carousal = await this.get(id);
|
|
return this.carousalRepository.remove(carousal);
|
|
}
|
|
|
|
async activeList() {
|
|
return this.carousalRepository.find({
|
|
where: {
|
|
status: CarousalStatus.ACTIVE,
|
|
},
|
|
order: { createdTime: 'DESC' },
|
|
});
|
|
}
|
|
}
|