-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
5 changed files
with
171 additions
and
0 deletions.
There are no files selected for viewing
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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -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 | ||
}); | ||
}); |
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,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'); | ||
} | ||
}; |
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,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; | ||
}; |
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,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; |