diff --git a/server/src/entities/Post.ts b/server/src/entities/Post.ts index 793b2e48..acfc2f52 100644 --- a/server/src/entities/Post.ts +++ b/server/src/entities/Post.ts @@ -7,8 +7,10 @@ import { UpdateDateColumn, BaseEntity, ManyToOne, + OneToMany, } from "typeorm"; import { User } from "./User"; +import { Updoot } from "./Updoot"; @ObjectType() @Entity() @@ -37,6 +39,9 @@ export class Post extends BaseEntity { @ManyToOne(() => User, (user) => user.posts) creator: User; + @OneToMany(() => Updoot, (updoot) => updoot.post) + updoots: Updoot[]; + @Field(() => String) @CreateDateColumn() createdAt: Date; diff --git a/server/src/entities/Updoot.ts b/server/src/entities/Updoot.ts new file mode 100644 index 00000000..e108dc93 --- /dev/null +++ b/server/src/entities/Updoot.ts @@ -0,0 +1,28 @@ +import { ObjectType, Field } from "type-graphql"; +import { Entity, BaseEntity, ManyToOne, PrimaryColumn, Column } from "typeorm"; +import { User } from "./User"; +import { Post } from "./Post"; + +// m to n +// many to many +// user <-> posts +// user -> join table <- posts +// user -> updoot <- posts + +@Entity() +export class Updoot extends BaseEntity { + @Column({ type: "int" }) + value: number; + + @PrimaryColumn() + userId: number; + + @ManyToOne(() => User, (user) => user.updoots) + user: User; + + @PrimaryColumn() + postId: number; + + @ManyToOne(() => Post, (post) => post.updoots) + post: Post; +} diff --git a/server/src/entities/User.ts b/server/src/entities/User.ts index a95d681d..3201f808 100644 --- a/server/src/entities/User.ts +++ b/server/src/entities/User.ts @@ -9,6 +9,7 @@ import { OneToMany, } from "typeorm"; import { Post } from "./Post"; +import { Updoot } from "./Updoot"; @ObjectType() @Entity() @@ -31,6 +32,9 @@ export class User extends BaseEntity { @OneToMany(() => Post, (post) => post.creator) posts: Post[]; + @OneToMany(() => Updoot, (updoot) => updoot.user) + updoots: Updoot[]; + @Field(() => String) @CreateDateColumn() createdAt: Date; diff --git a/server/src/index.ts b/server/src/index.ts index f0e7000b..7a65cb4b 100644 --- a/server/src/index.ts +++ b/server/src/index.ts @@ -14,6 +14,7 @@ import { createConnection } from "typeorm"; import { Post } from "./entities/Post"; import { User } from "./entities/User"; import path from "path"; +import { Updoot } from "./entities/Updoot"; const main = async () => { const conn = await createConnection({ @@ -24,7 +25,7 @@ const main = async () => { logging: true, synchronize: true, migrations: [path.join(__dirname, "./migrations/*")], - entities: [Post, User], + entities: [Post, User, Updoot], }); await conn.runMigrations(); diff --git a/server/src/resolvers/post.ts b/server/src/resolvers/post.ts index 8ead40e0..82a9a038 100644 --- a/server/src/resolvers/post.ts +++ b/server/src/resolvers/post.ts @@ -17,6 +17,7 @@ import { Post } from "../entities/Post"; import { MyContext } from "../types"; import { isAuth } from "../middleware/isAuth"; import { getConnection } from "typeorm"; +import { Updoot } from "../entities/Updoot"; @InputType() class PostInput { @@ -41,6 +42,38 @@ export class PostResolver { return post.text.slice(0, 50); } + @Mutation(() => Boolean) + @UseMiddleware(isAuth) + async vote( + @Arg("postId", () => Int) postId: number, + @Arg("value", () => Int) value: number, + @Ctx() { req }: MyContext + ) { + const isUpdoot = value !== -1; + const realValue = isUpdoot ? 1 : -1; + const { userId } = req.session; + // await Updoot.insert({ + // userId, + // postId, + // value: realValue, + // }); + await getConnection().query( + ` + START TRANSACTION; + + insert into updoot ("userId", "postId", value) + values (${userId},${postId},${realValue}); + + update post + set points = points + ${realValue} + where id = ${postId}; + + COMMIT; + ` + ); + return true; + } + @Query(() => PaginatedPosts) async posts( @Arg("limit", () => Int) limit: number, @@ -89,7 +122,7 @@ export class PostResolver { // } // const posts = await qb.getMany(); - console.log("posts: ", posts); + // console.log("posts: ", posts); return { posts: posts.slice(0, realLimit),