Skip to content

Commit

Permalink
many-to-one
Browse files Browse the repository at this point in the history
  • Loading branch information
benawad committed Aug 12, 2020
1 parent d744f22 commit df68f94
Show file tree
Hide file tree
Showing 4 changed files with 73 additions and 15 deletions.
25 changes: 21 additions & 4 deletions server/src/entities/Post.ts
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,9 @@ import {
CreateDateColumn,
UpdateDateColumn,
BaseEntity,
ManyToOne,
} from "typeorm";
import { User } from "./User";

@ObjectType()
@Entity()
Expand All @@ -15,15 +17,30 @@ export class Post extends BaseEntity {
@PrimaryGeneratedColumn()
id!: number;

@Field()
@Column()
title!: string;

@Field()
@Column()
text!: string;

@Field()
@Column({ type: "int", default: 0 })
points!: number;

@Field()
@Column()
creatorId: number;

@ManyToOne(() => User, (user) => user.posts)
creator: User;

@Field(() => String)
@CreateDateColumn()
createdAt: Date;

@Field(() => String)
@UpdateDateColumn()
updatedAt: Date;

@Field()
@Column()
title!: string;
}
21 changes: 13 additions & 8 deletions server/src/entities/User.ts
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,9 @@ import {
UpdateDateColumn,
Column,
BaseEntity,
OneToMany,
} from "typeorm";
import { Post } from "./Post";

@ObjectType()
@Entity()
Expand All @@ -15,14 +17,6 @@ export class User extends BaseEntity {
@PrimaryGeneratedColumn()
id!: number;

@Field(() => String)
@CreateDateColumn()
createdAt: Date;

@Field(() => String)
@UpdateDateColumn()
updatedAt: Date;

@Field()
@Column({ unique: true })
username!: string;
Expand All @@ -33,4 +27,15 @@ export class User extends BaseEntity {

@Column()
password!: string;

@OneToMany(() => Post, (post) => post.creator)
posts: Post[];

@Field(() => String)
@CreateDateColumn()
createdAt: Date;

@Field(() => String)
@UpdateDateColumn()
updatedAt: Date;
}
10 changes: 10 additions & 0 deletions server/src/middleware/isAuth.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,10 @@
import { MiddlewareFn } from "type-graphql";
import { MyContext } from "../types";

export const isAuth: MiddlewareFn<MyContext> = ({ context }, next) => {
if (!context.req.session.userId) {
throw new Error("not authenticated");
}

return next();
};
32 changes: 29 additions & 3 deletions server/src/resolvers/post.ts
Original file line number Diff line number Diff line change
@@ -1,5 +1,24 @@
import { Resolver, Query, Arg, Mutation } from "type-graphql";
import {
Resolver,
Query,
Arg,
Mutation,
InputType,
Field,
Ctx,
UseMiddleware,
} from "type-graphql";
import { Post } from "../entities/Post";
import { MyContext } from "../types";
import { isAuth } from "../middleware/isAuth";

@InputType()
class PostInput {
@Field()
title: string;
@Field()
text: string;
}

@Resolver()
export class PostResolver {
Expand All @@ -14,8 +33,15 @@ export class PostResolver {
}

@Mutation(() => Post)
async createPost(@Arg("title") title: string): Promise<Post> {
return Post.create({ title }).save();
@UseMiddleware(isAuth)
async createPost(
@Arg("input") input: PostInput,
@Ctx() { req }: MyContext
): Promise<Post> {
return Post.create({
...input,
creatorId: req.session.userId,
}).save();
}

@Mutation(() => Post, { nullable: true })
Expand Down

0 comments on commit df68f94

Please sign in to comment.