Skip to content

Commit

Permalink
fixing category icons
Browse files Browse the repository at this point in the history
  • Loading branch information
niyobern authored Jul 8, 2024
1 parent 60a10d5 commit 43d546e
Show file tree
Hide file tree
Showing 10 changed files with 39 additions and 3 deletions.
2 changes: 1 addition & 1 deletion package-lock.json

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

1 change: 1 addition & 0 deletions src/__test__/buyerController.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -25,6 +25,7 @@ describe('Buyer Controller test', () => {
const categoryData = {
name: 'Category4',
description: 'category description',
icon: 'category icon',
};

const categoryResponse = await request(app)
Expand Down
1 change: 1 addition & 0 deletions src/__test__/buyerWishlist.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,7 @@ beforeAll(async () => {
const categoryData = {
name: 'Category4',
description: 'category description',
icon: 'category icon',
};

const categoryResponse = await request(app)
Expand Down
1 change: 1 addition & 0 deletions src/__test__/cartController.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -30,6 +30,7 @@ describe('Cart controller tests', () => {
const categoryData = {
name: 'Category4',
description: 'category description',
icon: 'Category icon',
};

const categoryResponse = await request(app)
Expand Down
23 changes: 23 additions & 0 deletions src/__test__/category.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,7 @@ describe('Category Creation Tests', () => {
const categoryData = {
name: 'Test Category',
description: 'Test category description',
icon: 'Test category icon'
};

const response = await request(app)
Expand All @@ -36,6 +37,7 @@ describe('Category Creation Tests', () => {
it('should return a 400 status code if name is missing', async () => {
const invalidData = {
description: 'Test category description',
icon: 'Test category icon'
};

const response = await request(app)
Expand All @@ -47,6 +49,21 @@ describe('Category Creation Tests', () => {
expect(response.body.errors[0].msg).toBe('Category name is required');
});

it('should return a 400 status code if icon is missing', async () => {
const invalidData = {
description: 'Test category description',
name: 'Test category name'
};

const response = await request(app)
.post('/api/v1/category')
.set('Authorization', `Bearer ${token}`)
.send(invalidData);

expect(response.status).toBe(400);
expect(response.body.errors[0].msg).toBe('Category icon is required');
});

it('should return 400 if request data is invalid', async () => {
const invalidData = {};

Expand All @@ -63,6 +80,7 @@ describe('Category Creation Tests', () => {
const existingCategoryData = {
name: 'Existing Category',
description: 'Existing category description',
icon: 'Existing category icon'
};
await request(app)
.post('/api/v1/category')
Expand All @@ -72,6 +90,7 @@ describe('Category Creation Tests', () => {
const newCategoryData = {
name: 'Existing Category',
description: 'Existing category description',
icon: 'Existing category icon'
};
const response = await request(app)
.post('/api/v1/category')
Expand Down Expand Up @@ -112,6 +131,7 @@ describe('Category Creation Tests', () => {
const updatedCategoryData = {
name: 'Updated Category Name',
description: 'Updated category description',
icon: 'Updated category icon'
};

const response = await request(app)
Expand All @@ -131,6 +151,7 @@ describe('Category Creation Tests', () => {
const existingCategoryData = {
name: 'Existing Category',
description: 'Existing category description',
icon: 'Existing category icon'
};
await request(app)
.post('/api/v1/category')
Expand All @@ -140,6 +161,7 @@ describe('Category Creation Tests', () => {
const updateCategoryData = {
name: 'Existing Category',
description: 'Existing category description',
icon: 'Existing category icon'
};
const response = await request(app)
.put(`/api/v1/category/${categoryId}`)
Expand All @@ -157,6 +179,7 @@ describe('Category Creation Tests', () => {
.send({
name: 'Updated Category Name',
description: 'Updated category description',
icon: 'Updated category icon'
});

expect(response.status).toBe(404);
Expand Down
1 change: 1 addition & 0 deletions src/__test__/coupon.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,7 @@ describe('Coupon Controller Tests', () => {
const categoryData = {
name: 'Category',
description: 'category description',
icon: 'category icon'
};

const categoryResponse = await request(app)
Expand Down
1 change: 1 addition & 0 deletions src/__test__/product.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,7 @@ describe('Product Controller Tests', () => {
const categoryData = {
name: 'Category',
description: 'category description',
icon: 'Category icon',
};

const categoryResponse = await request(app)
Expand Down
1 change: 1 addition & 0 deletions src/__test__/review.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -24,6 +24,7 @@ describe('Review controller test', () => {
const categoryData = {
name: 'Category4',
description: 'category description',
icon: 'category icon',
};

const categoryResponse = await request(app)
Expand Down
8 changes: 6 additions & 2 deletions src/controller/categoryController.ts
Original file line number Diff line number Diff line change
Expand Up @@ -9,10 +9,12 @@ const categoryRepository = dbConnection.getRepository(Category);
interface categoryRequestBody {
name: string;
description: string;
icon: string;
}

const createCategoryRules = [
check('name').isLength({ min: 1 }).withMessage('Category name is required'),
check('icon').isLength({ min: 1 }).withMessage('Category icon is required'),
check('description')
.isLength({ min: 1 })
.withMessage('Category description is required'),
Expand All @@ -25,7 +27,7 @@ export const createCategory = [
if (!errors.isEmpty()) {
return res.status(400).json({ errors: errors.array() });
}
const { name, description } = req.body as categoryRequestBody;
const { name, description, icon } = req.body as categoryRequestBody;

const existingCategory = await categoryRepository.findOne({
where: { name },
Expand All @@ -36,6 +38,7 @@ export const createCategory = [
const newCategory = new Category({
name: name,
description: description,
icon: icon,
});
const updatedCategory = await categoryRepository.save(newCategory);
return res.status(201).json({
Expand Down Expand Up @@ -79,7 +82,7 @@ export const updateCategory = [
}

const categoryId: number = parseInt(req.params.categoryId);
const { name, description } = req.body as categoryRequestBody;
const { name, description, icon } = req.body as categoryRequestBody;

const category = await categoryRepository.findOne({
where: { id: categoryId },
Expand All @@ -99,6 +102,7 @@ export const updateCategory = [

category.name = name;
category.description = description;
category.icon = icon;

const updatedCategory = await categoryRepository.save(category);

Expand Down
3 changes: 3 additions & 0 deletions src/database/models/categoryEntity.ts
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,9 @@ export default class Category {
@Column({ length: 250 })
description: string;

@Column({ length: 250, default: 'icon unavailable' })
icon: string;

@OneToMany(() => Product, (product) => product.category, {
cascade: ['update'],
})
Expand Down

0 comments on commit 43d546e

Please sign in to comment.