Skip to content

Commit

Permalink
add default constructor to FileData, implement DetachFilesFromContexts
Browse files Browse the repository at this point in the history
  • Loading branch information
codingawayy committed Jun 7, 2017
1 parent b8db6e1 commit 63f395b
Show file tree
Hide file tree
Showing 12 changed files with 159 additions and 109 deletions.
4 changes: 2 additions & 2 deletions Filer.Core/File.cs
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,7 @@ public class File
/// Initializes a new instance of the <see cref="File"/> class.
/// </summary>
/// <param name="createdByUserId">Id of the user who created the file.</param>
public File(int? createdByUserId = null)
internal File(int? createdByUserId = null)
{
this.CreatedByUserId = createdByUserId;
this.CreatedOn = DateTime.UtcNow;
Expand All @@ -22,7 +22,7 @@ public File(int? createdByUserId = null)
/// <summary>
/// Initializes a new instance of the File class.
/// </summary>
public File()
internal File()
: this(null)
{
}
Expand Down
4 changes: 2 additions & 2 deletions Filer.Core/FileContext.cs
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,7 @@ public class FileContext
/// </summary>
public const int ValueMaxLength = 50;

private FileContext()
protected FileContext()
{
}

Expand All @@ -21,7 +21,7 @@ private FileContext()
/// </summary>
/// <param name="fileId"></param>
/// <param name="value"></param>
public FileContext(int fileId, string value)
internal FileContext(int fileId, string value)
{
if (value?.Length > ValueMaxLength)
{
Expand Down
6 changes: 5 additions & 1 deletion Filer.Core/FileData.cs
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,11 @@
/// </summary>
public class FileData
{
public FileData(byte[] data, CompressionFormat compressionFormat)
protected FileData()
{
}

internal FileData(byte[] data, CompressionFormat compressionFormat)
{
switch (compressionFormat)
{
Expand Down
2 changes: 1 addition & 1 deletion Filer.Core/Filer.Core.csproj
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@
<PropertyGroup>
<TargetFramework>netstandard1.6</TargetFramework>
<PackageId>Filer.Core</PackageId>
<PackageVersion>0.0.5-beta</PackageVersion>
<PackageVersion>0.0.6-beta</PackageVersion>
<Authors>UNOPS</Authors>
<Description>Filer is a very simple .NET Standard library to help you manage documents inside your app. This package includes core functionality without any storage implementation.</Description>
<PackageRequireLicenseAcceptance>false</PackageRequireLicenseAcceptance>
Expand Down
16 changes: 16 additions & 0 deletions Filer.EntityFrameworkCore.Tests/DbTest.cs
Original file line number Diff line number Diff line change
Expand Up @@ -108,5 +108,21 @@ public async void CanSpecifyUploader()

Assert.StrictEqual(12345, file.CreatedByUserId);
}

[Fact]
public async void CanRemoveContext()
{
var fileManager = new FileManager(this.dbFixture.CreateDataContext());
var fileId1 = await fileManager.SaveFile("test.txt", "text/plain", new byte[0], CompressionFormat.GZip);
var fileId2 = await fileManager.SaveFile("test.txt", "text/plain", new byte[0], CompressionFormat.GZip);

await fileManager.AttachFileToContexts(fileId1, "invoice:1", "contract:1", "user:123");
await fileManager.AttachFileToContexts(fileId2, "invoice:2", "contract:2", "user:123");

await fileManager.DetachFilesFromContexts("user:123");

var userFilesExist = await fileManager.FileContexts.AnyAsync(t => t.Value == "user:123");
Assert.False(userFilesExist);
}
}
}
10 changes: 10 additions & 0 deletions Filer.EntityFrameworkCore/FileManager.cs
Original file line number Diff line number Diff line change
Expand Up @@ -162,5 +162,15 @@ public async Task DetachFileFromContexts(int fileId, params string[] contexts)

await this.dbContext.SaveChangesAsync();
}

/// <summary>
/// Disassociates all files from specified contexts.
/// </summary>
/// <param name="contexts">Context identifiers.</param>
public async Task DetachFilesFromContexts(params string[] contexts)
{
var contextsCsv = string.Join(",", contexts.Select(t => $"'{t}'"));
await this.dbContext.Database.ExecuteSqlCommandAsync($"delete from FileContext where Value in ({contextsCsv})");
}
}
}
2 changes: 1 addition & 1 deletion Filer.EntityFrameworkCore/Filer.EntityFrameworkCore.csproj
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@
<PropertyGroup>
<TargetFramework>netstandard1.6</TargetFramework>
<PackageId>Filer.EntityFrameworkCore</PackageId>
<PackageVersion>0.0.5-beta</PackageVersion>
<PackageVersion>0.0.6-beta</PackageVersion>
<Authors>UNOPS</Authors>
<Description>Filer is a very simple .NET Standard library to help you manage documents inside your app. This package includes implementation of SQL Store.</Description>
<PackageRequireLicenseAcceptance>false</PackageRequireLicenseAcceptance>
Expand Down
2 changes: 2 additions & 0 deletions Filer.EntityFrameworkCore/Mappings/FileContextMap.cs
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,8 @@ public override void Configure(EntityTypeBuilder<FileContext> entity)

