feat(user): 添加用户相关错误消息和事务处理功能
This commit is contained in:
parent
9dab2c0ae5
commit
2734217bcd
26
config/errorMessages.js
Normal file
26
config/errorMessages.js
Normal file
@ -0,0 +1,26 @@
|
||||
module.exports = {
|
||||
user: {
|
||||
not_found: 'User not found',
|
||||
already_exists: 'User already exists',
|
||||
created: 'User created',
|
||||
updated: 'User updated',
|
||||
deleted: 'User deleted',
|
||||
not_deleted: 'User not deleted',
|
||||
not_updated: 'User not updated',
|
||||
not_created: 'User not created',
|
||||
not_found_id: 'User not found',
|
||||
not_found_account: 'User not found',
|
||||
not_found_email: 'User not found',
|
||||
not_found_phone: 'User not found',
|
||||
not_found_username: 'User not found',
|
||||
not_found_role: 'User not found',
|
||||
not_found_status: 'User not found',
|
||||
not_found_created_at: 'User not found',
|
||||
not_found_updated_at: 'User not found',
|
||||
not_found_deleted_at: 'User not found',
|
||||
not_found_created_by: 'User not found',
|
||||
not_found_updated_by: 'User not found',
|
||||
not_found_deleted_by: 'User not found',
|
||||
not_found_password: 'User not found',
|
||||
}
|
||||
}
|
||||
@ -1,5 +1,6 @@
|
||||
const { body, validationResult } = require('express-validator')
|
||||
const userService = require('../services/userService')
|
||||
const errorMessages = require('../config/errorMessages')
|
||||
|
||||
exports.getAllUsers = async (req, res) => {
|
||||
try {
|
||||
@ -11,18 +12,31 @@ exports.getAllUsers = async (req, res) => {
|
||||
}
|
||||
|
||||
exports.createUser = [
|
||||
body('account').isLength({ min: 3 }),
|
||||
body('password').isLength({ min: 6 }),
|
||||
body('account').isLength({ min: 3 }).withMessage('Account must be at least 3 characters long'),
|
||||
body('password').isLength({ min: 6 }).withMessage('Password must be at least 6 characters long'),
|
||||
async (req, res, next) => {
|
||||
const errors = validationResult(req)
|
||||
if (!errors.isEmpty()) {
|
||||
return res.status(400).json({ errors: errors.array() })
|
||||
const formattedErrors = errors.array().map((err) => ({
|
||||
field: err.path, // 错误字段名
|
||||
message: err.msg // 错误提示消息
|
||||
}))
|
||||
return res.status(400).json({ errors: formattedErrors })
|
||||
}
|
||||
|
||||
next()
|
||||
},
|
||||
async (req, res) => {
|
||||
try {
|
||||
await userService.create_user(req.body)
|
||||
res.status(201).json({ message: 'User created successfully' })
|
||||
const user = req.body
|
||||
await userService.create_user(user)
|
||||
res.status(201).json({ message: errorMessages.user.created })
|
||||
} catch (err) {
|
||||
res.status(500).json({ error: err.message })
|
||||
if (err.message === errorMessages.user.already_exists) {
|
||||
return res.status(400).json({ error: err.message })
|
||||
}
|
||||
logger('Error creating user: ', err)
|
||||
return res.status(500).json({ error: 'Internal server error' })
|
||||
}
|
||||
}
|
||||
]
|
||||
|
||||
@ -1,9 +1,63 @@
|
||||
const UserModel = require('../models/userModel')
|
||||
|
||||
exports.startTransaction = async () => {
|
||||
const seesion = await UserModel.startSession()
|
||||
seesion.startTransaction()
|
||||
return seesion
|
||||
}
|
||||
|
||||
exports.commitTransaction = async (session) => {
|
||||
return await session.commitTransaction()
|
||||
}
|
||||
|
||||
exports.rollbackTransaction = async (session) => {
|
||||
return await session.abortTransaction()
|
||||
}
|
||||
|
||||
exports.selectAllUser = async () => {
|
||||
return await UserModel.find()
|
||||
}
|
||||
|
||||
exports.selectUserById = async (id) => {
|
||||
return await UserModel.findById(id)
|
||||
}
|
||||
|
||||
exports.selectUserByUsernameAndPassword = async (username, password) => {
|
||||
return await UserModel.findOne({ username: username, password: password })
|
||||
}
|
||||
|
||||
exports.selectUserByAccountAndPassword = async (account, password) => {
|
||||
return await UserModel.findOne({ account: account, password: password })
|
||||
}
|
||||
|
||||
exports.selectUserByAccount = async (account) => {
|
||||
return await UserModel.findOne({ account: account })
|
||||
}
|
||||
|
||||
exports.selectUserByEmail = async (email) => {
|
||||
return await UserModel.findOne({ email: email })
|
||||
}
|
||||
|
||||
exports.selectUserByUsername = async (username) => {
|
||||
return await UserModel.findOne({ username: username })
|
||||
}
|
||||
|
||||
exports.selectUserByPhone = async (phone) => {
|
||||
return await UserModel.findOne({ phone: phone })
|
||||
}
|
||||
|
||||
exports.createUser = async (user) => {
|
||||
return await UserModel.create(user)
|
||||
}
|
||||
|
||||
exports.updateUserById = async (id, user) => {
|
||||
return await UserModel.findByIdAndUpdate(id, user)
|
||||
}
|
||||
|
||||
exports.deleteUserById = async (id) => {
|
||||
return await UserModel.findByIdAndDelete(id)
|
||||
}
|
||||
|
||||
exports.deleteAllUser = async () => {
|
||||
return await UserModel.deleteMany()
|
||||
}
|
||||
|
||||
@ -1,4 +1,6 @@
|
||||
const userMapper = require('../repositories/userRepository')
|
||||
const logger = require('morgan')
|
||||
const errorMessages = require('../config/errorMessages')
|
||||
|
||||
exports.user_list = async () => {
|
||||
try {
|
||||
@ -11,9 +13,31 @@ exports.user_list = async () => {
|
||||
|
||||
exports.create_user = async (user) => {
|
||||
try {
|
||||
const { account } = user
|
||||
|
||||
const session = await userMapper.startTransaction()
|
||||
|
||||
try {
|
||||
const check_user = await userMapper.selectUserByAccount(account)
|
||||
|
||||
if (check_user) {
|
||||
throw new Error(errorMessages.user.already_exists)
|
||||
}
|
||||
|
||||
const result = await userMapper.createUser(user)
|
||||
|
||||
await userMapper.commitTransaction(session)
|
||||
return result
|
||||
} catch (err) {
|
||||
try {
|
||||
await userMapper.rollbackTransaction(session)
|
||||
} catch (roolbackErr) {
|
||||
logger('Error rolling back transaction: ', roolbackErr)
|
||||
}
|
||||
throw err
|
||||
}
|
||||
} catch (err) {
|
||||
logger('Error creating user: ', err)
|
||||
throw err
|
||||
}
|
||||
}
|
||||
|
||||
Loading…
Reference in New Issue
Block a user