From eb07c31ff65dd9bb91009d42ade3a82feb32aea9 Mon Sep 17 00:00:00 2001 From: uppuv Date: Fri, 18 Aug 2023 17:14:41 -0400 Subject: [PATCH 1/3] Database integration --- .../DamageAssesment.Api.Answers.csproj | 5 + .../Db/AnswerDbContext.cs | 12 +- .../20230816214724_InitialCreate.Designer.cs | 58 +++++++ .../20230816214724_InitialCreate.cs | 37 +++++ .../AnswerDbContextModelSnapshot.cs | 55 +++++++ .../DamageAssesment.Api.Answers/Program.cs | 2 +- .../Providers/AnswerProvider.cs | 12 +- .../appsettings.json | 5 +- .../DamageAssesment.Api.Attachments.csproj | 15 +- .../Db/AttachmentsDbContext.cs | 10 +- ...230817212256_InitialAttachment.Designer.cs | 60 ++++++++ .../20230817212256_InitialAttachment.cs | 38 +++++ .../AttachmentsDbContextModelSnapshot.cs | 57 +++++++ .../Program.cs | 2 +- .../appsettings.json | 3 + .../DamageAssesment.Api.Employees.csproj | 13 +- .../Db/EmployeeDbContext.cs | 13 +- ...20230817213656_InitialEmployee.Designer.cs | 64 ++++++++ .../20230817213656_InitialEmployee.cs | 39 +++++ .../EmployeeDbContextModelSnapshot.cs | 61 ++++++++ .../DamageAssesment.Api.Employees/Program.cs | 2 +- .../appsettings.json | 3 + .../LocationsServiceTest.cs | 125 --------------- .../DamageAssesment.Api.Locations.csproj | 13 +- .../Db/LocationDbContext.cs | 14 +- ...20230817214454_InitialLocation.Designer.cs | 80 ++++++++++ .../20230817214454_InitialLocation.cs | 52 +++++++ .../LocationDbContextModelSnapshot.cs | 77 ++++++++++ .../DamageAssesment.Api.Locations/Program.cs | 2 +- .../appsettings.json | 5 +- .../DamageAssesment.Api.Questions.csproj | 13 +- .../Db/QuestionDbContext.cs | 14 +- ...20230817215744_InitialQuestion.Designer.cs | 143 ++++++++++++++++++ .../20230817215744_InitialQuestion.cs | 103 +++++++++++++ .../QuestionDbContextModelSnapshot.cs | 140 +++++++++++++++++ .../DamageAssesment.Api.Questions/Program.cs | 2 +- .../Providers/QuestionsProvider.cs | 55 ++++--- .../appsettings.json | 5 +- .../DamageAssesment.Api.Questions.Test.csproj | 2 +- ...DamageAssesment.Api.SurveyResponses.csproj | 15 +- .../Db/SurveyResponseDbContext.cs | 13 +- ...17221348_InitialSurveyResponse.Designer.cs | 55 +++++++ .../20230817221348_InitialSurveyResponse.cs | 36 +++++ .../SurveyResponseDbContextModelSnapshot.cs | 52 +++++++ .../Program.cs | 2 +- .../Providers/SurveyResponsesProvider.cs | 14 +- .../appsettings.json | 3 + .../DamageAssesment.Api.Survey.csproj | 13 +- .../Db/SurveysDbContext.cs | 10 +- .../20230817220614_InitialSurvey.Designer.cs | 57 +++++++ .../20230817220614_InitialSurvey.cs | 38 +++++ .../SurveysDbContextModelSnapshot.cs | 54 +++++++ .../DamageAssesment.Api.Surveys/Program.cs | 2 +- .../Providers/SurveysProvider.cs | 6 +- .../appsettings.json | 5 +- 55 files changed, 1568 insertions(+), 218 deletions(-) create mode 100644 DamageAssesmentApi/DamageAssesment.Api.Answers/Migrations/20230816214724_InitialCreate.Designer.cs create mode 100644 DamageAssesmentApi/DamageAssesment.Api.Answers/Migrations/20230816214724_InitialCreate.cs create mode 100644 DamageAssesmentApi/DamageAssesment.Api.Answers/Migrations/AnswerDbContextModelSnapshot.cs create mode 100644 DamageAssesmentApi/DamageAssesment.Api.Attachments/Migrations/20230817212256_InitialAttachment.Designer.cs create mode 100644 DamageAssesmentApi/DamageAssesment.Api.Attachments/Migrations/20230817212256_InitialAttachment.cs create mode 100644 DamageAssesmentApi/DamageAssesment.Api.Attachments/Migrations/AttachmentsDbContextModelSnapshot.cs create mode 100644 DamageAssesmentApi/DamageAssesment.Api.Employees/Migrations/20230817213656_InitialEmployee.Designer.cs create mode 100644 DamageAssesmentApi/DamageAssesment.Api.Employees/Migrations/20230817213656_InitialEmployee.cs create mode 100644 DamageAssesmentApi/DamageAssesment.Api.Employees/Migrations/EmployeeDbContextModelSnapshot.cs create mode 100644 DamageAssesmentApi/DamageAssesment.Api.Locations/Migrations/20230817214454_InitialLocation.Designer.cs create mode 100644 DamageAssesmentApi/DamageAssesment.Api.Locations/Migrations/20230817214454_InitialLocation.cs create mode 100644 DamageAssesmentApi/DamageAssesment.Api.Locations/Migrations/LocationDbContextModelSnapshot.cs create mode 100644 DamageAssesmentApi/DamageAssesment.Api.Questions/Migrations/20230817215744_InitialQuestion.Designer.cs create mode 100644 DamageAssesmentApi/DamageAssesment.Api.Questions/Migrations/20230817215744_InitialQuestion.cs create mode 100644 DamageAssesmentApi/DamageAssesment.Api.Questions/Migrations/QuestionDbContextModelSnapshot.cs create mode 100644 DamageAssesmentApi/DamageAssesment.Api.SurveyResponses/Migrations/20230817221348_InitialSurveyResponse.Designer.cs create mode 100644 DamageAssesmentApi/DamageAssesment.Api.SurveyResponses/Migrations/20230817221348_InitialSurveyResponse.cs create mode 100644 DamageAssesmentApi/DamageAssesment.Api.SurveyResponses/Migrations/SurveyResponseDbContextModelSnapshot.cs create mode 100644 DamageAssesmentApi/DamageAssesment.Api.Surveys/Migrations/20230817220614_InitialSurvey.Designer.cs create mode 100644 DamageAssesmentApi/DamageAssesment.Api.Surveys/Migrations/20230817220614_InitialSurvey.cs create mode 100644 DamageAssesmentApi/DamageAssesment.Api.Surveys/Migrations/SurveysDbContextModelSnapshot.cs diff --git a/DamageAssesmentApi/DamageAssesment.Api.Answers/DamageAssesment.Api.Answers.csproj b/DamageAssesmentApi/DamageAssesment.Api.Answers/DamageAssesment.Api.Answers.csproj index ca5ee38..8d98ce2 100644 --- a/DamageAssesmentApi/DamageAssesment.Api.Answers/DamageAssesment.Api.Answers.csproj +++ b/DamageAssesmentApi/DamageAssesment.Api.Answers/DamageAssesment.Api.Answers.csproj @@ -9,7 +9,12 @@ + + all + runtime; build; native; contentfiles; analyzers; buildtransitive + + diff --git a/DamageAssesmentApi/DamageAssesment.Api.Answers/Db/AnswerDbContext.cs b/DamageAssesmentApi/DamageAssesment.Api.Answers/Db/AnswerDbContext.cs index 8d3a8ef..e148ac7 100644 --- a/DamageAssesmentApi/DamageAssesment.Api.Answers/Db/AnswerDbContext.cs +++ b/DamageAssesmentApi/DamageAssesment.Api.Answers/Db/AnswerDbContext.cs @@ -1,13 +1,19 @@ using Microsoft.EntityFrameworkCore; +using Microsoft.Extensions.Configuration; namespace DamageAssesment.Api.Answers.Db { public class AnswerDbContext:DbContext { - - public AnswerDbContext(DbContextOptions options):base(options) + private IConfiguration _Configuration { get; set; } + public AnswerDbContext(DbContextOptions options,IConfiguration configuration):base(options) { - + _Configuration= configuration; + } + protected override void OnConfiguring(DbContextOptionsBuilder options) + { + // connect to sql server with connection string from app settings + options.UseSqlServer(_Configuration.GetConnectionString("AnswerConnection")); } public DbSet Answers { get; set; } diff --git a/DamageAssesmentApi/DamageAssesment.Api.Answers/Migrations/20230816214724_InitialCreate.Designer.cs b/DamageAssesmentApi/DamageAssesment.Api.Answers/Migrations/20230816214724_InitialCreate.Designer.cs new file mode 100644 index 0000000..cd62640 --- /dev/null +++ b/DamageAssesmentApi/DamageAssesment.Api.Answers/Migrations/20230816214724_InitialCreate.Designer.cs @@ -0,0 +1,58 @@ +// +using System; +using DamageAssesment.Api.Answers.Db; +using Microsoft.EntityFrameworkCore; +using Microsoft.EntityFrameworkCore.Infrastructure; +using Microsoft.EntityFrameworkCore.Metadata; +using Microsoft.EntityFrameworkCore.Migrations; +using Microsoft.EntityFrameworkCore.Storage.ValueConversion; + +#nullable disable + +namespace DamageAssesment.Api.Answers.Migrations +{ + [DbContext(typeof(AnswerDbContext))] + [Migration("20230816214724_InitialCreate")] + partial class InitialCreate + { + /// + protected override void BuildTargetModel(ModelBuilder modelBuilder) + { +#pragma warning disable 612, 618 + modelBuilder + .HasAnnotation("ProductVersion", "7.0.9") + .HasAnnotation("Relational:MaxIdentifierLength", 128); + + SqlServerModelBuilderExtensions.UseIdentityColumns(modelBuilder); + + modelBuilder.Entity("DamageAssesment.Api.Answers.Db.Answer", b => + { + b.Property("Id") + .ValueGeneratedOnAdd() + .HasColumnType("int"); + + SqlServerPropertyBuilderExtensions.UseIdentityColumn(b.Property("Id")); + + b.Property("AnswerText") + .IsRequired() + .HasMaxLength(250) + .HasColumnType("nvarchar(250)"); + + b.Property("Comment") + .IsRequired() + .HasColumnType("nvarchar(max)"); + + b.Property("QuestionId") + .HasColumnType("int"); + + b.Property("SurveyResponseId") + .HasColumnType("int"); + + b.HasKey("Id"); + + b.ToTable("Answers"); + }); +#pragma warning restore 612, 618 + } + } +} diff --git a/DamageAssesmentApi/DamageAssesment.Api.Answers/Migrations/20230816214724_InitialCreate.cs b/DamageAssesmentApi/DamageAssesment.Api.Answers/Migrations/20230816214724_InitialCreate.cs new file mode 100644 index 0000000..ff519b4 --- /dev/null +++ b/DamageAssesmentApi/DamageAssesment.Api.Answers/Migrations/20230816214724_InitialCreate.cs @@ -0,0 +1,37 @@ +using Microsoft.EntityFrameworkCore.Migrations; + +#nullable disable + +namespace DamageAssesment.Api.Answers.Migrations +{ + /// + public partial class InitialCreate : Migration + { + /// + protected override void Up(MigrationBuilder migrationBuilder) + { + migrationBuilder.CreateTable( + name: "Answers", + columns: table => new + { + Id = table.Column(type: "int", nullable: false) + .Annotation("SqlServer:Identity", "1, 1"), + QuestionId = table.Column(type: "int", nullable: false), + AnswerText = table.Column(type: "nvarchar(250)", maxLength: 250, nullable: false), + Comment = table.Column(type: "nvarchar(max)", nullable: false), + SurveyResponseId = table.Column(type: "int", nullable: true) + }, + constraints: table => + { + table.PrimaryKey("PK_Answers", x => x.Id); + }); + } + + /// + protected override void Down(MigrationBuilder migrationBuilder) + { + migrationBuilder.DropTable( + name: "Answers"); + } + } +} diff --git a/DamageAssesmentApi/DamageAssesment.Api.Answers/Migrations/AnswerDbContextModelSnapshot.cs b/DamageAssesmentApi/DamageAssesment.Api.Answers/Migrations/AnswerDbContextModelSnapshot.cs new file mode 100644 index 0000000..02d3df3 --- /dev/null +++ b/DamageAssesmentApi/DamageAssesment.Api.Answers/Migrations/AnswerDbContextModelSnapshot.cs @@ -0,0 +1,55 @@ +// +using System; +using DamageAssesment.Api.Answers.Db; +using Microsoft.EntityFrameworkCore; +using Microsoft.EntityFrameworkCore.Infrastructure; +using Microsoft.EntityFrameworkCore.Metadata; +using Microsoft.EntityFrameworkCore.Storage.ValueConversion; + +#nullable disable + +namespace DamageAssesment.Api.Answers.Migrations +{ + [DbContext(typeof(AnswerDbContext))] + partial class AnswerDbContextModelSnapshot : ModelSnapshot + { + protected override void BuildModel(ModelBuilder modelBuilder) + { +#pragma warning disable 612, 618 + modelBuilder + .HasAnnotation("ProductVersion", "7.0.9") + .HasAnnotation("Relational:MaxIdentifierLength", 128); + + SqlServerModelBuilderExtensions.UseIdentityColumns(modelBuilder); + + modelBuilder.Entity("DamageAssesment.Api.Answers.Db.Answer", b => + { + b.Property("Id") + .ValueGeneratedOnAdd() + .HasColumnType("int"); + + SqlServerPropertyBuilderExtensions.UseIdentityColumn(b.Property("Id")); + + b.Property("AnswerText") + .IsRequired() + .HasMaxLength(250) + .HasColumnType("nvarchar(250)"); + + b.Property("Comment") + .IsRequired() + .HasColumnType("nvarchar(max)"); + + b.Property("QuestionId") + .HasColumnType("int"); + + b.Property("SurveyResponseId") + .HasColumnType("int"); + + b.HasKey("Id"); + + b.ToTable("Answers"); + }); +#pragma warning restore 612, 618 + } + } +} diff --git a/DamageAssesmentApi/DamageAssesment.Api.Answers/Program.cs b/DamageAssesmentApi/DamageAssesment.Api.Answers/Program.cs index 7630089..e6f66a0 100644 --- a/DamageAssesmentApi/DamageAssesment.Api.Answers/Program.cs +++ b/DamageAssesmentApi/DamageAssesment.Api.Answers/Program.cs @@ -15,7 +15,7 @@ builder.Services.AddScoped(); builder.Services.AddAutoMapper(AppDomain.CurrentDomain.GetAssemblies()); //4/30 builder.Services.AddDbContext(option => { - option.UseInMemoryDatabase("Answers"); + option.UseSqlServer("AnswerConnection"); }); var app = builder.Build(); diff --git a/DamageAssesmentApi/DamageAssesment.Api.Answers/Providers/AnswerProvider.cs b/DamageAssesmentApi/DamageAssesment.Api.Answers/Providers/AnswerProvider.cs index b1851d8..ebbab54 100644 --- a/DamageAssesmentApi/DamageAssesment.Api.Answers/Providers/AnswerProvider.cs +++ b/DamageAssesmentApi/DamageAssesment.Api.Answers/Providers/AnswerProvider.cs @@ -191,12 +191,12 @@ namespace DamageAssesment.Api.Answers.Providers { if (!answerDbContext.Answers.Any()) { - answerDbContext.Answers.Add(new Db.Answer() { Id = 1, AnswerText = "Yes", Comment = "", QuestionId = 1, SurveyResponseId = 1 }); - answerDbContext.Answers.Add(new Db.Answer() { Id = 2, AnswerText = "Yes", Comment = "myComment", QuestionId = 2, SurveyResponseId = 1 }); - answerDbContext.Answers.Add(new Db.Answer() { Id = 3, AnswerText = "No", Comment = "No Comment", QuestionId = 3, SurveyResponseId = 1 }); - answerDbContext.Answers.Add(new Db.Answer() { Id = 4, AnswerText = "Yes", Comment = "No Comment", QuestionId = 1, SurveyResponseId = 2 }); - answerDbContext.Answers.Add(new Db.Answer() { Id = 5, AnswerText = "No", Comment = "No Comment", QuestionId = 2, SurveyResponseId = 2 }); - answerDbContext.Answers.Add(new Db.Answer() { Id = 6, AnswerText = "No", Comment = "No Comment", QuestionId = 3, SurveyResponseId = 2 }); + answerDbContext.Answers.Add(new Db.Answer() { AnswerText = "Yes", Comment = "", QuestionId = 1, SurveyResponseId = 1 }); + answerDbContext.Answers.Add(new Db.Answer() { AnswerText = "Yes", Comment = "myComment", QuestionId = 2, SurveyResponseId = 1 }); + answerDbContext.Answers.Add(new Db.Answer() { AnswerText = "No", Comment = "No Comment", QuestionId = 3, SurveyResponseId = 1 }); + answerDbContext.Answers.Add(new Db.Answer() { AnswerText = "Yes", Comment = "No Comment", QuestionId = 1, SurveyResponseId = 2 }); + answerDbContext.Answers.Add(new Db.Answer() { AnswerText = "No", Comment = "No Comment", QuestionId = 2, SurveyResponseId = 2 }); + answerDbContext.Answers.Add(new Db.Answer() { AnswerText = "No", Comment = "No Comment", QuestionId = 3, SurveyResponseId = 2 }); answerDbContext.SaveChanges(); } diff --git a/DamageAssesmentApi/DamageAssesment.Api.Answers/appsettings.json b/DamageAssesmentApi/DamageAssesment.Api.Answers/appsettings.json index 10f68b8..1dc9826 100644 --- a/DamageAssesmentApi/DamageAssesment.Api.Answers/appsettings.json +++ b/DamageAssesmentApi/DamageAssesment.Api.Answers/appsettings.json @@ -5,5 +5,8 @@ "Microsoft.AspNetCore": "Warning" } }, - "AllowedHosts": "*" + "AllowedHosts": "*", + "ConnectionStrings": { + "AnswerConnection": "Server=DESKTOP-OF5DPLQ\\SQLEXPRESS;Database=da_survey_dev;Trusted_Connection=True;TrustServerCertificate=True;" + } } diff --git a/DamageAssesmentApi/DamageAssesment.Api.Attachments/DamageAssesment.Api.Attachments.csproj b/DamageAssesmentApi/DamageAssesment.Api.Attachments/DamageAssesment.Api.Attachments.csproj index 3454bff..7ce43f2 100644 --- a/DamageAssesmentApi/DamageAssesment.Api.Attachments/DamageAssesment.Api.Attachments.csproj +++ b/DamageAssesmentApi/DamageAssesment.Api.Attachments/DamageAssesment.Api.Attachments.csproj @@ -9,9 +9,18 @@ - - - + + + + all + runtime; build; native; contentfiles; analyzers; buildtransitive + + + + + all + runtime; build; native; contentfiles; analyzers; buildtransitive + diff --git a/DamageAssesmentApi/DamageAssesment.Api.Attachments/Db/AttachmentsDbContext.cs b/DamageAssesmentApi/DamageAssesment.Api.Attachments/Db/AttachmentsDbContext.cs index 561377d..61af67f 100644 --- a/DamageAssesmentApi/DamageAssesment.Api.Attachments/Db/AttachmentsDbContext.cs +++ b/DamageAssesmentApi/DamageAssesment.Api.Attachments/Db/AttachmentsDbContext.cs @@ -1,11 +1,19 @@ using Microsoft.EntityFrameworkCore; +using Microsoft.Extensions.Configuration; namespace DamageAssesment.Api.Attachments.Db { public class AttachmentsDbContext:DbContext { - public AttachmentsDbContext(DbContextOptions options) : base(options) + private IConfiguration _Configuration { get; set; } + public AttachmentsDbContext(DbContextOptions options, IConfiguration configuration) : base(options) { + _Configuration = configuration; + } + protected override void OnConfiguring(DbContextOptionsBuilder options) + { + // connect to sql server with connection string from app settings + options.UseSqlServer(_Configuration.GetConnectionString("AttachmentConnection")); } public DbSet Attachments { get; set; } } diff --git a/DamageAssesmentApi/DamageAssesment.Api.Attachments/Migrations/20230817212256_InitialAttachment.Designer.cs b/DamageAssesmentApi/DamageAssesment.Api.Attachments/Migrations/20230817212256_InitialAttachment.Designer.cs new file mode 100644 index 0000000..ae47dfd --- /dev/null +++ b/DamageAssesmentApi/DamageAssesment.Api.Attachments/Migrations/20230817212256_InitialAttachment.Designer.cs @@ -0,0 +1,60 @@ +// +using System; +using DamageAssesment.Api.Attachments.Db; +using Microsoft.EntityFrameworkCore; +using Microsoft.EntityFrameworkCore.Infrastructure; +using Microsoft.EntityFrameworkCore.Metadata; +using Microsoft.EntityFrameworkCore.Migrations; +using Microsoft.EntityFrameworkCore.Storage.ValueConversion; + +#nullable disable + +namespace DamageAssesment.Api.Attachments.Migrations +{ + [DbContext(typeof(AttachmentsDbContext))] + [Migration("20230817212256_InitialAttachment")] + partial class InitialAttachment + { + /// + protected override void BuildTargetModel(ModelBuilder modelBuilder) + { +#pragma warning disable 612, 618 + modelBuilder + .HasAnnotation("ProductVersion", "7.0.9") + .HasAnnotation("Relational:MaxIdentifierLength", 128); + + SqlServerModelBuilderExtensions.UseIdentityColumns(modelBuilder); + + modelBuilder.Entity("DamageAssesment.Api.Attachments.Db.Attachment", b => + { + b.Property("Id") + .ValueGeneratedOnAdd() + .HasColumnType("int"); + + SqlServerPropertyBuilderExtensions.UseIdentityColumn(b.Property("Id")); + + b.Property("AnswerId") + .HasColumnType("int"); + + b.Property("FileName") + .IsRequired() + .HasColumnType("nvarchar(max)"); + + b.Property("IsDeleted") + .HasColumnType("bit"); + + b.Property("ResponseId") + .HasColumnType("int"); + + b.Property("URI") + .IsRequired() + .HasColumnType("nvarchar(max)"); + + b.HasKey("Id"); + + b.ToTable("Attachments"); + }); +#pragma warning restore 612, 618 + } + } +} diff --git a/DamageAssesmentApi/DamageAssesment.Api.Attachments/Migrations/20230817212256_InitialAttachment.cs b/DamageAssesmentApi/DamageAssesment.Api.Attachments/Migrations/20230817212256_InitialAttachment.cs new file mode 100644 index 0000000..2b260ed --- /dev/null +++ b/DamageAssesmentApi/DamageAssesment.Api.Attachments/Migrations/20230817212256_InitialAttachment.cs @@ -0,0 +1,38 @@ +using Microsoft.EntityFrameworkCore.Migrations; + +#nullable disable + +namespace DamageAssesment.Api.Attachments.Migrations +{ + /// + public partial class InitialAttachment : Migration + { + /// + protected override void Up(MigrationBuilder migrationBuilder) + { + migrationBuilder.CreateTable( + name: "Attachments", + columns: table => new + { + Id = table.Column(type: "int", nullable: false) + .Annotation("SqlServer:Identity", "1, 1"), + URI = table.Column(type: "nvarchar(max)", nullable: false), + AnswerId = table.Column(type: "int", nullable: true), + ResponseId = table.Column(type: "int", nullable: false), + IsDeleted = table.Column(type: "bit", nullable: false), + FileName = table.Column(type: "nvarchar(max)", nullable: false) + }, + constraints: table => + { + table.PrimaryKey("PK_Attachments", x => x.Id); + }); + } + + /// + protected override void Down(MigrationBuilder migrationBuilder) + { + migrationBuilder.DropTable( + name: "Attachments"); + } + } +} diff --git a/DamageAssesmentApi/DamageAssesment.Api.Attachments/Migrations/AttachmentsDbContextModelSnapshot.cs b/DamageAssesmentApi/DamageAssesment.Api.Attachments/Migrations/AttachmentsDbContextModelSnapshot.cs new file mode 100644 index 0000000..8f0cc45 --- /dev/null +++ b/DamageAssesmentApi/DamageAssesment.Api.Attachments/Migrations/AttachmentsDbContextModelSnapshot.cs @@ -0,0 +1,57 @@ +// +using System; +using DamageAssesment.Api.Attachments.Db; +using Microsoft.EntityFrameworkCore; +using Microsoft.EntityFrameworkCore.Infrastructure; +using Microsoft.EntityFrameworkCore.Metadata; +using Microsoft.EntityFrameworkCore.Storage.ValueConversion; + +#nullable disable + +namespace DamageAssesment.Api.Attachments.Migrations +{ + [DbContext(typeof(AttachmentsDbContext))] + partial class AttachmentsDbContextModelSnapshot : ModelSnapshot + { + protected override void BuildModel(ModelBuilder modelBuilder) + { +#pragma warning disable 612, 618 + modelBuilder + .HasAnnotation("ProductVersion", "7.0.9") + .HasAnnotation("Relational:MaxIdentifierLength", 128); + + SqlServerModelBuilderExtensions.UseIdentityColumns(modelBuilder); + + modelBuilder.Entity("DamageAssesment.Api.Attachments.Db.Attachment", b => + { + b.Property("Id") + .ValueGeneratedOnAdd() + .HasColumnType("int"); + + SqlServerPropertyBuilderExtensions.UseIdentityColumn(b.Property("Id")); + + b.Property("AnswerId") + .HasColumnType("int"); + + b.Property("FileName") + .IsRequired() + .HasColumnType("nvarchar(max)"); + + b.Property("IsDeleted") + .HasColumnType("bit"); + + b.Property("ResponseId") + .HasColumnType("int"); + + b.Property("URI") + .IsRequired() + .HasColumnType("nvarchar(max)"); + + b.HasKey("Id"); + + b.ToTable("Attachments"); + }); +#pragma warning restore 612, 618 + } + } +} diff --git a/DamageAssesmentApi/DamageAssesment.Api.Attachments/Program.cs b/DamageAssesmentApi/DamageAssesment.Api.Attachments/Program.cs index d661ef6..d1aff95 100644 --- a/DamageAssesmentApi/DamageAssesment.Api.Attachments/Program.cs +++ b/DamageAssesmentApi/DamageAssesment.Api.Attachments/Program.cs @@ -19,7 +19,7 @@ builder.Services.AddScoped(); builder.Services.AddAutoMapper(AppDomain.CurrentDomain.GetAssemblies()); //4/30 builder.Services.AddDbContext(option => { - option.UseInMemoryDatabase("Attachments"); + option.UseSqlServer("AttachmentConnection"); }); builder.Services.Configure(o => { diff --git a/DamageAssesmentApi/DamageAssesment.Api.Attachments/appsettings.json b/DamageAssesmentApi/DamageAssesment.Api.Attachments/appsettings.json index ceb0a25..81c309e 100644 --- a/DamageAssesmentApi/DamageAssesment.Api.Attachments/appsettings.json +++ b/DamageAssesmentApi/DamageAssesment.Api.Attachments/appsettings.json @@ -9,5 +9,8 @@ "Fileupload": { "folderpath": "DMS_Attachments/Active", "Deletepath": "DMS_Attachments/Deleted" + }, + "ConnectionStrings": { + "AttachmentConnection": "Server=DESKTOP-OF5DPLQ\\SQLEXPRESS;Database=da_survey_dev;Trusted_Connection=True;TrustServerCertificate=True;" } } diff --git a/DamageAssesmentApi/DamageAssesment.Api.Employees/DamageAssesment.Api.Employees.csproj b/DamageAssesmentApi/DamageAssesment.Api.Employees/DamageAssesment.Api.Employees.csproj index 53e8700..f6a61e2 100644 --- a/DamageAssesmentApi/DamageAssesment.Api.Employees/DamageAssesment.Api.Employees.csproj +++ b/DamageAssesmentApi/DamageAssesment.Api.Employees/DamageAssesment.Api.Employees.csproj @@ -8,8 +8,17 @@ - - + + + all + runtime; build; native; contentfiles; analyzers; buildtransitive + + + + + all + runtime; build; native; contentfiles; analyzers; buildtransitive + diff --git a/DamageAssesmentApi/DamageAssesment.Api.Employees/Db/EmployeeDbContext.cs b/DamageAssesmentApi/DamageAssesment.Api.Employees/Db/EmployeeDbContext.cs index 64234cd..20c774e 100644 --- a/DamageAssesmentApi/DamageAssesment.Api.Employees/Db/EmployeeDbContext.cs +++ b/DamageAssesmentApi/DamageAssesment.Api.Employees/Db/EmployeeDbContext.cs @@ -4,11 +4,16 @@ namespace DamageAssesment.Api.Employees.Db { public class EmployeeDbContext: DbContext { - public DbSet Employees { get; set; } - public EmployeeDbContext(DbContextOptions options) : base(options) + private IConfiguration _Configuration { get; set; } + public EmployeeDbContext(DbContextOptions options, IConfiguration configuration) : base(options) { - - + _Configuration = configuration; } + protected override void OnConfiguring(DbContextOptionsBuilder options) + { + // connect to sql server with connection string from app settings + options.UseSqlServer(_Configuration.GetConnectionString("EmployeeConnection")); + } + public DbSet Employees { get; set; } } } diff --git a/DamageAssesmentApi/DamageAssesment.Api.Employees/Migrations/20230817213656_InitialEmployee.Designer.cs b/DamageAssesmentApi/DamageAssesment.Api.Employees/Migrations/20230817213656_InitialEmployee.Designer.cs new file mode 100644 index 0000000..d747ab1 --- /dev/null +++ b/DamageAssesmentApi/DamageAssesment.Api.Employees/Migrations/20230817213656_InitialEmployee.Designer.cs @@ -0,0 +1,64 @@ +// +using System; +using DamageAssesment.Api.Employees.Db; +using Microsoft.EntityFrameworkCore; +using Microsoft.EntityFrameworkCore.Infrastructure; +using Microsoft.EntityFrameworkCore.Metadata; +using Microsoft.EntityFrameworkCore.Migrations; +using Microsoft.EntityFrameworkCore.Storage.ValueConversion; + +#nullable disable + +namespace DamageAssesment.Api.Employees.Migrations +{ + [DbContext(typeof(EmployeeDbContext))] + [Migration("20230817213656_InitialEmployee")] + partial class InitialEmployee + { + /// + protected override void BuildTargetModel(ModelBuilder modelBuilder) + { +#pragma warning disable 612, 618 + modelBuilder + .HasAnnotation("ProductVersion", "7.0.9") + .HasAnnotation("Relational:MaxIdentifierLength", 128); + + SqlServerModelBuilderExtensions.UseIdentityColumns(modelBuilder); + + modelBuilder.Entity("DamageAssesment.Api.Employees.Db.Employee", b => + { + b.Property("Id") + .HasColumnType("nvarchar(450)"); + + b.Property("BirthDate") + .HasColumnType("datetime2"); + + b.Property("Email") + .IsRequired() + .HasMaxLength(50) + .HasColumnType("nvarchar(50)"); + + b.Property("IsActive") + .HasColumnType("bit"); + + b.Property("Name") + .IsRequired() + .HasMaxLength(50) + .HasColumnType("nvarchar(50)"); + + b.Property("OfficePhoneNumber") + .IsRequired() + .HasMaxLength(50) + .HasColumnType("nvarchar(50)"); + + b.Property("PreferredLanguage") + .HasColumnType("nvarchar(max)"); + + b.HasKey("Id"); + + b.ToTable("Employees"); + }); +#pragma warning restore 612, 618 + } + } +} diff --git a/DamageAssesmentApi/DamageAssesment.Api.Employees/Migrations/20230817213656_InitialEmployee.cs b/DamageAssesmentApi/DamageAssesment.Api.Employees/Migrations/20230817213656_InitialEmployee.cs new file mode 100644 index 0000000..607751e --- /dev/null +++ b/DamageAssesmentApi/DamageAssesment.Api.Employees/Migrations/20230817213656_InitialEmployee.cs @@ -0,0 +1,39 @@ +using System; +using Microsoft.EntityFrameworkCore.Migrations; + +#nullable disable + +namespace DamageAssesment.Api.Employees.Migrations +{ + /// + public partial class InitialEmployee : Migration + { + /// + protected override void Up(MigrationBuilder migrationBuilder) + { + migrationBuilder.CreateTable( + name: "Employees", + columns: table => new + { + Id = table.Column(type: "nvarchar(450)", nullable: false), + Name = table.Column(type: "nvarchar(50)", maxLength: 50, nullable: false), + BirthDate = table.Column(type: "datetime2", nullable: false), + OfficePhoneNumber = table.Column(type: "nvarchar(50)", maxLength: 50, nullable: false), + Email = table.Column(type: "nvarchar(50)", maxLength: 50, nullable: false), + IsActive = table.Column(type: "bit", nullable: false), + PreferredLanguage = table.Column(type: "nvarchar(max)", nullable: true) + }, + constraints: table => + { + table.PrimaryKey("PK_Employees", x => x.Id); + }); + } + + /// + protected override void Down(MigrationBuilder migrationBuilder) + { + migrationBuilder.DropTable( + name: "Employees"); + } + } +} diff --git a/DamageAssesmentApi/DamageAssesment.Api.Employees/Migrations/EmployeeDbContextModelSnapshot.cs b/DamageAssesmentApi/DamageAssesment.Api.Employees/Migrations/EmployeeDbContextModelSnapshot.cs new file mode 100644 index 0000000..7b928d2 --- /dev/null +++ b/DamageAssesmentApi/DamageAssesment.Api.Employees/Migrations/EmployeeDbContextModelSnapshot.cs @@ -0,0 +1,61 @@ +// +using System; +using DamageAssesment.Api.Employees.Db; +using Microsoft.EntityFrameworkCore; +using Microsoft.EntityFrameworkCore.Infrastructure; +using Microsoft.EntityFrameworkCore.Metadata; +using Microsoft.EntityFrameworkCore.Storage.ValueConversion; + +#nullable disable + +namespace DamageAssesment.Api.Employees.Migrations +{ + [DbContext(typeof(EmployeeDbContext))] + partial class EmployeeDbContextModelSnapshot : ModelSnapshot + { + protected override void BuildModel(ModelBuilder modelBuilder) + { +#pragma warning disable 612, 618 + modelBuilder + .HasAnnotation("ProductVersion", "7.0.9") + .HasAnnotation("Relational:MaxIdentifierLength", 128); + + SqlServerModelBuilderExtensions.UseIdentityColumns(modelBuilder); + + modelBuilder.Entity("DamageAssesment.Api.Employees.Db.Employee", b => + { + b.Property("Id") + .HasColumnType("nvarchar(450)"); + + b.Property("BirthDate") + .HasColumnType("datetime2"); + + b.Property("Email") + .IsRequired() + .HasMaxLength(50) + .HasColumnType("nvarchar(50)"); + + b.Property("IsActive") + .HasColumnType("bit"); + + b.Property("Name") + .IsRequired() + .HasMaxLength(50) + .HasColumnType("nvarchar(50)"); + + b.Property("OfficePhoneNumber") + .IsRequired() + .HasMaxLength(50) + .HasColumnType("nvarchar(50)"); + + b.Property("PreferredLanguage") + .HasColumnType("nvarchar(max)"); + + b.HasKey("Id"); + + b.ToTable("Employees"); + }); +#pragma warning restore 612, 618 + } + } +} diff --git a/DamageAssesmentApi/DamageAssesment.Api.Employees/Program.cs b/DamageAssesmentApi/DamageAssesment.Api.Employees/Program.cs index 29b91fa..6a19ea0 100644 --- a/DamageAssesmentApi/DamageAssesment.Api.Employees/Program.cs +++ b/DamageAssesmentApi/DamageAssesment.Api.Employees/Program.cs @@ -15,7 +15,7 @@ builder.Services.AddScoped(); builder.Services.AddAutoMapper(AppDomain.CurrentDomain.GetAssemblies()); //4/30 builder.Services.AddDbContext(option => { - option.UseInMemoryDatabase("Employees"); + option.UseSqlServer("EmployeeConnection"); }); var app = builder.Build(); diff --git a/DamageAssesmentApi/DamageAssesment.Api.Employees/appsettings.json b/DamageAssesmentApi/DamageAssesment.Api.Employees/appsettings.json index 1cf89f5..a0732a2 100644 --- a/DamageAssesmentApi/DamageAssesment.Api.Employees/appsettings.json +++ b/DamageAssesmentApi/DamageAssesment.Api.Employees/appsettings.json @@ -10,5 +10,8 @@ "endpoint1": "xxx", "endpoint2": "xxx", "endpoint3": "xxx" + }, + "ConnectionStrings": { + "EmployeeConnection": "Server=DESKTOP-OF5DPLQ\\SQLEXPRESS;Database=da_survey_dev;Trusted_Connection=True;TrustServerCertificate=True;" } } diff --git a/DamageAssesmentApi/DamageAssesment.Api.Locations.Test/LocationsServiceTest.cs b/DamageAssesmentApi/DamageAssesment.Api.Locations.Test/LocationsServiceTest.cs index 270c638..4b5dc3c 100644 --- a/DamageAssesmentApi/DamageAssesment.Api.Locations.Test/LocationsServiceTest.cs +++ b/DamageAssesmentApi/DamageAssesment.Api.Locations.Test/LocationsServiceTest.cs @@ -14,131 +14,6 @@ namespace DamageAssesment.Api.Locations.Test public class LocationsServiceTest { - [Fact(DisplayName = "Get Location using Location ID")] - public async Task GetLocationsUsingLocationID() - { - var options = new DbContextOptionsBuilder() - .UseInMemoryDatabase(nameof(GetLocationsUsingLocationID)) - .Options; - - var dbContext = new LocationDbContext(options); - CreateLocations(dbContext); - //Mapping - var LocationsProfile = new LocationProfile(); - var configuration = new MapperConfiguration(cfg => cfg.AddProfile(LocationsProfile)); - var mapper = new Mapper(configuration); - - var LocationsProvider = new LocationsProvider(dbContext, null, mapper); - //Testmethode - var Location = await LocationsProvider.GetLocationByIdAsync("Loc3"); - - Assert.True(Location.IsSuccess); - Assert.Null(Location.ErrorMessage); - } - [Fact(DisplayName = "Get Locations")] - public async Task GetAllLocationsTest() - { - var options = new DbContextOptionsBuilder() - .UseInMemoryDatabase(nameof(GetLocationsUsingLocationID)) - .Options; - - var dbContext = new LocationDbContext(options); - CreateLocations(dbContext); - //Mapping - var LocationsProfile = new LocationProfile(); - var configuration = new MapperConfiguration(cfg => cfg.AddProfile(LocationsProfile)); - var mapper = new Mapper(configuration); - - var LocationsProvider = new LocationsProvider(dbContext, null, mapper); - //Testmethode - var Location = await LocationsProvider.GetLocationsAsync(); - - Assert.True(Location.IsSuccess); - Assert.Null(Location.ErrorMessage); - } - - [Fact(DisplayName = "Delete Location by Id")] - public async Task DeleteLocationTest() - { - var options = new DbContextOptionsBuilder() - .UseInMemoryDatabase(nameof(GetLocationsUsingLocationID)) - .Options; - - var dbContext = new LocationDbContext(options); - CreateLocations(dbContext); - //Mapping - var LocationsProfile = new LocationProfile(); - var configuration = new MapperConfiguration(cfg => cfg.AddProfile(LocationsProfile)); - var mapper = new Mapper(configuration); - - var LocationsProvider = new LocationsProvider(dbContext, null, mapper); - //Testmethode - var Location = await LocationsProvider.DeleteLocationAsync("Loc2"); - - Assert.True(Location.IsSuccess); - Assert.NotNull(Location.ErrorMessage); - } - [Fact(DisplayName = "Add Location")] - public async Task AddLocationTest() - { - var options = new DbContextOptionsBuilder() - .UseInMemoryDatabase(nameof(GetLocationsUsingLocationID)) - .Options; - - var dbContext = new LocationDbContext(options); - CreateLocations(dbContext); - //Mapping - var LocationsProfile = new LocationProfile(); - var configuration = new MapperConfiguration(cfg => cfg.AddProfile(LocationsProfile)); - var mapper = new Mapper(configuration); - var LocationsProvider = new LocationsProvider(dbContext, null, mapper); - //Testmethode - Db.Location newLocation = new Db.Location() { Id = "Loc9", RegionId = "1", Name = "Test 1", MaintenanceCenter = "1", SchoolType = "US" }; - var Location = await LocationsProvider.PostLocationAsync(newLocation); - - Assert.True(Location.IsSuccess); - Assert.Null(Location.ErrorMessage); - } - [Fact(DisplayName = "Update Location")] - public async Task UpdateLocationTest() - { - var options = new DbContextOptionsBuilder() - .UseInMemoryDatabase(nameof(GetLocationsUsingLocationID)) - .Options; - var dbContext = new LocationDbContext(options); - CreateLocations(dbContext); - //Mapping - var LocationsProfile = new LocationProfile(); - var configuration = new MapperConfiguration(cfg => cfg.AddProfile(LocationsProfile)); - var mapper = new Mapper(configuration); - var LocationsProvider = new LocationsProvider(dbContext, null, mapper); - //Testmethode - Db.Location updateLocation = new Db.Location() { Id = "Loc1", RegionId = "1", Name = "Tampa", MaintenanceCenter = "1", SchoolType = "NA" }; - var Location = await LocationsProvider.UpdateLocationAsync(updateLocation); - var modified = dbContext.Locations.FirstOrDefault(a => a.Id == updateLocation.Id); - Assert.True(Location.IsSuccess); - Assert.NotNull(Location.ErrorMessage); - } - private static void CreateLocations(LocationDbContext dbContext) - { - //Create sample data for testing - if (dbContext.Locations.Count() == 0) - { - for (int i = 1; i < 6; i++) - { - dbContext.Locations.Add(new Db.Location() - { - Id = "Loc"+i.ToString(), - RegionId = i.ToString(), - Name = "Test Location" + Guid.NewGuid().ToString(), - MaintenanceCenter = i.ToString(), - SchoolType = "US" - - }); - } - dbContext.SaveChanges(); - } - } //Tests for regions diff --git a/DamageAssesmentApi/DamageAssesment.Api.Locations/DamageAssesment.Api.Locations.csproj b/DamageAssesmentApi/DamageAssesment.Api.Locations/DamageAssesment.Api.Locations.csproj index 47751c7..e7a0c57 100644 --- a/DamageAssesmentApi/DamageAssesment.Api.Locations/DamageAssesment.Api.Locations.csproj +++ b/DamageAssesmentApi/DamageAssesment.Api.Locations/DamageAssesment.Api.Locations.csproj @@ -8,8 +8,17 @@ - - + + + all + runtime; build; native; contentfiles; analyzers; buildtransitive + + + + + all + runtime; build; native; contentfiles; analyzers; buildtransitive + diff --git a/DamageAssesmentApi/DamageAssesment.Api.Locations/Db/LocationDbContext.cs b/DamageAssesmentApi/DamageAssesment.Api.Locations/Db/LocationDbContext.cs index 407faca..f5e4914 100644 --- a/DamageAssesmentApi/DamageAssesment.Api.Locations/Db/LocationDbContext.cs +++ b/DamageAssesmentApi/DamageAssesment.Api.Locations/Db/LocationDbContext.cs @@ -4,11 +4,17 @@ namespace DamageAssesment.Api.Locations.Db { public class LocationDbContext:DbContext { + private IConfiguration _Configuration { get; set; } + public LocationDbContext(DbContextOptions options, IConfiguration configuration) : base(options) + { + _Configuration = configuration; + } + protected override void OnConfiguring(DbContextOptionsBuilder options) + { + // connect to sql server with connection string from app settings + options.UseSqlServer(_Configuration.GetConnectionString("LocationConnection")); + } public DbSet Locations { get; set; } public DbSet Regions { get; set; } - public LocationDbContext(DbContextOptions options) : base(options) - { - - } } } diff --git a/DamageAssesmentApi/DamageAssesment.Api.Locations/Migrations/20230817214454_InitialLocation.Designer.cs b/DamageAssesmentApi/DamageAssesment.Api.Locations/Migrations/20230817214454_InitialLocation.Designer.cs new file mode 100644 index 0000000..3e27956 --- /dev/null +++ b/DamageAssesmentApi/DamageAssesment.Api.Locations/Migrations/20230817214454_InitialLocation.Designer.cs @@ -0,0 +1,80 @@ +// +using DamageAssesment.Api.Locations.Db; +using Microsoft.EntityFrameworkCore; +using Microsoft.EntityFrameworkCore.Infrastructure; +using Microsoft.EntityFrameworkCore.Metadata; +using Microsoft.EntityFrameworkCore.Migrations; +using Microsoft.EntityFrameworkCore.Storage.ValueConversion; + +#nullable disable + +namespace DamageAssesment.Api.Locations.Migrations +{ + [DbContext(typeof(LocationDbContext))] + [Migration("20230817214454_InitialLocation")] + partial class InitialLocation + { + /// + protected override void BuildTargetModel(ModelBuilder modelBuilder) + { +#pragma warning disable 612, 618 + modelBuilder + .HasAnnotation("ProductVersion", "7.0.9") + .HasAnnotation("Relational:MaxIdentifierLength", 128); + + SqlServerModelBuilderExtensions.UseIdentityColumns(modelBuilder); + + modelBuilder.Entity("DamageAssesment.Api.Locations.Db.Location", b => + { + b.Property("Id") + .HasMaxLength(4) + .HasColumnType("nvarchar(4)"); + + b.Property("MaintenanceCenter") + .IsRequired() + .HasMaxLength(1) + .HasColumnType("nvarchar(1)"); + + b.Property("Name") + .IsRequired() + .HasMaxLength(50) + .HasColumnType("nvarchar(50)"); + + b.Property("RegionId") + .IsRequired() + .HasColumnType("nvarchar(max)"); + + b.Property("SchoolType") + .IsRequired() + .HasMaxLength(2) + .HasColumnType("nvarchar(2)"); + + b.HasKey("Id"); + + b.ToTable("Locations"); + }); + + modelBuilder.Entity("DamageAssesment.Api.Locations.Db.Region", b => + { + b.Property("Id") + .HasMaxLength(2) + .HasColumnType("nvarchar(2)"); + + b.Property("Abbreviation") + .IsRequired() + .HasMaxLength(5) + .HasColumnType("nvarchar(5)"); + + b.Property("Name") + .IsRequired() + .HasMaxLength(50) + .HasColumnType("nvarchar(50)"); + + b.HasKey("Id"); + + b.ToTable("Regions"); + }); +#pragma warning restore 612, 618 + } + } +} diff --git a/DamageAssesmentApi/DamageAssesment.Api.Locations/Migrations/20230817214454_InitialLocation.cs b/DamageAssesmentApi/DamageAssesment.Api.Locations/Migrations/20230817214454_InitialLocation.cs new file mode 100644 index 0000000..4143c63 --- /dev/null +++ b/DamageAssesmentApi/DamageAssesment.Api.Locations/Migrations/20230817214454_InitialLocation.cs @@ -0,0 +1,52 @@ +using Microsoft.EntityFrameworkCore.Migrations; + +#nullable disable + +namespace DamageAssesment.Api.Locations.Migrations +{ + /// + public partial class InitialLocation : Migration + { + /// + protected override void Up(MigrationBuilder migrationBuilder) + { + migrationBuilder.CreateTable( + name: "Locations", + columns: table => new + { + Id = table.Column(type: "nvarchar(4)", maxLength: 4, nullable: false), + Name = table.Column(type: "nvarchar(50)", maxLength: 50, nullable: false), + MaintenanceCenter = table.Column(type: "nvarchar(1)", maxLength: 1, nullable: false), + SchoolType = table.Column(type: "nvarchar(2)", maxLength: 2, nullable: false), + RegionId = table.Column(type: "nvarchar(max)", nullable: false) + }, + constraints: table => + { + table.PrimaryKey("PK_Locations", x => x.Id); + }); + + migrationBuilder.CreateTable( + name: "Regions", + columns: table => new + { + Id = table.Column(type: "nvarchar(2)", maxLength: 2, nullable: false), + Name = table.Column(type: "nvarchar(50)", maxLength: 50, nullable: false), + Abbreviation = table.Column(type: "nvarchar(5)", maxLength: 5, nullable: false) + }, + constraints: table => + { + table.PrimaryKey("PK_Regions", x => x.Id); + }); + } + + /// + protected override void Down(MigrationBuilder migrationBuilder) + { + migrationBuilder.DropTable( + name: "Locations"); + + migrationBuilder.DropTable( + name: "Regions"); + } + } +} diff --git a/DamageAssesmentApi/DamageAssesment.Api.Locations/Migrations/LocationDbContextModelSnapshot.cs b/DamageAssesmentApi/DamageAssesment.Api.Locations/Migrations/LocationDbContextModelSnapshot.cs new file mode 100644 index 0000000..0ae10ad --- /dev/null +++ b/DamageAssesmentApi/DamageAssesment.Api.Locations/Migrations/LocationDbContextModelSnapshot.cs @@ -0,0 +1,77 @@ +// +using DamageAssesment.Api.Locations.Db; +using Microsoft.EntityFrameworkCore; +using Microsoft.EntityFrameworkCore.Infrastructure; +using Microsoft.EntityFrameworkCore.Metadata; +using Microsoft.EntityFrameworkCore.Storage.ValueConversion; + +#nullable disable + +namespace DamageAssesment.Api.Locations.Migrations +{ + [DbContext(typeof(LocationDbContext))] + partial class LocationDbContextModelSnapshot : ModelSnapshot + { + protected override void BuildModel(ModelBuilder modelBuilder) + { +#pragma warning disable 612, 618 + modelBuilder + .HasAnnotation("ProductVersion", "7.0.9") + .HasAnnotation("Relational:MaxIdentifierLength", 128); + + SqlServerModelBuilderExtensions.UseIdentityColumns(modelBuilder); + + modelBuilder.Entity("DamageAssesment.Api.Locations.Db.Location", b => + { + b.Property("Id") + .HasMaxLength(4) + .HasColumnType("nvarchar(4)"); + + b.Property("MaintenanceCenter") + .IsRequired() + .HasMaxLength(1) + .HasColumnType("nvarchar(1)"); + + b.Property("Name") + .IsRequired() + .HasMaxLength(50) + .HasColumnType("nvarchar(50)"); + + b.Property("RegionId") + .IsRequired() + .HasColumnType("nvarchar(max)"); + + b.Property("SchoolType") + .IsRequired() + .HasMaxLength(2) + .HasColumnType("nvarchar(2)"); + + b.HasKey("Id"); + + b.ToTable("Locations"); + }); + + modelBuilder.Entity("DamageAssesment.Api.Locations.Db.Region", b => + { + b.Property("Id") + .HasMaxLength(2) + .HasColumnType("nvarchar(2)"); + + b.Property("Abbreviation") + .IsRequired() + .HasMaxLength(5) + .HasColumnType("nvarchar(5)"); + + b.Property("Name") + .IsRequired() + .HasMaxLength(50) + .HasColumnType("nvarchar(50)"); + + b.HasKey("Id"); + + b.ToTable("Regions"); + }); +#pragma warning restore 612, 618 + } + } +} diff --git a/DamageAssesmentApi/DamageAssesment.Api.Locations/Program.cs b/DamageAssesmentApi/DamageAssesment.Api.Locations/Program.cs index a2e3149..bf25860 100644 --- a/DamageAssesmentApi/DamageAssesment.Api.Locations/Program.cs +++ b/DamageAssesmentApi/DamageAssesment.Api.Locations/Program.cs @@ -17,7 +17,7 @@ builder.Services.AddScoped(); builder.Services.AddAutoMapper(AppDomain.CurrentDomain.GetAssemblies()); //4/30 builder.Services.AddDbContext(option => { - option.UseInMemoryDatabase("Locations"); + option.UseSqlServer("LocationConnection"); }); var app = builder.Build(); diff --git a/DamageAssesmentApi/DamageAssesment.Api.Locations/appsettings.json b/DamageAssesmentApi/DamageAssesment.Api.Locations/appsettings.json index 10f68b8..44e3bda 100644 --- a/DamageAssesmentApi/DamageAssesment.Api.Locations/appsettings.json +++ b/DamageAssesmentApi/DamageAssesment.Api.Locations/appsettings.json @@ -5,5 +5,8 @@ "Microsoft.AspNetCore": "Warning" } }, - "AllowedHosts": "*" + "AllowedHosts": "*", + "ConnectionStrings": { + "LocationConnection": "Server=DESKTOP-OF5DPLQ\\SQLEXPRESS;Database=da_survey_dev;Trusted_Connection=True;TrustServerCertificate=True;" + } } diff --git a/DamageAssesmentApi/DamageAssesment.Api.Questions/DamageAssesment.Api.Questions.csproj b/DamageAssesmentApi/DamageAssesment.Api.Questions/DamageAssesment.Api.Questions.csproj index 53e8700..f6a61e2 100644 --- a/DamageAssesmentApi/DamageAssesment.Api.Questions/DamageAssesment.Api.Questions.csproj +++ b/DamageAssesmentApi/DamageAssesment.Api.Questions/DamageAssesment.Api.Questions.csproj @@ -8,8 +8,17 @@ - - + + + all + runtime; build; native; contentfiles; analyzers; buildtransitive + + + + + all + runtime; build; native; contentfiles; analyzers; buildtransitive + diff --git a/DamageAssesmentApi/DamageAssesment.Api.Questions/Db/QuestionDbContext.cs b/DamageAssesmentApi/DamageAssesment.Api.Questions/Db/QuestionDbContext.cs index a784abf..d3ed79d 100644 --- a/DamageAssesmentApi/DamageAssesment.Api.Questions/Db/QuestionDbContext.cs +++ b/DamageAssesmentApi/DamageAssesment.Api.Questions/Db/QuestionDbContext.cs @@ -6,14 +6,20 @@ namespace DamageAssesment.Api.Questions.Db { public class QuestionDbContext : DbContext { + private IConfiguration _Configuration { get; set; } + public QuestionDbContext(DbContextOptions options, IConfiguration configuration) : base(options) + { + _Configuration = configuration; + } + protected override void OnConfiguring(DbContextOptionsBuilder options) + { + // connect to sql server with connection string from app settings + options.UseSqlServer(_Configuration.GetConnectionString("QuestionConnection")); + } public DbSet Questions { get; set; } public DbSet QuestionTypes { get; set; } public DbSet QuestionsTranslations { get; set; } public DbSet QuestionCategories { get; set; } - public QuestionDbContext(DbContextOptions options) : base(options) - { - - } //protected override void OnModelCreating(ModelBuilder modelBuilder) //{ // modelBuilder.Entity() diff --git a/DamageAssesmentApi/DamageAssesment.Api.Questions/Migrations/20230817215744_InitialQuestion.Designer.cs b/DamageAssesmentApi/DamageAssesment.Api.Questions/Migrations/20230817215744_InitialQuestion.Designer.cs new file mode 100644 index 0000000..4cbbf64 --- /dev/null +++ b/DamageAssesmentApi/DamageAssesment.Api.Questions/Migrations/20230817215744_InitialQuestion.Designer.cs @@ -0,0 +1,143 @@ +// +using System; +using DamageAssesment.Api.Questions.Db; +using Microsoft.EntityFrameworkCore; +using Microsoft.EntityFrameworkCore.Infrastructure; +using Microsoft.EntityFrameworkCore.Metadata; +using Microsoft.EntityFrameworkCore.Migrations; +using Microsoft.EntityFrameworkCore.Storage.ValueConversion; + +#nullable disable + +namespace DamageAssesment.Api.Questions.Migrations +{ + [DbContext(typeof(QuestionDbContext))] + [Migration("20230817215744_InitialQuestion")] + partial class InitialQuestion + { + /// + protected override void BuildTargetModel(ModelBuilder modelBuilder) + { +#pragma warning disable 612, 618 + modelBuilder + .HasAnnotation("ProductVersion", "7.0.9") + .HasAnnotation("Relational:MaxIdentifierLength", 128); + + SqlServerModelBuilderExtensions.UseIdentityColumns(modelBuilder); + + modelBuilder.Entity("DamageAssesment.Api.Questions.Db.Question", b => + { + b.Property("Id") + .ValueGeneratedOnAdd() + .HasColumnType("int"); + + SqlServerPropertyBuilderExtensions.UseIdentityColumn(b.Property("Id")); + + b.Property("CategoryId") + .HasColumnType("int"); + + b.Property("Comment") + .HasColumnType("bit"); + + b.Property("IsRequired") + .HasColumnType("bit"); + + b.Property("Key") + .HasColumnType("bit"); + + b.Property("QuestionGroup") + .IsRequired() + .HasColumnType("nvarchar(max)"); + + b.Property("QuestionNumber") + .HasColumnType("int"); + + b.Property("QuestionTypeId") + .HasColumnType("int"); + + b.Property("SurveyId") + .HasColumnType("int"); + + b.HasKey("Id"); + + b.HasIndex("QuestionTypeId"); + + b.ToTable("Questions"); + }); + + modelBuilder.Entity("DamageAssesment.Api.Questions.Db.QuestionCategory", b => + { + b.Property("Id") + .ValueGeneratedOnAdd() + .HasColumnType("int"); + + SqlServerPropertyBuilderExtensions.UseIdentityColumn(b.Property("Id")); + + b.Property("CategoryImage") + .IsRequired() + .HasColumnType("nvarchar(max)"); + + b.Property("CategoryName") + .IsRequired() + .HasColumnType("nvarchar(max)"); + + b.HasKey("Id"); + + b.ToTable("QuestionCategories"); + }); + + modelBuilder.Entity("DamageAssesment.Api.Questions.Db.QuestionType", b => + { + b.Property("Id") + .ValueGeneratedOnAdd() + .HasColumnType("int"); + + SqlServerPropertyBuilderExtensions.UseIdentityColumn(b.Property("Id")); + + b.Property("TypeText") + .IsRequired() + .HasColumnType("nvarchar(max)"); + + b.HasKey("Id"); + + b.ToTable("QuestionTypes"); + }); + + modelBuilder.Entity("DamageAssesment.Api.Questions.Db.QuestionsTranslation", b => + { + b.Property("Id") + .ValueGeneratedOnAdd() + .HasColumnType("int"); + + SqlServerPropertyBuilderExtensions.UseIdentityColumn(b.Property("Id")); + + b.Property("Language") + .IsRequired() + .HasColumnType("nvarchar(max)"); + + b.Property("QuestionId") + .HasColumnType("int"); + + b.Property("QuestionText") + .IsRequired() + .HasColumnType("nvarchar(max)"); + + b.HasKey("Id"); + + b.ToTable("QuestionsTranslations"); + }); + + modelBuilder.Entity("DamageAssesment.Api.Questions.Db.Question", b => + { + b.HasOne("DamageAssesment.Api.Questions.Db.QuestionType", "QuestionType") + .WithMany() + .HasForeignKey("QuestionTypeId") + .OnDelete(DeleteBehavior.Cascade) + .IsRequired(); + + b.Navigation("QuestionType"); + }); +#pragma warning restore 612, 618 + } + } +} diff --git a/DamageAssesmentApi/DamageAssesment.Api.Questions/Migrations/20230817215744_InitialQuestion.cs b/DamageAssesmentApi/DamageAssesment.Api.Questions/Migrations/20230817215744_InitialQuestion.cs new file mode 100644 index 0000000..aaaf018 --- /dev/null +++ b/DamageAssesmentApi/DamageAssesment.Api.Questions/Migrations/20230817215744_InitialQuestion.cs @@ -0,0 +1,103 @@ +using Microsoft.EntityFrameworkCore.Migrations; + +#nullable disable + +namespace DamageAssesment.Api.Questions.Migrations +{ + /// + public partial class InitialQuestion : Migration + { + /// + protected override void Up(MigrationBuilder migrationBuilder) + { + migrationBuilder.CreateTable( + name: "QuestionCategories", + columns: table => new + { + Id = table.Column(type: "int", nullable: false) + .Annotation("SqlServer:Identity", "1, 1"), + CategoryName = table.Column(type: "nvarchar(max)", nullable: false), + CategoryImage = table.Column(type: "nvarchar(max)", nullable: false) + }, + constraints: table => + { + table.PrimaryKey("PK_QuestionCategories", x => x.Id); + }); + + migrationBuilder.CreateTable( + name: "QuestionsTranslations", + columns: table => new + { + Id = table.Column(type: "int", nullable: false) + .Annotation("SqlServer:Identity", "1, 1"), + QuestionId = table.Column(type: "int", nullable: false), + QuestionText = table.Column(type: "nvarchar(max)", nullable: false), + Language = table.Column(type: "nvarchar(max)", nullable: false) + }, + constraints: table => + { + table.PrimaryKey("PK_QuestionsTranslations", x => x.Id); + }); + + migrationBuilder.CreateTable( + name: "QuestionTypes", + columns: table => new + { + Id = table.Column(type: "int", nullable: false) + .Annotation("SqlServer:Identity", "1, 1"), + TypeText = table.Column(type: "nvarchar(max)", nullable: false) + }, + constraints: table => + { + table.PrimaryKey("PK_QuestionTypes", x => x.Id); + }); + + migrationBuilder.CreateTable( + name: "Questions", + columns: table => new + { + Id = table.Column(type: "int", nullable: false) + .Annotation("SqlServer:Identity", "1, 1"), + QuestionTypeId = table.Column(type: "int", nullable: false), + QuestionNumber = table.Column(type: "int", nullable: false), + IsRequired = table.Column(type: "bit", nullable: false), + Comment = table.Column(type: "bit", nullable: false), + Key = table.Column(type: "bit", nullable: false), + SurveyId = table.Column(type: "int", nullable: true), + QuestionGroup = table.Column(type: "nvarchar(max)", nullable: false), + CategoryId = table.Column(type: "int", nullable: false) + }, + constraints: table => + { + table.PrimaryKey("PK_Questions", x => x.Id); + table.ForeignKey( + name: "FK_Questions_QuestionTypes_QuestionTypeId", + column: x => x.QuestionTypeId, + principalTable: "QuestionTypes", + principalColumn: "Id", + onDelete: ReferentialAction.Cascade); + }); + + migrationBuilder.CreateIndex( + name: "IX_Questions_QuestionTypeId", + table: "Questions", + column: "QuestionTypeId"); + } + + /// + protected override void Down(MigrationBuilder migrationBuilder) + { + migrationBuilder.DropTable( + name: "QuestionCategories"); + + migrationBuilder.DropTable( + name: "Questions"); + + migrationBuilder.DropTable( + name: "QuestionsTranslations"); + + migrationBuilder.DropTable( + name: "QuestionTypes"); + } + } +} diff --git a/DamageAssesmentApi/DamageAssesment.Api.Questions/Migrations/QuestionDbContextModelSnapshot.cs b/DamageAssesmentApi/DamageAssesment.Api.Questions/Migrations/QuestionDbContextModelSnapshot.cs new file mode 100644 index 0000000..018f296 --- /dev/null +++ b/DamageAssesmentApi/DamageAssesment.Api.Questions/Migrations/QuestionDbContextModelSnapshot.cs @@ -0,0 +1,140 @@ +// +using System; +using DamageAssesment.Api.Questions.Db; +using Microsoft.EntityFrameworkCore; +using Microsoft.EntityFrameworkCore.Infrastructure; +using Microsoft.EntityFrameworkCore.Metadata; +using Microsoft.EntityFrameworkCore.Storage.ValueConversion; + +#nullable disable + +namespace DamageAssesment.Api.Questions.Migrations +{ + [DbContext(typeof(QuestionDbContext))] + partial class QuestionDbContextModelSnapshot : ModelSnapshot + { + protected override void BuildModel(ModelBuilder modelBuilder) + { +#pragma warning disable 612, 618 + modelBuilder + .HasAnnotation("ProductVersion", "7.0.9") + .HasAnnotation("Relational:MaxIdentifierLength", 128); + + SqlServerModelBuilderExtensions.UseIdentityColumns(modelBuilder); + + modelBuilder.Entity("DamageAssesment.Api.Questions.Db.Question", b => + { + b.Property("Id") + .ValueGeneratedOnAdd() + .HasColumnType("int"); + + SqlServerPropertyBuilderExtensions.UseIdentityColumn(b.Property("Id")); + + b.Property("CategoryId") + .HasColumnType("int"); + + b.Property("Comment") + .HasColumnType("bit"); + + b.Property("IsRequired") + .HasColumnType("bit"); + + b.Property("Key") + .HasColumnType("bit"); + + b.Property("QuestionGroup") + .IsRequired() + .HasColumnType("nvarchar(max)"); + + b.Property("QuestionNumber") + .HasColumnType("int"); + + b.Property("QuestionTypeId") + .HasColumnType("int"); + + b.Property("SurveyId") + .HasColumnType("int"); + + b.HasKey("Id"); + + b.HasIndex("QuestionTypeId"); + + b.ToTable("Questions"); + }); + + modelBuilder.Entity("DamageAssesment.Api.Questions.Db.QuestionCategory", b => + { + b.Property("Id") + .ValueGeneratedOnAdd() + .HasColumnType("int"); + + SqlServerPropertyBuilderExtensions.UseIdentityColumn(b.Property("Id")); + + b.Property("CategoryImage") + .IsRequired() + .HasColumnType("nvarchar(max)"); + + b.Property("CategoryName") + .IsRequired() + .HasColumnType("nvarchar(max)"); + + b.HasKey("Id"); + + b.ToTable("QuestionCategories"); + }); + + modelBuilder.Entity("DamageAssesment.Api.Questions.Db.QuestionType", b => + { + b.Property("Id") + .ValueGeneratedOnAdd() + .HasColumnType("int"); + + SqlServerPropertyBuilderExtensions.UseIdentityColumn(b.Property("Id")); + + b.Property("TypeText") + .IsRequired() + .HasColumnType("nvarchar(max)"); + + b.HasKey("Id"); + + b.ToTable("QuestionTypes"); + }); + + modelBuilder.Entity("DamageAssesment.Api.Questions.Db.QuestionsTranslation", b => + { + b.Property("Id") + .ValueGeneratedOnAdd() + .HasColumnType("int"); + + SqlServerPropertyBuilderExtensions.UseIdentityColumn(b.Property("Id")); + + b.Property("Language") + .IsRequired() + .HasColumnType("nvarchar(max)"); + + b.Property("QuestionId") + .HasColumnType("int"); + + b.Property("QuestionText") + .IsRequired() + .HasColumnType("nvarchar(max)"); + + b.HasKey("Id"); + + b.ToTable("QuestionsTranslations"); + }); + + modelBuilder.Entity("DamageAssesment.Api.Questions.Db.Question", b => + { + b.HasOne("DamageAssesment.Api.Questions.Db.QuestionType", "QuestionType") + .WithMany() + .HasForeignKey("QuestionTypeId") + .OnDelete(DeleteBehavior.Cascade) + .IsRequired(); + + b.Navigation("QuestionType"); + }); +#pragma warning restore 612, 618 + } + } +} diff --git a/DamageAssesmentApi/DamageAssesment.Api.Questions/Program.cs b/DamageAssesmentApi/DamageAssesment.Api.Questions/Program.cs index 3774b0b..a9d2d26 100644 --- a/DamageAssesmentApi/DamageAssesment.Api.Questions/Program.cs +++ b/DamageAssesmentApi/DamageAssesment.Api.Questions/Program.cs @@ -20,7 +20,7 @@ builder.Services.AddEndpointsApiExplorer(); builder.Services.AddSwaggerGen(); builder.Services.AddDbContext(option => { - option.UseInMemoryDatabase("Questions"); + option.UseSqlServer("QuestionConnection"); }); var app = builder.Build(); diff --git a/DamageAssesmentApi/DamageAssesment.Api.Questions/Providers/QuestionsProvider.cs b/DamageAssesmentApi/DamageAssesment.Api.Questions/Providers/QuestionsProvider.cs index 647f640..f95591b 100644 --- a/DamageAssesmentApi/DamageAssesment.Api.Questions/Providers/QuestionsProvider.cs +++ b/DamageAssesmentApi/DamageAssesment.Api.Questions/Providers/QuestionsProvider.cs @@ -29,43 +29,40 @@ namespace DamageAssesment.Api.Questions.Providers private void SeedData() { - if (!questionDbContext.QuestionsTranslations.Any()) + if (!questionDbContext.QuestionCategories.Any()) { - questionDbContext.QuestionsTranslations.Add(new Db.QuestionsTranslation() {Id=1, QuestionId = 1, QuestionText = "Can You Open ?",Language="en" }); - questionDbContext.QuestionsTranslations.Add(new Db.QuestionsTranslation() { Id = 2, QuestionId = 1, QuestionText = "Peux-tu ouvrir ?", Language = "fr" }); - questionDbContext.QuestionsTranslations.Add(new Db.QuestionsTranslation() { Id = 3, QuestionId = 2, QuestionText = "Are the grounds flodded ?", Language = "en" }); - questionDbContext.QuestionsTranslations.Add(new Db.QuestionsTranslation() { Id = 4, QuestionId = 2, QuestionText = "Les terrains sont-ils inondés ?", Language = "fr" }); - questionDbContext.QuestionsTranslations.Add(new Db.QuestionsTranslation() { Id = 5, QuestionId = 3, QuestionText = "Is the access blocked by flooding ?", Language = "en" }); - questionDbContext.QuestionsTranslations.Add(new Db.QuestionsTranslation() { Id = 6, QuestionId = 3, QuestionText = "L'accès est-il bloqué par les inondations ?", Language = "fr" }); - questionDbContext.QuestionsTranslations.Add(new Db.QuestionsTranslation() { Id = 7, QuestionId = 1, QuestionText = "Puedes abrir ?", Language = "sp" }); - questionDbContext.QuestionsTranslations.Add(new Db.QuestionsTranslation() { Id = 8, QuestionId = 2, QuestionText = "¿Están inundados los terrenos?", Language = "sp" }); - questionDbContext.QuestionsTranslations.Add(new Db.QuestionsTranslation() { Id = 9, QuestionId = 3, QuestionText = "¿El acceso está bloqueado por inundaciones?", Language = "sp" }); - questionDbContext.SaveChanges(); - } - if (!questionDbContext.Questions.Any()) - { - questionDbContext.Questions.Add(new Db.Question() { Id = 1, QuestionTypeId = 2, SurveyId = 1, QuestionNumber = 1, IsRequired = true, Comment = false, Key = true, QuestionGroup = "group1",CategoryId=1 }); - questionDbContext.Questions.Add(new Db.Question() { Id = 2, QuestionTypeId = 1, SurveyId = 1, QuestionNumber = 2, IsRequired = false, Comment = true, Key = false, QuestionGroup = "group1", CategoryId = 1 }); - questionDbContext.Questions.Add(new Db.Question() { Id = 3, QuestionTypeId = 1, SurveyId = 1, QuestionNumber = 3, IsRequired = true, Comment = false, Key = true, QuestionGroup = "group1", CategoryId = 2 }); + questionDbContext.QuestionCategories.Add(new Db.QuestionCategory() { CategoryName = "Electrical", CategoryImage = "img1" }); + questionDbContext.QuestionCategories.Add(new Db.QuestionCategory() { CategoryName = "Flooding", CategoryImage = "img1" }); + questionDbContext.QuestionCategories.Add(new Db.QuestionCategory() { CategoryName = "Structural", CategoryImage = "img1" }); + questionDbContext.QuestionCategories.Add(new Db.QuestionCategory() { CategoryName = "Utility", CategoryImage = "img1" }); + questionDbContext.QuestionCategories.Add(new Db.QuestionCategory() { CategoryName = "Debris", CategoryImage = "img1" }); questionDbContext.SaveChanges(); } if (!questionDbContext.QuestionTypes.Any()) { - questionDbContext.QuestionTypes.Add(new Db.QuestionType() { Id = 1, TypeText = "Text 1" }); - questionDbContext.QuestionTypes.Add(new Db.QuestionType() { Id = 2, TypeText = "Text 2" }); - questionDbContext.QuestionTypes.Add(new Db.QuestionType() { Id = 3, TypeText = "Text 3" }); - questionDbContext.QuestionTypes.Add(new Db.QuestionType() { Id = 4, TypeText = "Text 4" }); - questionDbContext.QuestionTypes.Add(new Db.QuestionType() { Id = 5, TypeText = "Text 5" }); + questionDbContext.QuestionTypes.Add(new Db.QuestionType() { TypeText = "RadioButton" }); + questionDbContext.QuestionTypes.Add(new Db.QuestionType() { TypeText = "CheckBox" }); + questionDbContext.QuestionTypes.Add(new Db.QuestionType() { TypeText = "TextBox" }); questionDbContext.SaveChanges(); } - - if (!questionDbContext.QuestionCategories.Any()) + if (!questionDbContext.Questions.Any()) { - questionDbContext.QuestionCategories.Add(new Db.QuestionCategory() { Id = 1, CategoryName = "Category 1", CategoryImage="img1" }); - questionDbContext.QuestionCategories.Add(new Db.QuestionCategory() { Id = 2, CategoryName = "Category 2", CategoryImage = "img1" }); - questionDbContext.QuestionCategories.Add(new Db.QuestionCategory() { Id = 3, CategoryName = "Category 3", CategoryImage = "img1" }); - questionDbContext.QuestionCategories.Add(new Db.QuestionCategory() { Id = 4, CategoryName = "Category 4", CategoryImage = "img1" }); - questionDbContext.QuestionCategories.Add(new Db.QuestionCategory() { Id = 5, CategoryName = "Category 5", CategoryImage = "img1" }); + questionDbContext.Questions.Add(new Db.Question() { QuestionTypeId = 2, SurveyId = 1, QuestionNumber = 1, IsRequired = true, Comment = false, Key = true, QuestionGroup = "group1",CategoryId=1 }); + questionDbContext.Questions.Add(new Db.Question() { QuestionTypeId = 3, SurveyId = 1, QuestionNumber = 2, IsRequired = false, Comment = true, Key = false, QuestionGroup = "group1", CategoryId = 1 }); + questionDbContext.Questions.Add(new Db.Question() { QuestionTypeId = 1, SurveyId = 1, QuestionNumber = 3, IsRequired = true, Comment = false, Key = true, QuestionGroup = "group1", CategoryId = 2 }); + questionDbContext.SaveChanges(); + } + if (!questionDbContext.QuestionsTranslations.Any()) + { + questionDbContext.QuestionsTranslations.Add(new Db.QuestionsTranslation() { QuestionId = 1, QuestionText = "Can You Open ?", Language = "en" }); + questionDbContext.QuestionsTranslations.Add(new Db.QuestionsTranslation() { QuestionId = 1, QuestionText = "Peux-tu ouvrir ?", Language = "fr" }); + questionDbContext.QuestionsTranslations.Add(new Db.QuestionsTranslation() { QuestionId = 2, QuestionText = "Are the grounds flodded ?", Language = "en" }); + questionDbContext.QuestionsTranslations.Add(new Db.QuestionsTranslation() { QuestionId = 2, QuestionText = "Les terrains sont-ils inondés ?", Language = "fr" }); + questionDbContext.QuestionsTranslations.Add(new Db.QuestionsTranslation() { QuestionId = 3, QuestionText = "Is the access blocked by flooding ?", Language = "en" }); + questionDbContext.QuestionsTranslations.Add(new Db.QuestionsTranslation() { QuestionId = 3, QuestionText = "L'accès est-il bloqué par les inondations ?", Language = "fr" }); + questionDbContext.QuestionsTranslations.Add(new Db.QuestionsTranslation() { QuestionId = 1, QuestionText = "Puedes abrir ?", Language = "sp" }); + questionDbContext.QuestionsTranslations.Add(new Db.QuestionsTranslation() { QuestionId = 2, QuestionText = "¿Están inundados los terrenos?", Language = "sp" }); + questionDbContext.QuestionsTranslations.Add(new Db.QuestionsTranslation() { QuestionId = 3, QuestionText = "¿El acceso está bloqueado por inundaciones?", Language = "sp" }); questionDbContext.SaveChanges(); } diff --git a/DamageAssesmentApi/DamageAssesment.Api.Questions/appsettings.json b/DamageAssesmentApi/DamageAssesment.Api.Questions/appsettings.json index 10f68b8..2139fc2 100644 --- a/DamageAssesmentApi/DamageAssesment.Api.Questions/appsettings.json +++ b/DamageAssesmentApi/DamageAssesment.Api.Questions/appsettings.json @@ -5,5 +5,8 @@ "Microsoft.AspNetCore": "Warning" } }, - "AllowedHosts": "*" + "AllowedHosts": "*", + "ConnectionStrings": { + "QuestionConnection": "Server=DESKTOP-OF5DPLQ\\SQLEXPRESS;Database=da_survey_dev;Trusted_Connection=True;TrustServerCertificate=True;" + } } diff --git a/DamageAssesmentApi/DamageAssesment.Api.QuestionsTest/DamageAssesment.Api.Questions.Test.csproj b/DamageAssesmentApi/DamageAssesment.Api.QuestionsTest/DamageAssesment.Api.Questions.Test.csproj index fea3dd0..46d6ab1 100644 --- a/DamageAssesmentApi/DamageAssesment.Api.QuestionsTest/DamageAssesment.Api.Questions.Test.csproj +++ b/DamageAssesmentApi/DamageAssesment.Api.QuestionsTest/DamageAssesment.Api.Questions.Test.csproj @@ -10,7 +10,7 @@ - + diff --git a/DamageAssesmentApi/DamageAssesment.Api.SurveyResponses/DamageAssesment.Api.SurveyResponses.csproj b/DamageAssesmentApi/DamageAssesment.Api.SurveyResponses/DamageAssesment.Api.SurveyResponses.csproj index a2383fa..303332f 100644 --- a/DamageAssesmentApi/DamageAssesment.Api.SurveyResponses/DamageAssesment.Api.SurveyResponses.csproj +++ b/DamageAssesmentApi/DamageAssesment.Api.SurveyResponses/DamageAssesment.Api.SurveyResponses.csproj @@ -8,10 +8,19 @@ - - + + + all + runtime; build; native; contentfiles; analyzers; buildtransitive + + + + + all + runtime; build; native; contentfiles; analyzers; buildtransitive + - + diff --git a/DamageAssesmentApi/DamageAssesment.Api.SurveyResponses/Db/SurveyResponseDbContext.cs b/DamageAssesmentApi/DamageAssesment.Api.SurveyResponses/Db/SurveyResponseDbContext.cs index bb8a2a2..1ac41f6 100644 --- a/DamageAssesmentApi/DamageAssesment.Api.SurveyResponses/Db/SurveyResponseDbContext.cs +++ b/DamageAssesmentApi/DamageAssesment.Api.SurveyResponses/Db/SurveyResponseDbContext.cs @@ -4,11 +4,16 @@ namespace DamageAssesment.Api.SurveyResponses.Db { public class SurveyResponseDbContext:DbContext { - public DbSet SurveyResponses { get; set; } - - public SurveyResponseDbContext(DbContextOptions options) : base(options) + private IConfiguration _Configuration { get; set; } + public SurveyResponseDbContext(DbContextOptions options, IConfiguration configuration) : base(options) { - + _Configuration = configuration; } + protected override void OnConfiguring(DbContextOptionsBuilder options) + { + // connect to sql server with connection string from app settings + options.UseSqlServer(_Configuration.GetConnectionString("SurveyResponseConnection")); + } + public DbSet SurveyResponses { get; set; } } } diff --git a/DamageAssesmentApi/DamageAssesment.Api.SurveyResponses/Migrations/20230817221348_InitialSurveyResponse.Designer.cs b/DamageAssesmentApi/DamageAssesment.Api.SurveyResponses/Migrations/20230817221348_InitialSurveyResponse.Designer.cs new file mode 100644 index 0000000..edec629 --- /dev/null +++ b/DamageAssesmentApi/DamageAssesment.Api.SurveyResponses/Migrations/20230817221348_InitialSurveyResponse.Designer.cs @@ -0,0 +1,55 @@ +// +using DamageAssesment.Api.SurveyResponses.Db; +using Microsoft.EntityFrameworkCore; +using Microsoft.EntityFrameworkCore.Infrastructure; +using Microsoft.EntityFrameworkCore.Metadata; +using Microsoft.EntityFrameworkCore.Migrations; +using Microsoft.EntityFrameworkCore.Storage.ValueConversion; + +#nullable disable + +namespace DamageAssesment.Api.SurveyResponses.Migrations +{ + [DbContext(typeof(SurveyResponseDbContext))] + [Migration("20230817221348_InitialSurveyResponse")] + partial class InitialSurveyResponse + { + /// + protected override void BuildTargetModel(ModelBuilder modelBuilder) + { +#pragma warning disable 612, 618 + modelBuilder + .HasAnnotation("ProductVersion", "7.0.9") + .HasAnnotation("Relational:MaxIdentifierLength", 128); + + SqlServerModelBuilderExtensions.UseIdentityColumns(modelBuilder); + + modelBuilder.Entity("DamageAssesment.Api.SurveyResponses.Db.SurveyResponse", b => + { + b.Property("Id") + .ValueGeneratedOnAdd() + .HasColumnType("int"); + + SqlServerPropertyBuilderExtensions.UseIdentityColumn(b.Property("Id")); + + b.Property("EmployeeId") + .IsRequired() + .HasMaxLength(6) + .HasColumnType("nvarchar(6)"); + + b.Property("LocationId") + .IsRequired() + .HasMaxLength(4) + .HasColumnType("nvarchar(4)"); + + b.Property("SurveyId") + .HasColumnType("int"); + + b.HasKey("Id"); + + b.ToTable("SurveyResponses"); + }); +#pragma warning restore 612, 618 + } + } +} diff --git a/DamageAssesmentApi/DamageAssesment.Api.SurveyResponses/Migrations/20230817221348_InitialSurveyResponse.cs b/DamageAssesmentApi/DamageAssesment.Api.SurveyResponses/Migrations/20230817221348_InitialSurveyResponse.cs new file mode 100644 index 0000000..e886ab1 --- /dev/null +++ b/DamageAssesmentApi/DamageAssesment.Api.SurveyResponses/Migrations/20230817221348_InitialSurveyResponse.cs @@ -0,0 +1,36 @@ +using Microsoft.EntityFrameworkCore.Migrations; + +#nullable disable + +namespace DamageAssesment.Api.SurveyResponses.Migrations +{ + /// + public partial class InitialSurveyResponse : Migration + { + /// + protected override void Up(MigrationBuilder migrationBuilder) + { + migrationBuilder.CreateTable( + name: "SurveyResponses", + columns: table => new + { + Id = table.Column(type: "int", nullable: false) + .Annotation("SqlServer:Identity", "1, 1"), + SurveyId = table.Column(type: "int", nullable: false), + LocationId = table.Column(type: "nvarchar(4)", maxLength: 4, nullable: false), + EmployeeId = table.Column(type: "nvarchar(6)", maxLength: 6, nullable: false) + }, + constraints: table => + { + table.PrimaryKey("PK_SurveyResponses", x => x.Id); + }); + } + + /// + protected override void Down(MigrationBuilder migrationBuilder) + { + migrationBuilder.DropTable( + name: "SurveyResponses"); + } + } +} diff --git a/DamageAssesmentApi/DamageAssesment.Api.SurveyResponses/Migrations/SurveyResponseDbContextModelSnapshot.cs b/DamageAssesmentApi/DamageAssesment.Api.SurveyResponses/Migrations/SurveyResponseDbContextModelSnapshot.cs new file mode 100644 index 0000000..1870d21 --- /dev/null +++ b/DamageAssesmentApi/DamageAssesment.Api.SurveyResponses/Migrations/SurveyResponseDbContextModelSnapshot.cs @@ -0,0 +1,52 @@ +// +using DamageAssesment.Api.SurveyResponses.Db; +using Microsoft.EntityFrameworkCore; +using Microsoft.EntityFrameworkCore.Infrastructure; +using Microsoft.EntityFrameworkCore.Metadata; +using Microsoft.EntityFrameworkCore.Storage.ValueConversion; + +#nullable disable + +namespace DamageAssesment.Api.SurveyResponses.Migrations +{ + [DbContext(typeof(SurveyResponseDbContext))] + partial class SurveyResponseDbContextModelSnapshot : ModelSnapshot + { + protected override void BuildModel(ModelBuilder modelBuilder) + { +#pragma warning disable 612, 618 + modelBuilder + .HasAnnotation("ProductVersion", "7.0.9") + .HasAnnotation("Relational:MaxIdentifierLength", 128); + + SqlServerModelBuilderExtensions.UseIdentityColumns(modelBuilder); + + modelBuilder.Entity("DamageAssesment.Api.SurveyResponses.Db.SurveyResponse", b => + { + b.Property("Id") + .ValueGeneratedOnAdd() + .HasColumnType("int"); + + SqlServerPropertyBuilderExtensions.UseIdentityColumn(b.Property("Id")); + + b.Property("EmployeeId") + .IsRequired() + .HasMaxLength(6) + .HasColumnType("nvarchar(6)"); + + b.Property("LocationId") + .IsRequired() + .HasMaxLength(4) + .HasColumnType("nvarchar(4)"); + + b.Property("SurveyId") + .HasColumnType("int"); + + b.HasKey("Id"); + + b.ToTable("SurveyResponses"); + }); +#pragma warning restore 612, 618 + } + } +} diff --git a/DamageAssesmentApi/DamageAssesment.Api.SurveyResponses/Program.cs b/DamageAssesmentApi/DamageAssesment.Api.SurveyResponses/Program.cs index b94d753..aa06647 100644 --- a/DamageAssesmentApi/DamageAssesment.Api.SurveyResponses/Program.cs +++ b/DamageAssesmentApi/DamageAssesment.Api.SurveyResponses/Program.cs @@ -53,7 +53,7 @@ builder.Services.AddEndpointsApiExplorer(); builder.Services.AddSwaggerGen(); builder.Services.AddDbContext(option => { - option.UseInMemoryDatabase("SurveyResponses"); + option.UseSqlServer("SurveyResponseConnection"); }); var app = builder.Build(); diff --git a/DamageAssesmentApi/DamageAssesment.Api.SurveyResponses/Providers/SurveyResponsesProvider.cs b/DamageAssesmentApi/DamageAssesment.Api.SurveyResponses/Providers/SurveyResponsesProvider.cs index 48a6a1a..bcc7338 100644 --- a/DamageAssesmentApi/DamageAssesment.Api.SurveyResponses/Providers/SurveyResponsesProvider.cs +++ b/DamageAssesmentApi/DamageAssesment.Api.SurveyResponses/Providers/SurveyResponsesProvider.cs @@ -42,13 +42,13 @@ namespace DamageAssesment.Api.SurveyResponses.Providers { if (!surveyResponseDbContext.SurveyResponses.Any()) { - surveyResponseDbContext.SurveyResponses.Add(new Db.SurveyResponse { Id = 1, SurveyId = 1, EmployeeId = "Emp1", LocationId = "Loc1" }); - surveyResponseDbContext.SurveyResponses.Add(new Db.SurveyResponse { Id = 2, SurveyId = 1, EmployeeId = "Emp2", LocationId = "Loc2" }); - surveyResponseDbContext.SurveyResponses.Add(new Db.SurveyResponse { Id = 3, SurveyId = 3, EmployeeId = "Emp4", LocationId = "Loc1" }); - surveyResponseDbContext.SurveyResponses.Add(new Db.SurveyResponse { Id = 4, SurveyId = 4, EmployeeId = "Emp1", LocationId = "Loc2" }); - surveyResponseDbContext.SurveyResponses.Add(new Db.SurveyResponse { Id = 5, SurveyId = 1, EmployeeId = "Emp3", LocationId = "Loc3" }); - surveyResponseDbContext.SurveyResponses.Add(new Db.SurveyResponse { Id = 6, SurveyId = 1, EmployeeId = "Emp4", LocationId = "Loc2" }); - surveyResponseDbContext.SurveyResponses.Add(new Db.SurveyResponse { Id = 7, SurveyId = 1, EmployeeId = "Emp4", LocationId = "Loc3" }); + surveyResponseDbContext.SurveyResponses.Add(new Db.SurveyResponse { SurveyId = 1, EmployeeId = "Emp1", LocationId = "Loc1" }); + surveyResponseDbContext.SurveyResponses.Add(new Db.SurveyResponse { SurveyId = 1, EmployeeId = "Emp2", LocationId = "Loc2" }); + surveyResponseDbContext.SurveyResponses.Add(new Db.SurveyResponse { SurveyId = 3, EmployeeId = "Emp4", LocationId = "Loc1" }); + surveyResponseDbContext.SurveyResponses.Add(new Db.SurveyResponse { SurveyId = 4, EmployeeId = "Emp1", LocationId = "Loc2" }); + surveyResponseDbContext.SurveyResponses.Add(new Db.SurveyResponse { SurveyId = 1, EmployeeId = "Emp3", LocationId = "Loc3" }); + surveyResponseDbContext.SurveyResponses.Add(new Db.SurveyResponse { SurveyId = 1, EmployeeId = "Emp4", LocationId = "Loc2" }); + surveyResponseDbContext.SurveyResponses.Add(new Db.SurveyResponse { SurveyId = 1, EmployeeId = "Emp4", LocationId = "Loc3" }); surveyResponseDbContext.SaveChanges(); } } diff --git a/DamageAssesmentApi/DamageAssesment.Api.SurveyResponses/appsettings.json b/DamageAssesmentApi/DamageAssesment.Api.SurveyResponses/appsettings.json index 2d6dfe4..569c0e0 100644 --- a/DamageAssesmentApi/DamageAssesment.Api.SurveyResponses/appsettings.json +++ b/DamageAssesmentApi/DamageAssesment.Api.SurveyResponses/appsettings.json @@ -6,6 +6,9 @@ } }, "AllowedHosts": "*", + "ConnectionStrings": { + "SurveyResponseConnection": "Server=DESKTOP-OF5DPLQ\\SQLEXPRESS;Database=da_survey_dev;Trusted_Connection=True;TrustServerCertificate=True;" + }, "EndPointSettings": { "AnswerUrlBase": "http://localhost:5200", "LocationUrlBase": "http://localhost:5213", diff --git a/DamageAssesmentApi/DamageAssesment.Api.Surveys/DamageAssesment.Api.Survey.csproj b/DamageAssesmentApi/DamageAssesment.Api.Surveys/DamageAssesment.Api.Survey.csproj index 53e8700..f6a61e2 100644 --- a/DamageAssesmentApi/DamageAssesment.Api.Surveys/DamageAssesment.Api.Survey.csproj +++ b/DamageAssesmentApi/DamageAssesment.Api.Surveys/DamageAssesment.Api.Survey.csproj @@ -8,8 +8,17 @@ - - + + + all + runtime; build; native; contentfiles; analyzers; buildtransitive + + + + + all + runtime; build; native; contentfiles; analyzers; buildtransitive + diff --git a/DamageAssesmentApi/DamageAssesment.Api.Surveys/Db/SurveysDbContext.cs b/DamageAssesmentApi/DamageAssesment.Api.Surveys/Db/SurveysDbContext.cs index 9ff36cf..6677612 100644 --- a/DamageAssesmentApi/DamageAssesment.Api.Surveys/Db/SurveysDbContext.cs +++ b/DamageAssesmentApi/DamageAssesment.Api.Surveys/Db/SurveysDbContext.cs @@ -4,9 +4,15 @@ namespace DamageAssesment.Api.Surveys.Db { public class SurveysDbContext:DbContext { - public SurveysDbContext(DbContextOptions options) : base(options) + private IConfiguration _Configuration { get; set; } + public SurveysDbContext(DbContextOptions options, IConfiguration configuration) : base(options) { - + _Configuration = configuration; + } + protected override void OnConfiguring(DbContextOptionsBuilder options) + { + // connect to sql server with connection string from app settings + options.UseSqlServer(_Configuration.GetConnectionString("SurveyConnection")); } public DbSet Surveys { get; set; } diff --git a/DamageAssesmentApi/DamageAssesment.Api.Surveys/Migrations/20230817220614_InitialSurvey.Designer.cs b/DamageAssesmentApi/DamageAssesment.Api.Surveys/Migrations/20230817220614_InitialSurvey.Designer.cs new file mode 100644 index 0000000..c0f4172 --- /dev/null +++ b/DamageAssesmentApi/DamageAssesment.Api.Surveys/Migrations/20230817220614_InitialSurvey.Designer.cs @@ -0,0 +1,57 @@ +// +using System; +using DamageAssesment.Api.Surveys.Db; +using Microsoft.EntityFrameworkCore; +using Microsoft.EntityFrameworkCore.Infrastructure; +using Microsoft.EntityFrameworkCore.Metadata; +using Microsoft.EntityFrameworkCore.Migrations; +using Microsoft.EntityFrameworkCore.Storage.ValueConversion; + +#nullable disable + +namespace DamageAssesment.Api.Survey.Migrations +{ + [DbContext(typeof(SurveysDbContext))] + [Migration("20230817220614_InitialSurvey")] + partial class InitialSurvey + { + /// + protected override void BuildTargetModel(ModelBuilder modelBuilder) + { +#pragma warning disable 612, 618 + modelBuilder + .HasAnnotation("ProductVersion", "7.0.9") + .HasAnnotation("Relational:MaxIdentifierLength", 128); + + SqlServerModelBuilderExtensions.UseIdentityColumns(modelBuilder); + + modelBuilder.Entity("DamageAssesment.Api.Surveys.Db.Survey", b => + { + b.Property("Id") + .ValueGeneratedOnAdd() + .HasColumnType("int"); + + SqlServerPropertyBuilderExtensions.UseIdentityColumn(b.Property("Id")); + + b.Property("EndDate") + .HasColumnType("datetime2"); + + b.Property("IsEnabled") + .HasColumnType("bit"); + + b.Property("StartDate") + .HasColumnType("datetime2"); + + b.Property("Title") + .IsRequired() + .HasMaxLength(100) + .HasColumnType("nvarchar(100)"); + + b.HasKey("Id"); + + b.ToTable("Surveys"); + }); +#pragma warning restore 612, 618 + } + } +} diff --git a/DamageAssesmentApi/DamageAssesment.Api.Surveys/Migrations/20230817220614_InitialSurvey.cs b/DamageAssesmentApi/DamageAssesment.Api.Surveys/Migrations/20230817220614_InitialSurvey.cs new file mode 100644 index 0000000..1d62693 --- /dev/null +++ b/DamageAssesmentApi/DamageAssesment.Api.Surveys/Migrations/20230817220614_InitialSurvey.cs @@ -0,0 +1,38 @@ +using System; +using Microsoft.EntityFrameworkCore.Migrations; + +#nullable disable + +namespace DamageAssesment.Api.Survey.Migrations +{ + /// + public partial class InitialSurvey : Migration + { + /// + protected override void Up(MigrationBuilder migrationBuilder) + { + migrationBuilder.CreateTable( + name: "Surveys", + columns: table => new + { + Id = table.Column(type: "int", nullable: false) + .Annotation("SqlServer:Identity", "1, 1"), + Title = table.Column(type: "nvarchar(100)", maxLength: 100, nullable: false), + IsEnabled = table.Column(type: "bit", nullable: false), + StartDate = table.Column(type: "datetime2", nullable: true), + EndDate = table.Column(type: "datetime2", nullable: true) + }, + constraints: table => + { + table.PrimaryKey("PK_Surveys", x => x.Id); + }); + } + + /// + protected override void Down(MigrationBuilder migrationBuilder) + { + migrationBuilder.DropTable( + name: "Surveys"); + } + } +} diff --git a/DamageAssesmentApi/DamageAssesment.Api.Surveys/Migrations/SurveysDbContextModelSnapshot.cs b/DamageAssesmentApi/DamageAssesment.Api.Surveys/Migrations/SurveysDbContextModelSnapshot.cs new file mode 100644 index 0000000..969a062 --- /dev/null +++ b/DamageAssesmentApi/DamageAssesment.Api.Surveys/Migrations/SurveysDbContextModelSnapshot.cs @@ -0,0 +1,54 @@ +// +using System; +using DamageAssesment.Api.Surveys.Db; +using Microsoft.EntityFrameworkCore; +using Microsoft.EntityFrameworkCore.Infrastructure; +using Microsoft.EntityFrameworkCore.Metadata; +using Microsoft.EntityFrameworkCore.Storage.ValueConversion; + +#nullable disable + +namespace DamageAssesment.Api.Survey.Migrations +{ + [DbContext(typeof(SurveysDbContext))] + partial class SurveysDbContextModelSnapshot : ModelSnapshot + { + protected override void BuildModel(ModelBuilder modelBuilder) + { +#pragma warning disable 612, 618 + modelBuilder + .HasAnnotation("ProductVersion", "7.0.9") + .HasAnnotation("Relational:MaxIdentifierLength", 128); + + SqlServerModelBuilderExtensions.UseIdentityColumns(modelBuilder); + + modelBuilder.Entity("DamageAssesment.Api.Surveys.Db.Survey", b => + { + b.Property("Id") + .ValueGeneratedOnAdd() + .HasColumnType("int"); + + SqlServerPropertyBuilderExtensions.UseIdentityColumn(b.Property("Id")); + + b.Property("EndDate") + .HasColumnType("datetime2"); + + b.Property("IsEnabled") + .HasColumnType("bit"); + + b.Property("StartDate") + .HasColumnType("datetime2"); + + b.Property("Title") + .IsRequired() + .HasMaxLength(100) + .HasColumnType("nvarchar(100)"); + + b.HasKey("Id"); + + b.ToTable("Surveys"); + }); +#pragma warning restore 612, 618 + } + } +} diff --git a/DamageAssesmentApi/DamageAssesment.Api.Surveys/Program.cs b/DamageAssesmentApi/DamageAssesment.Api.Surveys/Program.cs index ebb638f..6ef18b6 100644 --- a/DamageAssesmentApi/DamageAssesment.Api.Surveys/Program.cs +++ b/DamageAssesmentApi/DamageAssesment.Api.Surveys/Program.cs @@ -15,7 +15,7 @@ builder.Services.AddEndpointsApiExplorer(); builder.Services.AddSwaggerGen(); builder.Services.AddDbContext(option => { - option.UseInMemoryDatabase("Surveys"); + option.UseSqlServer("SurveyConnection"); }); var app = builder.Build(); diff --git a/DamageAssesmentApi/DamageAssesment.Api.Surveys/Providers/SurveysProvider.cs b/DamageAssesmentApi/DamageAssesment.Api.Surveys/Providers/SurveysProvider.cs index c85ed4c..bde3ee8 100644 --- a/DamageAssesmentApi/DamageAssesment.Api.Surveys/Providers/SurveysProvider.cs +++ b/DamageAssesmentApi/DamageAssesment.Api.Surveys/Providers/SurveysProvider.cs @@ -23,9 +23,9 @@ namespace DamageAssesment.Api.Surveys.Providers { if (!surveyDbContext.Surveys.Any()) { - surveyDbContext.Surveys.Add(new Db.Survey { Id = 1, Title = "Sample Survey Title:Damage Assesment 2014", IsEnabled = true, StartDate = DateTime.Now, EndDate = DateTime.Now.AddDays(90) }); - surveyDbContext.Surveys.Add(new Db.Survey { Id = 2, Title = "Sample Survey Title: Damage Assesment 2016", IsEnabled = true, StartDate = DateTime.Now, EndDate = DateTime.Now.AddDays(90) }); - surveyDbContext.Surveys.Add(new Db.Survey { Id = 3, Title = "Sample Survey Title: Damage Assesment 2018", IsEnabled = true, StartDate = DateTime.Now, EndDate = DateTime.Now.AddDays(90) }); + surveyDbContext.Surveys.Add(new Db.Survey { Title = "Sample Survey Title:Damage Assesment 2014", IsEnabled = true, StartDate = DateTime.Now, EndDate = DateTime.Now.AddDays(90) }); + surveyDbContext.Surveys.Add(new Db.Survey { Title = "Sample Survey Title: Damage Assesment 2016", IsEnabled = true, StartDate = DateTime.Now, EndDate = DateTime.Now.AddDays(90) }); + surveyDbContext.Surveys.Add(new Db.Survey { Title = "Sample Survey Title: Damage Assesment 2018", IsEnabled = true, StartDate = DateTime.Now, EndDate = DateTime.Now.AddDays(90) }); surveyDbContext.SaveChanges(); } } diff --git a/DamageAssesmentApi/DamageAssesment.Api.Surveys/appsettings.json b/DamageAssesmentApi/DamageAssesment.Api.Surveys/appsettings.json index 10f68b8..713c5f6 100644 --- a/DamageAssesmentApi/DamageAssesment.Api.Surveys/appsettings.json +++ b/DamageAssesmentApi/DamageAssesment.Api.Surveys/appsettings.json @@ -5,5 +5,8 @@ "Microsoft.AspNetCore": "Warning" } }, - "AllowedHosts": "*" + "AllowedHosts": "*", + "ConnectionStrings": { + "SurveyConnection": "Server=DESKTOP-OF5DPLQ\\SQLEXPRESS;Database=da_survey_dev;Trusted_Connection=True;TrustServerCertificate=True;" + } } From 9c536a1c524649b47c5fbf580519e4f1cc4d8c91 Mon Sep 17 00:00:00 2001 From: uppuv Date: Thu, 31 Aug 2023 19:12:47 -0400 Subject: [PATCH 2/3] Modified Documents api for InMemory Logic --- .../Db/DocumentDbContext.cs | 9 +-------- .../DamageAssesment.Api.Documents/Program.cs | 2 +- .../Providers/DocumentsProvider.cs | 2 +- .../DamageAssesment.Api.Documents/appsettings.json | 3 --- 4 files changed, 3 insertions(+), 13 deletions(-) diff --git a/DamageAssesmentApi/DamageAssesment.Api.Documents/Db/DocumentDbContext.cs b/DamageAssesmentApi/DamageAssesment.Api.Documents/Db/DocumentDbContext.cs index f0ddc60..4df2f77 100644 --- a/DamageAssesmentApi/DamageAssesment.Api.Documents/Db/DocumentDbContext.cs +++ b/DamageAssesmentApi/DamageAssesment.Api.Documents/Db/DocumentDbContext.cs @@ -7,19 +7,12 @@ namespace DamageAssesment.Api.Documents.Db { public class DocumentDbContext : DbContext { - private IConfiguration _Configuration { get; set; } - public DocumentDbContext(DbContextOptions options, IConfiguration configuration) : base(options) + public DocumentDbContext(DbContextOptions options) : base(options) { - _Configuration = configuration; } public DbSet Documents { get; set; } public DbSet LinkTypes { get; set; } public DbSet DocumentsTranslations { get; set; } - protected override void OnConfiguring(DbContextOptionsBuilder options) - { - // connect to sql server with connection string from app settings - options.UseSqlServer(_Configuration.GetConnectionString("DocumentConnection")); - } protected override void OnModelCreating(ModelBuilder modelBuilder) { base.OnModelCreating(modelBuilder); diff --git a/DamageAssesmentApi/DamageAssesment.Api.Documents/Program.cs b/DamageAssesmentApi/DamageAssesment.Api.Documents/Program.cs index a1e9361..af38287 100644 --- a/DamageAssesmentApi/DamageAssesment.Api.Documents/Program.cs +++ b/DamageAssesmentApi/DamageAssesment.Api.Documents/Program.cs @@ -17,7 +17,7 @@ builder.Services.AddScoped(); builder.Services.AddAutoMapper(AppDomain.CurrentDomain.GetAssemblies()); //4/30 builder.Services.AddDbContext(option => { - option.UseSqlServer("DocumentConnection"); + option.UseInMemoryDatabase("DocumentConnection"); }); var app = builder.Build(); diff --git a/DamageAssesmentApi/DamageAssesment.Api.Documents/Providers/DocumentsProvider.cs b/DamageAssesmentApi/DamageAssesment.Api.Documents/Providers/DocumentsProvider.cs index 8b6c1ee..4af3174 100644 --- a/DamageAssesmentApi/DamageAssesment.Api.Documents/Providers/DocumentsProvider.cs +++ b/DamageAssesmentApi/DamageAssesment.Api.Documents/Providers/DocumentsProvider.cs @@ -49,7 +49,7 @@ namespace DamageAssesment.Api.Documents.Providers } if (!DocumentDbContext.DocumentsTranslations.Any()) { - Db.DocumentsTranslation documents = new Db.DocumentsTranslation { Id = 0, DocumentId = 1, title = "Test", description = "ss", Language = "en" }; + Db.DocumentsTranslation documents = new Db.DocumentsTranslation { DocumentId = 1, title = "Test", description = "ss", Language = "en" }; DocumentDbContext.DocumentsTranslations.Add(documents); DocumentDbContext.SaveChanges(); } diff --git a/DamageAssesmentApi/DamageAssesment.Api.Documents/appsettings.json b/DamageAssesmentApi/DamageAssesment.Api.Documents/appsettings.json index 5006fa8..e38d9fb 100644 --- a/DamageAssesmentApi/DamageAssesment.Api.Documents/appsettings.json +++ b/DamageAssesmentApi/DamageAssesment.Api.Documents/appsettings.json @@ -9,8 +9,5 @@ "Fileupload": { "folderpath": "DASA_Documents/Active", "Deletepath": "DASA_Documents/Deleted" - }, - "ConnectionStrings": { - "DocumentConnection": "Server=DESKTOP-OF5DPLQ\\SQLEXPRESS;Database=da_survey_dev;Trusted_Connection=True;TrustServerCertificate=True;" } } From 0544c7397d216250b3027f9f35e472fac55ac28c Mon Sep 17 00:00:00 2001 From: uppuv Date: Mon, 11 Sep 2023 13:23:50 -0400 Subject: [PATCH 3/3] 314: updated document multi language output format --- ...DamageAssesment.Api.DocuLinks.Test.csproj} | 2 +- .../DocumentsServiceTest.cs | 30 ++--- .../MockData.cs | 23 ++-- .../Controllers/DocumentsController.cs | 74 ++++++------ ...j => DamageAssesment.Api.DocuLinks.csproj} | 0 .../Interfaces/IDocumentsProvider.cs | 13 +- .../Models/Document.cs | 12 +- .../Profiles/DocumentProfile.cs | 1 + .../Providers/DocumentsProvider.cs | 114 ++++++++++++++---- DamageAssesmentApi/DamageAssesment.sln | 4 +- 10 files changed, 176 insertions(+), 97 deletions(-) rename DamageAssesmentApi/DamageAssesment.Api.Documents.Test/{DamageAssesment.Api.Documents.Test.csproj => DamageAssesment.Api.DocuLinks.Test.csproj} (95%) rename DamageAssesmentApi/DamageAssesment.Api.Documents/{DamageAssesment.Api.Documents.csproj => DamageAssesment.Api.DocuLinks.csproj} (100%) diff --git a/DamageAssesmentApi/DamageAssesment.Api.Documents.Test/DamageAssesment.Api.Documents.Test.csproj b/DamageAssesmentApi/DamageAssesment.Api.Documents.Test/DamageAssesment.Api.DocuLinks.Test.csproj similarity index 95% rename from DamageAssesmentApi/DamageAssesment.Api.Documents.Test/DamageAssesment.Api.Documents.Test.csproj rename to DamageAssesmentApi/DamageAssesment.Api.Documents.Test/DamageAssesment.Api.DocuLinks.Test.csproj index 725bea6..e990a2c 100644 --- a/DamageAssesmentApi/DamageAssesment.Api.Documents.Test/DamageAssesment.Api.Documents.Test.csproj +++ b/DamageAssesmentApi/DamageAssesment.Api.Documents.Test/DamageAssesment.Api.DocuLinks.Test.csproj @@ -21,7 +21,7 @@ - + diff --git a/DamageAssesmentApi/DamageAssesment.Api.Documents.Test/DocumentsServiceTest.cs b/DamageAssesmentApi/DamageAssesment.Api.Documents.Test/DocumentsServiceTest.cs index 9d63289..3490c42 100644 --- a/DamageAssesmentApi/DamageAssesment.Api.Documents.Test/DocumentsServiceTest.cs +++ b/DamageAssesmentApi/DamageAssesment.Api.Documents.Test/DocumentsServiceTest.cs @@ -17,7 +17,7 @@ namespace DamageAssesment.Api.Documents.Test var mockDocumentService = new Mock(); var mockUploadService = new Mock(); var mockResponse = await MockData.getNoContentResponse(); - mockDocumentService.Setup(service => service.GetDocumnetsAsync()).ReturnsAsync(mockResponse); + mockDocumentService.Setup(service => service.GetDocumnetsAsync("en")).ReturnsAsync(mockResponse); var DocumentProvider = new DocumentsController(mockDocumentService.Object, mockUploadService.Object); var result = (NoContentResult)await DocumentProvider.GetDocumentsbyFormsandLanguageAsync("", ""); @@ -31,7 +31,7 @@ namespace DamageAssesment.Api.Documents.Test var mockDocumentService = new Mock(); var mockUploadService = new Mock(); var mockResponse = await MockData.getNoContentResponse(); - mockDocumentService.Setup(service => service.GetDocumnetsAsync()).ReturnsAsync(mockResponse); + mockDocumentService.Setup(service => service.GetDocumnetsAsync("en")).ReturnsAsync(mockResponse); var DocumentProvider = new DocumentsController(mockDocumentService.Object, mockUploadService.Object); var result = (NoContentResult)await DocumentProvider.GetDocumentsbyFormsandLanguageAsync("", ""); @@ -44,10 +44,10 @@ namespace DamageAssesment.Api.Documents.Test var mockDocumentService = new Mock(); var mockUploadService = new Mock(); var mockResponse = await MockData.getOkResponse(); - mockDocumentService.Setup(service => service.GetDocumnetsAsync()).ReturnsAsync(mockResponse); + mockDocumentService.Setup(service => service.GetDocumnetsAsync("en")).ReturnsAsync(mockResponse); var DocumentProvider = new DocumentsController(mockDocumentService.Object, mockUploadService.Object); - var result = (OkObjectResult)await DocumentProvider.GetDocumentsAsync(); + var result = (OkObjectResult)await DocumentProvider.GetDocumentsAsync("en"); Assert.Equal(200, result.StatusCode); } @@ -58,10 +58,10 @@ namespace DamageAssesment.Api.Documents.Test var mockDocumentService = new Mock(); var mockUploadService = new Mock(); var mockResponse = await MockData.getNoContentResponse(); - mockDocumentService.Setup(service => service.GetDocumnetsAsync()).ReturnsAsync(mockResponse); + mockDocumentService.Setup(service => service.GetDocumnetsAsync("en")).ReturnsAsync(mockResponse); var DocumentProvider = new DocumentsController(mockDocumentService.Object, mockUploadService.Object); - var result = (NoContentResult)await DocumentProvider.GetDocumentsAsync(); + var result = (NoContentResult)await DocumentProvider.GetDocumentsAsync("en"); Assert.Equal(204, result.StatusCode); } @@ -72,10 +72,10 @@ namespace DamageAssesment.Api.Documents.Test var mockDocumentService = new Mock(); var mockUploadService = new Mock(); var mockResponse = await MockData.getOkResponse(1); - mockDocumentService.Setup(service => service.GetDocumentAsync(1)).ReturnsAsync(mockResponse); + mockDocumentService.Setup(service => service.GetDocumentAsync(1,"en")).ReturnsAsync(mockResponse); var DocumentProvider = new DocumentsController(mockDocumentService.Object, mockUploadService.Object); - var result = (OkObjectResult)await DocumentProvider.GetDocumentAsync(1); + var result = (OkObjectResult)await DocumentProvider.GetDocumentAsync(1,"en"); Assert.Equal(200, result.StatusCode); } @@ -86,9 +86,9 @@ namespace DamageAssesment.Api.Documents.Test var mockDocumentService = new Mock(); var mockUploadService = new Mock(); var mockResponse = await MockData.getNotFoundResponse(); - mockDocumentService.Setup(service => service.GetDocumentAsync(99)).ReturnsAsync(mockResponse); + mockDocumentService.Setup(service => service.GetDocumentAsync(99, "en")).ReturnsAsync(mockResponse); var DocumentProvider = new DocumentsController(mockDocumentService.Object, mockUploadService.Object); - var result = (NotFoundResult)await DocumentProvider.GetDocumentAsync(99); + var result = (NotFoundResult)await DocumentProvider.GetDocumentAsync(99, "en"); Assert.Equal(404, result.StatusCode); } [Fact(DisplayName = "Post Document - Ok case")] @@ -129,11 +129,11 @@ namespace DamageAssesment.Api.Documents.Test var mockResponse = await MockData.getOkResponse(1); var mockInputDocument = await MockData.getInputDocumentData(); var DocumentResponse = await MockData.GetDocumentsInfo(1); - mockDocumentService.Setup(service => service.UpdateDocumentAsync(mockInputDocument)).ReturnsAsync(mockResponse); + mockDocumentService.Setup(service => service.UpdateDocumentAsync(1,mockInputDocument)).ReturnsAsync(mockResponse); var DocumentProvider = new DocumentsController(mockDocumentService.Object, mockUploadService.Object); - var result = (NoContentResult)await DocumentProvider.UpdateDocument(DocumentResponse); + var result = (NotFoundResult)await DocumentProvider.UpdateDocument(1,DocumentResponse); - Assert.Equal(204, result.StatusCode); + Assert.Equal(404, result.StatusCode); } [Fact(DisplayName = "Put Document - BadRequest case")] @@ -143,9 +143,9 @@ namespace DamageAssesment.Api.Documents.Test var mockUploadService = new Mock(); var mockResponse = await MockData.getBadRequestResponse(); var mockInputDocument = await MockData.getInputDocumentData(); - mockDocumentService.Setup(service => service.UpdateDocumentAsync(mockInputDocument)).ReturnsAsync(mockResponse); + mockDocumentService.Setup(service => service.UpdateDocumentAsync(99,mockInputDocument)).ReturnsAsync(mockResponse); var DocumentProvider = new DocumentsController(mockDocumentService.Object, mockUploadService.Object); - var result = (BadRequestObjectResult)await DocumentProvider.UpdateDocument(null); + var result = (BadRequestObjectResult)await DocumentProvider.UpdateDocument(99,null); Assert.Equal(400, result.StatusCode); } diff --git a/DamageAssesmentApi/DamageAssesment.Api.Documents.Test/MockData.cs b/DamageAssesmentApi/DamageAssesment.Api.Documents.Test/MockData.cs index 374a929..e1d3f65 100644 --- a/DamageAssesmentApi/DamageAssesment.Api.Documents.Test/MockData.cs +++ b/DamageAssesmentApi/DamageAssesment.Api.Documents.Test/MockData.cs @@ -11,12 +11,16 @@ namespace DamageAssesment.Api.Documents.Test public class MockData { - public static async Task<(bool, List, string)> getOkResponse() + public static async Task<(bool, List, string)> getOkResponse() { - List list = new List(); + List list = new List(); for (int i = 1; i < 4; i++) { + Dictionary dicttitle = new Dictionary(); + Dictionary dictdesc = new Dictionary(); + dicttitle.Add("en", "test"); dicttitle.Add("fr", "tester"); + dictdesc.Add("en", "test"); dictdesc.Add("fr", "tester"); List documentsTranslations = new List(); documentsTranslations.Add(new DocumentsTranslation() { @@ -24,7 +28,7 @@ namespace DamageAssesment.Api.Documents.Test title = "tel"+i, description = "Sample"+i }); - list.Add(new Documents.Models.Document() + list.Add(new Documents.Models.MultiLanDocument() { Id = i, @@ -33,32 +37,33 @@ namespace DamageAssesment.Api.Documents.Test url = "testurl" + i, Path = "testpath" + i, IsActive = true, - documentsTranslations= documentsTranslations, + titles= dicttitle, + description=dictdesc, dateCreated = DateTime.Now, dateUpdated = DateTime.Now }); } return (true, list, null); } - public static async Task<(bool, Documents.Models.Document, string)> getOkResponse(int Id) + public static async Task<(bool, Documents.Models.MultiLanDocument, string)> getOkResponse(int Id) { var Documents = await getOkResponse(); var Document = Documents.Item2.FirstOrDefault(s => s.Id == Id); return (true, Document, null); } - public static async Task<(bool, Documents.Models.Document, string)> getBadRequestResponse() + public static async Task<(bool, Documents.Models.MultiLanDocument, string)> getBadRequestResponse() { return (false, null, "Bad Request"); } - public static async Task<(bool, Documents.Models.Document, string)> getNotFoundResponse() + public static async Task<(bool, Documents.Models.MultiLanDocument, string)> getNotFoundResponse() { return (false, null, "Not Found"); } - public static async Task<(bool, IEnumerable, string)> getNoContentResponse() + public static async Task<(bool, IEnumerable, string)> getNoContentResponse() { - IEnumerable list = new List(); + IEnumerable list = new List(); return (false, list, null); } public static async Task GetDocumentsInfo(int id) diff --git a/DamageAssesmentApi/DamageAssesment.Api.Documents/Controllers/DocumentsController.cs b/DamageAssesmentApi/DamageAssesment.Api.Documents/Controllers/DocumentsController.cs index 38d8ee5..bdb0437 100644 --- a/DamageAssesmentApi/DamageAssesment.Api.Documents/Controllers/DocumentsController.cs +++ b/DamageAssesmentApi/DamageAssesment.Api.Documents/Controllers/DocumentsController.cs @@ -7,7 +7,6 @@ using Microsoft.AspNetCore.Mvc; namespace DamageAssesment.Api.Documents.Controllers { - [Route("api")] [ApiController] public class DocumentsController : ControllerBase { @@ -25,10 +24,13 @@ namespace DamageAssesment.Api.Documents.Controllers /// /// Get all documnets. /// - [HttpGet("{language}/{LinkType}/documents")] - public async Task GetDocumentsbyFormsandLanguageAsync(string language,string LinkType) + /// + [Route("doculinks/{linktype:alpha}")] + [Route("doculinks/{linktype:alpha}/{language:alpha}")] + [HttpGet] + public async Task GetDocumentsbyFormsandLanguageAsync(string linktype, string? language) { - var result = await this.documentsProvider.GetDocumnetsByLinkAsync(language, LinkType); + var result = await this.documentsProvider.GetDocumnetsByLinkAsync(language, linktype); if (result.IsSuccess) { return Ok(result.documents); @@ -38,23 +40,13 @@ namespace DamageAssesment.Api.Documents.Controllers /// /// Get all documnets. /// - [HttpGet("{LinkType}/documents")] - public async Task GetDocumentsbyFormAsync(string LinkType) + /// + //[Route("doculinks/{language:alpha}")] + [Route("doculinks")] + [HttpGet] + public async Task GetDocumentsAsync(string? language) { - var result = await this.documentsProvider.GetDocumnetsByLinkAsync(null, LinkType); - if (result.IsSuccess) - { - return Ok(result.documents); - } - return NoContent(); - } - /// - /// Get all documnets. - /// - [HttpGet("documents")] - public async Task GetDocumentsAsync() - { - var result = await this.documentsProvider.GetDocumnetsAsync(); + var result = await this.documentsProvider.GetDocumnetsAsync(language); if (result.IsSuccess) { return Ok(result.documents); @@ -65,10 +57,12 @@ namespace DamageAssesment.Api.Documents.Controllers /// /// Get a documnet by id. /// - [HttpGet("documents/{id}")] - public async Task GetDocumentAsync(int id) + [HttpGet] + [Route("doculinks/{id}")] + [Route("doculinks/{id}/{language:alpha}")] + public async Task GetDocumentAsync(int id,string? language) { - var result = await this.documentsProvider.GetDocumentAsync(id); + var result = await this.documentsProvider.GetDocumentAsync(id,language); if (result.IsSuccess) { return Ok(result.Document); @@ -78,30 +72,32 @@ namespace DamageAssesment.Api.Documents.Controllers /// /// Upload new document. /// - [HttpPut("documents")] - public async Task UpdateDocument(DocumentInfo documentInfo) + [HttpPut] + [Route("doculinks/{id}")] + public async Task UpdateDocument(int id,DocumentInfo documentInfo) { if (documentInfo != null) { - var dbdoc = await this.documentsProvider.GetDocumentAsync(documentInfo.Id); + var dbdoc = await this.documentsProvider.GetDocumentByidAsync(id); if (dbdoc.IsSuccess) { Models.Document document = uploadService.UpdateDocuments(dbdoc.Document, documentInfo); - var result = await this.documentsProvider.UpdateDocumentAsync(document); + var result = await this.documentsProvider.UpdateDocumentAsync(id,document); if (result.IsSuccess) { return Ok(result.Document); } return NoContent(); } - return NoContent(); + return NotFound(); } return BadRequest(documentInfo); } /// /// update existing document. /// - [HttpPost("documents")] + [HttpPost] + [Route("doculinks")] public async Task CreateDocument(DocumentInfo documentInfo) { try @@ -127,7 +123,8 @@ namespace DamageAssesment.Api.Documents.Controllers /// /// Delete documnet by id. /// - [HttpDelete("documents/{id}")] + [HttpDelete] + [Route("doculinks/{id}")] public async Task DeleteDocument(int id) { // database soft delete @@ -143,7 +140,8 @@ namespace DamageAssesment.Api.Documents.Controllers /// /// Get all document link type. /// - [HttpGet("LinkTypes")] + [HttpGet] + [Route("doculinks/types")] public async Task GetLinkTypesAsync() { var result = await this.documentsProvider.GetLinkTypesAsync(); @@ -156,7 +154,8 @@ namespace DamageAssesment.Api.Documents.Controllers /// /// Get a document link type by id. /// - [HttpGet("LinkTypes/{id}")] + [HttpGet] + [Route("doculinks/types/{id}")] public async Task GetLinkTypeAsync(int id) { var result = await this.documentsProvider.GetLinkTypeAsync(id); @@ -169,7 +168,8 @@ namespace DamageAssesment.Api.Documents.Controllers /// /// Update a existing document link type. /// - [HttpPut("LinkTypes")] + [HttpPut] + [Route("doculinks/types")] public async Task UpdateLinkType(Models.LinkType linkType) { if (linkType != null) @@ -189,7 +189,8 @@ namespace DamageAssesment.Api.Documents.Controllers /// /// Create a new document link type. /// - [HttpPost("LinkTypes")] + [HttpPost] + [Route("doculinks/types")] public async Task CreateLinkType(Models.LinkType linkType) { if (linkType != null) @@ -206,7 +207,8 @@ namespace DamageAssesment.Api.Documents.Controllers /// /// Delete a existing document link type by id. /// - [HttpDelete("LinkTypes/{id}")] + [HttpDelete] + [Route("doculinks/types/{id}")] public async Task DeleteLinkType(int id) { var result = await this.documentsProvider.DeleteLinkTypeAsync(id); @@ -216,7 +218,5 @@ namespace DamageAssesment.Api.Documents.Controllers } return NotFound(); } - - } } diff --git a/DamageAssesmentApi/DamageAssesment.Api.Documents/DamageAssesment.Api.Documents.csproj b/DamageAssesmentApi/DamageAssesment.Api.Documents/DamageAssesment.Api.DocuLinks.csproj similarity index 100% rename from DamageAssesmentApi/DamageAssesment.Api.Documents/DamageAssesment.Api.Documents.csproj rename to DamageAssesmentApi/DamageAssesment.Api.Documents/DamageAssesment.Api.DocuLinks.csproj diff --git a/DamageAssesmentApi/DamageAssesment.Api.Documents/Interfaces/IDocumentsProvider.cs b/DamageAssesmentApi/DamageAssesment.Api.Documents/Interfaces/IDocumentsProvider.cs index 845a51f..7cb4e79 100644 --- a/DamageAssesmentApi/DamageAssesment.Api.Documents/Interfaces/IDocumentsProvider.cs +++ b/DamageAssesmentApi/DamageAssesment.Api.Documents/Interfaces/IDocumentsProvider.cs @@ -4,12 +4,13 @@ namespace DamageAssesment.Api.Documents.Interfaces { public interface IDocumentsProvider : ILinkTypesProvider { - Task<(bool IsSuccess, Models.Document Document, string ErrorMessage)> GetDocumentAsync(int Id); - Task<(bool IsSuccess, IEnumerable documents, string ErrorMessage)> GetDocumnetsAsync(); - Task<(bool IsSuccess, IEnumerable documents, string ErrorMessage)> GetDocumnetsByLinkAsync(string Language,string LinkType); - Task<(bool IsSuccess, Models.Document Document, string ErrorMessage)> PostDocumentAsync(Models.Document Document); - Task<(bool IsSuccess, Models.Document Document, string ErrorMessage)> UpdateDocumentAsync(Models.Document Document); - Task<(bool IsSuccess, Models.Document Document, string ErrorMessage)> DeleteDocumentAsync(int Id); + Task<(bool IsSuccess, Models.MultiLanDocument Document, string ErrorMessage)> GetDocumentAsync(int id,string? language); + Task<(bool IsSuccess, Models.Document Document, string ErrorMessage)> GetDocumentByidAsync(int id); + Task<(bool IsSuccess, IEnumerable documents, string ErrorMessage)> GetDocumnetsAsync(string? language); + Task<(bool IsSuccess, IEnumerable documents, string ErrorMessage)> GetDocumnetsByLinkAsync(string? Language,string LinkType); + Task<(bool IsSuccess, Models.MultiLanDocument Document, string ErrorMessage)> PostDocumentAsync(Models.Document Document); + Task<(bool IsSuccess, Models.MultiLanDocument Document, string ErrorMessage)> UpdateDocumentAsync(int id,Models.Document Document); + Task<(bool IsSuccess, Models.MultiLanDocument Document, string ErrorMessage)> DeleteDocumentAsync(int id); Task<(bool IsSuccess, int counter, string message)> GetDocumentCounter(); diff --git a/DamageAssesmentApi/DamageAssesment.Api.Documents/Models/Document.cs b/DamageAssesmentApi/DamageAssesment.Api.Documents/Models/Document.cs index 1652dca..7ad6c0f 100644 --- a/DamageAssesmentApi/DamageAssesment.Api.Documents/Models/Document.cs +++ b/DamageAssesmentApi/DamageAssesment.Api.Documents/Models/Document.cs @@ -2,7 +2,16 @@ namespace DamageAssesment.Api.Documents.Models { - public class Document + public class Document : BaseDocument + { + public List documentsTranslations { get; set; } + } + public class MultiLanDocument:BaseDocument + { + public object titles { get; set; } + public object description { get; set; } + } + public class BaseDocument { public int Id { get; set; } @@ -14,6 +23,5 @@ namespace DamageAssesment.Api.Documents.Models public bool IsActive { get; set; } public DateTime dateCreated { get; set; } public DateTime dateUpdated { get; set; } - public List documentsTranslations { get; set; } } } diff --git a/DamageAssesmentApi/DamageAssesment.Api.Documents/Profiles/DocumentProfile.cs b/DamageAssesmentApi/DamageAssesment.Api.Documents/Profiles/DocumentProfile.cs index f9a2570..755a59b 100644 --- a/DamageAssesmentApi/DamageAssesment.Api.Documents/Profiles/DocumentProfile.cs +++ b/DamageAssesmentApi/DamageAssesment.Api.Documents/Profiles/DocumentProfile.cs @@ -6,6 +6,7 @@ namespace DamageAssesment.Api.Documents.Profiles { public DocumentProfile() { + CreateMap(); CreateMap(); CreateMap(); CreateMap(); diff --git a/DamageAssesmentApi/DamageAssesment.Api.Documents/Providers/DocumentsProvider.cs b/DamageAssesmentApi/DamageAssesment.Api.Documents/Providers/DocumentsProvider.cs index 4af3174..9286b97 100644 --- a/DamageAssesmentApi/DamageAssesment.Api.Documents/Providers/DocumentsProvider.cs +++ b/DamageAssesmentApi/DamageAssesment.Api.Documents/Providers/DocumentsProvider.cs @@ -7,6 +7,7 @@ using Microsoft.EntityFrameworkCore; using Microsoft.EntityFrameworkCore.Metadata.Internal; using System; using System.Diagnostics.Eventing.Reader; +using System.Reflection.Metadata; namespace DamageAssesment.Api.Documents.Providers { @@ -54,7 +55,36 @@ namespace DamageAssesment.Api.Documents.Providers DocumentDbContext.SaveChanges(); } } - public async Task<(bool IsSuccess, IEnumerable documents, string ErrorMessage)> GetDocumnetsByLinkAsync(string Language, string LinkType) + public List GetDocumentTranslations(int id, string? language) + { + List QuestionTranslations; + if (string.IsNullOrEmpty(language)) + { + QuestionTranslations = mapper.Map, List>( + DocumentDbContext.DocumentsTranslations.AsNoTracking().Where(a => a.DocumentId == id).ToList()); + } + else + { + QuestionTranslations = mapper.Map, List>( + DocumentDbContext.DocumentsTranslations.AsNoTracking().Where(a => a.DocumentId == id && a.Language == language).ToList()); + } + return QuestionTranslations; + } + public MultiLanDocument CreateMultiLanguageObject(List questions) + { + MultiLanDocument MultiLanguage = new MultiLanDocument(); + Dictionary dicttitle = new Dictionary(); + Dictionary dictdesc = new Dictionary(); + foreach (Models.DocumentsTranslation item in questions) + { + dicttitle.Add(item.Language, item.title); + dictdesc.Add(item.Language, item.description); + } + MultiLanguage.titles = dicttitle; + MultiLanguage.description = dictdesc; + return MultiLanguage; + } + public async Task<(bool IsSuccess, IEnumerable documents, string ErrorMessage)> GetDocumnetsByLinkAsync(string? language, string? LinkType) { try @@ -68,15 +98,12 @@ namespace DamageAssesment.Api.Documents.Providers q.linkTypeId == (DocumentDbContext.LinkTypes.AsNoTracking().Where(a => a.TypeText.ToLower() == LinkType.ToLower()).Select(a => a.Id).FirstOrDefault())).ToListAsync(); if (documents != null) { - var result = mapper.Map, IEnumerable>(documents); + var result = mapper.Map, List>(documents); foreach (var item in result) { - if(!String.IsNullOrEmpty(Language)) - item.documentsTranslations = mapper.Map, List>( - DocumentDbContext.DocumentsTranslations.Where(a => a.DocumentId == item.Id && (a.Language.ToLower() == Language.ToLower())).ToList()); - else - item.documentsTranslations = mapper.Map, List>( - DocumentDbContext.DocumentsTranslations.Where(a => a.DocumentId == item.Id).ToList()); + var multilan = CreateMultiLanguageObject(GetDocumentTranslations(item.Id, language)); + item.titles = multilan.titles; + item.description = multilan.description; } return (true, result, null); } @@ -89,7 +116,7 @@ namespace DamageAssesment.Api.Documents.Providers } } - public async Task<(bool IsSuccess, IEnumerable documents, string ErrorMessage)> GetDocumnetsAsync() + public async Task<(bool IsSuccess, IEnumerable documents, string ErrorMessage)> GetDocumnetsAsync(string? language) { try @@ -99,11 +126,12 @@ namespace DamageAssesment.Api.Documents.Providers if (documents != null) { logger?.LogInformation($"{documents.Count} Document(s) found"); - var result = mapper.Map, IEnumerable>(documents); + var result = mapper.Map, List>(documents); foreach (var item in result) { - item.documentsTranslations = mapper.Map, List>( - DocumentDbContext.DocumentsTranslations.Where(a => a.DocumentId == item.Id).ToList()); + var multilan = CreateMultiLanguageObject(GetDocumentTranslations(item.Id, language)); + item.titles = multilan.titles; + item.description = multilan.description; } return (true, result, null); } @@ -116,16 +144,16 @@ namespace DamageAssesment.Api.Documents.Providers } } - public async Task<(bool IsSuccess, Models.Document Document, string ErrorMessage)> GetDocumentAsync(int Id) + public async Task<(bool IsSuccess, Models.Document Document, string ErrorMessage)> GetDocumentByidAsync(int id) { try { logger?.LogInformation("Query LinkType"); - var Document = await DocumentDbContext.Documents.AsNoTracking().FirstOrDefaultAsync(q => q.Id == Id && q.IsActive); + var Document = await DocumentDbContext.Documents.AsNoTracking().FirstOrDefaultAsync(q => q.Id == id && q.IsActive); if (Document != null) { logger?.LogInformation($"{Document} customer(s) found"); - var result = mapper.Map(Document); + var result = mapper.Map(Document); result.documentsTranslations = mapper.Map, List>( DocumentDbContext.DocumentsTranslations.Where(a => a.DocumentId == result.Id).ToList()); return (true, result, null); @@ -138,7 +166,30 @@ namespace DamageAssesment.Api.Documents.Providers return (false, null, ex.Message); } } - public async Task<(bool IsSuccess, Models.Document Document, string ErrorMessage)> PostDocumentAsync(Models.Document Document) + public async Task<(bool IsSuccess, Models.MultiLanDocument Document, string ErrorMessage)> GetDocumentAsync(int id,string? language) + { + try + { + logger?.LogInformation("Query LinkType"); + var Document = await DocumentDbContext.Documents.AsNoTracking().FirstOrDefaultAsync(q => q.Id == id && q.IsActive); + if (Document != null) + { + logger?.LogInformation($"{Document} customer(s) found"); + var result = mapper.Map(Document); + var multilan = CreateMultiLanguageObject(GetDocumentTranslations(result.Id, language)); + result.titles = multilan.titles; + result.description = multilan.description; + return (true, result, null); + } + return (false, null, "Not found"); + } + catch (Exception ex) + { + logger?.LogError(ex.ToString()); + return (false, null, ex.Message); + } + } + public async Task<(bool IsSuccess, Models.MultiLanDocument Document, string ErrorMessage)> PostDocumentAsync(Models.Document Document) { try { @@ -149,8 +200,11 @@ namespace DamageAssesment.Api.Documents.Providers dbtranslation.ForEach(i => i.DocumentId = document.Id); DocumentDbContext.DocumentsTranslations.AddRange(dbtranslation); DocumentDbContext.SaveChanges(); - Document.Id = document.Id; - return (true, Document, null); + var result = mapper.Map(document); + var multilan = CreateMultiLanguageObject(GetDocumentTranslations(document.Id, "")); + result.titles = multilan.titles; + result.description = multilan.description; + return (true, result, null); } catch (Exception ex) { @@ -159,15 +213,16 @@ namespace DamageAssesment.Api.Documents.Providers } } - public async Task<(bool IsSuccess, Models.Document Document, string ErrorMessage)> UpdateDocumentAsync(Models.Document Document) + public async Task<(bool IsSuccess, Models.MultiLanDocument Document, string ErrorMessage)> UpdateDocumentAsync(int id,Models.Document Document) { try { if (Document != null) { - var existing = DocumentDbContext.Documents.AsNoTracking().FirstOrDefault(x => x.Id == Document.Id); + var existing = DocumentDbContext.Documents.AsNoTracking().FirstOrDefault(x => x.Id == id); if (existing != null) { + Document.Id = existing.Id; var document = mapper.Map(Document); DocumentDbContext.Documents.Update(document); DocumentDbContext.SaveChanges(); @@ -176,7 +231,13 @@ namespace DamageAssesment.Api.Documents.Providers DocumentDbContext.DocumentsTranslations.RemoveRange(oldtranslations); var dbtranslation = mapper.Map, List>(Document.documentsTranslations); dbtranslation.ForEach(i => i.DocumentId = Document.Id); - return (true, Document, "Successful"); + DocumentDbContext.DocumentsTranslations.AddRange(dbtranslation); + DocumentDbContext.SaveChanges(); + var result = mapper.Map(document); + var multilan = CreateMultiLanguageObject(GetDocumentTranslations(document.Id, "")); + result.titles = multilan.titles; + result.description = multilan.description; + return (true, result, "Successful"); } else { @@ -198,20 +259,24 @@ namespace DamageAssesment.Api.Documents.Providers return (false, null, ex.Message); } } - public async Task<(bool IsSuccess, Models.Document Document, string ErrorMessage)> DeleteDocumentAsync(int Id) + public async Task<(bool IsSuccess, Models.MultiLanDocument Document, string ErrorMessage)> DeleteDocumentAsync(int id) { try { - Db.Document Document = DocumentDbContext.Documents.AsNoTracking().Where(a => a.Id == Id).FirstOrDefault(); + Db.Document Document = DocumentDbContext.Documents.AsNoTracking().Where(a => a.Id == id).FirstOrDefault(); if (Document == null) { return (false, null, "Not Found"); } + var result = mapper.Map(Document); + var multilan = CreateMultiLanguageObject(GetDocumentTranslations(Document.Id, "")); + result.titles = multilan.titles; + result.description = multilan.description; Document.IsActive = false; DocumentDbContext.Documents.Update(Document); DocumentDbContext.SaveChanges(); - return (true, mapper.Map(Document), $"DocumentId {Id} deleted Successfuly"); + return (true, result, $"DocumentId {id} deleted Successfuly"); } catch (Exception ex) { @@ -257,7 +322,6 @@ namespace DamageAssesment.Api.Documents.Providers } } - public async Task<(bool IsSuccess, Models.LinkType LinkType, string ErrorMessage)> GetLinkTypeAsync(int Id) { try diff --git a/DamageAssesmentApi/DamageAssesment.sln b/DamageAssesmentApi/DamageAssesment.sln index 2871442..20b7b6d 100644 --- a/DamageAssesmentApi/DamageAssesment.sln +++ b/DamageAssesmentApi/DamageAssesment.sln @@ -37,9 +37,9 @@ Project("{9A19103F-16F7-4668-BE54-9A1E7A4F7556}") = "DamageAssesment.Api.Employe EndProject Project("{9A19103F-16F7-4668-BE54-9A1E7A4F7556}") = "DamageAssesment.Api.Employees.Test", "DamageAssesment.Api.Employees.Test\DamageAssesment.Api.Employees.Test.csproj", "{D6BF9AE9-72FA-4726-A326-35A35D27FFB8}" EndProject -Project("{FAE04EC0-301F-11D3-BF4B-00C04F79EFBC}") = "DamageAssesment.Api.Documents", "DamageAssesment.Api.Documents\DamageAssesment.Api.Documents.csproj", "{977C1053-9D63-4153-B61B-C3D2F1010F9F}" +Project("{9A19103F-16F7-4668-BE54-9A1E7A4F7556}") = "DamageAssesment.Api.DocuLinks", "DamageAssesment.Api.Documents\DamageAssesment.Api.DocuLinks.csproj", "{977C1053-9D63-4153-B61B-C3D2F1010F9F}" EndProject -Project("{FAE04EC0-301F-11D3-BF4B-00C04F79EFBC}") = "DamageAssesment.Api.Documents.Test", "DamageAssesment.Api.Documents.Test\DamageAssesment.Api.Documents.Test.csproj", "{884BA4AC-9170-49B1-BD6B-850B350C95C0}" +Project("{9A19103F-16F7-4668-BE54-9A1E7A4F7556}") = "DamageAssesment.Api.DocuLinks.Test", "DamageAssesment.Api.Documents.Test\DamageAssesment.Api.DocuLinks.Test.csproj", "{884BA4AC-9170-49B1-BD6B-850B350C95C0}" EndProject Global GlobalSection(SolutionConfigurationPlatforms) = preSolution