This commit is contained in:
Francesco 2023-09-22 17:18:18 +02:00
parent e6065b3dc3
commit 6597a228f0
5 changed files with 162 additions and 53 deletions

View File

@ -38,7 +38,7 @@ import tech.ailef.dbadmin.external.dto.PaginatedResult;
import tech.ailef.dbadmin.external.dto.QueryFilter; import tech.ailef.dbadmin.external.dto.QueryFilter;
import tech.ailef.dbadmin.external.exceptions.InvalidPageException; import tech.ailef.dbadmin.external.exceptions.InvalidPageException;
import tech.ailef.dbadmin.external.misc.Utils; import tech.ailef.dbadmin.external.misc.Utils;
import tech.ailef.dbadmin.internal.model.Action; import tech.ailef.dbadmin.internal.model.UserAction;
import tech.ailef.dbadmin.internal.repository.ActionRepository; import tech.ailef.dbadmin.internal.repository.ActionRepository;
/** /**
@ -66,18 +66,7 @@ public class DefaultDbAdminController {
* @return * @return
*/ */
@GetMapping @GetMapping
@Transactional("internalTransactionManager")
public String index(Model model, @RequestParam(required = false) String query) { public String index(Model model, @RequestParam(required = false) String query) {
Action a = new Action();
a.setDescription("ciao");
// a.setId(1);
// entityManagerFactory.createEntityManager().persist(a);
// entityManager.persist(a);
Action save = repo.save(a);
System.out.println(save);
// repo.save(a);
// displayAllBeans();
List<DbObjectSchema> schemas = dbAdmin.getSchemas(); List<DbObjectSchema> schemas = dbAdmin.getSchemas();
if (query != null && !query.isBlank()) { if (query != null && !query.isBlank()) {
@ -282,6 +271,8 @@ public class DefaultDbAdminController {
attr.addFlashAttribute("error", e.getMessage()); attr.addFlashAttribute("error", e.getMessage());
} }
saveAction(new UserAction(schema.getTableName(), id, "DELETE"));
return "redirect:/" + properties.getBaseUrl() + "/model/" + className; return "redirect:/" + properties.getBaseUrl() + "/model/" + className;
} }
@ -308,7 +299,7 @@ public class DefaultDbAdminController {
if (countDeleted > 0) if (countDeleted > 0)
attr.addFlashAttribute("message", "Deleted " + countDeleted + " of " + ids.length + " items"); attr.addFlashAttribute("message", "Deleted " + countDeleted + " of " + ids.length + " items");
saveAction(new UserAction(schema.getTableName(), String.join(", ", ids), "DELETE"));
return "redirect:/" + properties.getBaseUrl() + "/model/" + className; return "redirect:/" + properties.getBaseUrl() + "/model/" + className;
} }
@ -371,6 +362,7 @@ public class DefaultDbAdminController {
repository.attachManyToMany(schema, newPrimaryKey, multiValuedParams); repository.attachManyToMany(schema, newPrimaryKey, multiValuedParams);
pkValue = newPrimaryKey.toString(); pkValue = newPrimaryKey.toString();
attr.addFlashAttribute("message", "Item created successfully."); attr.addFlashAttribute("message", "Item created successfully.");
saveAction(new UserAction(schema.getTableName(), pkValue, "CREATE"));
} catch (DataIntegrityViolationException e) { } catch (DataIntegrityViolationException e) {
attr.addFlashAttribute("errorTitle", "Unable to INSERT row"); attr.addFlashAttribute("errorTitle", "Unable to INSERT row");
attr.addFlashAttribute("error", e.getMessage()); attr.addFlashAttribute("error", e.getMessage());
@ -394,6 +386,7 @@ public class DefaultDbAdminController {
repository.update(schema, params, files); repository.update(schema, params, files);
repository.attachManyToMany(schema, pkValue, multiValuedParams); repository.attachManyToMany(schema, pkValue, multiValuedParams);
attr.addFlashAttribute("message", "Item saved successfully."); attr.addFlashAttribute("message", "Item saved successfully.");
saveAction(new UserAction(schema.getTableName(), pkValue, "EDIT"));
} catch (DataIntegrityViolationException e) { } catch (DataIntegrityViolationException e) {
attr.addFlashAttribute("errorTitle", "Unable to UPDATE row (no changes applied)"); attr.addFlashAttribute("errorTitle", "Unable to UPDATE row (no changes applied)");
attr.addFlashAttribute("error", e.getMessage()); attr.addFlashAttribute("error", e.getMessage());
@ -409,6 +402,7 @@ public class DefaultDbAdminController {
Object newPrimaryKey = repository.create(schema, params, files, pkValue); Object newPrimaryKey = repository.create(schema, params, files, pkValue);
repository.attachManyToMany(schema, newPrimaryKey, multiValuedParams); repository.attachManyToMany(schema, newPrimaryKey, multiValuedParams);
attr.addFlashAttribute("message", "Item created successfully"); attr.addFlashAttribute("message", "Item created successfully");
saveAction(new UserAction(schema.getTableName(), pkValue, "CREATE"));
} catch (DataIntegrityViolationException e) { } catch (DataIntegrityViolationException e) {
attr.addFlashAttribute("errorTitle", "Unable to INSERT row (no changes applied)"); attr.addFlashAttribute("errorTitle", "Unable to INSERT row (no changes applied)");
attr.addFlashAttribute("error", e.getMessage()); attr.addFlashAttribute("error", e.getMessage());
@ -427,6 +421,12 @@ public class DefaultDbAdminController {
} }
} }
@GetMapping("/logs")
public String logs(Model model) {
model.addAttribute("logs", repo.findAll());
return "logs";
}
@GetMapping("/settings") @GetMapping("/settings")
public String settings(Model model) { public String settings(Model model) {
@ -434,5 +434,8 @@ public class DefaultDbAdminController {
return "settings"; return "settings";
} }
@Transactional("internalTransactionManager")
private UserAction saveAction(UserAction action) {
return repo.save(action);
}
} }

