import { BeforeInsert , Column , Entity as TOEntity , Index , JoinColumn , ManyToOne , OneToMany , } from 'typeorm' ; import Entity from './Entity' ; import Post from './Post' ; import User from './User' ; import { makeId } from '../util/helpers' ; import Vote from './Vote' ; import { Exclude , Expose } from 'class-transformer' ; @ TOEntity ( 'comments' ) export default class Comment extends Entity { constructor ( comment ) { super ( ) ; Object . assign ( this , comment ) ; } @ Index ( ) @ Column ( ) identifier : string ; @ Column ( ) body : string ; @ Column ( ) username : string ; @ ManyToOne ( ( ) => User ) @ JoinColumn ( { name : 'username' , referencedColumnName : 'username' } ) user : User ; @ ManyToOne ( ( ) => Post , ( post ) => post . comments , { nullable : false } ) post : Post ; @ Exclude ( ) @ OneToMany ( ( ) => Vote , ( vote ) => vote . comment ) votes : Vote [ ] ; @ Expose ( ) get voteScore ( ) : number { return this . votes ?. reduce ( ( prev , curr ) => prev + ( curr . value || 0 ) , 0 ) ; } protected userVote : number ; setUserVote ( user ) { const index = this . votes ?. findIndex ( ( v ) => v . username === user . username ) ; this . userVote = index > - 1 ? this . votes [ index ] . value : 0 ; } @ BeforeInsert ( ) makeIdAndSlug ( ) { this . identifier = makeId ( 8 ) ; } }