Save DataSet
This commit is contained in:
@@ -53,7 +53,7 @@ namespace WebAPI.Controllers
|
||||
Subject = new ClaimsIdentity(new[]
|
||||
{
|
||||
new Claim("Id", Guid.NewGuid().ToString()),
|
||||
new Claim(JwtRegisteredClaimNames.Sub, user.UserName),
|
||||
new Claim(JwtRegisteredClaimNames.Sub, user.Id.ToString()),
|
||||
new Claim(JwtRegisteredClaimNames.Jti,
|
||||
Guid.NewGuid().ToString())
|
||||
}),
|
||||
|
||||
@@ -35,6 +35,19 @@ namespace WebAPI.Controllers
|
||||
}
|
||||
}
|
||||
[HttpPost]
|
||||
public IActionResult Save(DataSet input)
|
||||
{
|
||||
try
|
||||
{
|
||||
Request.Headers.TryGetValue("userId", out var value);
|
||||
Guid currentUserId = new Guid(value);
|
||||
return Ok(AddDataSet(input, currentUserId).Id);
|
||||
} catch(Exception e)
|
||||
{
|
||||
return BadRequest(e.ToString());
|
||||
}
|
||||
}
|
||||
[HttpPost]
|
||||
[DisableRequestSizeLimit]
|
||||
[Route("parseFile")]
|
||||
public IActionResult ParseFile()
|
||||
@@ -42,5 +55,42 @@ namespace WebAPI.Controllers
|
||||
var parser = new csvParser();
|
||||
return Ok(parser.parse(Request.Form.Files[0]));
|
||||
}
|
||||
|
||||
//
|
||||
private DataSet AddDataSet(DataSet input, Guid currentUserId)
|
||||
{
|
||||
input.Number = db.DataSets.Count() + 1;
|
||||
input.CreatedById = currentUserId;
|
||||
input.ModifiedById = currentUserId;
|
||||
input.CreatedAt = DateTime.UtcNow;
|
||||
input.ModifiedAt = DateTime.UtcNow;
|
||||
|
||||
db.DataSets.Add(input);
|
||||
SaveDataRows(input.Id, input.DataRows, currentUserId);
|
||||
db.SaveChanges();
|
||||
return input;
|
||||
}
|
||||
|
||||
private void SaveDataRows(Guid id, ICollection<Models.DataRow> dataRows, Guid currentUserId)
|
||||
{
|
||||
try
|
||||
{
|
||||
List<Guid> ids = new List<Guid>();
|
||||
foreach (Models.DataRow dataRow in dataRows)
|
||||
{
|
||||
dataRow.CreatedById = currentUserId;
|
||||
dataRow.CreatedAt = DateTime.UtcNow;
|
||||
dataRow.ModifiedById = currentUserId;
|
||||
dataRow.ModifiedAt = DateTime.UtcNow;
|
||||
dataRow.DataSetId= id;
|
||||
|
||||
db.DataRows.Add(dataRow);
|
||||
}
|
||||
}
|
||||
catch (Exception)
|
||||
{
|
||||
throw;
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
199
WebAPI/Migrations/20221221165749_DataSetIdOnDataRow.Designer.cs
generated
Normal file
199
WebAPI/Migrations/20221221165749_DataSetIdOnDataRow.Designer.cs
generated
Normal file
@@ -0,0 +1,199 @@
|
||||
// <auto-generated />
|
||||
using System;
|
||||
using Microsoft.EntityFrameworkCore;
|
||||
using Microsoft.EntityFrameworkCore.Infrastructure;
|
||||
using Microsoft.EntityFrameworkCore.Metadata;
|
||||
using Microsoft.EntityFrameworkCore.Migrations;
|
||||
using Microsoft.EntityFrameworkCore.Storage.ValueConversion;
|
||||
using WebAPI;
|
||||
|
||||
#nullable disable
|
||||
|
||||
namespace WebAPI.Migrations
|
||||
{
|
||||
[DbContext(typeof(AppDbContext))]
|
||||
[Migration("20221221165749_DataSetIdOnDataRow")]
|
||||
partial class DataSetIdOnDataRow
|
||||
{
|
||||
/// <inheritdoc />
|
||||
protected override void BuildTargetModel(ModelBuilder modelBuilder)
|
||||
{
|
||||
#pragma warning disable 612, 618
|
||||
modelBuilder
|
||||
.HasAnnotation("ProductVersion", "7.0.0")
|
||||
.HasAnnotation("Relational:MaxIdentifierLength", 128);
|
||||
|
||||
SqlServerModelBuilderExtensions.UseIdentityColumns(modelBuilder);
|
||||
|
||||
modelBuilder.Entity("WebAPI.Models.DataRow", b =>
|
||||
{
|
||||
b.Property<Guid>("Id")
|
||||
.ValueGeneratedOnAdd()
|
||||
.HasColumnType("uniqueidentifier");
|
||||
|
||||
b.Property<string>("Code")
|
||||
.IsRequired()
|
||||
.HasColumnType("nvarchar(max)");
|
||||
|
||||
b.Property<DateTime>("CreatedAt")
|
||||
.HasColumnType("datetime2");
|
||||
|
||||
b.Property<Guid>("CreatedById")
|
||||
.HasColumnType("uniqueidentifier");
|
||||
|
||||
b.Property<Guid>("DataSetId")
|
||||
.HasColumnType("uniqueidentifier");
|
||||
|
||||
b.Property<string>("Desc1")
|
||||
.HasColumnType("nvarchar(max)");
|
||||
|
||||
b.Property<string>("Desc2")
|
||||
.HasColumnType("nvarchar(max)");
|
||||
|
||||
b.Property<string>("Desc3")
|
||||
.HasColumnType("nvarchar(max)");
|
||||
|
||||
b.Property<string>("Desc4")
|
||||
.HasColumnType("nvarchar(max)");
|
||||
|
||||
b.Property<string>("Desc5")
|
||||
.HasColumnType("nvarchar(max)");
|
||||
|
||||
b.Property<bool>("IsDeleted")
|
||||
.HasColumnType("bit");
|
||||
|
||||
b.Property<DateTime>("ModifiedAt")
|
||||
.HasColumnType("datetime2");
|
||||
|
||||
b.Property<Guid>("ModifiedById")
|
||||
.HasColumnType("uniqueidentifier");
|
||||
|
||||
b.Property<float>("Value")
|
||||
.HasColumnType("real");
|
||||
|
||||
b.HasKey("Id");
|
||||
|
||||
b.HasIndex("CreatedById");
|
||||
|
||||
b.HasIndex("DataSetId");
|
||||
|
||||
b.HasIndex("ModifiedById");
|
||||
|
||||
b.ToTable("DataRows");
|
||||
});
|
||||
|
||||
modelBuilder.Entity("WebAPI.Models.DataSet", b =>
|
||||
{
|
||||
b.Property<Guid>("Id")
|
||||
.ValueGeneratedOnAdd()
|
||||
.HasColumnType("uniqueidentifier");
|
||||
|
||||
b.Property<DateTime>("CreatedAt")
|
||||
.HasColumnType("datetime2");
|
||||
|
||||
b.Property<Guid>("CreatedById")
|
||||
.HasColumnType("uniqueidentifier");
|
||||
|
||||
b.Property<bool>("IsDeleted")
|
||||
.HasColumnType("bit");
|
||||
|
||||
b.Property<DateTime>("ModifiedAt")
|
||||
.HasColumnType("datetime2");
|
||||
|
||||
b.Property<Guid>("ModifiedById")
|
||||
.HasColumnType("uniqueidentifier");
|
||||
|
||||
b.Property<string>("Name")
|
||||
.IsRequired()
|
||||
.HasColumnType("nvarchar(max)");
|
||||
|
||||
b.Property<int?>("Number")
|
||||
.IsRequired()
|
||||
.HasColumnType("int");
|
||||
|
||||
b.Property<string>("Source")
|
||||
.IsRequired()
|
||||
.HasColumnType("nvarchar(max)");
|
||||
|
||||
b.HasKey("Id");
|
||||
|
||||
b.HasIndex("CreatedById");
|
||||
|
||||
b.HasIndex("ModifiedById");
|
||||
|
||||
b.ToTable("DataSets");
|
||||
});
|
||||
|
||||
modelBuilder.Entity("WebAPI.Models.User", b =>
|
||||
{
|
||||
b.Property<Guid>("Id")
|
||||
.ValueGeneratedOnAdd()
|
||||
.HasColumnType("uniqueidentifier");
|
||||
|
||||
b.Property<DateTime>("CreatedAt")
|
||||
.HasColumnType("datetime2");
|
||||
|
||||
b.Property<string>("Email")
|
||||
.HasColumnType("nvarchar(max)");
|
||||
|
||||
b.Property<string>("UserName")
|
||||
.HasMaxLength(50)
|
||||
.HasColumnType("nvarchar(50)");
|
||||
|
||||
b.HasKey("Id");
|
||||
|
||||
b.ToTable("Users");
|
||||
});
|
||||
|
||||
modelBuilder.Entity("WebAPI.Models.DataRow", b =>
|
||||
{
|
||||
b.HasOne("WebAPI.Models.User", "CreatedBy")
|
||||
.WithMany()
|
||||
.HasForeignKey("CreatedById")
|
||||
.OnDelete(DeleteBehavior.Cascade)
|
||||
.IsRequired();
|
||||
|
||||
b.HasOne("WebAPI.Models.DataSet", null)
|
||||
.WithMany("DataRows")
|
||||
.HasForeignKey("DataSetId")
|
||||
.OnDelete(DeleteBehavior.Cascade)
|
||||
.IsRequired();
|
||||
|
||||
b.HasOne("WebAPI.Models.User", "ModifiedBy")
|
||||
.WithMany()
|
||||
.HasForeignKey("ModifiedById")
|
||||
.OnDelete(DeleteBehavior.Cascade)
|
||||
.IsRequired();
|
||||
|
||||
b.Navigation("CreatedBy");
|
||||
|
||||
b.Navigation("ModifiedBy");
|
||||
});
|
||||
|
||||
modelBuilder.Entity("WebAPI.Models.DataSet", b =>
|
||||
{
|
||||
b.HasOne("WebAPI.Models.User", "CreatedBy")
|
||||
.WithMany()
|
||||
.HasForeignKey("CreatedById")
|
||||
.OnDelete(DeleteBehavior.Cascade)
|
||||
.IsRequired();
|
||||
|
||||
b.HasOne("WebAPI.Models.User", "ModifiedBy")
|
||||
.WithMany()
|
||||
.HasForeignKey("ModifiedById")
|
||||
.OnDelete(DeleteBehavior.Cascade)
|
||||
.IsRequired();
|
||||
|
||||
b.Navigation("CreatedBy");
|
||||
|
||||
b.Navigation("ModifiedBy");
|
||||
});
|
||||
|
||||
modelBuilder.Entity("WebAPI.Models.DataSet", b =>
|
||||
{
|
||||
b.Navigation("DataRows");
|
||||
});
|
||||
#pragma warning restore 612, 618
|
||||
}
|
||||
}
|
||||
}
|
||||
60
WebAPI/Migrations/20221221165749_DataSetIdOnDataRow.cs
Normal file
60
WebAPI/Migrations/20221221165749_DataSetIdOnDataRow.cs
Normal file
@@ -0,0 +1,60 @@
|
||||
using System;
|
||||
using Microsoft.EntityFrameworkCore.Migrations;
|
||||
|
||||
#nullable disable
|
||||
|
||||
namespace WebAPI.Migrations
|
||||
{
|
||||
/// <inheritdoc />
|
||||
public partial class DataSetIdOnDataRow : Migration
|
||||
{
|
||||
/// <inheritdoc />
|
||||
protected override void Up(MigrationBuilder migrationBuilder)
|
||||
{
|
||||
migrationBuilder.DropForeignKey(
|
||||
name: "FK_DataRows_DataSets_DataSetId",
|
||||
table: "DataRows");
|
||||
|
||||
migrationBuilder.AlterColumn<Guid>(
|
||||
name: "DataSetId",
|
||||
table: "DataRows",
|
||||
type: "uniqueidentifier",
|
||||
nullable: false,
|
||||
defaultValue: new Guid("00000000-0000-0000-0000-000000000000"),
|
||||
oldClrType: typeof(Guid),
|
||||
oldType: "uniqueidentifier",
|
||||
oldNullable: true);
|
||||
|
||||
migrationBuilder.AddForeignKey(
|
||||
name: "FK_DataRows_DataSets_DataSetId",
|
||||
table: "DataRows",
|
||||
column: "DataSetId",
|
||||
principalTable: "DataSets",
|
||||
principalColumn: "Id",
|
||||
onDelete: ReferentialAction.Cascade);
|
||||
}
|
||||
|
||||
/// <inheritdoc />
|
||||
protected override void Down(MigrationBuilder migrationBuilder)
|
||||
{
|
||||
migrationBuilder.DropForeignKey(
|
||||
name: "FK_DataRows_DataSets_DataSetId",
|
||||
table: "DataRows");
|
||||
|
||||
migrationBuilder.AlterColumn<Guid>(
|
||||
name: "DataSetId",
|
||||
table: "DataRows",
|
||||
type: "uniqueidentifier",
|
||||
nullable: true,
|
||||
oldClrType: typeof(Guid),
|
||||
oldType: "uniqueidentifier");
|
||||
|
||||
migrationBuilder.AddForeignKey(
|
||||
name: "FK_DataRows_DataSets_DataSetId",
|
||||
table: "DataRows",
|
||||
column: "DataSetId",
|
||||
principalTable: "DataSets",
|
||||
principalColumn: "Id");
|
||||
}
|
||||
}
|
||||
}
|
||||
@@ -38,7 +38,7 @@ namespace WebAPI.Migrations
|
||||
b.Property<Guid>("CreatedById")
|
||||
.HasColumnType("uniqueidentifier");
|
||||
|
||||
b.Property<Guid?>("DataSetId")
|
||||
b.Property<Guid>("DataSetId")
|
||||
.HasColumnType("uniqueidentifier");
|
||||
|
||||
b.Property<string>("Desc1")
|
||||
@@ -152,7 +152,9 @@ namespace WebAPI.Migrations
|
||||
|
||||
b.HasOne("WebAPI.Models.DataSet", null)
|
||||
.WithMany("DataRows")
|
||||
.HasForeignKey("DataSetId");
|
||||
.HasForeignKey("DataSetId")
|
||||
.OnDelete(DeleteBehavior.Cascade)
|
||||
.IsRequired();
|
||||
|
||||
b.HasOne("WebAPI.Models.User", "ModifiedBy")
|
||||
.WithMany()
|
||||
|
||||
@@ -28,6 +28,7 @@ namespace WebAPI.Models
|
||||
[Required]
|
||||
public Guid ModifiedById { get; set; }
|
||||
public User? ModifiedBy { get; set; }
|
||||
public Guid DataSetId { get; set; }
|
||||
#endregion
|
||||
}
|
||||
}
|
||||
|
||||
@@ -1,6 +1,8 @@
|
||||
using Microsoft.AspNetCore.Authentication.JwtBearer;
|
||||
using Microsoft.EntityFrameworkCore;
|
||||
using Microsoft.IdentityModel.Tokens;
|
||||
using Newtonsoft.Json.Linq;
|
||||
using System.IdentityModel.Tokens.Jwt;
|
||||
using System.Text;
|
||||
using WebAPI;
|
||||
|
||||
@@ -42,6 +44,7 @@ builder.Services.AddAuthentication(options =>
|
||||
ValidateIssuerSigningKey = true,
|
||||
IssuerSigningKey = new SymmetricSecurityKey(Encoding.UTF8.GetBytes(builder.Configuration["Secret"]))
|
||||
};
|
||||
|
||||
});
|
||||
builder.Services.AddAuthorization();
|
||||
|
||||
@@ -54,7 +57,12 @@ var app = builder.Build();
|
||||
|
||||
app.Use(async (context, next) =>
|
||||
{
|
||||
context.Request.Headers.TryGetValue("Authorization", out var auth);
|
||||
string token = context.Request.Headers["Authorization"].ToString();
|
||||
if (token.Length > 0) {
|
||||
var handler = new JwtSecurityTokenHandler();
|
||||
var data = handler.ReadJwtToken(token.Split(' ')[1]);
|
||||
context.Request.Headers.Add("UserId", new Microsoft.Extensions.Primitives.StringValues(data.Subject));
|
||||
}
|
||||
await next(context);
|
||||
});
|
||||
|
||||
|
||||
@@ -37,6 +37,8 @@ namespace WebAPI.dataParsers
|
||||
dataRow.Code = data[0][i];
|
||||
dataRow.Desc1 = data[j][0];
|
||||
dataRow.Value = value;
|
||||
dataRow.CreatedAt = DateTime.UtcNow;
|
||||
dataRow.ModifiedAt= DateTime.UtcNow;
|
||||
dataRows.Add(dataRow);
|
||||
}
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user