View File

@ -1,37 +0,0 @@
package tech.ailef.dbadmin.internal.model;
import jakarta.persistence.Entity;
import jakarta.persistence.GeneratedValue;
import jakarta.persistence.GenerationType;
import jakarta.persistence.Id;
@Entity
public class Action {
@Id
@GeneratedValue(strategy = GenerationType.IDENTITY)
private Integer id;
private String description;
public Integer getId() {
return id;
}
public void setId(Integer id) {
this.id = id;
}
public String getDescription() {
return description;
}
public void setDescription(String description) {
this.description = description;
}
@Override
public String toString() {
return "Action [id=" + id + ", description=" + description + "]";
}
}

View File

@ -0,0 +1,93 @@
package tech.ailef.dbadmin.internal.model;
import java.time.LocalDateTime;
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;
}
}

View File

@ -3,9 +3,9 @@ package tech.ailef.dbadmin.internal.repository;
import org.springframework.data.jpa.repository.JpaRepository; import org.springframework.data.jpa.repository.JpaRepository;
import org.springframework.stereotype.Repository; import org.springframework.stereotype.Repository;
import tech.ailef.dbadmin.internal.model.Action; import tech.ailef.dbadmin.internal.model.UserAction;
@Repository @Repository
public interface ActionRepository extends JpaRepository<Action, Integer> { public interface ActionRepository extends JpaRepository<UserAction, Integer> {
} }

View File

@ -0,0 +1,50 @@
<!DOCTYPE html>
<html xmlns="http://www.w3.org/1999/xhtml" xmlns:th="https://www.thymeleaf.org">
<head th:replace="~{fragments/resources::head}">
</head>
<body>
<div class="bg-light main-wrapper">
<nav th:replace="~{fragments/resources :: navbar}"></nav>
<div class="d-flex">
<div th:replace="~{fragments/resources :: sidebar('logs')}"></div>
<div class="main-content bg-lighter">
<th:block th:replace="~{fragments/resources :: alerts}"></th:block>
<h1 class="fw-bold mb-4"><i class="align-middle bi bi-gear"></i>
<span class="align-middle">Logs</span>
</h1>
<div class="row mt-4">
<div class="col">
<div class="w-100 d-flex inner-navigation">
<a href="#" class="active">
<div class="ui-tab ps-5 pe-5 p-3">
<i class="bi bi-database pe-2"></i> APPEARANCE
</div>
</a>
<a href="#">
<div class="ui-tab ps-5 pe-5 p-3">
<i class="bi bi-table pe-2"></i> DATA
</div>
</a>
<div class="inner-navigation-border flex-grow-1">
</div>
</div>
<div class="box with-navigation">
<div class="row" th:each="entry : ${logs}">
<div class="col-3" th:text="${entry.getCreatedAt()}">
</div>
<div class="col-3" th:text="${entry.getActionType()}">
</div>
<div class="col-3" th:text="${entry.getOnTable()}">
</div>
<div class="col-3" th:text="${entry.getPrimaryKey()}">
</div>
</div>
</div>
</div>
</div>
</div>
</div>
</div>
</body>
</html>