-
Notifications
You must be signed in to change notification settings - Fork 2
added design feature #38
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
Merged
Merged
Changes from all commits
Commits
Show all changes
4 commits
Select commit
Hold shift + click to select a range
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -1,51 +1,107 @@ | ||
| // Copyright 2021 Carnegie Mellon University. All Rights Reserved. | ||
| // Released under a MIT (SEI)-style license. See LICENSE.md in the project root for license information. | ||
|
|
||
| using System; | ||
| using System.Collections.Generic; | ||
| using System.Linq; | ||
| using System.Threading; | ||
| using System.Threading.Tasks; | ||
| using Caster.Api.Domain.Models; | ||
| using Caster.Api.Infrastructure.Extensions; | ||
| using Microsoft.EntityFrameworkCore; | ||
|
|
||
| namespace Caster.Api.Data | ||
| namespace Caster.Api.Data; | ||
|
|
||
| public partial class CasterContext : DbContext | ||
| { | ||
| public partial class CasterContext : DbContext | ||
| public List<Entry> Entries { get; set; } = new List<Entry>(); | ||
|
|
||
| private DbContextOptions<CasterContext> _options; | ||
|
|
||
| public CasterContext(DbContextOptions<CasterContext> options) : base(options) | ||
| { | ||
| _options = options; | ||
| } | ||
|
|
||
| public DbSet<Project> Projects { get; set; } | ||
| public DbSet<Directory> Directories { get; set; } | ||
| public DbSet<File> Files { get; set; } | ||
| public DbSet<FileVersion> FileVersions { get; set; } | ||
| public DbSet<Workspace> Workspaces { get; set; } | ||
| public DbSet<Run> Runs { get; set; } | ||
| public DbSet<Plan> Plans { get; set; } | ||
| public DbSet<Apply> Applies { get; set; } | ||
| public DbSet<RemovedResource> RemovedResources { get; set; } | ||
| public DbSet<Module> Modules { get; set; } | ||
| public DbSet<ModuleVersion> ModuleVersions { get; set; } | ||
| public DbSet<Permission> Permissions { get; set; } | ||
| public DbSet<User> Users { get; set; } | ||
| public DbSet<UserPermission> UserPermissions { get; set; } | ||
| public DbSet<Host> Hosts { get; set; } | ||
| public DbSet<HostMachine> HostMachines { get; set; } | ||
| public DbSet<Design> Designs { get; set; } | ||
| public DbSet<DesignModule> DesignModules { get; set; } | ||
| public DbSet<Variable> Variables { get; set; } | ||
|
|
||
| protected override void OnModelCreating(ModelBuilder modelBuilder) | ||
| { | ||
| private DbContextOptions<CasterContext> _options; | ||
| modelBuilder.ApplyConfigurations(); | ||
|
|
||
| public CasterContext(DbContextOptions<CasterContext> options) : base(options) { | ||
| _options = options; | ||
| // Apply PostgreSQL specific options | ||
| if (Database.IsNpgsql()) | ||
| { | ||
| modelBuilder.AddPostgresUUIDGeneration(); | ||
| modelBuilder.UsePostgresCasing(); | ||
| } | ||
| } | ||
|
|
||
| public DbSet<Project> Projects { get; set; } | ||
| public DbSet<Directory> Directories { get; set; } | ||
| public DbSet<File> Files { get; set; } | ||
| public DbSet<FileVersion> FileVersions { get; set; } | ||
| public DbSet<Workspace> Workspaces { get; set; } | ||
| public DbSet<Run> Runs { get; set; } | ||
| public DbSet<Plan> Plans { get; set; } | ||
| public DbSet<Apply> Applies { get; set; } | ||
| public DbSet<RemovedResource> RemovedResources { get; set; } | ||
| public DbSet<Module> Modules { get; set; } | ||
| public DbSet<ModuleVersion> ModuleVersions { get; set; } | ||
| public DbSet<Permission> Permissions { get; set; } | ||
| public DbSet<User> Users { get; set; } | ||
| public DbSet<UserPermission> UserPermissions { get; set; } | ||
| public DbSet<Host> Hosts { get; set; } | ||
| public DbSet<HostMachine> HostMachines { get; set; } | ||
|
|
||
| protected override void OnModelCreating(ModelBuilder modelBuilder) | ||
| public override async Task<int> SaveChangesAsync(CancellationToken ct = default) | ||
| { | ||
| SaveEntries(); | ||
| return await base.SaveChangesAsync(ct); | ||
| } | ||
|
|
||
| /// <summary> | ||
| /// keep track of changes across multiple savechanges in a transaction, without duplicates | ||
| /// </summary> | ||
| private void SaveEntries() | ||
| { | ||
| foreach (var entry in ChangeTracker.Entries()) | ||
| { | ||
| modelBuilder.ApplyConfigurations(); | ||
| // find value of id property | ||
| var id = entry.Properties | ||
| .FirstOrDefault(x => | ||
| x.Metadata.ValueGenerated == Microsoft.EntityFrameworkCore.Metadata.ValueGenerated.OnAdd)?.CurrentValue; | ||
|
|
||
| // Apply PostgreSQL specific options | ||
| if (Database.IsNpgsql()) | ||
| // find matching existing entry, if any | ||
| var e = Entries.FirstOrDefault(x => x.Properties.FirstOrDefault(y => | ||
| y.Metadata.ValueGenerated == Microsoft.EntityFrameworkCore.Metadata.ValueGenerated.OnAdd)?.CurrentValue == id); | ||
|
|
||
| if (e != null) | ||
| { | ||
| // if entry already exists, mark which properties were previously modified, | ||
| // remove old entry and add new one, to avoid duplicates | ||
| var modifiedProperties = e.Properties | ||
| .Where(x => x.IsModified) | ||
| .Select(x => x.Metadata.Name) | ||
| .ToArray(); | ||
|
|
||
| var newEntry = new Entry(entry); | ||
|
|
||
| foreach (var property in newEntry.Properties) | ||
| { | ||
| if (modifiedProperties.Contains(property.Metadata.Name)) | ||
| { | ||
| property.IsModified = true; | ||
| } | ||
| } | ||
|
|
||
| Entries.Remove(e); | ||
| Entries.Add(newEntry); | ||
| } | ||
| else | ||
| { | ||
| modelBuilder.AddPostgresUUIDGeneration(); | ||
| modelBuilder.UsePostgresCasing(); | ||
| Entries.Add(new Entry(entry)); | ||
| } | ||
| } | ||
| } | ||
| } | ||
| } | ||
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,23 @@ | ||
| // Copyright 2021 Carnegie Mellon University. All Rights Reserved. | ||
| // Released under a MIT (SEI)-style license. See LICENSE.md in the project root for license information. | ||
|
|
||
| using System; | ||
| using System.Collections.Generic; | ||
| using Microsoft.EntityFrameworkCore; | ||
| using Microsoft.EntityFrameworkCore.ChangeTracking; | ||
|
|
||
| namespace Caster.Api.Data; | ||
|
|
||
| public class Entry | ||
| { | ||
| public Entry(EntityEntry entry) | ||
| { | ||
| Entity = entry.Entity; | ||
| State = entry.State; | ||
| Properties = entry.Properties; | ||
| } | ||
|
|
||
| public object Entity { get; set; } | ||
| public EntityState State { get; set; } | ||
| public IEnumerable<PropertyEntry> Properties { get; set; } | ||
| } |
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
This is very cool. I like it!