Skip to content

Commit

Permalink
Redaction example usage
Browse files Browse the repository at this point in the history
  • Loading branch information
mhelleborg committed Oct 23, 2024
1 parent 2385e8d commit 1166fd9
Show file tree
Hide file tree
Showing 3 changed files with 104 additions and 0 deletions.
58 changes: 58 additions & 0 deletions Samples/ASP.NET/Customers/Customer.cs
Original file line number Diff line number Diff line change
@@ -1,16 +1,24 @@
// Copyright (c) Dolittle. All rights reserved.
// Licensed under the MIT license. See LICENSE file in the project root for full license information.

using System;
using Dolittle.SDK.Aggregates;
using Dolittle.SDK.Events;
using Dolittle.SDK.Events.Redaction;
using Microsoft.Extensions.Logging;

namespace Customers;

[AggregateRoot("dcdaecc0-29c9-41f4-96d1-9bddefe8b39a")]
public class Customer : AggregateRoot
{
readonly ILogger<Customer> _logger;

string? _name;
string? _email;
string? _phoneNumber;
Address? _customerAddress;

public Customer(EventSourceId eventSource, ILogger<Customer> logger)
: base(eventSource)
{
Expand All @@ -19,9 +27,59 @@ public Customer(EventSourceId eventSource, ILogger<Customer> logger)

string Name => EventSourceId;

public void Register(string name, string email, string phoneNumber, Address address)
{
if (_name is not null)
{
throw new CustomerAlreadyRegistered();
}

Apply(new CustomerRegistered
{
Name = name,
Email = email,
PhoneNumber = phoneNumber,
CustomerAddress = address
});
}

public void GdprForget(string reason, string performedBy)
{
if (_name is null)
{
throw new CustomerNotRegistered();
}


Apply(Redactions.Create<CustomerRegistered, CustomerDetailsForgotten>(reason, performedBy));
}

public void EatDish(string dish)
{
Apply(new DishEaten(dish, Name));
_logger.LogInformation("Customer {Name} is eating {Dish}", Name, dish);
}

public void On(CustomerRegistered evt)
{
_name = evt.Name;
_email = evt.Email;
_phoneNumber = evt.PhoneNumber;
_customerAddress = evt.CustomerAddress;
}

public void On(CustomerDetailsForgotten _)
{
_email = null;
_phoneNumber = null;
_customerAddress = null;
}
}

public class CustomerNotRegistered : Exception
{
}

public class CustomerAlreadyRegistered : Exception
{
}
13 changes: 13 additions & 0 deletions Samples/ASP.NET/Customers/CustomerDetailsForgotten.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,13 @@
// Copyright (c) Dolittle. All rights reserved.
// Licensed under the MIT license. See LICENSE file in the project root for full license information.

using Dolittle.SDK.Events;
using Dolittle.SDK.Events.Redaction;

namespace Customers;

[EventType("849494db-3173-4259-a4f7-e409ae1785dc")]
public class CustomerDetailsForgotten : PersonalDataRedactedForEvent<CustomerRegistered>
{

}
33 changes: 33 additions & 0 deletions Samples/ASP.NET/Customers/CustomerRegistered.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,33 @@
// Copyright (c) Dolittle. All rights reserved.
// Licensed under the MIT license. See LICENSE file in the project root for full license information.

#nullable enable
using Dolittle.SDK.Events;
using Dolittle.SDK.Events.Redaction;

namespace Customers;

[EventType("24e3a119-57d5-45d7-b7ef-a736fe6331e7")]
public class CustomerRegistered
{
public string Name { get; init; }

// These will be replaced with the given value
[RedactablePersonalData<string>("<email redacted>")]
public string Email { get; init; }

[RedactablePersonalData<string>("<phone redacted>")]
public string PhoneNumber { get; init; }

// If the type is not specified, the property will be removed.
[RedactablePersonalData]
public Address? CustomerAddress { get; init; }

}

public class Address
{
public string Street { get; init; }
public string City { get; init; }
public string ZipCode { get; init; }
}

0 comments on commit 1166fd9

Please sign in to comment.