diff --git a/contracts/ApplicationManager.sol b/contracts/ApplicationManager.sol index 150273f..a918eff 100644 --- a/contracts/ApplicationManager.sol +++ b/contracts/ApplicationManager.sol @@ -25,32 +25,39 @@ contract ApplicationManager is } function createApplication( - Application memory newApplication + ApplicationDto memory dto ) external nonReentrant restricted { require( - !addressUsed[newApplication.account], + !addressUsed[dto.account], "Address already used for another application" ); - applications[nextApplicationId] = newApplication; - addressUsed[newApplication.account] = true; - emit ApplicationCreated( - nextApplicationId, - applications[nextApplicationId] - ); + uint id = nextApplicationId; nextApplicationId++; + Application memory newApplication = Application({ + id: id, + name: dto.name, + account: dto.account + }); + applications[id] = newApplication; + addressUsed[newApplication.account] = true; + emit ApplicationCreated(id, applications[id]); } function updateApplication( uint id, - Application memory updatedApplication + ApplicationDto memory dto ) external nonReentrant restricted { require(applicationExists(id), "Application does not exist"); require( - !addressUsed[updatedApplication.account] || - applications[id].account == updatedApplication.account, + !addressUsed[dto.account] || + applications[id].account == dto.account, "Account used by another application" ); - applications[id] = updatedApplication; + applications[id] = Application({ + id: id, + name: dto.name, + account: dto.account + }); emit ApplicationUpdated(id, applications[id]); } diff --git a/contracts/IApplicationManager.sol b/contracts/IApplicationManager.sol index ea4643e..8298a65 100644 --- a/contracts/IApplicationManager.sol +++ b/contracts/IApplicationManager.sol @@ -2,7 +2,13 @@ pragma solidity 0.8.26; interface IApplicationManager { + struct ApplicationDto { + string name; + address account; + } + struct Application { + uint id; string name; address account; } @@ -12,9 +18,17 @@ interface IApplicationManager { event ApplicationDeleted(uint id, Application application); function getNextApplicationId() external view returns (uint); - function createApplication(Application memory application) external; - function updateApplication(uint id, Application memory application) external; + + function createApplication(ApplicationDto memory dto) external; + + function updateApplication(uint id, ApplicationDto memory dto) external; + function deleteApplication(uint id) external; + function getApplication(uint id) external view returns (Application memory); - function getApplications(uint start, uint limit) external returns (Application[] memory); + + function getApplications( + uint start, + uint limit + ) external returns (Application[] memory); } diff --git a/test/ApplicationManager.ts b/test/ApplicationManager.ts index 36c353d..5ef27b3 100644 --- a/test/ApplicationManager.ts +++ b/test/ApplicationManager.ts @@ -11,6 +11,7 @@ import { import { generatePrivateKey, privateKeyToAccount } from "viem/accounts"; interface Application { + id?: bigint; name: string; account: Address; } @@ -139,7 +140,7 @@ describe("ApplicationManager", () => { }); expect( await contract.read.getApplication([applicationId]), - ).to.contain(app); + ).to.contain({ ...app, id: applicationId }); }); it("Should emit the ApplicationCreated event with the new application's details", async () => { const applicationId = await contract.read.getNextApplicationId(); @@ -191,6 +192,7 @@ describe("ApplicationManager", () => { expect( await contract.read.getApplication([applicationId]), ).to.contain({ + id: applicationId, account: app.account, name: newName, }); @@ -345,7 +347,7 @@ describe("ApplicationManager", () => { account: generateRandomAddress(), }; await contract.write.createApplication([app]); - apps.push(app); + apps.push({ ...app, id: parseUnits(`${i}`, 0) }); } });