Skip to content

Commit

Permalink
feat: add delete user api
Browse files Browse the repository at this point in the history
  • Loading branch information
damingerdai committed Aug 4, 2024
1 parent 93c8978 commit dc7a99c
Show file tree
Hide file tree
Showing 6 changed files with 75 additions and 31 deletions.
16 changes: 16 additions & 0 deletions src/main/java/org/daming/hoteler/api/web/UserController.java
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,7 @@
import org.daming.hoteler.pojo.User;
import org.daming.hoteler.pojo.builder.UserBuilder;
import org.daming.hoteler.pojo.request.CreateUserRequest;
import org.daming.hoteler.pojo.response.CommonResponse;
import org.daming.hoteler.pojo.response.DataResponse;
import org.daming.hoteler.pojo.response.ListResponse;
import org.daming.hoteler.service.IErrorService;
Expand Down Expand Up @@ -86,6 +87,21 @@ public DataResponse<User> get() {
}
}

@Operation(summary = "删除用户",security = { @SecurityRequirement(name = "bearer-key") })
@DeleteMapping("/user/{id}")
public CommonResponse delete(@PathVariable("id") String userId) {
try {
var id = Long.parseLong(userId);
this.userService.delete(id);
return new CommonResponse();
} catch (NumberFormatException nfe) {
var params = new Object[] { nfe.getMessage() };
throw errorService.createHotelerException(ErrorCodeConstants.BAD_REQUEST_ERROR_CODEE, params, nfe);
} catch (Exception ex) {
throw errorService.createHotelerException(ErrorCodeConstants.SYSTEM_ERROR_CODEE, ex);
}
}


public UserController(IUserService userService, IErrorService errorService) {
super();
Expand Down
Original file line number Diff line number Diff line change
@@ -1,15 +1,18 @@
package org.daming.hoteler.repository.jdbc;

import org.daming.hoteler.base.exceptions.HotelerException;
import org.daming.hoteler.pojo.User;

import java.util.List;
import java.util.Optional;

public interface IUserDao {

Optional<User> getUserByUsername(String username);
Optional<User> getUserByUsername(String username) throws HotelerException;

Optional<User> get(long id);
Optional<User> get(long id) throws HotelerException;

List<User> getUnlockUsers();
List<User> getUnlockUsers() throws HotelerException;

void delete(long id) throws HotelerException;
}
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
package org.daming.hoteler.repository.jdbc.impl;

import org.daming.hoteler.base.exceptions.HotelerException;
import org.daming.hoteler.base.logger.SqlLoggerUtil;
import org.daming.hoteler.repository.jdbc.IUserDao;
import org.daming.hoteler.pojo.User;
Expand Down Expand Up @@ -81,6 +82,21 @@ public List<User> getUnlockUsers() {
}
}

@Override
public void delete(long id) throws HotelerException {
var in = Instant.now();
var sql = "UPDATE users SET deleted_at = now(), update_dt = now(), update_user = 'system' WHERE id = ?";
var params = new Object[] { id };
try {
this.jdbcTemplate.update(sql, params);
} catch (Exception ex) {
SqlLoggerUtil.logSqlException(sql, params, ex);
throw this.errorService.createSqlHotelerException(ex, sql);
} finally {
SqlLoggerUtil.logSql(sql, params, Duration.between(in, Instant.now()));
}
}

private static Optional<User> getUser(ResultSet rs) throws SQLException {
while (rs.next()) {
var user = new User()
Expand Down
36 changes: 18 additions & 18 deletions src/main/java/org/daming/hoteler/repository/mapper/UserMapper.java
Original file line number Diff line number Diff line change
Expand Up @@ -10,24 +10,24 @@
public interface UserMapper {

@Select("""
SELECT
u.id, u.username, u.password, u.password_type,
u.failed_login_attempts, u.account_non_locked, u.lock_time
COALESCE(ARRAY_TO_JSON(
COALESCE(
ARRAY_AGG(
JSON_BUILD_OBJECT('id', r.id, 'name', r.name, 'description', r.description)
)
FILTER
(WHERE r.id IS NOT NULL)
)
), '[]') AS roles
FROM users u
LEFT JOIN user_roles ur ON ur.user_id = u.id AND ur.deleted_at IS NULL
LEFT JOIN roles r on r.id = ur.role_id and r.deleted_at IS NULL
WHERE u.deleted_at IS NULL
GROUP BY u.id
""")
SELECT
u.id, u.username, u.password, u.password_type,
u.failed_login_attempts, u.account_non_locked, u.lock_time,
COALESCE(ARRAY_TO_JSON(
COALESCE(
ARRAY_AGG(
JSON_BUILD_OBJECT('id', r.id, 'name', r.name, 'description', r.description)
)
FILTER
(WHERE r.id IS NOT NULL)
)
), '[]') AS roles
FROM users u
LEFT JOIN user_roles ur ON ur.user_id = u.id AND ur.deleted_at IS NULL
LEFT JOIN roles r on r.id = ur.role_id and r.deleted_at IS NULL
WHERE u.deleted_at IS NULL
GROUP BY u.id
""")
@Results(
value = {
@Result(property = "id", column = "id"),
Expand Down
23 changes: 13 additions & 10 deletions src/main/java/org/daming/hoteler/service/IUserService.java
Original file line number Diff line number Diff line change
@@ -1,29 +1,32 @@
package org.daming.hoteler.service;

import org.daming.hoteler.base.exceptions.HotelerException;
import org.daming.hoteler.pojo.User;
import org.daming.hoteler.pojo.request.CreateUserRequest;

import java.util.List;

public interface IUserService {

List<User> list();
List<User> list() throws HotelerException;

User getUserByUsername(String username);
User getUserByUsername(String username) throws HotelerException;

User get(long id);
User get(long id) throws HotelerException;

void create(User user);
void create(User user) throws HotelerException;

User create(CreateUserRequest createUserRequest);
User create(CreateUserRequest createUserRequest) throws HotelerException;

void loginFailed(User user);
void loginFailed(User user) throws HotelerException;

void resetFailedAttempts(User user);
void resetFailedAttempts(User user) throws HotelerException;

boolean isAccountLocked(long id);
boolean isAccountLocked(long id) throws HotelerException;

boolean isAccountLocked(User user);
boolean isAccountLocked(User user) throws HotelerException;

List<User> getUnlockUsers();
List<User> getUnlockUsers() throws HotelerException;

void delete(long id) throws HotelerException;
}
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
package org.daming.hoteler.service.impl;

import org.daming.hoteler.base.exceptions.HotelerException;
import org.daming.hoteler.pojo.Role;
import org.daming.hoteler.pojo.request.CreateUserRequest;
import org.daming.hoteler.repository.jdbc.IUserDao;
Expand Down Expand Up @@ -194,6 +195,11 @@ public List<User> getUnlockUsers() {
return this.userDao.getUnlockUsers();
}

@Override
public void delete(long id) throws HotelerException {
this.userDao.delete(id);
}

private IPasswordService getPasswordService(String passwordType) {
return Objects.requireNonNull(super.getApplicationContext()).getBean(passwordType + "PasswordService", IPasswordService.class);
}
Expand Down

0 comments on commit dc7a99c

Please sign in to comment.