Не получается придумать как это сделать "updated_at": "2021-10-05T10:06:28.000Z" ещё и время не правильное
async update(id: number, updatePostDto: UpdatePostDto) {
const posts = await this.postRepository.preload({
id: id,
updated_at: new Date(), //Ругается что должна быть строка
//updated_at: "2022-10-05T10:06:28.000Z",
...updatePostDto,
});
import { Injectable, NotFoundException } from '@nestjs/common';
import { InjectRepository } from '@nestjs/typeorm';
import { Repository } from 'typeorm';
import { CreatePostDto } from './dto/create-post.dto';
import { UpdatePostDto } from './dto/update-post.dto';
import { Post } from './entities/post.entity';
@Injectable()
export class PostsService {
constructor(
@InjectRepository(Post)
private readonly postRepository: Repository<Post>,
) {}
create(createPostDto: CreatePostDto) {
const posts = this.postRepository.create(createPostDto);
return this.postRepository.save(posts);
}
findAll() {
return this.postRepository.find();
}
async findOne(id: number) {
const posts = await this.postRepository.findOne(id);
if (!posts) {
throw new NotFoundException(`Post #${id} not found`);
}
return posts;
}
async update(id: number, updatePostDto: UpdatePostDto) {
const posts = await this.postRepository.preload({
id: id,
updated_at: ,
...updatePostDto,
});
if (!posts) {
throw new NotFoundException(`Post #${id} not found`);
}
return this.postRepository.save(posts);
}
async remove(id: number) {
const posts = await this.findOne(id);
return this.postRepository.remove(posts);
}
}
import { Controller, Get, Post, Body, Patch, Param, Delete } from '@nestjs/common';
import { PostsService } from './posts.service';
import { CreatePostDto } from './dto/create-post.dto';
import { UpdatePostDto } from './dto/update-post.dto';
@Controller('posts')
export class PostsController {
constructor(private readonly postsService: PostsService) {}
@Post()
create(@Body() createPostDto: CreatePostDto) {
return this.postsService.create(createPostDto);
}
@Get()
findAll() {
return this.postsService.findAll();
}
@Get(':id')
findOne(@Param('id') id: number) {
return this.postsService.findOne(id);
}
@Patch(':id')
update(@Param('id') id: number, @Body() updatePostDto: UpdatePostDto) {
return this.postsService.update(id, updatePostDto);
}
@Delete(':id')
remove(@Param('id') id: number) {
return this.postsService.remove(id);
}
}
import { IsNotEmpty } from 'class-validator';
export class CreatePostDto {
@IsNotEmpty()
title: string;
@IsNotEmpty()
text: string;
}
import { PartialType } from '@nestjs/mapped-types';
import { CreatePostDto } from './create-post.dto';
export class UpdatePostDto extends PartialType(CreatePostDto) {}
import { Column, Entity, PrimaryGeneratedColumn } from 'typeorm';
@Entity()
export class Post {
@PrimaryGeneratedColumn()
id: number;
@Column()
title: string;
@Column('text')
text: string;
@Column({ type: 'timestamp', default: () => 'CURRENT_TIMESTAMP'})
created_at: string;
@Column({ type: 'timestamp', default: () => 'CURRENT_TIMESTAMP'})
updated_at: string;
}