anatid-blog/backend/src/posts/posts.service.ts
Phill Pover 1a74bb3bcd
Some checks failed
Anatid Blog CI Workflow / test (./backend) (push) Failing after 39s
Anatid Blog CI Workflow / test (./frontend) (push) Successful in 30s
Anatid Blog CI Workflow / build-and-push-images (./backend/Dockerfile, git.anatid.net/tabris/anatid-blog-backend, ./backend) (push) Has been skipped
Anatid Blog CI Workflow / build-and-push-images (./frontend/Dockerfile, git.anatid.net/tabris/anatid-blog-frontend, ./frontend) (push) Has been skipped
Anatid Blog CI Workflow / deploy (push) Has been skipped
New posts and users modules
2025-03-31 11:08:29 +01:00

34 lines
824 B
TypeScript

import { Injectable } from '@nestjs/common';
import { InjectRepository } from '@nestjs/typeorm';
import { Repository } from 'typeorm';
import { Post } from './Post.entity';
@Injectable()
export class PostsService {
constructor(
@InjectRepository(Post)
private PostsRepository: Repository<Post>,
) {}
findAll(): Promise<Post[]> {
return this.PostsRepository.find();
}
findOne(id: number): Promise<Post | null> {
return this.PostsRepository.findOneBy({ id });
}
async create(title: string, body: string) Promise<void> {
const newPost = this.PostsRepository.create({
title: title,
body: body
});
const post = await this.PostsRepository.save(newPost);
return post.id;
}
async remove(id: number): Promise<void> {
await this.PostsRepository.delete(id);
}
}