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

Add image illustrating category #79

Merged
merged 4 commits into from
Jun 23, 2024
Merged
Show file tree
Hide file tree
Changes from all 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
37 changes: 27 additions & 10 deletions src/controllers/categoriesController.ts
Original file line number Diff line number Diff line change
@@ -1,23 +1,40 @@
import { Request, Response } from 'express';
import { Category, CategoryCreationAttributes } from '../database/models/Category';
import { Category } from '../database/models/Category';
import logger from '../logs/config';
import uploadImage from '../helpers/claudinary';

export const createCategory = async (req: Request, res: Response) => {
try {
const { name, description } = req.body as CategoryCreationAttributes;
await Category.create({
name,
description,
});
res.status(201).json({ ok: true, message: 'New category created successully!' });
} catch (error) {
const { name, description } = req.body as { name: string; description: string };
const image = req.file;

if (image) {
const imageBuffer: Buffer = image.buffer;
try {
const url = await uploadImage(imageBuffer);

await Category.create({ name, description, image: url });

return res.status(201).json({
ok: true,
message: 'New category created successfully!',
imageUrl: url,
});
} catch (error: any) {
return res.status(500).json({ message: 'Error uploading image', error: error.message });
}
} else {
return res.status(400).json({ message: 'Image is required' });
}
} catch (error: any) {
if (error instanceof Error) {
logger.error(error.message);
}
res.status(500).json({ error: 'Failed to create category' });
return res.status(500).json({ error: 'Failed to create category', message: error.message });
}
};
export const getAllCategories = async (req: Request, res: Response) => {

export const getAllCategories: any = async (req: Request, res: Response) => {
try {
const categories = await Category.findAll();
res.status(200).json({ ok: true, data: categories });
Expand Down
15 changes: 15 additions & 0 deletions src/database/migrations/20240620145819-add-image-to-categories.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,15 @@
'use strict';

/** @type {import('sequelize-cli').Migration} */
module.exports = {
async up(queryInterface, Sequelize) {
await queryInterface.addColumn('categories', 'image', {
type: Sequelize.STRING,
allowNull: true,
});
},

async down(queryInterface, Sequelize) {
await queryInterface.removeColumn('categories', 'image');
},
};
6 changes: 6 additions & 0 deletions src/database/models/Category.ts
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@ interface CategoryAttributes {
id: number;
name: string;
description?: string;
image?: string;
}

export interface CategoryCreationAttributes extends Optional<CategoryAttributes, 'id'> {}
Expand All @@ -13,6 +14,7 @@ export class Category extends Model<CategoryAttributes, CategoryCreationAttribut
public id!: number;
public name!: string;
public description!: string;
public image?: string;
}

Category.init(
Expand All @@ -32,6 +34,10 @@ Category.init(
type: DataTypes.TEXT,
allowNull: true,
},
image: {
type: DataTypes.STRING,
allowNull: true,
},
},
{
sequelize,
Expand Down
3 changes: 2 additions & 1 deletion src/routes/categoryRouter.ts
Original file line number Diff line number Diff line change
Expand Up @@ -2,10 +2,11 @@
import express from 'express';
import { checkUserRoles, isAuthenticated } from '../middlewares/authMiddlewares';
import { createCategory, getAllCategories } from '../controllers/categoriesController';
import multerUpload from '../helpers/multer';

export const categoryRouter = express.Router();

categoryRouter
.route('/')
.post(isAuthenticated, checkUserRoles('admin'), createCategory)
.post(isAuthenticated, multerUpload.single('image'), checkUserRoles('admin'), createCategory)
.get(isAuthenticated, checkUserRoles('seller'), getAllCategories);
Loading