Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Create Agency Entity #60

Merged
merged 8 commits into from
Jun 3, 2024
Merged
Show file tree
Hide file tree
Changes from 5 commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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()
zipcode: number
Arkantik marked this conversation as resolved.
Show resolved Hide resolved

@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()
zipcode: number

@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 })
zipcode?: number

@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",
zipcode: 31330,
Arkantik marked this conversation as resolved.
Show resolved Hide resolved
city: "Toulouse",
country: "France",
phone: "0504030201",
email: "geargo.wild@gmail.com",
})
await agency.save()

await db.destroy()
console.log("Done !")
}
Expand Down
97 changes: 97 additions & 0 deletions backend/src/resolvers/AgencyResolver.ts
Arkantik marked this conversation as resolved.
Show resolved Hide resolved
Original file line number Diff line number Diff line change
@@ -0,0 +1,97 @@
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"

@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,
})
Loading