Skip to content

Commit

Permalink
✨ feat(LoggerFilter.java): Set creator and updater using UserAuditor
Browse files Browse the repository at this point in the history
🎨 style(`schema-postgres.sql`): Align column definitions and add `extend` field to logs
♻️ refactor(`BaseEntity.java`): Simplify `isNew` logic with Optional handling
  • Loading branch information
vnobo committed Feb 18, 2025
1 parent b001665 commit fbf1d70
Show file tree
Hide file tree
Showing 3 changed files with 17 additions and 12 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -6,11 +6,11 @@
import com.plate.boot.commons.utils.query.QueryHelper;
import org.springframework.data.domain.Persistable;
import org.springframework.data.relational.core.query.Criteria;
import org.springframework.util.ObjectUtils;

import java.io.Serializable;
import java.util.Collection;
import java.util.Map;
import java.util.Optional;
import java.util.UUID;

/**
Expand All @@ -26,7 +26,9 @@ public interface BaseEntity<T> extends Serializable, Persistable<T> {
*
* @return The unique identifier (UUID) of the entity.
*/
<E> E getCode();
default <E> E getCode() {
return null;
}

/**
* Sets the unique code for an entity.
Expand Down Expand Up @@ -87,11 +89,10 @@ default void setQuery(Map<String, Object> query) {
@Override
@JsonIgnore
default boolean isNew() {
boolean isNew = ObjectUtils.isEmpty(getCode());
if (isNew) {
if (Optional.ofNullable(getCode()).isEmpty()) {
setCode(ContextUtils.nextId());
}
return isNew;
return Optional.ofNullable(getId()).isEmpty();
}

/**
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@
import com.plate.boot.commons.utils.ContextUtils;
import com.plate.boot.relational.logger.LoggerReq;
import com.plate.boot.security.SecurityDetails;
import com.plate.boot.security.core.UserAuditor;
import io.netty.buffer.Unpooled;
import io.netty.util.CharsetUtil;
import io.netty.util.internal.EmptyArrays;
Expand Down Expand Up @@ -389,6 +390,9 @@ private void logRequest(ServerWebExchange exchange, SecurityDetails userDetails)

LoggerReq logger = LoggerReq.of(tenantCode, userDetails.getUsername(), prefix,
method, status, path, contentNode);
var userAuditor = UserAuditor.of(userDetails.getCode(), userDetails.getUsername(), userDetails.getName());
logger.setCreator(userAuditor);
logger.setUpdater(userAuditor);
this.publisher.publishEvent(logger);
}

Expand Down
14 changes: 7 additions & 7 deletions boot/platform/src/main/resources/schema-postgres.sql
Original file line number Diff line number Diff line change
Expand Up @@ -27,7 +27,7 @@ create table oauth2_authorized_client
create table if not exists se_users
(
id serial8 primary key,
code uuid not null unique,
code uuid not null unique,
tenant_code varchar(64) not null default '0',
username varchar(256) not null unique,
password text not null,
Expand Down Expand Up @@ -178,23 +178,23 @@ create table if not exists se_loggers
code uuid not null unique,
tenant_code varchar(64) not null default '0',
prefix varchar(64),
operator uuid,
operator varchar(64),
status varchar(64),
method varchar(64),
url text,
context jsonb,
extend jsonb,
creator uuid,
updater uuid,
created_time timestamp default current_timestamp,
updated_time timestamp default current_timestamp,
text_search tsvector generated always as (
setweight(to_tsvector('chinese', code::text), 'A') || ' ' ||
setweight(to_tsvector('chinese', operator), 'A') || ' ' ||
setweight(to_tsvector('chinese', coalesce(method, '')), 'B') || ' ' ||
setweight(to_tsvector('chinese', coalesce(url, '')), 'C') || ' ' ||
setweight(jsonb_to_tsvector('chinese', context::jsonb, '[
"string",
"numeric",
"boolean",
"array",
"object"
"string"
]'), 'D')
) stored
);
Expand Down

0 comments on commit fbf1d70

Please sign in to comment.