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) } // 创建新用户