120 lines
3.8 KiB
TypeScript
120 lines
3.8 KiB
TypeScript
import { inject, injectable } from "inversify";
|
|
import { isValidObjectId } from "mongoose";
|
|
|
|
import { DisplayLocations } from "../../common/enums/media.enum";
|
|
import { CommonMessage, MediaMessage } from "../../common/enums/message.enum";
|
|
import { BadRequestError } from "../../core/app/app.errors";
|
|
import { IOCTYPES } from "../../IOC/ioc.types";
|
|
import { CreateBannerDTO, CreateSliderDTO, UpdateBannerDTO, UpdateSliderDTO } from "../admin/DTO/media.dto";
|
|
import { BannerRepository, SliderRepository } from "../admin/repository/media";
|
|
|
|
@injectable()
|
|
export class MediaService {
|
|
@inject(IOCTYPES.BannerRepository) bannerRepo: BannerRepository;
|
|
@inject(IOCTYPES.SliderRepository) sliderRepo: SliderRepository;
|
|
|
|
async getSliders() {
|
|
return this.sliderRepo.model.find({ isActive: true });
|
|
}
|
|
|
|
async getSliderForMobile() {
|
|
return this.sliderRepo.model.find({ isActive: true, displayLocation: DisplayLocations.Mobile });
|
|
}
|
|
|
|
async getSliderForDesktop() {
|
|
return this.sliderRepo.model.find({ isActive: true, displayLocation: DisplayLocations.Desktop });
|
|
}
|
|
|
|
async getBanners() {
|
|
return this.bannerRepo.model.find({ isActive: true });
|
|
}
|
|
|
|
async getAllBannersForAdmin() {
|
|
return { banners: await this.bannerRepo.findAll() };
|
|
}
|
|
async getAllSlidersForAdmin() {
|
|
return { sliders: await this.sliderRepo.findAll() };
|
|
}
|
|
|
|
async createSlider(createSliderDto: CreateSliderDTO) {
|
|
const newSlider = await this.sliderRepo.model.create({
|
|
...createSliderDto,
|
|
});
|
|
return {
|
|
message: MediaMessage.SliderCreated,
|
|
newSlider,
|
|
};
|
|
}
|
|
|
|
async createBanner(createBannerDto: CreateBannerDTO) {
|
|
const newBanner = await this.bannerRepo.model.create({
|
|
...createBannerDto,
|
|
});
|
|
return {
|
|
message: MediaMessage.BannerCreated,
|
|
newBanner,
|
|
};
|
|
}
|
|
|
|
async updateSlider(sliderId: string, updateSliderDto: UpdateSliderDTO) {
|
|
await this.validateMedia(sliderId);
|
|
const updatedSlider = await this.sliderRepo.model.findByIdAndUpdate(sliderId, updateSliderDto, { new: true });
|
|
return {
|
|
message: CommonMessage.Updated,
|
|
updatedSlider,
|
|
};
|
|
}
|
|
|
|
async updateBanner(bannerId: string, updateBannerDto: UpdateBannerDTO) {
|
|
await this.validateMedia(bannerId);
|
|
const updatedBanner = await this.bannerRepo.model.findByIdAndUpdate(bannerId, updateBannerDto, { new: true });
|
|
return {
|
|
message: CommonMessage.Updated,
|
|
updatedBanner,
|
|
};
|
|
}
|
|
|
|
async deleteMedia(mediaId: string, type: "banner" | "slider") {
|
|
await this.validateMedia(mediaId);
|
|
if (type === "banner") await this.bannerRepo.delete(mediaId);
|
|
else if (type === "slider") await this.sliderRepo.delete(mediaId);
|
|
return {
|
|
message: CommonMessage.Deleted,
|
|
};
|
|
}
|
|
|
|
async activateMediaS(MediaId: string, isActive: boolean, type: "banner" | "slider") {
|
|
await this.validateMedia(MediaId);
|
|
if (type === "banner") await this.bannerRepo.model.findByIdAndUpdate(MediaId, { isActive }, { new: true });
|
|
else if (type === "slider") await this.sliderRepo.model.findByIdAndUpdate(MediaId, { isActive }, { new: true });
|
|
return {
|
|
message: MediaMessage.Activated,
|
|
};
|
|
}
|
|
|
|
async getSliderById(sliderId: string) {
|
|
await this.validateMedia(sliderId);
|
|
return {
|
|
slider: await this.sliderRepo.findById(sliderId),
|
|
};
|
|
}
|
|
|
|
async getBannerById(bannerId: string) {
|
|
await this.validateMedia(bannerId);
|
|
return {
|
|
slider: await this.bannerRepo.findById(bannerId),
|
|
};
|
|
}
|
|
|
|
//=======================================>
|
|
//=======================================>
|
|
//helper method
|
|
|
|
async validateMedia(mediaId: string) {
|
|
if (!isValidObjectId(mediaId)) throw new BadRequestError(CommonMessage.NotValidId);
|
|
const media = (await this.sliderRepo.findById(mediaId)) || (await this.bannerRepo.findById(mediaId));
|
|
if (!media) throw new BadRequestError(CommonMessage.NotValidId);
|
|
return media;
|
|
}
|
|
}
|