entity.Property(t => t.FileId).HasColumnName("FileId");
entity.Property(t => t.Value).HasColumnName("Value").HasMaxLength(FileContext.ValueMaxLength).IsUnicode(false);

entity.HasIndex(t => new { t.Value, t.FileId }).IsUnique();
}
}
}

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

Original file line number Diff line number Diff line change
Expand Up @@ -76,6 +76,12 @@ protected override void Up(MigrationBuilder migrationBuilder)
name: "IX_File_CreatedByUserId",
table: "File",
column: "CreatedByUserId");

migrationBuilder.CreateIndex(
name: "IX_FileContext_Value_FileId",
table: "FileContext",
columns: new[] { "Value", "FileId" },
unique: true);
}
}
}
207 changes: 106 additions & 101 deletions Filer.EntityFrameworkCore/Migrations/FileStoreContextModelSnapshot.cs
Original file line number Diff line number Diff line change
@@ -1,102 +1,107 @@
namespace Filer.EntityFrameworkCore.Migrations
using System;
using Microsoft.EntityFrameworkCore;
using Microsoft.EntityFrameworkCore.Infrastructure;
using Microsoft.EntityFrameworkCore.Metadata;
using Microsoft.EntityFrameworkCore.Migrations;
using Filer.EntityFrameworkCore;

