// ------------------------------------------------------------------------ // Copyright 2021 The Dapr Authors // Licensed under the Apache License, Version 2.0 (the "License"); // you may not use this file except in compliance with the License. // You may obtain a copy of the License at // http://www.apache.org/licenses/LICENSE-2.0 // Unless required by applicable law or agreed to in writing, software // distributed under the License is distributed on an "AS IS" BASIS, // WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. // See the License for the specific language governing permissions and // limitations under the License. // ------------------------------------------------------------------------ namespace ControllerSample.Controllers { using System; using System.Threading.Tasks; using Dapr; using Dapr.Client; using Microsoft.AspNetCore.Mvc; using Microsoft.Extensions.Logging; /// /// Sample showing Dapr integration with controller. /// [ApiController] public class SampleController : ControllerBase { /// /// SampleController Constructor with logger injection /// /// public SampleController(ILogger logger) { this.logger = logger; } /// /// State store name. /// public const string StoreName = "statestore"; private readonly ILogger logger; /// /// Gets the account information as specified by the id. /// /// Account information for the id from Dapr state store. /// Account information. [HttpGet("{account}")] public ActionResult Get([FromState(StoreName)] StateEntry account) { if (account.Value is null) { return this.NotFound(); } return account.Value; } /// /// Method for depositing to account as specified in transaction. /// /// Transaction info. /// State client to interact with Dapr runtime. /// A representing the result of the asynchronous operation. /// "pubsub", the first parameter into the Topic attribute, is name of the default pub/sub configured by the Dapr CLI. [Topic("pubsub", "deposit")] [HttpPost("deposit")] public async Task> Deposit(Transaction transaction, [FromServices] DaprClient daprClient) { logger.LogDebug("Enter deposit"); var state = await daprClient.GetStateEntryAsync(StoreName, transaction.Id); state.Value ??= new Account() { Id = transaction.Id, }; state.Value.Balance += transaction.Amount; await state.SaveAsync(); return state.Value; } /// /// Method for withdrawing from account as specified in transaction. /// /// Transaction info. /// State client to interact with Dapr runtime. /// A representing the result of the asynchronous operation. /// "pubsub", the first parameter into the Topic attribute, is name of the default pub/sub configured by the Dapr CLI. [Topic("pubsub", "withdraw")] [HttpPost("withdraw")] public async Task> Withdraw(Transaction transaction, [FromServices] DaprClient daprClient) { logger.LogDebug("Enter withdraw"); var state = await daprClient.GetStateEntryAsync(StoreName, transaction.Id); if (state.Value == null) { return this.NotFound(); } state.Value.Balance -= transaction.Amount; await state.SaveAsync(); return state.Value; } /// /// Method for withdrawing from account as specified in transaction. /// /// Transaction info. /// State client to interact with Dapr runtime. /// A representing the result of the asynchronous operation. /// "pubsub", the first parameter into the Topic attribute, is name of the default pub/sub configured by the Dapr CLI. [Topic("pubsub", "withdraw", "event.type ==\"withdraw.v2\"", 1)] [HttpPost("withdraw.v2")] public async Task> WithdrawV2(TransactionV2 transaction, [FromServices] DaprClient daprClient) { logger.LogDebug("Enter withdraw.v2"); if (transaction.Channel == "mobile" && transaction.Amount > 10000) { return this.Unauthorized("mobile transactions for large amounts are not permitted."); } var state = await daprClient.GetStateEntryAsync(StoreName, transaction.Id); if (state.Value == null) { return this.NotFound(); } state.Value.Balance -= transaction.Amount; await state.SaveAsync(); return state.Value; } /// /// Method for returning a BadRequest result which will cause Dapr sidecar to throw an RpcException [HttpPost("throwException")] public async Task> ThrowException(Transaction transaction, [FromServices] DaprClient daprClient) { Console.WriteLine("Enter ThrowException"); var task = Task.Delay(10); await task; return BadRequest(new { statusCode = 400, message = "bad request" }); } /// /// /// Method which uses for binding this endpoint to a subscription. /// /// /// This endpoint will be bound to a subscription where the topic name is the value of the environment variable 'CUSTOM_TOPIC' /// and the pubsub name is the value of the environment variable 'CUSTOM_PUBSUB'. /// /// [CustomTopic("%CUSTOM_PUBSUB%", "%CUSTOM_TOPIC%")] [HttpPost("exampleCustomTopic")] public ActionResult ExampleCustomTopic(Transaction transaction) { return Ok(); } } }