Skip to content

Commit

Permalink
Merge pull request hngprojects#483 from deolla/feat/del
Browse files Browse the repository at this point in the history
feat: delete product in an organization By id
  • Loading branch information
AdeGneus authored Aug 8, 2024
2 parents 19d9c49 + 86787bc commit 95369e6
Show file tree
Hide file tree
Showing 4 changed files with 63 additions and 314 deletions.
145 changes: 0 additions & 145 deletions src/controllers/ProductController.ts
Original file line number Diff line number Diff line change
Expand Up @@ -106,151 +106,6 @@ class ProductController {
.json({ message: "Product created successfully", data: newProduct });
};

/**
* @openapi
* /api/v1/organisation/{:id}/product/search:
* get:
* tags:
* - Product API
* summary: Get All products.
* description: Get All products within an organisation.
* parameters:
* - name: org_id
* in: path
* required: true
* description: ID of the organisation
* schema:
* type: string
* - name: name
* in: query
* required: false
* description: Name of the product
* schema:
* type: string
* - name: category
* in: query
* required: false
* description: Category of the product
* schema:
* type: string
* - name: minPrice
* in: query
* required: false
* description: Minimum price of the product
* schema:
* type: number
* - name: maxPrice
* in: query
* required: false
* description: Maximum price of the product
* schema:
* type: number
* - name: page
* in: query
* required: false
* description: Page number for pagination
* schema:
* type: number
* default: 1
* - name: limit
* in: query
* required: false
* description: Number of results per page
* schema:
* type: number
* default: 10
* responses:
* 200:
* description: Product search successful
* content:
* application/json:
* schema:
* type: object
* properties:
* message:
* type: string
* example: "Product search successful"
* data:
* type: object
* properties:
* pagination:
* type: object
* properties:
* total:
* type: number
* page:
* type: number
* limit:
* type: number
* totalPages:
* type: number
* products:
* type: array
* items:
* type: object
* properties:
* id:
* type: string
* name:
* type: string
* description:
* type: string
* price:
* type: number
* category:
* type: string
* status:
* type: string
* quantity:
* type: number
* created_at:
* type: string
* format: date-time
* updated_at:
* type: string
* format: date-time
* 400:
* description: Invalid input or missing organisation ID
* 404:
* description: No products found
*/

public getProduct = async (
req: Request,
res: Response,
next: NextFunction,
) => {
try {
const orgId = req.params.org_id;
const {
name,
category,
minPrice,
maxPrice,
page = 1,
limit = 10,
} = req.query as any;
const searchCriteria = {
name,
category,
minPrice: Number(minPrice),
maxPrice: Number(maxPrice),
};

const products = await this.productService.getProducts(
orgId,
searchCriteria,
Number(page),
Number(limit),
);
return res
.status(200)
.json({ message: "Product search successful", data: products });
} catch (error) {
next(error);
}
};

/**
* @openapi
* /api/v1/products/{product_id}:
Expand Down
7 changes: 0 additions & 7 deletions src/routes/product.ts
Original file line number Diff line number Diff line change
Expand Up @@ -17,13 +17,6 @@ productRouter.post(
productController.createProduct,
);

productRouter.get(
"/organizations/:org_id/products/search",
authMiddleware,
validateUserToOrg,
productController.getProduct,
);

productRouter.delete(
"/organizations/:org_id/products/:product_id",
authMiddleware,
Expand Down
73 changes: 14 additions & 59 deletions src/services/product.services.ts
Original file line number Diff line number Diff line change
Expand Up @@ -105,66 +105,21 @@ export class ProductService {
},
};
}
public async deleteProduct(org_id: string, product_id: string) {
try {
const entities = await this.checkEntities({
organization: org_id,
product: product_id,
});

if (!entities.product) {
throw new Error("Product not found");
}

public async getProducts(
orgId: string,
query: {
name?: string;
category?: string;
minPrice?: number;
maxPrice?: number;
},
page: number = 1,
limit: number = 10,
) {
const org = await this.organizationRepository.findOne({
where: { id: orgId },
});
if (!org) {
throw new ServerError(
"Unprocessable entity exception: Invalid organization credentials",
);
}

const { name, category, minPrice, maxPrice } = query;
const queryBuilder = this.productRepository
.createQueryBuilder("product")
.where("product.orgId = :orgId", { orgId });

if (name) {
queryBuilder.andWhere("product.name ILIKE :name", { name: `%${name}%` });
}
if (minPrice) {
queryBuilder.andWhere("product.price >= :minPrice", { minPrice });
}
if (maxPrice) {
queryBuilder.andWhere("product.price <= :maxPrice", { maxPrice });
await this.productRepository.remove(entities.product);
return { message: "Product deleted successfully" };
} catch (error) {
throw new Error(`Failed to delete product: ${error.message}`);
}

const skip = (page - 1) * limit;
queryBuilder.skip(skip).take(limit);

const [products, total] = await queryBuilder.getManyAndCount();

return {
success: true,
statusCode: 200,
data: {
products,
pagination: {
total,
page,
limit,
totalPages: Math.ceil(total / limit),
},
},
};
}
public async deleteProduct(org_id: string, product_id: string) {
const entities = await this.checkEntities({
organization: org_id,
product: product_id,
});
return this.productRepository.remove(entities.product);
}
}
Loading

0 comments on commit 95369e6

Please sign in to comment.