Skip to content

Commit

Permalink
Merge pull request #60 from WildCodeSchool/features/agency-entity-and…
Browse files Browse the repository at this point in the history
…-resolver

Create Agency Entity
  • Loading branch information
xakaz committed Jun 3, 2024
2 parents 6e15619 + 5d4fc4b commit a5d6c57
Show file tree
Hide file tree
Showing 5 changed files with 215 additions and 1 deletion.
3 changes: 2 additions & 1 deletion backend/src/db.ts
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@ import Review from "./entities/Review"
import Product from "./entities/Product"
import Category from "./entities/Category"
import SubCategory from "./entities/SubCategory"
import Agency from "./entities/Agency"

const { DB_USER, DB_PASS, DB_NAME, DB_PORT, DB_HOST } = env

Expand All @@ -15,7 +16,7 @@ const db = new DataSource({
username: DB_USER,
password: DB_PASS,
database: DB_NAME,
entities: [Category, Product, SubCategory, Review, User],
entities: [Category, Product, SubCategory, Review, User, Agency],
synchronize: true,
})

Expand Down
100 changes: 100 additions & 0 deletions backend/src/entities/Agency.ts
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
13 changes: 13 additions & 0 deletions backend/src/resetDb.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
import db from "./db"
import Agency from "./entities/Agency"
import User, { UserRole } from "./entities/User"

export async function clearDB() {
Expand Down Expand Up @@ -54,6 +55,18 @@ async function main() {
})
await customer.save()

const agency = new Agency()
Object.assign(agency, {
name: "GearGo Capitol",
address: "31, rue de la Chocolatine",
postcode: "31330",
city: "Toulouse",
country: "France",
phone: "0504030201",
email: "geargo.wild@gmail.com",
})
await agency.save()

await db.destroy()
console.log("Done !")
}
Expand Down
98 changes: 98 additions & 0 deletions backend/src/resolvers/AgencyResolver.ts
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
2 changes: 2 additions & 0 deletions backend/src/schema.ts
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@ import ProductResolver from "./resolvers/ProductResolver"
import { ReviewResolver } from "./resolvers/ReviewResolver"
import SubCategoryResolver from "./resolvers/SubCategoryResolver"
import CategoryResolver from "./resolvers/CategoryResolver"
import AgencyResolver from "./resolvers/AgencyResolver"

export default buildSchema({
resolvers: [
Expand All @@ -13,6 +14,7 @@ export default buildSchema({
ReviewResolver,
SubCategoryResolver,
UserResolver,
AgencyResolver
],
authChecker,
})

0 comments on commit a5d6c57

Please sign in to comment.