modifica (importante) modallo dati x tags + MD% check

This commit is contained in:
Samuele Locatelli
2021-09-07 12:03:01 +02:00
parent 68b9cd4d78
commit 607e096609
7 changed files with 264 additions and 64 deletions
+76 -55
View File
@@ -101,7 +101,11 @@ namespace MP.FileData.Controllers
}
else
{
fileMod.Add(file);
// verifico se data modifica e dimensione siano cambiati...
if (currRecord.LastMod != file.LastWriteTime || currRecord.Size != file.Length)
{
fileMod.Add(file);
}
}
}
@@ -182,7 +186,7 @@ namespace MP.FileData.Controllers
{
dbResult = localDbCtx
.DbSetProgFile
.Where(x => x.Path.StartsWith(path) && ((onlyActive && x.Active) || !onlyActive))
.Where(x => x.Path.StartsWith(path) && ((onlyActive == x.Active) || !onlyActive))
.OrderBy(x => x.Name)
.ToList();
}
@@ -198,7 +202,7 @@ namespace MP.FileData.Controllers
.DbSetProgFile
.Include(m => m.Macchina)
.Include(a => a.Articolo)
.Where(x => ((x.IdxMacchina == IdxMacchina || IdxMacchina == "0") && (x.CodArticolo == CodArticolo || CodArticolo == "ND" || CodArticolo.StartsWith("##") || string.IsNullOrEmpty(CodArticolo))) && (x.Name.Contains(SearchVal) || string.IsNullOrEmpty(SearchVal)) && (!OnlyMod || x.Changed))
.Where(x => ((x.IdxMacchina == IdxMacchina || IdxMacchina == "0") && (x.CodArticolo == CodArticolo || CodArticolo == "ND" || CodArticolo.StartsWith("##") || string.IsNullOrEmpty(CodArticolo))) && (x.Name.Contains(SearchVal) || string.IsNullOrEmpty(SearchVal)) && (!OnlyMod || x.DiskState != FileState.Unchanged))
.OrderBy(x => x.Name)
.ToList();
}
@@ -215,33 +219,44 @@ namespace MP.FileData.Controllers
{
bool answ = false;
DateTime adesso = DateTime.Now;
using (MoonPro_ProgContext localDbCtx = new MoonPro_ProgContext(_configuration))
// MD5 hash
using (System.Security.Cryptography.MD5 md5 = System.Security.Cryptography.MD5.Create())
{
// converto
List<DatabaseModels.FileModel> newRec = newFiles.Select(o => new DatabaseModels.FileModel()
using (MoonPro_ProgContext localDbCtx = new MoonPro_ProgContext(_configuration))
{
Active = true,
CodArticolo = "ND",
Changed = false,
IdxMacchina = idxMacchina,
LastCheck = adesso,
LastMod = o.LastWriteTime,
MimeType = o.Extension,
Name = o.Name,
Path = o.FullName,
Rev = 0,
Size = o.Length,
FileContent = File.ReadAllBytes(o.FullName)
}).ToList();
// converto
List<DatabaseModels.FileModel> newRec = newFiles.Select(o => new DatabaseModels.FileModel()
{
Active = true,
CodArticolo = "ND",
DiskState = FileState.New,
IdxMacchina = idxMacchina,
LastCheck = adesso,
LastMod = o.LastWriteTime,
MimeType = o.Extension,
Name = o.Name,
Path = o.FullName,
Rev = 0,
Size = o.Length,
FileContent = File.ReadAllBytes(o.FullName)
}).ToList();
// aggiungo in blocco
localDbCtx
.DbSetProgFile
.AddRange(newRec);
// calcolo MD5
foreach (var item in newRec)
{
var hash = md5.ComputeHash(item.FileContent);
item.MD5 = BitConverter.ToString(hash).Replace("-", "").ToLowerInvariant();
}
// salvo
localDbCtx.SaveChanges();
answ = true;
// aggiungo in blocco
localDbCtx
.DbSetProgFile
.AddRange(newRec);
// salvo
localDbCtx.SaveChanges();
answ = true;
}
}
return answ;
}
@@ -265,12 +280,7 @@ namespace MP.FileData.Controllers
.FirstOrDefault();
if (currData != null)
{
//// se ho modificato data --> cambio codice ordine!
//if (!localDbCtx.Entry(updItem).OriginalValues["DtOrder"].Equals(localDbCtx.Entry(updItem).CurrentValues["DtOrder"]))
//{
// updItem.OrderCode = $"O{updItem.Plant.PlantCode}{updItem.DtOrder:yyMMddHHmm}";
// updItem.OrderDesc = $"Ordine {updItem.Plant.PlantDesc} - {updItem.DtOrder}";
//}
updItem.DiskState = FileState.Changed;
localDbCtx.Entry(updItem).State = EntityState.Modified;
localDbCtx.SaveChanges();
}
@@ -358,33 +368,44 @@ namespace MP.FileData.Controllers
{
bool answ = false;
DateTime adesso = DateTime.Now;
using (MoonPro_ProgContext localDbCtx = new MoonPro_ProgContext(_configuration))
// MD5 hash
using (System.Security.Cryptography.MD5 md5 = System.Security.Cryptography.MD5.Create())
{
// converto
List<DatabaseModels.FileModel> newRec = updFiles.Select(o => new DatabaseModels.FileModel()
using (MoonPro_ProgContext localDbCtx = new MoonPro_ProgContext(_configuration))
{
Active = true,
CodArticolo = "ND",
Changed = false,
IdxMacchina = idxMacchina,
LastCheck = adesso,
LastMod = o.LastWriteTime,
MimeType = o.Extension,
Name = o.Name,
Path = o.FullName,
Rev = 0,
Size = o.Length,
FileContent = File.ReadAllBytes(o.FullName)
}).ToList();
// converto
List<DatabaseModels.FileModel> newRec = updFiles.Select(o => new DatabaseModels.FileModel()
{
Active = true,
DiskState = FileState.Changed,
CodArticolo = "ND",
FileContent = File.ReadAllBytes(o.FullName),
IdxMacchina = idxMacchina,
LastCheck = adesso,
LastMod = o.LastWriteTime,
MimeType = o.Extension,
Name = o.Name,
Path = o.FullName,
Rev = 0,
Size = o.Length
}).ToList();
// aggiungo in blocco
localDbCtx
.DbSetProgFile
.UpdateRange(newRec);
// calcolo MD5
foreach (var item in newRec)
{
var hash = md5.ComputeHash(item.FileContent);
item.MD5 = BitConverter.ToString(hash).Replace("-", "").ToLowerInvariant();
}
// salvo
localDbCtx.SaveChanges();
answ = true;
// aggiungo in blocco
localDbCtx
.DbSetProgFile
.UpdateRange(newRec);
// salvo
localDbCtx.SaveChanges();
answ = true;
}
}
return answ;
}
+4 -1
View File
@@ -30,10 +30,13 @@ namespace MP.FileData.DatabaseModels
public long Size { get; set; } = 0;
public string Path { get; set; } = "";
public string MimeType { get; set; } = "";
public bool Changed { get; set; } = false;
public string MD5 { get; set; } = "";
public FileState DiskState { get; set; } = FileState.New;
public DateTime LastCheck { get; set; } = DateTime.Now.AddYears(-1);
public byte[] FileContent { get; set; }
public ICollection<TagModel> Tags { get; set; }
[NotMapped]
public string FileStringContent
{
+23
View File
@@ -0,0 +1,23 @@
using System;
using System.Collections.Generic;
using System.ComponentModel.DataAnnotations;
using System.ComponentModel.DataAnnotations.Schema;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
// <Auto-Generated>
// This is here so CodeMaid doesn't reorganize this document
// </Auto-Generated>
namespace MP.FileData.DatabaseModels
{
[Table("Tags")]
public partial class TagModel
{
[Key]
public string TagId { get; set; }
public ICollection<FileModel> Files { get; set; }
}
}
+20
View File
@@ -0,0 +1,20 @@
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
namespace MP.FileData
{
/// <summary>
/// Stato File
/// </summary>
public enum FileState
{
ND = 0,
New,
Changed,
Deleted,
Unchanged
}
}
@@ -10,7 +10,7 @@ using Microsoft.EntityFrameworkCore.Storage.ValueConversion;
namespace MP.FileData.Migrations
{
[DbContext(typeof(MoonPro_ProgContext))]
[Migration("20210903153915_InitDb")]
[Migration("20210907100115_InitDb")]
partial class InitDb
{
protected override void BuildTargetModel(ModelBuilder modelBuilder)
@@ -22,6 +22,21 @@ namespace MP.FileData.Migrations
.HasAnnotation("ProductVersion", "5.0.9")
.HasAnnotation("SqlServer:ValueGenerationStrategy", SqlServerValueGenerationStrategy.IdentityColumn);
modelBuilder.Entity("FileModelTagModel", b =>
{
b.Property<int>("FilesFileId")
.HasColumnType("int");
b.Property<string>("TagsTagId")
.HasColumnType("nvarchar(450)");
b.HasKey("FilesFileId", "TagsTagId");
b.HasIndex("TagsTagId");
b.ToTable("FileModelTagModel");
});
modelBuilder.Entity("MP.FileData.DatabaseModels.ArticoloModel", b =>
{
b.Property<string>("CodArticolo")
@@ -60,12 +75,12 @@ namespace MP.FileData.Migrations
b.Property<bool>("Active")
.HasColumnType("bit");
b.Property<bool>("Changed")
.HasColumnType("bit");
b.Property<string>("CodArticolo")
.HasColumnType("nvarchar(450)");
b.Property<int>("DiskState")
.HasColumnType("int");
b.Property<byte[]>("FileContent")
.HasColumnType("varbinary(max)");
@@ -78,6 +93,9 @@ namespace MP.FileData.Migrations
b.Property<DateTime>("LastMod")
.HasColumnType("datetime2");
b.Property<string>("MD5")
.HasColumnType("nvarchar(max)");
b.Property<string>("MimeType")
.HasColumnType("nvarchar(max)");
@@ -146,6 +164,31 @@ namespace MP.FileData.Migrations
});
});
modelBuilder.Entity("MP.FileData.DatabaseModels.TagModel", b =>
{
b.Property<string>("TagId")
.HasColumnType("nvarchar(450)");
b.HasKey("TagId");
b.ToTable("Tags");
});
modelBuilder.Entity("FileModelTagModel", b =>
{
b.HasOne("MP.FileData.DatabaseModels.FileModel", null)
.WithMany()
.HasForeignKey("FilesFileId")
.OnDelete(DeleteBehavior.Cascade)
.IsRequired();
b.HasOne("MP.FileData.DatabaseModels.TagModel", null)
.WithMany()
.HasForeignKey("TagsTagId")
.OnDelete(DeleteBehavior.Cascade)
.IsRequired();
});
modelBuilder.Entity("MP.FileData.DatabaseModels.FileModel", b =>
{
b.HasOne("MP.FileData.DatabaseModels.ArticoloModel", "Articolo")
@@ -39,6 +39,17 @@ namespace MP.FileData.Migrations
table.PrimaryKey("PK_Macchine", x => x.IdxMacchina);
});
migrationBuilder.CreateTable(
name: "Tags",
columns: table => new
{
TagId = table.Column<string>(type: "nvarchar(450)", nullable: false)
},
constraints: table =>
{
table.PrimaryKey("PK_Tags", x => x.TagId);
});
migrationBuilder.CreateTable(
name: "Files",
columns: table => new
@@ -54,7 +65,8 @@ namespace MP.FileData.Migrations
Size = table.Column<long>(type: "bigint", nullable: false),
Path = table.Column<string>(type: "nvarchar(max)", nullable: true),
MimeType = table.Column<string>(type: "nvarchar(max)", nullable: true),
Changed = table.Column<bool>(type: "bit", nullable: false),
MD5 = table.Column<string>(type: "nvarchar(max)", nullable: true),
DiskState = table.Column<int>(type: "int", nullable: false),
LastCheck = table.Column<DateTime>(type: "datetime2", nullable: false),
FileContent = table.Column<byte[]>(type: "varbinary(max)", nullable: true)
},
@@ -75,6 +87,30 @@ namespace MP.FileData.Migrations
onDelete: ReferentialAction.Restrict);
});
migrationBuilder.CreateTable(
name: "FileModelTagModel",
columns: table => new
{
FilesFileId = table.Column<int>(type: "int", nullable: false),
TagsTagId = table.Column<string>(type: "nvarchar(450)", nullable: false)
},
constraints: table =>
{
table.PrimaryKey("PK_FileModelTagModel", x => new { x.FilesFileId, x.TagsTagId });
table.ForeignKey(
name: "FK_FileModelTagModel_Files_FilesFileId",
column: x => x.FilesFileId,
principalTable: "Files",
principalColumn: "FileId",
onDelete: ReferentialAction.Cascade);
table.ForeignKey(
name: "FK_FileModelTagModel_Tags_TagsTagId",
column: x => x.TagsTagId,
principalTable: "Tags",
principalColumn: "TagId",
onDelete: ReferentialAction.Cascade);
});
migrationBuilder.InsertData(
table: "Articoli",
columns: new[] { "CodArticolo", "DescArticolo", "Disegno", "Tipo" },
@@ -85,6 +121,11 @@ namespace MP.FileData.Migrations
columns: new[] { "IdxMacchina", "BasePath", "CodMacchina", "Descrizione", "ImgUrl", "Nome", "Note", "ShowOrder" },
values: new object[] { "0", "", "0", "--- Tutte ---", "", "--- Tutte ---", "", 0 });
migrationBuilder.CreateIndex(
name: "IX_FileModelTagModel_TagsTagId",
table: "FileModelTagModel",
column: "TagsTagId");
migrationBuilder.CreateIndex(
name: "IX_Files_CodArticolo",
table: "Files",
@@ -98,9 +139,15 @@ namespace MP.FileData.Migrations
protected override void Down(MigrationBuilder migrationBuilder)
{
migrationBuilder.DropTable(
name: "FileModelTagModel");
migrationBuilder.DropTable(
name: "Files");
migrationBuilder.DropTable(
name: "Tags");
migrationBuilder.DropTable(
name: "Articoli");
@@ -20,6 +20,21 @@ namespace MP.FileData.Migrations
.HasAnnotation("ProductVersion", "5.0.9")
.HasAnnotation("SqlServer:ValueGenerationStrategy", SqlServerValueGenerationStrategy.IdentityColumn);
modelBuilder.Entity("FileModelTagModel", b =>
{
b.Property<int>("FilesFileId")
.HasColumnType("int");
b.Property<string>("TagsTagId")
.HasColumnType("nvarchar(450)");
b.HasKey("FilesFileId", "TagsTagId");
b.HasIndex("TagsTagId");
b.ToTable("FileModelTagModel");
});
modelBuilder.Entity("MP.FileData.DatabaseModels.ArticoloModel", b =>
{
b.Property<string>("CodArticolo")
@@ -58,12 +73,12 @@ namespace MP.FileData.Migrations
b.Property<bool>("Active")
.HasColumnType("bit");
b.Property<bool>("Changed")
.HasColumnType("bit");
b.Property<string>("CodArticolo")
.HasColumnType("nvarchar(450)");
b.Property<int>("DiskState")
.HasColumnType("int");
b.Property<byte[]>("FileContent")
.HasColumnType("varbinary(max)");
@@ -76,6 +91,9 @@ namespace MP.FileData.Migrations
b.Property<DateTime>("LastMod")
.HasColumnType("datetime2");
b.Property<string>("MD5")
.HasColumnType("nvarchar(max)");
b.Property<string>("MimeType")
.HasColumnType("nvarchar(max)");
@@ -144,6 +162,31 @@ namespace MP.FileData.Migrations
});
});
modelBuilder.Entity("MP.FileData.DatabaseModels.TagModel", b =>
{
b.Property<string>("TagId")
.HasColumnType("nvarchar(450)");
b.HasKey("TagId");
b.ToTable("Tags");
});
modelBuilder.Entity("FileModelTagModel", b =>
{
b.HasOne("MP.FileData.DatabaseModels.FileModel", null)
.WithMany()
.HasForeignKey("FilesFileId")
.OnDelete(DeleteBehavior.Cascade)
.IsRequired();
b.HasOne("MP.FileData.DatabaseModels.TagModel", null)
.WithMany()
.HasForeignKey("TagsTagId")
.OnDelete(DeleteBehavior.Cascade)
.IsRequired();
});
modelBuilder.Entity("MP.FileData.DatabaseModels.FileModel", b =>
{
b.HasOne("MP.FileData.DatabaseModels.ArticoloModel", "Articolo")