mirror of
https://github.com/Kingsrook/qqq.git
synced 2025-07-18 21:20:45 +00:00
Implemented Delete
This commit is contained in:
@ -0,0 +1,32 @@
|
||||
package com.kingsrook.qqq.backend.core.actions;
|
||||
|
||||
|
||||
import com.kingsrook.qqq.backend.core.exceptions.QException;
|
||||
import com.kingsrook.qqq.backend.core.model.actions.DeleteRequest;
|
||||
import com.kingsrook.qqq.backend.core.model.actions.DeleteResult;
|
||||
import com.kingsrook.qqq.backend.core.model.metadata.QBackendMetaData;
|
||||
import com.kingsrook.qqq.backend.core.modules.QModuleDispatcher;
|
||||
import com.kingsrook.qqq.backend.core.modules.interfaces.QModuleInterface;
|
||||
|
||||
|
||||
/*******************************************************************************
|
||||
**
|
||||
*******************************************************************************/
|
||||
public class DeleteAction
|
||||
{
|
||||
/*******************************************************************************
|
||||
**
|
||||
*******************************************************************************/
|
||||
public DeleteResult execute(DeleteRequest deleteRequest) throws QException
|
||||
{
|
||||
QModuleDispatcher qModuleDispatcher = new QModuleDispatcher();
|
||||
|
||||
QBackendMetaData backend = deleteRequest.getBackend();
|
||||
|
||||
QModuleInterface qModule = qModuleDispatcher.getQModule(backend);
|
||||
// todo pre-customization - just get to modify the request?
|
||||
DeleteResult deleteResult = qModule.getDeleteInterface().execute(deleteRequest);
|
||||
// todo post-customization - can do whatever w/ the result if you want
|
||||
return deleteResult;
|
||||
}
|
||||
}
|
@ -0,0 +1,68 @@
|
||||
package com.kingsrook.qqq.backend.core.model.actions;
|
||||
|
||||
|
||||
import java.io.Serializable;
|
||||
import java.util.List;
|
||||
import com.kingsrook.qqq.backend.core.model.metadata.QInstance;
|
||||
|
||||
|
||||
/*******************************************************************************
|
||||
**
|
||||
*******************************************************************************/
|
||||
public class DeleteRequest extends AbstractQTableRequest
|
||||
{
|
||||
private List<Serializable> primaryKeys;
|
||||
|
||||
|
||||
|
||||
/*******************************************************************************
|
||||
**
|
||||
*******************************************************************************/
|
||||
public DeleteRequest()
|
||||
{
|
||||
}
|
||||
|
||||
|
||||
|
||||
/*******************************************************************************
|
||||
**
|
||||
*******************************************************************************/
|
||||
public DeleteRequest(QInstance instance)
|
||||
{
|
||||
super(instance);
|
||||
}
|
||||
|
||||
|
||||
|
||||
/*******************************************************************************
|
||||
** Getter for ids
|
||||
**
|
||||
*******************************************************************************/
|
||||
public List<Serializable> getPrimaryKeys()
|
||||
{
|
||||
return primaryKeys;
|
||||
}
|
||||
|
||||
|
||||
|
||||
/*******************************************************************************
|
||||
** Setter for ids
|
||||
**
|
||||
*******************************************************************************/
|
||||
public void setPrimaryKeys(List<Serializable> primaryKeys)
|
||||
{
|
||||
this.primaryKeys = primaryKeys;
|
||||
}
|
||||
|
||||
|
||||
|
||||
/*******************************************************************************
|
||||
** Setter for ids
|
||||
**
|
||||
*******************************************************************************/
|
||||
public DeleteRequest withPrimaryKeys(List<Serializable> primaryKeys)
|
||||
{
|
||||
this.primaryKeys = primaryKeys;
|
||||
return (this);
|
||||
}
|
||||
}
|
@ -0,0 +1,35 @@
|
||||
package com.kingsrook.qqq.backend.core.model.actions;
|
||||
|
||||
|
||||
import java.util.List;
|
||||
import com.kingsrook.qqq.backend.core.model.data.QRecordWithStatus;
|
||||
|
||||
|
||||
/*******************************************************************************
|
||||
* Result for a delete action
|
||||
*
|
||||
*******************************************************************************/
|
||||
public class DeleteResult extends AbstractQResult
|
||||
{
|
||||
List<QRecordWithStatus> records;
|
||||
|
||||
|
||||
|
||||
/*******************************************************************************
|
||||
**
|
||||
*******************************************************************************/
|
||||
public List<QRecordWithStatus> getRecords()
|
||||
{
|
||||
return records;
|
||||
}
|
||||
|
||||
|
||||
|
||||
/*******************************************************************************
|
||||
**
|
||||
*******************************************************************************/
|
||||
public void setRecords(List<QRecordWithStatus> records)
|
||||
{
|
||||
this.records = records;
|
||||
}
|
||||
}
|
@ -0,0 +1,18 @@
|
||||
package com.kingsrook.qqq.backend.core.modules.interfaces;
|
||||
|
||||
|
||||
import com.kingsrook.qqq.backend.core.exceptions.QException;
|
||||
import com.kingsrook.qqq.backend.core.model.actions.DeleteRequest;
|
||||
import com.kingsrook.qqq.backend.core.model.actions.DeleteResult;
|
||||
|
||||
|
||||
/*******************************************************************************
|
||||
**
|
||||
*******************************************************************************/
|
||||
public interface DeleteInterface
|
||||
{
|
||||
/*******************************************************************************
|
||||
**
|
||||
*******************************************************************************/
|
||||
DeleteResult execute(DeleteRequest deleteRequest) throws QException;
|
||||
}
|
@ -1,9 +1,6 @@
|
||||
package com.kingsrook.qqq.backend.core.modules.interfaces;
|
||||
|
||||
|
||||
import com.kingsrook.qqq.backend.core.actions.InsertAction;
|
||||
|
||||
|
||||
/*******************************************************************************
|
||||
**
|
||||
*******************************************************************************/
|
||||
@ -12,10 +9,35 @@ public interface QModuleInterface
|
||||
/*******************************************************************************
|
||||
**
|
||||
*******************************************************************************/
|
||||
QueryInterface getQueryInterface();
|
||||
default QueryInterface getQueryInterface()
|
||||
{
|
||||
throwNotImplemented("Query");
|
||||
return null;
|
||||
}
|
||||
|
||||
/*******************************************************************************
|
||||
**
|
||||
*******************************************************************************/
|
||||
InsertInterface getInsertInterface();
|
||||
default InsertInterface getInsertInterface()
|
||||
{
|
||||
throwNotImplemented("Insert");
|
||||
return null;
|
||||
}
|
||||
|
||||
/*******************************************************************************
|
||||
**
|
||||
*******************************************************************************/
|
||||
default DeleteInterface getDeleteInterface()
|
||||
{
|
||||
throwNotImplemented("Delete");
|
||||
return null;
|
||||
}
|
||||
|
||||
/*******************************************************************************
|
||||
**
|
||||
*******************************************************************************/
|
||||
private void throwNotImplemented(String actionName)
|
||||
{
|
||||
throw new IllegalStateException(actionName + " is not implemented in this module: " + this.getClass().getSimpleName());
|
||||
}
|
||||
}
|
||||
|
@ -0,0 +1,41 @@
|
||||
package com.kingsrook.qqq.backend.core.modules.mock;
|
||||
|
||||
|
||||
import com.kingsrook.qqq.backend.core.exceptions.QException;
|
||||
import com.kingsrook.qqq.backend.core.model.actions.DeleteRequest;
|
||||
import com.kingsrook.qqq.backend.core.model.actions.DeleteResult;
|
||||
import com.kingsrook.qqq.backend.core.model.data.QRecord;
|
||||
import com.kingsrook.qqq.backend.core.model.data.QRecordWithStatus;
|
||||
import com.kingsrook.qqq.backend.core.modules.interfaces.DeleteInterface;
|
||||
|
||||
|
||||
/*******************************************************************************
|
||||
**
|
||||
*******************************************************************************/
|
||||
public class MockDeleteAction implements DeleteInterface
|
||||
{
|
||||
|
||||
/*******************************************************************************
|
||||
**
|
||||
*******************************************************************************/
|
||||
public DeleteResult execute(DeleteRequest deleteRequest) throws QException
|
||||
{
|
||||
try
|
||||
{
|
||||
DeleteResult rs = new DeleteResult();
|
||||
|
||||
rs.setRecords(deleteRequest.getPrimaryKeys().stream().map(primaryKey ->
|
||||
{
|
||||
QRecord qRecord = new QRecord().withTableName(deleteRequest.getTableName()).withPrimaryKey(primaryKey);
|
||||
return new QRecordWithStatus(qRecord);
|
||||
}).toList());
|
||||
|
||||
return rs;
|
||||
}
|
||||
catch(Exception e)
|
||||
{
|
||||
throw new QException("Error executing delete: " + e.getMessage(), e);
|
||||
}
|
||||
}
|
||||
|
||||
}
|
@ -0,0 +1,36 @@
|
||||
package com.kingsrook.qqq.backend.core.actions;
|
||||
|
||||
|
||||
import java.util.List;
|
||||
import com.kingsrook.qqq.backend.core.exceptions.QException;
|
||||
import com.kingsrook.qqq.backend.core.model.actions.DeleteRequest;
|
||||
import com.kingsrook.qqq.backend.core.model.actions.DeleteResult;
|
||||
import com.kingsrook.qqq.backend.core.utils.TestUtils;
|
||||
import org.junit.jupiter.api.Test;
|
||||
import static org.junit.jupiter.api.Assertions.assertTrue;
|
||||
import static org.junit.jupiter.api.Assertions.assertEquals;
|
||||
import static org.junit.jupiter.api.Assertions.assertNotNull;
|
||||
|
||||
|
||||
/*******************************************************************************
|
||||
**
|
||||
*******************************************************************************/
|
||||
class DeleteActionTest
|
||||
{
|
||||
|
||||
/*******************************************************************************
|
||||
**
|
||||
*******************************************************************************/
|
||||
@Test
|
||||
public void test() throws QException
|
||||
{
|
||||
DeleteRequest request = new DeleteRequest(TestUtils.defineInstance());
|
||||
request.setTableName("person");
|
||||
request.setPrimaryKeys(List.of(1, 2));
|
||||
DeleteResult result = new DeleteAction().execute(request);
|
||||
assertNotNull(result);
|
||||
assertEquals(2, result.getRecords().size());
|
||||
assertTrue(result.getRecords().stream().allMatch(r -> r.getErrors() == null));
|
||||
}
|
||||
|
||||
}
|
Reference in New Issue
Block a user