Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Restructure Module 5 #135

Merged
merged 15 commits into from
Nov 9, 2023
Merged
Show file tree
Hide file tree
Changes from 13 commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -103,7 +103,7 @@ public async Task<bool> UpdateTask(Guid taskId, string taskName, string assigned

private async Task PublishTaskSavedEvent(TaskModel taskModel)
{
_logger.LogInformation("Publish Task Saved event for task with Id: '{0}' and Name: '{1}' for Assigne: '{2}'",
_logger.LogInformation("Publish Task Saved event for task with Id: '{0}' and Name: '{1}' for Assignee: '{2}'",
taskModel.TaskId, taskModel.TaskName, taskModel.TaskAssignedTo);
await _daprClient.PublishEventAsync("dapr-pubsub-servicebus", "tasksavedtopic", taskModel);
}
Expand Down
73 changes: 8 additions & 65 deletions docs/aca/03-aca-dapr-integration/index.md
Original file line number Diff line number Diff line change
Expand Up @@ -68,33 +68,11 @@ You are now ready to run the applications locally using the Dapr sidecar in a se

=== ".NET 6 or below"

```shell
cd ~\TasksTracker.ContainerApps\TasksTracker.TasksManager.Backend.Api
```

```shell
dapr run `
--app-id tasksmanager-backend-api `
--app-port $API_APP_PORT `
--dapr-http-port 3500 `
--app-ssl `
-- dotnet run
```
--8<-- "snippets/dapr-run-backend-api.md:basic-dotnet6"

=== ".NET 7 or above"

```shell
cd ~\TasksTracker.ContainerApps\TasksTracker.TasksManager.Backend.Api
```

```shell
dapr run `
--app-id tasksmanager-backend-api `
--app-port $API_APP_PORT `
--dapr-http-port 3500 `
--app-ssl `
-- dotnet run --launch-profile https
```
--8<-- "snippets/dapr-run-backend-api.md:basic"

![app-port](../../assets/images/03-aca-dapr-integration/self_hosted_dapr_app-port.png)

Expand Down Expand Up @@ -247,50 +225,15 @@ We are ready now to verify the changes on the Frontend Web App and test locally.

=== ".NET 6 or below"

```shell
cd ~\TasksTracker.ContainerApps\TasksTracker.WebPortal.Frontend.Ui

dapr run `
--app-id tasksmanager-frontend-webapp `
--app-port $UI_APP_PORT `
--dapr-http-port 3501 `
--app-ssl `
-- dotnet run
```

```shell
cd ~\TasksTracker.ContainerApps\TasksTracker.TasksManager.Backend.Api
dapr run `
--app-id tasksmanager-backend-api `
--app-port $API_APP_PORT `
--dapr-http-port 3500 `
--app-ssl `
-- dotnet run
```
--8<-- "snippets/dapr-run-frontend-webapp.md:basic-dotnet6"

--8<-- "snippets/dapr-run-backend-api.md:basic-dotnet6"

=== ".NET 7 or above"

```shell
cd ~\TasksTracker.ContainerApps\TasksTracker.WebPortal.Frontend.Ui

dapr run `
--app-id tasksmanager-frontend-webapp `
--app-port $UI_APP_PORT `
--dapr-http-port 3501 `
--app-ssl `
-- dotnet run --launch-profile https
```

```shell
cd ~\TasksTracker.ContainerApps\TasksTracker.TasksManager.Backend.Api

dapr run `
--app-id tasksmanager-backend-api `
--app-port $API_APP_PORT `
--dapr-http-port 3500 `
--app-ssl `
-- dotnet run --launch-profile https
```
--8<-- "snippets/dapr-run-frontend-webapp.md:basic"

--8<-- "snippets/dapr-run-backend-api.md:basic"

Notice how we assigned the Dapr App Id “tasksmanager-frontend-webapp” to the Frontend WebApp.

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@ componentType: state.azure.cosmosdb
version: v1
metadata:
- name: url
value: <https://cosmos-tasks-tracker-state-store-<UNIQUE-STRING>.documents.azure.com:443/>
value: <https://cosmos-tasks-tracker-state-store-<$RANDOM_STRING>.documents.azure.com:443/>
- name: database
value: tasksmanagerdb
- name: collection
Expand Down
2 changes: 1 addition & 1 deletion docs/aca/04-aca-dapr-stateapi/dapr-statestore-cosmos.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@ spec:
version: v1
metadata:
- name: url
value: <https://cosmos-tasks-tracker-state-store-<UNIQUE-STRING>.documents.azure.com:443/>
value: <https://cosmos-tasks-tracker-state-store-<$RANDOM_STRING>.documents.azure.com:443/>
- name: masterKey
value: "<value>"
- name: database
Expand Down
44 changes: 5 additions & 39 deletions docs/aca/04-aca-dapr-stateapi/index.md
Original file line number Diff line number Diff line change
Expand Up @@ -34,30 +34,11 @@ To try out the State Management APIs, run the Backend API from VS Code by runnin

=== ".NET 6 or below"

```shell
cd ~\TasksTracker.ContainerApps\TasksTracker.TasksManager.Backend.Api

dapr run `
--app-id tasksmanager-backend-api `
--app-port $API_APP_PORT `
--dapr-http-port 3500 `
--app-ssl `
-- dotnet run
```
--8<-- "snippets/dapr-run-backend-api.md:basic-dotnet6"

=== ".NET 7 or above"

```shell

cd ~\TasksTracker.ContainerApps\TasksTracker.TasksManager.Backend.Api

dapr run `
--app-id tasksmanager-backend-api `
--app-port $API_APP_PORT `
--dapr-http-port 3500 `
--app-ssl `
-- dotnet run --launch-profile https
```
--8<-- "snippets/dapr-run-backend-api.md:basic"

Now from any rest client, invoke the below **POST** request to the endpoint: [http://localhost:3500/v1.0/state/statestore](http://localhost:3500/v1.0/state/statestore){target=_blank}

Expand Down Expand Up @@ -319,26 +300,11 @@ If you have been using the dapr cli commands instead of the aforementioned debug

=== ".NET 6 or below"

```shell
dapr run `
--app-id tasksmanager-backend-api `
--app-port $API_APP_PORT `
--dapr-http-port 3500 `
--app-ssl `
--resources-path "../components" `
dotnet run
```
--8<-- "snippets/dapr-run-backend-api.md:dapr-components-dotnet6"

=== ".NET 7 or above"

```shell
dapr run `
--app-id tasksmanager-backend-api `
--app-port $API_APP_PORT `
--dapr-http-port 3500 `
--app-ssl `
--resources-path "../components" `
-- dotnet run --launch-profile https
```
--8<-- "snippets/dapr-run-backend-api.md:dapr-components"

!!! note "Deprecation Warning"
components-path is being deprecated in favor of --resources-path. At the time of producing this workshop the --resources-path was not supported yet by the VS code extension. Hence, you will notice the use of the property "componentsPath": "./components" in the tasks.json file. Check the extension documentation in case that has changed.
Expand Down
15 changes: 15 additions & 0 deletions docs/aca/05-aca-dapr-pubsubapi/Backend.Svc.csproj
Original file line number Diff line number Diff line change
@@ -0,0 +1,15 @@
<Project Sdk="Microsoft.NET.Sdk.Web">

<PropertyGroup>
<TargetFramework>net7.0</TargetFramework>
<Nullable>enable</Nullable>
<ImplicitUsings>enable</ImplicitUsings>
</PropertyGroup>

<ItemGroup>
<PackageReference Include="Dapr.AspNetCore" Version="1.9.0" />
<PackageReference Include="Microsoft.AspNetCore.OpenApi" Version="7.0.13" />
<PackageReference Include="Swashbuckle.AspNetCore" Version="6.5.0" />
</ItemGroup>

</Project>
19 changes: 19 additions & 0 deletions docs/aca/05-aca-dapr-pubsubapi/Program-dotnet6.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,19 @@
namespace TasksTracker.Processor.Backend.Svc
{
public class Program
{
public static void Main(string[] args)
{
var builder = WebApplication.CreateBuilder(args);
// Add services to the container.
builder.Services.AddControllers().AddDapr();
var app = builder.Build();
app.UseHttpsRedirection();
app.UseAuthorization();
app.UseCloudEvents();
app.MapControllers();
app.MapSubscribeHandler();
app.Run();
}
}
}
48 changes: 29 additions & 19 deletions docs/aca/05-aca-dapr-pubsubapi/Program.cs
Original file line number Diff line number Diff line change
@@ -1,19 +1,29 @@
namespace TasksTracker.Processor.Backend.Svc
{
public class Program
{
public static void Main(string[] args)
{
var builder = WebApplication.CreateBuilder(args);
// Add services to the container.
builder.Services.AddControllers().AddDapr();
var app = builder.Build();
app.UseHttpsRedirection();
app.UseAuthorization();
app.UseCloudEvents();
app.MapControllers();
app.MapSubscribeHandler();
app.Run();
}
}
}
var builder = WebApplication.CreateBuilder(args);

// Add services to the container.

builder.Services.AddControllers().AddDapr();
// Learn more about configuring Swagger/OpenAPI at https://aka.ms/aspnetcore/swashbuckle
builder.Services.AddEndpointsApiExplorer();
builder.Services.AddSwaggerGen();

var app = builder.Build();

// Configure the HTTP request pipeline.
if (app.Environment.IsDevelopment())
{
app.UseSwagger();
app.UseSwaggerUI();
}

app.UseHttpsRedirection();

app.UseAuthorization();

app.UseCloudEvents();

app.MapControllers();

app.MapSubscribeHandler();

app.Run();
4 changes: 2 additions & 2 deletions docs/aca/05-aca-dapr-pubsubapi/TasksNotifierController.cs
Original file line number Diff line number Diff line change
Expand Up @@ -20,8 +20,8 @@ public TasksNotifierController(IConfiguration config, ILogger<TasksNotifierContr
_daprClient = daprClient;
}

//[Topic("dapr-pubsub-servicebus", "tasksavedtopic")] //Dapr Pub Sub Service Bus
[Topic("taskspubsub", "tasksavedtopic")] //Redis
[Topic("dapr-pubsub-servicebus", "tasksavedtopic")] //Dapr Pub Sub Service Bus
[Topic("taskspubsub", "tasksavedtopic")] //Redis
[HttpPost("tasksaved")]
public IActionResult TaskSaved([FromBody] TaskModel taskModel)
{
Expand Down
104 changes: 104 additions & 0 deletions docs/aca/05-aca-dapr-pubsubapi/TasksStoreManager.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,104 @@
using Dapr.Client;
using TasksTracker.TasksManager.Backend.Api.Models;

namespace TasksTracker.TasksManager.Backend.Api.Services
{
public class TasksStoreManager : ITasksManager
{
private static string STORE_NAME = "statestore";
private readonly DaprClient _daprClient;
private readonly IConfiguration _config;
private readonly ILogger<TasksStoreManager> _logger;

public TasksStoreManager(DaprClient daprClient, IConfiguration config, ILogger<TasksStoreManager> logger)
{
_daprClient = daprClient;
_config = config;
_logger = logger;
}
public async Task<Guid> CreateNewTask(string taskName, string createdBy, string assignedTo, DateTime dueDate)
{
var taskModel = new TaskModel()
{
TaskId = Guid.NewGuid(),
TaskName = taskName,
TaskCreatedBy = createdBy,
TaskCreatedOn = DateTime.UtcNow,
TaskDueDate = dueDate,
TaskAssignedTo = assignedTo,
};

_logger.LogInformation("Save a new task with name: '{0}' to state store", taskModel.TaskName);
await _daprClient.SaveStateAsync<TaskModel>(STORE_NAME, taskModel.TaskId.ToString(), taskModel);
await PublishTaskSavedEvent(taskModel);
return taskModel.TaskId;
}

public async Task<bool> DeleteTask(Guid taskId)
{
_logger.LogInformation("Delete task with Id: '{0}'", taskId);
await _daprClient.DeleteStateAsync(STORE_NAME, taskId.ToString());
return true;
}

public async Task<TaskModel?> GetTaskById(Guid taskId)
{
_logger.LogInformation("Getting task with Id: '{0}'", taskId);
var taskModel = await _daprClient.GetStateAsync<TaskModel>(STORE_NAME, taskId.ToString());
return taskModel;
}

public async Task<List<TaskModel>> GetTasksByCreator(string createdBy)
{
var query = "{" +
"\"filter\": {" +
"\"EQ\": { \"taskCreatedBy\": \"" + createdBy + "\" }" +
"}}";

var queryResponse = await _daprClient.QueryStateAsync<TaskModel>(STORE_NAME, query);

var tasksList = queryResponse.Results.Select(q => q.Data).OrderByDescending(o=>o.TaskCreatedOn);
return tasksList.ToList();
}

public async Task<bool> MarkTaskCompleted(Guid taskId)
{
_logger.LogInformation("Mark task with Id: '{0}' as completed", taskId);
var taskModel = await _daprClient.GetStateAsync<TaskModel>(STORE_NAME, taskId.ToString());
if (taskModel != null)
{
taskModel.IsCompleted = true;
await _daprClient.SaveStateAsync<TaskModel>(STORE_NAME, taskModel.TaskId.ToString(), taskModel);
return true;
}
return false;
}

public async Task<bool> UpdateTask(Guid taskId, string taskName, string assignedTo, DateTime dueDate)
{
_logger.LogInformation("Update task with Id: '{0}'", taskId);
var taskModel = await _daprClient.GetStateAsync<TaskModel>(STORE_NAME, taskId.ToString());
var currentAssignee = taskModel.TaskAssignedTo;
if (taskModel != null)
{
taskModel.TaskName = taskName;
taskModel.TaskAssignedTo = assignedTo;
taskModel.TaskDueDate = dueDate;
await _daprClient.SaveStateAsync<TaskModel>(STORE_NAME, taskModel.TaskId.ToString(), taskModel);
if (!taskModel.TaskAssignedTo.Equals(currentAssignee, StringComparison.OrdinalIgnoreCase))
{
await PublishTaskSavedEvent(taskModel);
}
return true;
}
return false;
}

private async Task PublishTaskSavedEvent(TaskModel taskModel)
{
_logger.LogInformation("Publish Task Saved event for task with Id: '{0}' and Name: '{1}' for Assignee: '{2}'",
taskModel.TaskId, taskModel.TaskName, taskModel.TaskAssignedTo);
await _daprClient.PublishEventAsync("dapr-pubsub-servicebus", "tasksavedtopic", taskModel);
}
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@ componentType: pubsub.azure.servicebus
version: v1
metadata:
- name: namespaceName
value: "sbns-taskstracker-<UNIQUE-STRING>.servicebus.windows.net"
value: "sbns-taskstracker-<$RANDOM_STRING>.servicebus.windows.net"
- name: consumerID
value: "sbts-tasks-processor"
# Application scopes
Expand Down
Loading