From 04885717c2ca024f453d30f87e9d2793545a8d25 Mon Sep 17 00:00:00 2001 From: LingandRX Date: Sat, 4 Jan 2025 23:31:15 +0800 Subject: [PATCH] =?UTF-8?q?refactor(user):=20=E9=87=8D=E6=9E=84=E7=94=A8?= =?UTF-8?q?=E6=88=B7=E7=9B=B8=E5=85=B3=E4=BB=A3=E7=A0=81=E5=B9=B6=E7=BB=9F?= =?UTF-8?q?=E4=B8=80=E9=94=99=E8=AF=AF=E4=BF=A1=E6=81=AF=E6=A0=BC=E5=BC=8F?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit - 修改用户控制器、模型和服务中的方法命名,使用驼峰命名法 - 更新错误信息常量,使用驼峰命名法 - 优化部分代码结构,提高可读性 --- controllers/userController.js | 8 ++++---- models/userModel.js | 4 ++-- services/userService.js | 12 ++++++------ 3 files changed, 12 insertions(+), 12 deletions(-) diff --git a/controllers/userController.js b/controllers/userController.js index 59482af..53f7d4a 100644 --- a/controllers/userController.js +++ b/controllers/userController.js @@ -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) diff --git a/models/userModel.js b/models/userModel.js index f194c9a..75aee0b 100644 --- a/models/userModel.js +++ b/models/userModel.js @@ -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' } }) diff --git a/services/userService.js b/services/userService.js index f56d46c..745d1f8 100644 --- a/services/userService.js +++ b/services/userService.js @@ -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} */ -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) } // 创建新用户