import { IMedia } from "../interfaces/media.interface" ; import { InjectModel } from "@nestjs/mongoose" ; import { Model } from "mongoose" ; import { CreateMediaDto } from "../dto/create-media.dto" ; import { Media } from "../models/media.model" ; import { UpdateMediaDto } from "../dto/update-media.dto" ; import { ErrorMessages } from "../medias.constants" ; import { UpdateMediaCreatorDto } from "../dto/update-media-creator.dto" ; export class MediaRepository { constructor ( @ InjectModel ( 'Media' ) private readonly mediaModel ) { } async createMedia ( createMediaDto ) { const createdMedia = await new this . mediaModel ( createMediaDto ) . save ( ) ; return new Media ( createdMedia . _id ) ; } async updateMedia ( updateMediaDto ) { const updatedMedia = await this . mediaModel . findOneAndUpdate ( { _id : updateMediaDto . _id } , { $set : updateMediaDto } ) . exec ( ) ; if ( ! updatedMedia ) { throw new Error ( ErrorMessages . MEDIA_NOT_FOUND ) ; } return new Media ( updatedMedia . _id ) ; } async updateMediaCreator ( updateMediaCreator ) { const mediaToUpdate = await this . mediaModel . findById ( updateMediaCreator . _id ) . exec ( ) ; if ( ! mediaToUpdate ) { throw new Error ( ErrorMessages . MEDIA_NOT_FOUND ) ; } Object . keys ( updateMediaCreator . creator ) . forEach ( k => { mediaToUpdate . creator [ k ] = updateMediaCreator . creator [ k ] || mediaToUpdate . creator [ k ] ; } ) ; await mediaToUpdate . save ( ) ; return new Media ( mediaToUpdate . _id ) ; } async findByCreatorId ( id ) { return await this . mediaModel . find ( { "creator._id" : id } ) . exec ( ) ; } async findById ( id ) { const media = await this . mediaModel . findById ( id ) . exec ( ) ; if ( ! media ) { throw new Error ( ErrorMessages . MEDIA_NOT_FOUND ) ; } return media ; } async findAll ( ) { return await this . mediaModel . find ( ) . exec ( ) ; } }