From f355d5941ace2376d5601eecc8f0ee4ba27d191e Mon Sep 17 00:00:00 2001 From: Ahmad Nurul Laiq Date: Thu, 25 Apr 2024 12:02:03 +0700 Subject: [PATCH] chore: one to one relationship --- app.js | 2 + controllers/profileController.js | 51 ++++++++++++++++++ migrations/20240425041201-create-profile.js | 49 ++++++++++++++++++ models/profile.js | 57 +++++++++++++++++++++ routes/profile.js | 12 +++++ 5 files changed, 171 insertions(+) create mode 100644 controllers/profileController.js create mode 100644 migrations/20240425041201-create-profile.js create mode 100644 models/profile.js create mode 100644 routes/profile.js diff --git a/app.js b/app.js index 010ef97..c474d0f 100644 --- a/app.js +++ b/app.js @@ -5,6 +5,7 @@ const cors = require('cors'); const CategoriesRouter = require('./routes/catagories'); const AuthRouter = require('./routes/auth'); const ProductRouter = require('./routes/product'); +const ProfileRouter = require('./routes/profile'); const morgan = require('morgan'); const {errorHandler, notFound} = require('./middleware/errorMiddleware'); const cookieParser = require('cookie-parser'); @@ -24,6 +25,7 @@ app.use('/public/uploads', express.static(path.join(__dirname + '/public/uploads app.use('/api/v1/categories', CategoriesRouter); app.use('/api/v1/auth', AuthRouter); app.use('/api/v1/products', ProductRouter); +app.use('/api/v1/profiles', ProfileRouter); // Error handling app.use(notFound); diff --git a/controllers/profileController.js b/controllers/profileController.js new file mode 100644 index 0000000..bc7f41f --- /dev/null +++ b/controllers/profileController.js @@ -0,0 +1,51 @@ +const asyncHandle = require('../middleware/asyncHandle'); +const { + Profile +} = require("../models"); + +//update or create profile data for user with userId +exports.updateOrCreateProfile = asyncHandle(async (req, res) => { + const { + age, + bio, + address, + } = req.body; + + const idUser = req.user.id; + + const profile = await Profile.findOne({ + where: { + userId: idUser + } + }); + + let message = ""; + + if (profile) { + await Profile.update({ + age, + bio, + address + }, { + where: { + userId: idUser + } + }); + + message = "Profile updated successfully"; + } else { + await Profile.create({ + age, + bio, + address, + userId: idUser + }); + + message = "Profile created successfully"; + } + + res.status(200).json({ + status: "success", + message: message + }); +}); \ No newline at end of file diff --git a/migrations/20240425041201-create-profile.js b/migrations/20240425041201-create-profile.js new file mode 100644 index 0000000..395a3ae --- /dev/null +++ b/migrations/20240425041201-create-profile.js @@ -0,0 +1,49 @@ +'use strict'; +/** @type {import('sequelize-cli').Migration} */ +module.exports = { + async up(queryInterface, Sequelize) { + await queryInterface.createTable('Profiles', { + id: { + allowNull: false, + primaryKey: true, + type: Sequelize.UUID, + defaultValue: Sequelize.UUIDV4 + }, + age: { + type: Sequelize.INTEGER, + allowNull: false + }, + bio: { + type: Sequelize.TEXT + }, + address: { + type: Sequelize.STRING, + allowNull: false + }, + image: { + type: Sequelize.STRING, + }, + userId: { + type: Sequelize.UUID, + allowNull: false, + references: { + model: 'Users', + key: 'id' + }, + onDelete: 'CASCADE', + onUpdate: 'CASCADE' + }, + createdAt: { + allowNull: false, + type: Sequelize.DATE + }, + updatedAt: { + allowNull: false, + type: Sequelize.DATE + } + }); + }, + async down(queryInterface, Sequelize) { + await queryInterface.dropTable('Profiles'); + } +}; \ No newline at end of file diff --git a/models/profile.js b/models/profile.js new file mode 100644 index 0000000..6251d3b --- /dev/null +++ b/models/profile.js @@ -0,0 +1,57 @@ +'use strict'; +const { + Model +} = require('sequelize'); +module.exports = (sequelize, DataTypes) => { + class Profile extends Model { + /** + * Helper method for defining associations. + * This method is not a part of Sequelize lifecycle. + * The `models/index` file will call this method automatically. + */ + static associate(models) { + // define association here + } + } + Profile.init({ + id: { + type: DataTypes.UUID, + primaryKey: true, + defaultValue: DataTypes.UUIDV4 + }, + age: { + type: DataTypes.INTEGER, + allowNull: false + }, + bio: { + type: DataTypes.TEXT + }, + address: { + type: DataTypes.STRING, + allowNull: false + }, + image: { + type: DataTypes.STRING, + }, + userId: { + type: DataTypes.UUID, + allowNull: false, + validate : { + notNull : { + msg : "userId is required" + }, + isExist(value) { + return sequelize.models.User.findByPk(value).then((user) => { + if (!user) { + throw new Error('User not found'); + } + }); + } + } + } + }, { + sequelize, + modelName: 'Profile', + }); + return Profile; +}; \ No newline at end of file diff --git a/routes/profile.js b/routes/profile.js new file mode 100644 index 0000000..98a8a46 --- /dev/null +++ b/routes/profile.js @@ -0,0 +1,12 @@ +const express = require('express'); +const router = express.Router(); + +const { + updateOrCreateProfile +} = require('../controllers/profileController'); + +const {authMiddleware} = require('../middleware/userMiddleware'); + +router.post('/', authMiddleware, updateOrCreateProfile); + +module.exports = router; \ No newline at end of file