Skip to content

Commit

Permalink
updoots
Browse files Browse the repository at this point in the history
  • Loading branch information
benawad committed Aug 14, 2020
1 parent dcb28a1 commit 4852f74
Showing 5 changed files with 73 additions and 2 deletions.
5 changes: 5 additions & 0 deletions server/src/entities/Post.ts
Original file line number Diff line number Diff line change
@@ -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;
28 changes: 28 additions & 0 deletions server/src/entities/Updoot.ts
Original file line number Diff line number Diff line change
@@ -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;
}
4 changes: 4 additions & 0 deletions server/src/entities/User.ts
Original file line number Diff line number Diff line change
@@ -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;
3 changes: 2 additions & 1 deletion server/src/index.ts
Original file line number Diff line number Diff line change
@@ -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();

35 changes: 34 additions & 1 deletion server/src/resolvers/post.ts
Original file line number Diff line number Diff line change
@@ -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),

0 comments on commit 4852f74

Please sign in to comment.