Skip to content

Commit

Permalink
Replace Device model with MyThing model
Browse files Browse the repository at this point in the history
The commit represents a significant refactoring in favor of an abstracted 'MyThing' model replacing the previous 'Device' model. All instances and usages of 'Device' have been removed and replaced with 'MyThing', going as far as to change endpoint routes and update tests to match the changes. Events and handlers have also been updated to match MyThing's functionality.
  • Loading branch information
Al4ric committed Dec 29, 2023
1 parent 8eed841 commit bc7f3ea
Show file tree
Hide file tree
Showing 5 changed files with 130 additions and 78 deletions.
55 changes: 51 additions & 4 deletions SimpleWebApi8.Tests/WebApiTests.cs
Original file line number Diff line number Diff line change
Expand Up @@ -12,19 +12,66 @@ public WebAppTest(CustomWebAppFactory<Program> factory)
}

[Fact]
public async Task TestCreateDeviceEndpoint()
public async Task TestCreateMyThingEndpoint()
{
// Arrange
var httpRequest = new HttpRequestMessage(HttpMethod.Get, "/createDevice");
var httpRequest = new HttpRequestMessage(HttpMethod.Get, "/createMyThing");

// Act
var httpResponse = await _client.SendAsync(httpRequest);

// Assert
httpResponse.EnsureSuccessStatusCode(); // fails the test if the status code is not a success status code (2xx)
var responseContent = await httpResponse.Content.ReadAsStringAsync();
var device = JsonConvert.DeserializeObject<Device>(responseContent);
var myThing = JsonConvert.DeserializeObject<MyThing>(responseContent);

Assert.Equal("My Device", device?.Name);
Assert.Equal("My Thing", myThing?.Name);
}

[Fact]
public async Task TestCreateAndRetrieveMyThingEndpoint()
{
// Arrange
var createRequest = new HttpRequestMessage(HttpMethod.Get, $"/createMyThing");

// Act
var createResponse = await _client.SendAsync(createRequest);

// Assert
createResponse.EnsureSuccessStatusCode(); // fails the test if the status code is not a success status code (2xx)
var createResponseContent = await createResponse.Content.ReadAsStringAsync();
var createdMyThing = JsonConvert.DeserializeObject<MyThing>(createResponseContent);

var myThingId = createdMyThing?.Id ?? Guid.Empty;
var myThing = new MyThing("My Thing", myThingId)
{
Description = "This is my thing",
Tags = ["tag1", "tag2"],
Picture = [], // replace with your picture bytes
Place = "My place"
};

Assert.Equal(myThing.Name, createdMyThing?.Name);
Assert.Equal(myThing.Description, createdMyThing?.Description);
Assert.Equal(myThing.Tags, createdMyThing?.Tags);
Assert.Equal(myThing.Picture, createdMyThing?.Picture);
Assert.Equal(myThing.Place, createdMyThing?.Place);

// Arrange
var retrieveRequest = new HttpRequestMessage(HttpMethod.Get, $"/myThing/{myThingId}");

// Act
var retrieveResponse = await _client.SendAsync(retrieveRequest);

// Assert
retrieveResponse.EnsureSuccessStatusCode(); // fails the test if the status code is not a success status code (2xx)
var retrieveResponseContent = await retrieveResponse.Content.ReadAsStringAsync();
var retrievedMyThing = JsonConvert.DeserializeObject<MyThing>(retrieveResponseContent);

Assert.Equal(myThing.Name, retrievedMyThing?.Name);
Assert.Equal(myThing.Description, retrievedMyThing?.Description);
Assert.Equal(myThing.Tags, retrievedMyThing?.Tags);
Assert.Equal(myThing.Picture, retrievedMyThing?.Picture);
Assert.Equal(myThing.Place, retrievedMyThing?.Place);
}
}
31 changes: 0 additions & 31 deletions SimpleWebApi8/DeviceHandlers.cs

This file was deleted.

84 changes: 44 additions & 40 deletions SimpleWebApi8/EventSourcing.cs
Original file line number Diff line number Diff line change
Expand Up @@ -2,76 +2,80 @@

namespace SimpleWebApi8;

public class VolumeChanged
public abstract class NameChanged(string name)
{
public int Volume { get; init; }
public string Name { get; set; } = name;

public override string ToString()
{
return $"New volume: {Volume} ";
return $"New name: {Name} ";
}
}

public abstract class MuteToggled(bool muted)
public class DescriptionChanged
{
public bool Muted { get; set; } = muted;

public override string ToString()
{
return $"Muted: {Muted} ";
}
public required string Description { get; set; }
}

public abstract class NameChanged(string name)
public class TagsChanged
{
public string Name { get; set; } = name;
public required string[] Tags { get; set; }
}

public override string ToString()
{
return $"New name: {Name} ";
}
public class PictureChanged
{
public required byte[] Picture { get; set; }
}

public class DeviceInitialized(string name)
public class PlaceChanged
{
public string Name { get; init; } = name;
public int Volume { get; init; }
public bool Muted { get; init; }
public required string Place { get; set; }
}

public override string ToString()
{
return $"Initialized with name: {Name}, volume: {Volume}, muted: {Muted} ";
}
public class MyThingInitialized
{
public required string Name { get; set; }
public required string Description { get; set; }
public required string[] Tags { get; set; }
public required byte[] Picture { get; set; }
public required string Place { get; set; }
}

public class Device(string name, Guid id)
public class MyThing(string name, Guid id)
{
public Guid Id { get; set; } = id;
public string Name { get; private set; } = name;
public int Volume { get; set; }
public bool Muted { get; set; }
public required string Description { get; set; }
public required string[] Tags { get; set; }
public required byte[] Picture { get; set; }
public required string Place { get; set; }

public void Apply(VolumeChanged newVolume) => Volume = newVolume.Volume;
public void Apply(MuteToggled muteToggled) => Muted = muteToggled.Muted;
public void Apply(NameChanged newName) => Name = newName.Name;
public void Apply(DeviceInitialized deviceInitialized)
public void Apply(DescriptionChanged newDescription) => Description = newDescription.Description;
public void Apply(TagsChanged newTags) => Tags = newTags.Tags;
public void Apply(PictureChanged newPicture) => Picture = newPicture.Picture;
public void Apply(PlaceChanged newPlace) => Place = newPlace.Place;
public void Apply(MyThingInitialized myThingInitialized)
{
Name = deviceInitialized.Name;
Volume = deviceInitialized.Volume;
Muted = deviceInitialized.Muted;
Name = myThingInitialized.Name;
Description = myThingInitialized.Description;
Tags = myThingInitialized.Tags;
Picture = myThingInitialized.Picture;
Place = myThingInitialized.Place;
}

public override string ToString()
{
return $"Device {Id} with name: {Name}, volume: {Volume}, muted: {Muted} ";
return $"MyThing {Id} with name: {Name}, description: {Description}, tags: {string.Join(", ", Tags)}, place: {Place}";
}
public static Device Create(IEvent<DeviceInitialized> @event)

public static MyThing Create(IEvent<MyThingInitialized> @event)
{
var newDevice = new Device(@event.Data.Name, @event.StreamId)
var newDevice = new MyThing(@event.Data.Name, @event.StreamId)
{
Muted = @event.Data.Muted,
Volume = @event.Data.Volume
Description = @event.Data.Description,
Tags = @event.Data.Tags,
Picture = @event.Data.Picture,
Place = @event.Data.Place
};

return newDevice;
Expand Down
6 changes: 3 additions & 3 deletions SimpleWebApi8/Program.cs
Original file line number Diff line number Diff line change
Expand Up @@ -23,8 +23,8 @@

app.MapGet("/", () => "Hello World!");
app.MapGet("/random", () => new Random().Next(1, 100).ToString());
app.MapGet("createDevice", DeviceHandlers.CreateDevice);
app.MapGet("device/{deviceId:guid}", DeviceHandlers.GetDeviceById);
app.MapGet("createMyThing", ThingHandlers.CreateMyThing);
app.MapGet("myThing/{myThingId:guid}", ThingHandlers.GetMyThingById);
app.Run();
return;

Expand All @@ -33,7 +33,7 @@ DocumentStore GetDeviceStore(IServiceProvider serviceProvider)
var documentStore = DocumentStore.For(options =>
{
if (connectionString != null) options.Connection(connectionString);
options.Projections.Snapshot<Device>(SnapshotLifecycle.Inline);
options.Projections.Snapshot<MyThing>(SnapshotLifecycle.Inline);
});
return documentStore;
}
Expand Down
32 changes: 32 additions & 0 deletions SimpleWebApi8/ThingHandlers.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,32 @@
using Marten;

namespace SimpleWebApi8;

public static class ThingHandlers
{
public static async Task<MyThing?> CreateMyThing(DocumentStore store)
{
var thingId = Guid.NewGuid();

await using var session = store.LightweightSession();
var myThingInitialized = new MyThingInitialized
{
Name = "My Thing",
Description = "This is my thing",
Tags = ["tag1", "tag2"],
Picture = [], // replace with your picture bytes
Place = "My place"
};

session.Events.StartStream<MyThing>(thingId, myThingInitialized);
await session.SaveChangesAsync();

return session.Events.AggregateStream<MyThing>(thingId);
}

public static async Task<MyThing?> GetMyThingById(Guid myThingId, DocumentStore store)
{
await using var session = store.LightweightSession();
return session.Events.AggregateStream<MyThing>(myThingId);
}
}

0 comments on commit bc7f3ea

Please sign in to comment.