-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #60 from WildCodeSchool/features/agency-entity-and…
…-resolver Create Agency Entity
- Loading branch information
Showing
5 changed files
with
215 additions
and
1 deletion.
There are no files selected for viewing
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
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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,100 @@ | ||
import { Length, Min } from "class-validator" | ||
import { Field, InputType, Int, ObjectType } from "type-graphql" | ||
import { | ||
BaseEntity, | ||
Column, | ||
Entity, | ||
OneToMany, | ||
PrimaryGeneratedColumn, | ||
} from "typeorm" | ||
// import ProductCode from "./ProductCode" | ||
|
||
@Entity() | ||
@ObjectType() | ||
export class Agency extends BaseEntity { | ||
@PrimaryGeneratedColumn() | ||
@Field(() => Int) | ||
id: number | ||
|
||
@Column() | ||
@Field() | ||
name: string | ||
|
||
@Column() | ||
@Field() | ||
address: string | ||
|
||
@Column() | ||
@Field() | ||
postcode: string | ||
|
||
@Column() | ||
@Field() | ||
city: string | ||
|
||
@Column() | ||
@Field() | ||
country: string | ||
|
||
@Column() | ||
@Field() | ||
phone: string | ||
|
||
@Column() | ||
@Field() | ||
email: string | ||
|
||
// @OneToMany(() => ProductCode, (products) => products.agency) | ||
// @Field(() => [ProductCode]) | ||
// products: ProductCode[] | ||
} | ||
|
||
@InputType() | ||
export class NewAgencyInput { | ||
@Field() | ||
name: string | ||
|
||
@Field() | ||
address: string | ||
|
||
@Field() | ||
postcode: string | ||
|
||
@Field() | ||
city: string | ||
|
||
@Field() | ||
country: string | ||
|
||
@Field() | ||
phone: string | ||
|
||
@Field() | ||
email: string | ||
} | ||
|
||
@InputType() | ||
export class UpdateAgencyInput { | ||
@Field({ nullable: true }) | ||
name?: string | ||
|
||
@Field({ nullable: true }) | ||
address?: string | ||
|
||
@Field({ nullable: true }) | ||
postcode?: string | ||
|
||
@Field({ nullable: true }) | ||
city?: string | ||
|
||
@Field({ nullable: true }) | ||
country?: string | ||
|
||
@Field({ nullable: true }) | ||
phone?: string | ||
|
||
@Field({ nullable: true }) | ||
email?: string | ||
} | ||
|
||
export default Agency |
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
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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,98 @@ | ||
import { | ||
Resolver, | ||
Query, | ||
Arg, | ||
Mutation, | ||
Int, | ||
Ctx, | ||
Authorized, | ||
} from "type-graphql" | ||
import { GraphQLError } from "graphql" | ||
import { ILike } from "typeorm" | ||
import { Context } from "../utils" | ||
import Agency, { | ||
NewAgencyInput, | ||
UpdateAgencyInput, | ||
} from "../entities/Agency" | ||
import { UserRole } from "../entities/User"; | ||
|
||
@Resolver() | ||
class AgencyResolver { | ||
@Query(() => [Agency]) | ||
async getAllAgencies() { | ||
return Agency.find() | ||
} | ||
|
||
@Query(() => Agency) | ||
async getAgencyById(@Arg("agencyId", () => Int) id: number) { | ||
const agency = await Agency.findOne({ | ||
where: { id }, | ||
}) | ||
if (!agency) throw new GraphQLError("Agency Not found") | ||
return agency | ||
} | ||
|
||
@Authorized([UserRole.ADMIN]) | ||
@Mutation(() => Agency) | ||
async createAgency( | ||
@Arg("data", | ||
{ validate: true } | ||
) | ||
data: NewAgencyInput, | ||
@Ctx() ctx: Context | ||
) { | ||
if (!ctx.currentUser) throw new GraphQLError("Not authenticated") | ||
const newAgency = new Agency() | ||
|
||
if (ctx.currentUser.role !== UserRole.ADMIN) | ||
throw new GraphQLError("Not authorized") | ||
Object.assign(newAgency, data) | ||
|
||
const { id } = await newAgency.save() | ||
return Agency.findOne({ | ||
where: { id }, | ||
}) | ||
} | ||
|
||
|
||
@Authorized([UserRole.ADMIN]) | ||
@Mutation(() => Agency) | ||
async updateAgency( | ||
@Arg("agencyId") id: number, | ||
@Arg("data", { validate: true }) data: UpdateAgencyInput, | ||
@Ctx() ctx: Context | ||
) { | ||
if (!ctx.currentUser) throw new GraphQLError("Not authenticated") | ||
const agencyToUpdate = await Agency.findOne({ where: { id } }) | ||
if (!agencyToUpdate) throw new GraphQLError("Agency not found") | ||
|
||
if (ctx.currentUser.role !== UserRole.ADMIN) | ||
throw new GraphQLError("Not authorized") | ||
await Object.assign(agencyToUpdate, data) | ||
|
||
await agencyToUpdate.save() | ||
return Agency.findOne({ | ||
where: { id }, | ||
}) | ||
} | ||
|
||
@Authorized([UserRole.ADMIN]) | ||
@Mutation(() => String) | ||
async deleteAgency( | ||
@Arg("agencyId") id: number, | ||
@Ctx() ctx: Context | ||
) { | ||
if (!ctx.currentUser) throw new GraphQLError("Not authenticated") | ||
const agencyToDelete = await Agency.findOne({ where: { id } }) | ||
if (!agencyToDelete) throw new GraphQLError("Agency not found") | ||
|
||
if (ctx.currentUser.role !== UserRole.ADMIN) | ||
throw new GraphQLError("Not authorized") | ||
|
||
await agencyToDelete.remove() | ||
return "Agency deleted" | ||
} | ||
|
||
} | ||
|
||
export default AgencyResolver |
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