-
Notifications
You must be signed in to change notification settings - Fork 59
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Browse files
Browse the repository at this point in the history
* Issue #132: Added Prisma Model For ProductImages * Issue #132: Added provisional product object, service and resolver * Added Product Image Object entity * Added product Image dto * Added a service to add product image to the database * added service and resolver to query and fetch all product images * service ad resolver for getting a single product image by its ID * feat: PR changes effected * feat: prisma model and migration updated
- Loading branch information
Showing
24 changed files
with
384 additions
and
49 deletions.
There are no files selected for viewing
23 changes: 23 additions & 0 deletions
23
apps/backend/prisma/migrations/20250211173007_add_product_images/migration.sql
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,23 @@ | ||
-- CreateTable | ||
CREATE TABLE "productImages" ( | ||
"id" TEXT NOT NULL, | ||
"image_url" TEXT NOT NULL, | ||
"product_id" TEXT NOT NULL, | ||
"created_at" TIMESTAMP(3) NOT NULL DEFAULT CURRENT_TIMESTAMP, | ||
"updated_at" TIMESTAMP(3) NOT NULL, | ||
|
||
CONSTRAINT "productImages_pkey" PRIMARY KEY ("id") | ||
); | ||
|
||
-- CreateTable | ||
CREATE TABLE "products" ( | ||
"id" TEXT NOT NULL, | ||
"name" TEXT NOT NULL, | ||
"created_at" TIMESTAMP(3) NOT NULL DEFAULT CURRENT_TIMESTAMP, | ||
"updated_at" TIMESTAMP(3) NOT NULL, | ||
|
||
CONSTRAINT "products_pkey" PRIMARY KEY ("id") | ||
); | ||
|
||
-- AddForeignKey | ||
ALTER TABLE "productImages" ADD CONSTRAINT "productImages_product_id_fkey" FOREIGN KEY ("product_id") REFERENCES "products"("id") ON DELETE RESTRICT ON UPDATE CASCADE; |
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 |
---|---|---|
@@ -1,3 +1,3 @@ | ||
# Please do not edit this file manually | ||
# It should be added in your version-control system (e.g., Git) | ||
provider = "postgresql" | ||
provider = "postgresql" |
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
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
12 changes: 6 additions & 6 deletions
12
apps/backend/src/modules/categories/category.resolver.spec.ts
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
10 changes: 5 additions & 5 deletions
10
apps/backend/src/modules/categories/category.service.spec.ts
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
10 changes: 10 additions & 0 deletions
10
apps/backend/src/modules/product-image/dto/product-image.input.ts
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,10 @@ | ||
import { Field, InputType } from "@nestjs/graphql"; | ||
|
||
@InputType() | ||
export class ProductImageDTO { | ||
@Field(() => String) | ||
imageUrl: string; | ||
|
||
@Field(() => String) | ||
productId: string; | ||
} |
20 changes: 20 additions & 0 deletions
20
apps/backend/src/modules/product-image/entities/product-image.entity.ts
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,20 @@ | ||
import { Field, ID, ObjectType } from "@nestjs/graphql"; | ||
import { Product } from "../../product/entities/product.entity"; | ||
|
||
@ObjectType() | ||
export class ProductImage { | ||
@Field(() => ID) | ||
id: string; | ||
|
||
@Field(() => String) | ||
imageUrl: string; | ||
|
||
@Field(() => String) | ||
productId: string; | ||
|
||
@Field(() => Date) | ||
createdAt: Date; | ||
|
||
@Field(() => Date) | ||
updatedAt: Date; | ||
} |
28 changes: 28 additions & 0 deletions
28
apps/backend/src/modules/product-image/product-image.graphql
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,28 @@ | ||
type ProductImage { | ||
# Example field (placeholder) | ||
exampleField: Int | ||
} | ||
|
||
input CreateProductImageInput { | ||
# Example field (placeholder) | ||
exampleField: Int | ||
} | ||
|
||
input UpdateProductImageInput { | ||
id: Int! | ||
} | ||
|
||
type Query { | ||
productImage: [ProductImage]! | ||
productImage(id: Int!): ProductImage | ||
} | ||
|
||
type Mutation { | ||
createProductImage( | ||
createProductImageInput: CreateProductImageInput! | ||
): ProductImage! | ||
updateProductImage( | ||
updateProductImageInput: UpdateProductImageInput! | ||
): ProductImage! | ||
removeProductImage(id: Int!): ProductImage | ||
} |
8 changes: 8 additions & 0 deletions
8
apps/backend/src/modules/product-image/product-image.module.ts
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,8 @@ | ||
import { Module } from "@nestjs/common"; | ||
import { ProductImageResolver } from "./product-image.resolver"; | ||
import { ProductImageService } from "./product-image.service"; | ||
|
||
@Module({ | ||
providers: [ProductImageResolver, ProductImageService], | ||
}) | ||
export class ProductImageModule {} |
19 changes: 19 additions & 0 deletions
19
apps/backend/src/modules/product-image/product-image.resolver.spec.ts
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,19 @@ | ||
import { Test, TestingModule } from "@nestjs/testing"; | ||
import { ProductImageResolver } from "./product-image.resolver"; | ||
import { ProductImageService } from "./product-image.service"; | ||
|
||
describe("ProductImageResolver", () => { | ||
let resolver: ProductImageResolver; | ||
|
||
beforeEach(async () => { | ||
const module: TestingModule = await Test.createTestingModule({ | ||
providers: [ProductImageResolver, ProductImageService], | ||
}).compile(); | ||
|
||
resolver = module.get<ProductImageResolver>(ProductImageResolver); | ||
}); | ||
|
||
it("should be defined", () => { | ||
expect(resolver).toBeDefined(); | ||
}); | ||
}); |
26 changes: 26 additions & 0 deletions
26
apps/backend/src/modules/product-image/product-image.resolver.ts
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,26 @@ | ||
import { Args, Mutation, Query, Resolver } from "@nestjs/graphql"; | ||
import { ProductImageDTO } from "./dto/product-image.input"; | ||
import { ProductImage } from "./entities/product-image.entity"; | ||
import { ProductImageService } from "./product-image.service"; | ||
|
||
@Resolver("ProductImage") | ||
export class ProductImageResolver { | ||
constructor(private readonly productImageService: ProductImageService) {} | ||
|
||
@Mutation(() => ProductImage) | ||
async createProductImage( | ||
@Args("createProductImage") payload: ProductImageDTO, | ||
): Promise<ProductImage> { | ||
return await this.productImageService.createProductImage(payload); | ||
} | ||
|
||
@Query(() => [ProductImage]) | ||
async productImages() { | ||
return await this.productImageService.getAllProductImages(); | ||
} | ||
|
||
@Query(() => ProductImage, { nullable: true }) | ||
async productImage(@Args("id") id: string) { | ||
return await this.productImageService.getAProductImage(id); | ||
} | ||
} |
18 changes: 18 additions & 0 deletions
18
apps/backend/src/modules/product-image/product-image.service.spec.ts
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,18 @@ | ||
import { Test, TestingModule } from "@nestjs/testing"; | ||
import { ProductImageService } from "./product-image.service"; | ||
|
||
describe("ProductImageService", () => { | ||
let service: ProductImageService; | ||
|
||
beforeEach(async () => { | ||
const module: TestingModule = await Test.createTestingModule({ | ||
providers: [ProductImageService], | ||
}).compile(); | ||
|
||
service = module.get<ProductImageService>(ProductImageService); | ||
}); | ||
|
||
it("should be defined", () => { | ||
expect(service).toBeDefined(); | ||
}); | ||
}); |
33 changes: 33 additions & 0 deletions
33
apps/backend/src/modules/product-image/product-image.service.ts
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,33 @@ | ||
import { Injectable } from "@nestjs/common"; | ||
import { PrismaService } from "src/core/prisma/prisma.service"; | ||
import { ProductImageDTO } from "./dto/product-image.input"; | ||
|
||
@Injectable() | ||
export class ProductImageService { | ||
constructor(private readonly prisma: PrismaService) {} | ||
|
||
// Service to create product Image | ||
async createProductImage(data: ProductImageDTO) { | ||
return await this.prisma.productImage.create({ data }); | ||
} | ||
|
||
async getAllProductImages() { | ||
const productImages = await this.prisma.productImage.findMany(); | ||
if (productImages.length === 0) { | ||
return []; | ||
} | ||
return productImages; | ||
} | ||
|
||
async getAProductImage(id: string) { | ||
const productImage = await this.prisma.productImage.findUnique({ | ||
where: { id }, | ||
}); | ||
|
||
if (!productImage) { | ||
throw new Error("Product image not found."); | ||
} | ||
|
||
return productImage; | ||
} | ||
} |
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,7 @@ | ||
import { Field, InputType } from "@nestjs/graphql"; | ||
|
||
@InputType() | ||
export class ProductDTO { | ||
@Field(() => String) | ||
name: string; | ||
} |
16 changes: 16 additions & 0 deletions
16
apps/backend/src/modules/product/entities/product.entity.ts
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,16 @@ | ||
import { Field, ID, ObjectType } from "@nestjs/graphql"; | ||
|
||
@ObjectType() | ||
export class Product { | ||
@Field(() => ID) | ||
id: string; | ||
|
||
@Field(() => String) | ||
name: string; | ||
|
||
@Field(() => Date) | ||
createdAt: Date; | ||
|
||
@Field(() => Date) | ||
updatedAt: Date; | ||
} |
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,8 @@ | ||
import { Module } from "@nestjs/common"; | ||
import { ProductResolver } from "./product.resolver"; | ||
import { ProductService } from "./product.service"; | ||
|
||
@Module({ | ||
providers: [ProductResolver, ProductService], | ||
}) | ||
export class ProductModule {} |
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,19 @@ | ||
import { Test, TestingModule } from "@nestjs/testing"; | ||
import { ProductResolver } from "./product.resolver"; | ||
import { ProductService } from "./product.service"; | ||
|
||
describe("ProductResolver", () => { | ||
let resolver: ProductResolver; | ||
|
||
beforeEach(async () => { | ||
const module: TestingModule = await Test.createTestingModule({ | ||
providers: [ProductResolver, ProductService], | ||
}).compile(); | ||
|
||
resolver = module.get<ProductResolver>(ProductResolver); | ||
}); | ||
|
||
it("should be defined", () => { | ||
expect(resolver).toBeDefined(); | ||
}); | ||
}); |
Oops, something went wrong.