This repository was archived by the owner on Feb 7, 2025. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 4
[SVR-303] 무역상점 가격 수정 기능 추가 #161
Merged
Atralupus
merged 2 commits into
not-blond-beard:main
from
Atralupus:feat/update-product-action
Apr 14, 2024
Merged
Changes from all commits
Commits
Show all changes
2 commits
Select commit
Hold shift + click to select a range
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
88 changes: 88 additions & 0 deletions
88
backend/app/Savor22b.Tests/Action/UpdateTradeGoodActionTests.cs
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,88 @@ | ||
namespace Savor22b.Tests.Action; | ||
|
||
using System; | ||
using System.Collections.Immutable; | ||
using Libplanet; | ||
using Libplanet.Assets; | ||
using Libplanet.State; | ||
using Savor22b.Action; | ||
using Savor22b.States; | ||
using Savor22b.States.Trade; | ||
using Xunit; | ||
|
||
public class UpdateTradeGoodActionTests : ActionTests | ||
{ | ||
|
||
public UpdateTradeGoodActionTests() { } | ||
|
||
[Fact] | ||
public void RegisterTradeGoodActionExecute_Success() | ||
{ | ||
|
||
var (beforeState, foodProductId) = CreatePresetStateDelta(); | ||
var action = new UpdateTradeGoodAction( | ||
foodProductId, | ||
FungibleAssetValue.Parse( | ||
Currencies.KeyCurrency, | ||
"10000" | ||
) | ||
); | ||
|
||
var afterState = action.Execute( | ||
new DummyActionContext | ||
{ | ||
PreviousStates = beforeState, | ||
Signer = SignerAddress(), | ||
Random = random, | ||
Rehearsal = false, | ||
BlockIndex = 1, | ||
} | ||
); | ||
|
||
var afterTradeInventoryState = DeriveTradeInventoryStateDelta(afterState); | ||
var tradeGood = afterTradeInventoryState.TradeGoods.First(g => g.Value.SellerAddress == SignerAddress()).Value; | ||
|
||
if (tradeGood is FoodGoodState foodGoodState) | ||
{ | ||
Assert.Equal(foodGoodState.Price, FungibleAssetValue.Parse(Currencies.KeyCurrency, "10000")); | ||
} | ||
else | ||
{ | ||
throw new Exception(); | ||
} | ||
} | ||
|
||
private (IAccountStateDelta, Guid) CreatePresetStateDelta() | ||
{ | ||
IAccountStateDelta state = new DummyState(); | ||
Address signerAddress = SignerAddress(); | ||
|
||
TradeInventoryState tradeInventoryState = state.GetState(TradeInventoryState.StateAddress) is Bencodex.Types.Dictionary tradeInventoryStateEncoded | ||
? new TradeInventoryState(tradeInventoryStateEncoded) | ||
: new TradeInventoryState(); | ||
|
||
var foodProductId = Guid.NewGuid(); | ||
|
||
var food = RefrigeratorState.CreateFood( | ||
Guid.NewGuid(), | ||
1, | ||
"D", | ||
1, | ||
1, | ||
1, | ||
1, | ||
1, | ||
ImmutableList<Guid>.Empty | ||
); | ||
|
||
tradeInventoryState = tradeInventoryState.RegisterGood( | ||
new FoodGoodState( | ||
signerAddress, | ||
foodProductId, | ||
FungibleAssetValue.Parse(Currencies.KeyCurrency, "10"), | ||
food) | ||
); | ||
|
||
return (state.SetState(TradeInventoryState.StateAddress, tradeInventoryState.Serialize()), foodProductId); | ||
} | ||
} |
11 changes: 11 additions & 0 deletions
11
backend/app/Savor22b/Action/Exceptions/PermissionDeniedException.cs
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,11 @@ | ||
namespace Savor22b.Action.Exceptions; | ||
|
||
|
||
[Serializable] | ||
public class PermissionDeniedException : ActionException | ||
{ | ||
public PermissionDeniedException(string message, int? errorCode = null) | ||
: base(message, "PermissionDeniedException", errorCode) | ||
{ | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,69 @@ | ||
namespace Savor22b.Action; | ||
|
||
using System; | ||
using System.Collections.Immutable; | ||
using Bencodex.Types; | ||
using Libplanet.Action; | ||
using Libplanet.Assets; | ||
using Libplanet.Headless.Extensions; | ||
using Libplanet.State; | ||
using Savor22b.States; | ||
using Savor22b.States.Trade; | ||
using Savor22b.Action.Exceptions; | ||
|
||
[ActionType(nameof(UpdateTradeGoodAction))] | ||
public class UpdateTradeGoodAction : SVRAction | ||
{ | ||
public UpdateTradeGoodAction() { } | ||
|
||
public UpdateTradeGoodAction(Guid productId, FungibleAssetValue price) | ||
{ | ||
ProductId = productId; | ||
Price = price; | ||
} | ||
|
||
public FungibleAssetValue Price; | ||
|
||
public Guid ProductId; | ||
|
||
protected override IImmutableDictionary<string, IValue> PlainValueInternal => | ||
new Dictionary<string, IValue>() | ||
{ | ||
[nameof(ProductId)] = ProductId.Serialize(), | ||
[nameof(Price)] = Price.ToBencodex(), | ||
}.ToImmutableDictionary(); | ||
|
||
protected override void LoadPlainValueInternal(IImmutableDictionary<string, IValue> plainValue) | ||
{ | ||
ProductId = plainValue[nameof(ProductId)].ToGuid(); | ||
Price = plainValue[nameof(Price)].ToFungibleAssetValue(); | ||
} | ||
|
||
public override IAccountStateDelta Execute(IActionContext ctx) | ||
{ | ||
if (ctx.Rehearsal) | ||
{ | ||
return ctx.PreviousStates; | ||
} | ||
|
||
IAccountStateDelta states = ctx.PreviousStates; | ||
|
||
TradeInventoryState tradeInventoryState = states.GetState(TradeInventoryState.StateAddress) is Dictionary tradeInventoryStateEncoded | ||
? new TradeInventoryState(tradeInventoryStateEncoded) | ||
: new TradeInventoryState(); | ||
|
||
var good = tradeInventoryState.TradeGoods.First(g => g.Key == ProductId); | ||
|
||
if (good.Value.SellerAddress != ctx.Signer) | ||
{ | ||
throw new PermissionDeniedException("You not have permission"); | ||
} | ||
|
||
good.Value.UpdatePrice(Price); | ||
|
||
tradeInventoryState.TradeGoods.Remove(good.Key); | ||
tradeInventoryState.TradeGoods.Add(good.Key, good.Value); | ||
|
||
return states.SetState(TradeInventoryState.StateAddress, tradeInventoryState.Serialize()); | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
아 이거 너무 티나요
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
쉿 지금 아무도 몰라요