Created
October 20, 2020 12:15
-
-
Save sidneyroberto/f6b10c4375a858381e4fc3853cbb21a8 to your computer and use it in GitHub Desktop.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
@Entity() | |
class Editor { | |
@PrimaryGeneratedColumn() | |
id: number; | |
@Column() | |
nome: string; | |
@Column() | |
email: string; | |
@OneToMany(() => Post, post => post.editor) | |
posts: Post[]; | |
} | |
@Entity() | |
class Post { | |
@PrimaryGeneratedColumn() | |
id: number; | |
@Column() | |
titulo: string; | |
@Column() | |
texto: string; | |
@Column() | |
data: Date; | |
@Column() | |
quantidadePalavras: number; | |
@ManyToOne(() => Editor) | |
editor: Editor; | |
} | |
/** | |
* Atualizar um post | |
*/ | |
const idPost = parseInt(req.params.idPost); | |
const payload = req.body; | |
let post = await postCtrl.recuperarPorId(idPost); | |
if (post) { | |
post.titulo = payload.titulo; | |
post.texto = payload.texto; | |
post.data = payload.data; | |
post.quantidadePalavras = payload.quantidadePalavras; | |
const postSalvo = postCtrl.salvar(post); | |
res.json(postSalvo); | |
} else { | |
res.status(404).json({ mensagem: 'Nenhum post com este id encontrado' }); | |
} | |
/** | |
* Recuperar todos os posts de um determinado editor com pelo menos 100 palavras | |
*/ | |
async recuperarPostsComPeloMenosCemPalavras(idEditor: number) { | |
const posts = await getManager().find(Post, { | |
where: { | |
editor: idEditor, | |
quantidadePalavras: MoreThanOrEqual(100) | |
} | |
}); | |
return posts; | |
} | |
/** | |
* Recuperar todos os posts de um determinado editor com menos de 100 palavras | |
*/ | |
async recuperarPostsComPeloMenosDePalavras(idEditor: number) { | |
const posts = await getManager().find(Post, { | |
where: { | |
editor: idEditor, | |
quantidadePalavras: LessThan(100) | |
} | |
}); | |
return posts; | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment