Skip to content

Commit 191eb5f

Browse files
Change to domain driven events.
1 parent 52233bc commit 191eb5f

File tree

152 files changed

+1198
-6723
lines changed

Some content is hidden

Large Commits have some content hidden by default. Use the searchbox below for content that may be hidden.

152 files changed

+1198
-6723
lines changed

.gitignore

+479
Large diffs are not rendered by default.
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file not shown.
730 Bytes
Binary file not shown.

.vs/heimdall_web_api/v17/.suo

9.5 KB
Binary file not shown.
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,109 @@
1+
using Heimdall.Logic.Core;
2+
using Heimdall.Logic.WorkInstructions;
3+
using Heimdall.Logic.WorkInstructions.Commands;
4+
using Heimdall.Logic.WorkInstructions.Entities;
5+
using Heimdall.Logic.WorkInstructions.Events;
6+
using Microsoft.EntityFrameworkCore;
7+
using Npgsql.EntityFrameworkCore.PostgreSQL.Query.ExpressionTranslators.Internal;
8+
using OneOf;
9+
using System;
10+
using System.Collections.Generic;
11+
using System.Linq;
12+
using System.Text;
13+
using System.Threading.Tasks;
14+
15+
namespace Heimdall.Data.Accessors
16+
{
17+
internal class WorkInstructionAccessor : IWorkInstructionAccessor
18+
{
19+
private readonly WorkInstructionDatabaseContext _context;
20+
21+
public WorkInstructionAccessor(WorkInstructionDatabaseContext context)
22+
{
23+
_context = context;
24+
}
25+
26+
public async Task<OneOf<WorkInstructionCreated, CreateWorkInstructionFailed>> AddAsync(CreateWorkInstruction command)
27+
{
28+
var workInstruction = new WorkInstruction()
29+
{
30+
31+
Title = command.Title,
32+
Description = command.Description,
33+
InstructionObject = command.InstructionObject,
34+
};
35+
try
36+
{
37+
_context.WorkInstructions.Add(workInstruction);
38+
await _context.SaveChangesAsync();
39+
return new WorkInstructionCreated(workInstruction);
40+
}
41+
catch (Exception ex)
42+
{
43+
return new CreateWorkInstructionFailed(command, ex.Message);
44+
}
45+
}
46+
47+
public async Task<OneOf<WorkInstructionDeleted, DeleteWorkInstructionFailed>> DeleteAsync(DeleteWorkInstruction command)
48+
{
49+
var workInstruction = await _context.WorkInstructions.FindAsync(command.Id);
50+
51+
if (workInstruction is null)
52+
{
53+
return new DeleteWorkInstructionFailed(command, $"Could not find work instruction with id {command.Id}");
54+
}
55+
56+
_context.Remove(workInstruction);
57+
await _context.SaveChangesAsync();
58+
59+
return new WorkInstructionDeleted(command.Id);
60+
}
61+
62+
public async Task<OneOf<WorkInstructionsRead, WorkInstructionNotFound>> GetAllAsync()
63+
{
64+
WorkInstruction[] databaseInstructions = (await _context.WorkInstructions.ToArrayAsync()) ?? Array.Empty<WorkInstruction>();
65+
66+
return new WorkInstructionsRead(databaseInstructions);
67+
}
68+
69+
public async Task<OneOf<WorkInstructionRead, WorkInstructionNotFound>> GetAsync(ReadWorkInstruction command)
70+
{
71+
WorkInstruction? workInstruction = await _context.WorkInstructions.FindAsync(command.Id);
72+
if (workInstruction is null)
73+
{
74+
return new WorkInstructionNotFound(command.Id);
75+
}
76+
77+
return new WorkInstructionRead(workInstruction);
78+
}
79+
80+
public async Task<OneOf<WorkInstructionUpdated, UpdateWorkInstructionFailed>> UpdateAsync(UpdateWorkInstruction command)
81+
{
82+
83+
var workInSet = await _context.WorkInstructions.FindAsync(command.Id);
84+
85+
// RLP - Fail early. Find bad cases, return, and continue with good cases.
86+
if (workInSet is null)
87+
{
88+
return new UpdateWorkInstructionFailed(command, $"Unable to find Work Instruction with id {command.Id}");
89+
}
90+
91+
if (command.Title is not null)
92+
{
93+
workInSet.Title = command.Title;
94+
}
95+
if (command.Description is not null)
96+
{
97+
workInSet.Description = command.Description;
98+
}
99+
if (command.InstructionObject is not null)
100+
{
101+
workInSet.InstructionObject = command.InstructionObject;
102+
}
103+
104+
await _context.SaveChangesAsync();
105+
106+
return new WorkInstructionUpdated(workInSet);
107+
}
108+
}
109+
}

Heimdall.Data/WorkInstructionSetConfig.cs Heimdall.Data/EntityConfigurations/WorkInstructionConfiguration.cs

+10-3
Original file line numberDiff line numberDiff line change
@@ -1,12 +1,19 @@
11
using Heimdall.Data;
2+
using Heimdall.Logic.WorkInstructions.Entities;
23
using Microsoft.EntityFrameworkCore;
34
using Microsoft.EntityFrameworkCore.Metadata.Builders;
45

5-
namespace heimdall_web_api.Configs
6+
namespace Heimdall.Data.EntityConfigurations
67
{
7-
public class WorkInstructionSetConfig : IEntityTypeConfiguration<WorkInstructionSet>
8+
/// <summary>
9+
/// Configuration for the <see cref="WorkInstruction"/>
10+
/// </summary>
11+
/// <remarks>
12+
/// RLP - I'd encourage using the full name of the entity element that this extends (e.g., it's a configuration)
13+
/// </remarks>
14+
public class WorkInstructionConfiguration : IEntityTypeConfiguration<WorkInstruction>
815
{
9-
public void Configure(EntityTypeBuilder<WorkInstructionSet> builder)
16+
public void Configure(EntityTypeBuilder<WorkInstruction> builder)
1017
{
1118
builder.HasKey(x => x.Id);
1219

Heimdall.Data/Heimdall.Data.csproj

+4-1
Original file line numberDiff line numberDiff line change
@@ -7,9 +7,12 @@
77
</PropertyGroup>
88

99
<ItemGroup>
10-
<PackageReference Include="EntityFramework" Version="6.4.4" />
1110
<PackageReference Include="Microsoft.EntityFrameworkCore" Version="7.0.2" />
1211
<PackageReference Include="Npgsql.EntityFrameworkCore.PostgreSQL" Version="7.0.1" />
1312
</ItemGroup>
1413

14+
<ItemGroup>
15+
<ProjectReference Include="..\Heimdall.Logic\Heimdall.Logic.csproj" />
16+
</ItemGroup>
17+
1518
</Project>
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,28 @@
1+
using Heimdall.Data.Accessors;
2+
using Heimdall.Logic.WorkInstructions;
3+
using Microsoft.Extensions.DependencyInjection;
4+
using System;
5+
using System.Collections.Generic;
6+
using System.Linq;
7+
using System.Text;
8+
using System.Threading.Tasks;
9+
10+
namespace Heimdall.Data
11+
{
12+
/// <summary>
13+
///
14+
/// </summary>
15+
/// <remarks>
16+
/// This lets your business logic decide what it makes available to dependency injection
17+
/// without mucking up the Program.cs file.
18+
/// </remarks>
19+
public static class IServiceCollectionExtensions
20+
{
21+
public static IServiceCollection AddHeimdallData(this IServiceCollection services)
22+
{
23+
services.AddScoped<IWorkInstructionAccessor, WorkInstructionAccessor>();
24+
25+
return services;
26+
}
27+
}
28+
}
Original file line numberDiff line numberDiff line change
@@ -1,10 +1,11 @@
11
using Microsoft.EntityFrameworkCore;
2-
using heimdall_web_api.Configs;
32
using System;
43
using System.Collections.Generic;
54
using System.Linq;
65
using System.Text;
76
using System.Threading.Tasks;
7+
using Heimdall.Logic.WorkInstructions.Entities;
8+
using System.Reflection;
89

910
namespace Heimdall.Data
1011
{
@@ -13,12 +14,14 @@ public class WorkInstructionDatabaseContext : DbContext
1314
public WorkInstructionDatabaseContext(DbContextOptions<WorkInstructionDatabaseContext> options) : base(options)
1415
{
1516
}
16-
public DbSet<WorkInstructionSet> workInstructionSets { get; set; }
17+
18+
// RLP - use PascalCasing for entities. The word "Set" isn't needed to define a set.
19+
public DbSet<WorkInstruction> WorkInstructions { get; set; }
1720

1821
protected override void OnModelCreating(ModelBuilder modelBuilder)
1922
{
20-
new WorkInstructionSetConfig().Configure(modelBuilder.Entity<WorkInstructionSet>());
21-
//now using IEntityTypeConfiguration to setup data accsess in a organized way
23+
// RLP - There way will load all configurations in the entire assembly without having to specify them individually.
24+
modelBuilder.ApplyConfigurationsFromAssembly(Assembly.GetExecutingAssembly());
2225
}
2326
}
2427
}

Heimdall.Data/WorkInstructionSet.cs

-22
This file was deleted.

0 commit comments

Comments
 (0)