namespace Filer.EntityFrameworkCore.Migrations
{
using System;
using Microsoft.EntityFrameworkCore;
using Microsoft.EntityFrameworkCore.Infrastructure;
using Microsoft.EntityFrameworkCore.Metadata;

[DbContext(typeof(FileStoreContext))]
partial class FileStoreContextModelSnapshot : ModelSnapshot
{
protected override void BuildModel(ModelBuilder modelBuilder)
{
modelBuilder
.HasAnnotation("ProductVersion", "1.1.2")
.HasAnnotation("SqlServer:ValueGenerationStrategy", SqlServerValueGenerationStrategy.IdentityColumn);

modelBuilder.Entity("Filer.Core.File", b =>
{
b.Property<int>("Id")
.ValueGeneratedOnAdd()
.HasColumnName("Id");

b.Property<byte>("CompressionFormatId")
.HasColumnName("CompressionFormat");

b.Property<int?>("CreatedByUserId")
.HasColumnName("CreatedByUserId");

b.Property<DateTime>("CreatedOn")
.HasColumnName("CreatedOn");

b.Property<string>("Extension")
.HasColumnName("Extension")
.HasMaxLength(20)
.IsUnicode(true);

b.Property<string>("MimeType")
.HasColumnName("MimeType")
.HasMaxLength(100)
.IsUnicode(false);

b.Property<string>("Name")
.HasColumnName("Name")
.HasMaxLength(255)
.IsUnicode(true);

b.Property<long>("Size")
.HasColumnName("Size");

b.HasKey("Id");

b.HasIndex("CreatedByUserId")
.HasName("IX_File_CreatedByUserId");

b.ToTable("File");
});

modelBuilder.Entity("Filer.Core.FileContext", b =>
{
b.Property<int>("FileId")
.HasColumnName("FileId");

b.Property<string>("Value")
.HasColumnName("Value")
.HasMaxLength(50)
.IsUnicode(false);

b.HasKey("FileId", "Value");

b.ToTable("FileContext");
});

modelBuilder.Entity("Filer.Core.FileData", b =>
{
b.Property<int>("FileId")
.HasColumnName("Id");

b.Property<byte[]>("Data")
.HasColumnName("Data");

b.HasKey("FileId");

b.ToTable("FileData");
});

modelBuilder.Entity("Filer.Core.FileContext", b =>
{
b.HasOne("Filer.Core.File", "File")
.WithMany("Contexts")
.HasForeignKey("FileId");
});

modelBuilder.Entity("Filer.Core.FileData", b =>
{
b.HasOne("Filer.Core.File")
.WithOne("Data")
.HasForeignKey("Filer.Core.FileData", "FileId")
.OnDelete(DeleteBehavior.Cascade);
});
}
}
}
[DbContext(typeof(FileStoreContext))]
partial class FileStoreContextModelSnapshot : ModelSnapshot
{
protected override void BuildModel(ModelBuilder modelBuilder)
{
modelBuilder
.HasAnnotation("ProductVersion", "1.1.2")
.HasAnnotation("SqlServer:ValueGenerationStrategy", SqlServerValueGenerationStrategy.IdentityColumn);

modelBuilder.Entity("Filer.Core.File", b =>
{
b.Property<int>("Id")
.ValueGeneratedOnAdd()
.HasColumnName("Id");

b.Property<byte>("CompressionFormatId")
.HasColumnName("CompressionFormat");

b.Property<int?>("CreatedByUserId")
.HasColumnName("CreatedByUserId");

b.Property<DateTime>("CreatedOn")
.HasColumnName("CreatedOn");

b.Property<string>("Extension")
.HasColumnName("Extension")
.HasMaxLength(20)
.IsUnicode(true);

b.Property<string>("MimeType")
.HasColumnName("MimeType")
.HasMaxLength(100)
.IsUnicode(false);

b.Property<string>("Name")
.HasColumnName("Name")
.HasMaxLength(255)
.IsUnicode(true);

b.Property<long>("Size")
.HasColumnName("Size");

b.HasKey("Id");

b.HasIndex("CreatedByUserId")
.HasName("IX_File_CreatedByUserId");

b.ToTable("File");
});

modelBuilder.Entity("Filer.Core.FileContext", b =>
{
b.Property<int>("FileId")
.HasColumnName("FileId");

b.Property<string>("Value")
.HasColumnName("Value")
.HasMaxLength(50)
.IsUnicode(false);

b.HasKey("FileId", "Value");

b.HasIndex("Value", "FileId")
.IsUnique();

b.ToTable("FileContext");
});

modelBuilder.Entity("Filer.Core.FileData", b =>
{
b.Property<int>("FileId")
.HasColumnName("Id");

b.Property<byte[]>("Data")
.HasColumnName("Data");

b.HasKey("FileId");

b.ToTable("FileData");
});

modelBuilder.Entity("Filer.Core.FileContext", b =>
{
b.HasOne("Filer.Core.File", "File")
.WithMany("Contexts")
.HasForeignKey("FileId");
});

modelBuilder.Entity("Filer.Core.FileData", b =>
{
b.HasOne("Filer.Core.File")
.WithOne("Data")
.HasForeignKey("Filer.Core.FileData", "FileId")
.OnDelete(DeleteBehavior.Cascade);
});
}
}
}
4 changes: 4 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -47,5 +47,9 @@ GO
CREATE INDEX [IX_File_CreatedByUserId] ON [File] ([CreatedByUserId]);
GO
CREATE UNIQUE INDEX [IX_FileContext_Value_FileId] ON [FileContext] ([Value], [FileId]);
GO
```

0 comments on commit 63f395b

Please sign in to comment.