Artteiv's picture
feat: add feeds
2fc4f90
raw
history blame
1.62 kB
import { Injectable, NotFoundException } from '@nestjs/common';
import { CreateFeedDto } from './dto/create-feed.dto.js';
import { UpdateFeedDto } from './dto/update-feed.dto.js';
import { FeedEntity } from '../../entities/feed.entity.js';
import { plainToClass } from 'class-transformer';
import {
FilterOperator,
paginate,
PaginateConfig,
PaginateQuery,
} from 'nestjs-paginate';
@Injectable()
export class FeedsService {
async create(createFeedDto: CreateFeedDto) {
return await FeedEntity.create({ ...createFeedDto }).save();
}
async findAll(paginateQuery: PaginateQuery) {
const paginateConfig: PaginateConfig<FeedEntity> = {
sortableColumns: ['id', 'create_at', 'author_id'],
filterableColumns: {
author_id: [FilterOperator.EQ],
},
defaultSortBy: [['id', 'DESC']],
};
return paginate(
paginateQuery,
FeedEntity.createQueryBuilder(),
paginateConfig,
);
}
async findOne(id: number) {
return await FeedEntity.findOneBy({ id: id });
}
async getFeedOrError(id: number) {
let feed = await FeedEntity.findOneBy({ id });
if (!feed) {
throw new NotFoundException('Menu item not found');
}
return feed;
}
async update(id: number, updateFeedDto: UpdateFeedDto) {
let feed = await this.getFeedOrError(id);
feed = plainToClass(FeedEntity, {
...feed,
...updateFeedDto,
});
return await feed.save();
}
async remove(id: number) {
let feed = await this.getFeedOrError(id);
return await feed.remove();
}
}