refactor(user): 重构用户相关代码并统一错误信息格式

- 修改用户控制器、模型和服务中的方法命名,使用驼峰命名法
- 更新错误信息常量,使用驼峰命名法
- 优化部分代码结构,提高可读性
This commit is contained in:
LingandRX 2025-01-04 23:31:15 +08:00
parent 602c0fff4b
commit 04885717c2
3 changed files with 12 additions and 12 deletions

View File

@ -8,7 +8,7 @@ const { SearchQuery } = require('../models/search')
exports.getAllUsers = async (res) => {
try {
const users = await userService.user_list()
const users = await userService.getUserList()
return FetchResult.formatResult(res, HTTP_STATUS.ACCEPTED, 'success', users)
} catch (err) {
return FetchResult.formatResult(res, HTTP_STATUS.INTERNAL_SERVER_ERROR, 'Internal server error')
@ -55,8 +55,8 @@ exports.createUser = [
return FetchResult.formatResult(res, HTTP_STATUS.CREATED, messages.user.created)
} catch (err) {
logger('Error creating user: ', err)
if (err.message === messages.user.already_exists) {
return FetchResult.formatResult(res, HTTP_STATUS.CONFLICT, messages.user.already_exists)
if (err.message === messages.user.alreadyExists) {
return FetchResult.formatResult(res, HTTP_STATUS.CONFLICT, messages.user.alreadyExists)
}
return FetchResult.formatResult(res, HTTP_STATUS.INTERNAL_SERVER_ERROR, 'Internal server error')
}
@ -113,7 +113,7 @@ exports.getUserExist = async (req, res) => {
try {
const { account } = req.query
const exist = await userService.get_user_exist(account)
const exist = await userService.getUserExists(account)
if (!exist) {
return FetchResult.formatResult(res, HTTP_STATUS.NOT_FOUND, messages.user.not_found)

View File

@ -11,8 +11,8 @@ const UserSchema = new Schema({
password: { type: String, required: true, maxlength: 100 },
email: { type: String, maxlength: 255, index: true },
phone: { type: String, maxlength: 11 },
register_date: { type: Date, default: Date.now },
last_login_date: { type: Date, default: Date.now },
registerDate: { type: Date, default: Date.now },
lastLoginDate: { type: Date, default: Date.now },
status: { type: String, enum: ['active', 'inactive', 'pending'], default: 'pending' }
})

View File

@ -21,12 +21,12 @@ exports.login = async (account, password) => {
// 密码不匹配
const isMatch = await comparePassword(password, user.password)
if (!isMatch) {
throw new Error(messages.user.password_incorrect)
throw new Error(messages.user.passwordIncorrect)
}
// 更新用户的最后登录时间
user.last_login_date = new Date()
await userMapper.updateUserByLoginDate(user.id, user.last_login_date)
user.lastLoginDate = new Date()
await userMapper.updateUserByLoginDate(user.id, user.lastLoginDate)
return user
}
@ -36,12 +36,12 @@ exports.login = async (account, password) => {
* @param {string} account
* @returns {Promise<boolean>}
*/
exports.get_user_exist = async (account) => {
exports.getUserExists = async (account) => {
return userMapper.selectUserByAccountExist(account)
}
// Removed the unnecessary try-catch block and simplified the function
exports.user_list = async () => {
exports.getAllUser = async () => {
return await userMapper.selectAllUser()
}
@ -63,7 +63,7 @@ exports.createUser = async (user) => {
// 检查用户是否已存在
const existingUser = await userMapper.selectUserByAccount(account)
if (existingUser) {
return new Error(messages.user.already_exists)
return new Error(messages.user.alreadyExists)
}
// 创建新用户