mirror of
https://github.com/dalbodeule/snap-admin.git
synced 2025-12-15 04:41:58 +09:00
0.1.0
This commit is contained in:
@@ -0,0 +1,12 @@
|
||||
package tech.ailef.dbadmin.internal;
|
||||
|
||||
import org.springframework.boot.autoconfigure.condition.ConditionalOnProperty;
|
||||
import org.springframework.context.annotation.ComponentScan;
|
||||
import org.springframework.context.annotation.Configuration;
|
||||
|
||||
@ConditionalOnProperty(name = "dbadmin.enabled", matchIfMissing = true)
|
||||
@ComponentScan
|
||||
@Configuration
|
||||
public class InternalDbAdminConfiguration {
|
||||
|
||||
}
|
||||
@@ -0,0 +1,30 @@
|
||||
package tech.ailef.dbadmin.internal;
|
||||
|
||||
import java.util.HashMap;
|
||||
import java.util.Map;
|
||||
import java.util.Optional;
|
||||
|
||||
import org.springframework.beans.factory.annotation.Autowired;
|
||||
import org.springframework.stereotype.Component;
|
||||
|
||||
import tech.ailef.dbadmin.internal.model.UserSetting;
|
||||
import tech.ailef.dbadmin.internal.repository.UserSettingsRepository;
|
||||
|
||||
@Component
|
||||
public class UserConfiguration {
|
||||
@Autowired
|
||||
private UserSettingsRepository repo;
|
||||
|
||||
public String get(String settingName) {
|
||||
Optional<UserSetting> setting = repo.findById(settingName);
|
||||
if (setting.isPresent())
|
||||
return setting.get().getSettingValue();
|
||||
return defaultValues().get(settingName);
|
||||
}
|
||||
|
||||
private Map<String, String> defaultValues() {
|
||||
Map<String, String> values = new HashMap<>();
|
||||
values.put("brandName", "Spring Boot Database Admin");
|
||||
return values;
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,98 @@
|
||||
package tech.ailef.dbadmin.internal.model;
|
||||
|
||||
import java.time.LocalDateTime;
|
||||
|
||||
import org.springframework.format.datetime.standard.DateTimeFormatterFactory;
|
||||
|
||||
import jakarta.persistence.Column;
|
||||
import jakarta.persistence.Entity;
|
||||
import jakarta.persistence.GeneratedValue;
|
||||
import jakarta.persistence.GenerationType;
|
||||
import jakarta.persistence.Id;
|
||||
import jakarta.persistence.Lob;
|
||||
|
||||
@Entity
|
||||
public class UserAction {
|
||||
@Id
|
||||
@GeneratedValue(strategy = GenerationType.IDENTITY)
|
||||
private Integer id;
|
||||
|
||||
@Column(nullable = false)
|
||||
private LocalDateTime createdAt;
|
||||
|
||||
@Lob
|
||||
@Column(nullable = false)
|
||||
private String sql;
|
||||
|
||||
@Column(nullable = false)
|
||||
private String onTable;
|
||||
|
||||
@Column(nullable = false)
|
||||
private String primaryKey;
|
||||
|
||||
@Column(nullable = false)
|
||||
private String actionType;
|
||||
|
||||
public UserAction() {
|
||||
}
|
||||
|
||||
public UserAction(String onTable, String primaryKey, String actionType) {
|
||||
this.createdAt = LocalDateTime.now();
|
||||
this.sql = "SQL TODO";
|
||||
this.onTable = onTable;
|
||||
this.actionType = actionType;
|
||||
this.primaryKey = primaryKey;
|
||||
}
|
||||
|
||||
public Integer getId() {
|
||||
return id;
|
||||
}
|
||||
|
||||
public void setId(Integer id) {
|
||||
this.id = id;
|
||||
}
|
||||
|
||||
public LocalDateTime getCreatedAt() {
|
||||
return createdAt;
|
||||
}
|
||||
|
||||
public void setCreatedAt(LocalDateTime createdAt) {
|
||||
this.createdAt = createdAt;
|
||||
}
|
||||
|
||||
public String getSql() {
|
||||
return sql;
|
||||
}
|
||||
|
||||
public void setSql(String sql) {
|
||||
this.sql = sql;
|
||||
}
|
||||
|
||||
public String getOnTable() {
|
||||
return onTable;
|
||||
}
|
||||
|
||||
public void setOnTable(String onTable) {
|
||||
this.onTable = onTable;
|
||||
}
|
||||
|
||||
public String getPrimaryKey() {
|
||||
return primaryKey;
|
||||
}
|
||||
|
||||
public void setPrimaryKey(String primaryKey) {
|
||||
this.primaryKey = primaryKey;
|
||||
}
|
||||
|
||||
public String getActionType() {
|
||||
return actionType;
|
||||
}
|
||||
|
||||
public void setActionType(String actionType) {
|
||||
this.actionType = actionType;
|
||||
}
|
||||
|
||||
public String getFormattedDate() {
|
||||
return new DateTimeFormatterFactory("YYYY-MM-dd HH:mm:ss").createDateTimeFormatter().format(createdAt);
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,37 @@
|
||||
package tech.ailef.dbadmin.internal.model;
|
||||
|
||||
import jakarta.persistence.Entity;
|
||||
import jakarta.persistence.Id;
|
||||
|
||||
@Entity
|
||||
public class UserSetting {
|
||||
@Id
|
||||
private String id;
|
||||
|
||||
private String settingValue;
|
||||
|
||||
public UserSetting() {
|
||||
}
|
||||
|
||||
public UserSetting(String id, String settingValue) {
|
||||
this.id = id;
|
||||
this.settingValue = settingValue;
|
||||
}
|
||||
|
||||
public String getId() {
|
||||
return id;
|
||||
}
|
||||
|
||||
public void setId(String id) {
|
||||
this.id = id;
|
||||
}
|
||||
|
||||
public String getSettingValue() {
|
||||
return settingValue;
|
||||
}
|
||||
|
||||
public void setSettingValue(String settingValue) {
|
||||
this.settingValue = settingValue;
|
||||
}
|
||||
|
||||
}
|
||||
@@ -0,0 +1,14 @@
|
||||
package tech.ailef.dbadmin.internal.repository;
|
||||
|
||||
import java.util.List;
|
||||
|
||||
import org.springframework.data.domain.PageRequest;
|
||||
|
||||
import tech.ailef.dbadmin.internal.model.UserAction;
|
||||
|
||||
public interface CustomActionRepository {
|
||||
public List<UserAction> findActions(String table, String actionType, String itemId, PageRequest pageRequest);
|
||||
|
||||
public long countActions(String table, String actionType, String itemId);
|
||||
|
||||
}
|
||||
@@ -0,0 +1,76 @@
|
||||
package tech.ailef.dbadmin.internal.repository;
|
||||
|
||||
import java.util.ArrayList;
|
||||
import java.util.List;
|
||||
|
||||
import org.springframework.data.domain.PageRequest;
|
||||
import org.springframework.stereotype.Component;
|
||||
|
||||
import jakarta.persistence.EntityManager;
|
||||
import jakarta.persistence.PersistenceContext;
|
||||
import jakarta.persistence.criteria.CriteriaBuilder;
|
||||
import jakarta.persistence.criteria.CriteriaQuery;
|
||||
import jakarta.persistence.criteria.Predicate;
|
||||
import jakarta.persistence.criteria.Root;
|
||||
import tech.ailef.dbadmin.internal.model.UserAction;
|
||||
|
||||
@Component
|
||||
public class CustomActionRepositoryImpl implements CustomActionRepository {
|
||||
|
||||
@PersistenceContext(unitName = "internal")
|
||||
private EntityManager entityManager;
|
||||
|
||||
@Override
|
||||
public List<UserAction> findActions(String table, String actionType, String itemId, PageRequest page) {
|
||||
|
||||
CriteriaBuilder cb = entityManager.getCriteriaBuilder();
|
||||
CriteriaQuery<UserAction> query = cb.createQuery(UserAction.class);
|
||||
Root<UserAction> userAction = query.from(UserAction.class);
|
||||
|
||||
List<Predicate> predicates = new ArrayList<Predicate>();
|
||||
if (table != null)
|
||||
predicates.add(cb.equal(userAction.get("onTable"), table));
|
||||
if (actionType != null)
|
||||
predicates.add(cb.equal(userAction.get("actionType"), actionType));
|
||||
if (itemId != null)
|
||||
predicates.add(cb.equal(userAction.get("primaryKey"), itemId));
|
||||
|
||||
if (!predicates.isEmpty()) {
|
||||
query.select(userAction)
|
||||
.where(cb.and(
|
||||
predicates.toArray(new Predicate[predicates.size()])));
|
||||
}
|
||||
|
||||
return entityManager.createQuery(query)
|
||||
.setMaxResults(page.getPageSize())
|
||||
.setFirstResult((int)page.getOffset())
|
||||
.getResultList();
|
||||
}
|
||||
|
||||
@Override
|
||||
public long countActions(String table, String actionType, String itemId) {
|
||||
|
||||
CriteriaBuilder cb = entityManager.getCriteriaBuilder();
|
||||
CriteriaQuery<Long> query = cb.createQuery(Long.class);
|
||||
Root<UserAction> userAction = query.from(UserAction.class);
|
||||
|
||||
List<Predicate> predicates = new ArrayList<>();
|
||||
if (table != null)
|
||||
predicates.add(cb.equal(userAction.get("onTable"), table));
|
||||
if (actionType != null)
|
||||
predicates.add(cb.equal(userAction.get("actionType"), actionType));
|
||||
if (itemId != null)
|
||||
predicates.add(cb.equal(userAction.get("primaryKey"), itemId));
|
||||
|
||||
if (!predicates.isEmpty()) {
|
||||
query.select(cb.count(userAction))
|
||||
.where(cb.and(
|
||||
predicates.toArray(new Predicate[predicates.size()])));
|
||||
} else {
|
||||
query.select(cb.count(userAction));
|
||||
}
|
||||
|
||||
return entityManager.createQuery(query).getSingleResult();
|
||||
}
|
||||
|
||||
}
|
||||
@@ -0,0 +1,15 @@
|
||||
package tech.ailef.dbadmin.internal.repository;
|
||||
|
||||
import java.util.List;
|
||||
|
||||
import org.springframework.data.domain.PageRequest;
|
||||
import org.springframework.data.jpa.repository.JpaRepository;
|
||||
import org.springframework.stereotype.Repository;
|
||||
|
||||
import tech.ailef.dbadmin.internal.model.UserAction;
|
||||
|
||||
@Repository
|
||||
public interface UserActionRepository extends JpaRepository<UserAction, Integer>, CustomActionRepository {
|
||||
public List<UserAction> findAllByOnTableAndActionTypeAndPrimaryKey(String table, String actionType, String primaryKey, PageRequest pageRequest);
|
||||
|
||||
}
|
||||
@@ -0,0 +1,11 @@
|
||||
package tech.ailef.dbadmin.internal.repository;
|
||||
|
||||
import org.springframework.data.jpa.repository.JpaRepository;
|
||||
import org.springframework.stereotype.Repository;
|
||||
|
||||
import tech.ailef.dbadmin.internal.model.UserSetting;
|
||||
|
||||
@Repository
|
||||
public interface UserSettingsRepository extends JpaRepository<UserSetting, String> {
|
||||
|
||||
}
|
||||
@@ -0,0 +1,40 @@
|
||||
package tech.ailef.dbadmin.internal.service;
|
||||
|
||||
import java.util.List;
|
||||
|
||||
import org.springframework.beans.factory.annotation.Autowired;
|
||||
import org.springframework.data.domain.PageRequest;
|
||||
import org.springframework.stereotype.Service;
|
||||
import org.springframework.transaction.annotation.Transactional;
|
||||
|
||||
import tech.ailef.dbadmin.external.dto.PaginatedResult;
|
||||
import tech.ailef.dbadmin.external.dto.PaginationInfo;
|
||||
import tech.ailef.dbadmin.internal.model.UserAction;
|
||||
import tech.ailef.dbadmin.internal.repository.CustomActionRepositoryImpl;
|
||||
import tech.ailef.dbadmin.internal.repository.UserActionRepository;
|
||||
|
||||
@Service
|
||||
public class UserActionService {
|
||||
@Autowired
|
||||
private UserActionRepository repo;
|
||||
|
||||
@Autowired
|
||||
private CustomActionRepositoryImpl customRepo;
|
||||
|
||||
@Transactional("internalTransactionManager")
|
||||
public UserAction save(UserAction a) {
|
||||
return repo.save(a);
|
||||
}
|
||||
|
||||
public PaginatedResult<UserAction> findActions(String table, String actionType, String userId, PageRequest page) {
|
||||
long count = customRepo.countActions(table, actionType, userId);
|
||||
List<UserAction> actions = customRepo.findActions(table, actionType, userId, page);
|
||||
int maxPage = (int)(Math.ceil ((double)count / page.getPageSize()));
|
||||
|
||||
return new PaginatedResult<>(
|
||||
new PaginationInfo(page.getPageNumber() + 1, maxPage, page.getPageSize(), count, null, null),
|
||||
actions
|
||||
);
|
||||
}
|
||||
|
||||
}
|
||||
Reference in New Issue
Block a user