Merge branch 'unstable' into 'main'

Add Excel export functionality and fix some errors

See merge request internship-2025/survey-webapp/survey-webapp!27
This commit is contained in:
Вячеслав 2025-06-08 21:15:08 +00:00
commit af2769ea59
9 changed files with 439 additions and 8 deletions

View file

@ -0,0 +1,26 @@
using Microsoft.AspNetCore.Mvc;
using SurveyLib.Tools.Tools;
namespace SurveyBackend.Controllers;
[ApiController]
[Route("api/export")]
public class ExportController
{
private readonly TableExporter _tableExporter;
public ExportController(TableExporter tableExporter)
{
_tableExporter = tableExporter;
}
[HttpGet("excel/{surveyId:int}")]
public async Task<IActionResult> ExportSurveyById(int surveyId)
{
var fileBytes = await _tableExporter.ExportDataBySurveyIdAsync(surveyId);
return new FileContentResult(fileBytes, "application/vnd.openxmlformats-officedocument.spreadsheetml.sheet")
{
FileDownloadName = $"survey_{surveyId}_{DateTime.UtcNow:yyyyMMdd_HHmmss}.xlsx"
};
}
}

View file

@ -70,8 +70,8 @@ public class QuestionController : ControllerBase
{
var question = QuestionMapper.QuestionUpdateToModel(dto, id);
await _questionService.UpdateQuestionAsync(question);
var result = QuestionMapper.ModelToQuestionDto(question);
return Ok(result);
var updatedQuestion = await _questionService.GetQuestionByIdAsync(id);
return Ok(updatedQuestion);
}
/// <summary>

View file

@ -12,6 +12,7 @@ using SurveyBackend.Middlewares;
using SurveyBackend.Services;
using SurveyLib.Infrastructure.EFCore;
using SurveyLib.Infrastructure.EFCore.Data;
using SurveyLib.Tools.Tools;
namespace SurveyBackend;
@ -38,6 +39,8 @@ public class Program
builder.Services.AddSurveyLibInfrastructure();
builder.Services.AddSurveyBackendServices();
builder.Services.AddScoped<TableExporter>();
builder.Services.AddAuthentication(JwtBearerDefaults.AuthenticationScheme)
.AddJwtBearer(options =>
{

View file

@ -33,6 +33,7 @@
</ItemGroup>
<ItemGroup>
<ProjectReference Include="..\..\SurveyLib\SurveyLib.Tools\SurveyLib.Tools.csproj" />
<ProjectReference Include="..\SurveyBackend.Core\SurveyBackend.Core.csproj"/>
<ProjectReference Include="..\SurveyBackend.Infrastructure\SurveyBackend.Infrastructure.csproj"/>
<ProjectReference Include="..\SurveyBackend.Services\SurveyBackend.Services.csproj"/>

View file

@ -0,0 +1,328 @@
// <auto-generated />
using System;
using Microsoft.EntityFrameworkCore;
using Microsoft.EntityFrameworkCore.Infrastructure;
using Microsoft.EntityFrameworkCore.Migrations;
using Microsoft.EntityFrameworkCore.Storage.ValueConversion;
using SurveyBackend.Infrastructure.Data;
#nullable disable
namespace SurveyBackend.Infrastructure.Data.Migrations
{
[DbContext(typeof(ApplicationDbContext))]
[Migration("20250608095147_Update answers")]
partial class Updateanswers
{
/// <inheritdoc />
protected override void BuildTargetModel(ModelBuilder modelBuilder)
{
#pragma warning disable 612, 618
modelBuilder.HasAnnotation("ProductVersion", "8.0.15");
modelBuilder.Entity("GroupUser", b =>
{
b.Property<int>("GroupsId")
.HasColumnType("INTEGER");
b.Property<int>("UsersId")
.HasColumnType("INTEGER");
b.HasKey("GroupsId", "UsersId");
b.HasIndex("UsersId");
b.ToTable("GroupUser");
});
modelBuilder.Entity("SurveyBackend.Core.Models.Group", b =>
{
b.Property<int>("Id")
.ValueGeneratedOnAdd()
.HasColumnType("INTEGER");
b.Property<string>("Label")
.IsRequired()
.HasColumnType("TEXT");
b.HasKey("Id");
b.ToTable("Groups");
});
modelBuilder.Entity("SurveyBackend.Core.Models.User", b =>
{
b.Property<int>("Id")
.ValueGeneratedOnAdd()
.HasColumnType("INTEGER");
b.Property<string>("Email")
.IsRequired()
.HasColumnType("TEXT");
b.Property<string>("FirstName")
.IsRequired()
.HasColumnType("TEXT");
b.Property<string>("LastName")
.IsRequired()
.HasColumnType("TEXT");
b.Property<string>("Password")
.IsRequired()
.HasColumnType("TEXT");
b.HasKey("Id");
b.ToTable("Users");
});
modelBuilder.Entity("SurveyLib.Core.Models.Answer", b =>
{
b.Property<int>("Id")
.ValueGeneratedOnAdd()
.HasColumnType("INTEGER");
b.Property<string>("AnswerText")
.IsRequired()
.HasColumnType("TEXT");
b.Property<int>("CompletionId")
.HasColumnType("INTEGER");
b.Property<int>("QuestionId")
.HasColumnType("INTEGER");
b.HasKey("Id");
b.HasIndex("QuestionId");
b.HasIndex("CompletionId", "QuestionId", "AnswerText")
.IsUnique();
b.ToTable("Answers");
});
modelBuilder.Entity("SurveyLib.Core.Models.AnswerVariant", b =>
{
b.Property<int>("Id")
.ValueGeneratedOnAdd()
.HasColumnType("INTEGER");
b.Property<int>("QuestionId")
.HasColumnType("INTEGER");
b.Property<string>("Text")
.IsRequired()
.HasColumnType("TEXT");
b.HasKey("Id");
b.HasIndex("QuestionId");
b.ToTable("AnswerVariants");
});
modelBuilder.Entity("SurveyLib.Core.Models.Completion", b =>
{
b.Property<int>("Id")
.ValueGeneratedOnAdd()
.HasColumnType("INTEGER");
b.Property<int?>("CompletedBy")
.HasColumnType("INTEGER");
b.Property<DateTime>("FinishedAt")
.HasColumnType("TEXT");
b.Property<int>("SurveyId")
.HasColumnType("INTEGER");
b.HasKey("Id");
b.HasIndex("CompletedBy");
b.HasIndex("SurveyId");
b.ToTable("Completions");
});
modelBuilder.Entity("SurveyLib.Core.Models.QuestionBase", b =>
{
b.Property<int>("Id")
.ValueGeneratedOnAdd()
.HasColumnType("INTEGER");
b.Property<string>("Discriminator")
.IsRequired()
.HasMaxLength(34)
.HasColumnType("TEXT");
b.Property<int>("SurveyId")
.HasColumnType("INTEGER");
b.Property<string>("Title")
.IsRequired()
.HasColumnType("TEXT");
b.HasKey("Id");
b.HasIndex("SurveyId");
b.ToTable("Questions");
b.HasDiscriminator().HasValue("QuestionBase");
b.UseTphMappingStrategy();
});
modelBuilder.Entity("SurveyLib.Core.Models.Survey", b =>
{
b.Property<int>("Id")
.ValueGeneratedOnAdd()
.HasColumnType("INTEGER");
b.Property<DateTime>("CreatedAt")
.HasColumnType("TEXT");
b.Property<int?>("CreatedBy")
.HasColumnType("INTEGER");
b.Property<string>("Description")
.IsRequired()
.HasColumnType("TEXT");
b.Property<string>("Title")
.IsRequired()
.HasColumnType("TEXT");
b.HasKey("Id");
b.HasIndex("CreatedBy");
b.ToTable("Surveys");
});
modelBuilder.Entity("SurveyLib.Core.Models.QuestionVariants.MultipleAnswerQuestion", b =>
{
b.HasBaseType("SurveyLib.Core.Models.QuestionBase");
b.HasDiscriminator().HasValue("MultipleAnswerQuestion");
});
modelBuilder.Entity("SurveyLib.Core.Models.QuestionVariants.SingleAnswerQuestion", b =>
{
b.HasBaseType("SurveyLib.Core.Models.QuestionBase");
b.HasDiscriminator().HasValue("SingleAnswerQuestion");
});
modelBuilder.Entity("SurveyLib.Core.Models.QuestionVariants.TextQuestion", b =>
{
b.HasBaseType("SurveyLib.Core.Models.QuestionBase");
b.HasDiscriminator().HasValue("TextQuestion");
});
modelBuilder.Entity("GroupUser", b =>
{
b.HasOne("SurveyBackend.Core.Models.Group", null)
.WithMany()
.HasForeignKey("GroupsId")
.OnDelete(DeleteBehavior.Cascade)
.IsRequired();
b.HasOne("SurveyBackend.Core.Models.User", null)
.WithMany()
.HasForeignKey("UsersId")
.OnDelete(DeleteBehavior.Cascade)
.IsRequired();
});
modelBuilder.Entity("SurveyLib.Core.Models.Answer", b =>
{
b.HasOne("SurveyLib.Core.Models.Completion", "Completion")
.WithMany("Answers")
.HasForeignKey("CompletionId")
.OnDelete(DeleteBehavior.Cascade)
.IsRequired();
b.HasOne("SurveyLib.Core.Models.QuestionBase", "Question")
.WithMany("Answers")
.HasForeignKey("QuestionId")
.OnDelete(DeleteBehavior.Cascade)
.IsRequired();
b.Navigation("Completion");
b.Navigation("Question");
});
modelBuilder.Entity("SurveyLib.Core.Models.AnswerVariant", b =>
{
b.HasOne("SurveyLib.Core.Models.QuestionBase", "Question")
.WithMany("AnswerVariants")
.HasForeignKey("QuestionId")
.OnDelete(DeleteBehavior.Cascade)
.IsRequired();
b.Navigation("Question");
});
modelBuilder.Entity("SurveyLib.Core.Models.Completion", b =>
{
b.HasOne("SurveyBackend.Core.Models.User", null)
.WithMany()
.HasForeignKey("CompletedBy")
.OnDelete(DeleteBehavior.SetNull);
b.HasOne("SurveyLib.Core.Models.Survey", "Survey")
.WithMany("Completions")
.HasForeignKey("SurveyId")
.OnDelete(DeleteBehavior.Cascade)
.IsRequired();
b.Navigation("Survey");
});
modelBuilder.Entity("SurveyLib.Core.Models.QuestionBase", b =>
{
b.HasOne("SurveyLib.Core.Models.Survey", "Survey")
.WithMany("Questions")
.HasForeignKey("SurveyId")
.OnDelete(DeleteBehavior.Cascade)
.IsRequired();
b.Navigation("Survey");
});
modelBuilder.Entity("SurveyLib.Core.Models.Survey", b =>
{
b.HasOne("SurveyBackend.Core.Models.User", null)
.WithMany()
.HasForeignKey("CreatedBy")
.OnDelete(DeleteBehavior.SetNull);
});
modelBuilder.Entity("SurveyLib.Core.Models.Completion", b =>
{
b.Navigation("Answers");
});
modelBuilder.Entity("SurveyLib.Core.Models.QuestionBase", b =>
{
b.Navigation("AnswerVariants");
b.Navigation("Answers");
});
modelBuilder.Entity("SurveyLib.Core.Models.Survey", b =>
{
b.Navigation("Completions");
b.Navigation("Questions");
});
#pragma warning restore 612, 618
}
}
}

View file

@ -0,0 +1,60 @@
using Microsoft.EntityFrameworkCore.Migrations;
#nullable disable
namespace SurveyBackend.Infrastructure.Data.Migrations
{
/// <inheritdoc />
public partial class Updateanswers : Migration
{
/// <inheritdoc />
protected override void Up(MigrationBuilder migrationBuilder)
{
migrationBuilder.DropPrimaryKey(
name: "PK_Answers",
table: "Answers");
migrationBuilder.AddColumn<int>(
name: "Id",
table: "Answers",
type: "INTEGER",
nullable: false,
defaultValue: 0)
.Annotation("Sqlite:Autoincrement", true);
migrationBuilder.Sql(@"UPDATE ""Answers"" SET ""Id"" = rowid;");
migrationBuilder.AddPrimaryKey(
name: "PK_Answers",
table: "Answers",
column: "Id");
migrationBuilder.CreateIndex(
name: "IX_Answers_CompletionId_QuestionId_AnswerText",
table: "Answers",
columns: new[] { "CompletionId", "QuestionId", "AnswerText" },
unique: true);
}
/// <inheritdoc />
protected override void Down(MigrationBuilder migrationBuilder)
{
migrationBuilder.DropPrimaryKey(
name: "PK_Answers",
table: "Answers");
migrationBuilder.DropIndex(
name: "IX_Answers_CompletionId_QuestionId_AnswerText",
table: "Answers");
migrationBuilder.DropColumn(
name: "Id",
table: "Answers");
migrationBuilder.AddPrimaryKey(
name: "PK_Answers",
table: "Answers",
columns: new[] { "CompletionId", "QuestionId" });
}
}
}

View file

@ -76,20 +76,27 @@ namespace SurveyBackend.Infrastructure.Data.Migrations
modelBuilder.Entity("SurveyLib.Core.Models.Answer", b =>
{
b.Property<int>("CompletionId")
.HasColumnType("INTEGER");
b.Property<int>("QuestionId")
b.Property<int>("Id")
.ValueGeneratedOnAdd()
.HasColumnType("INTEGER");
b.Property<string>("AnswerText")
.IsRequired()
.HasColumnType("TEXT");
b.HasKey("CompletionId", "QuestionId");
b.Property<int>("CompletionId")
.HasColumnType("INTEGER");
b.Property<int>("QuestionId")
.HasColumnType("INTEGER");
b.HasKey("Id");
b.HasIndex("QuestionId");
b.HasIndex("CompletionId", "QuestionId", "AnswerText")
.IsUnique();
b.ToTable("Answers");
});

View file

@ -12,6 +12,8 @@ Project("{FAE04EC0-301F-11D3-BF4B-00C04F79EFBC}") = "SurveyLib.Core", "..\Survey
EndProject
Project("{FAE04EC0-301F-11D3-BF4B-00C04F79EFBC}") = "SurveyBackend.Services", "SurveyBackend.Services\SurveyBackend.Services.csproj", "{3CDA6495-4FB2-4F07-8B2F-15BFD2A35181}"
EndProject
Project("{FAE04EC0-301F-11D3-BF4B-00C04F79EFBC}") = "SurveyLib.Tools", "..\SurveyLib\SurveyLib.Tools\SurveyLib.Tools.csproj", "{DA10F9E0-2682-438E-BC2B-C22B6BBD13CB}"
EndProject
Global
GlobalSection(SolutionConfigurationPlatforms) = preSolution
Debug|Any CPU = Debug|Any CPU
@ -42,5 +44,9 @@ Global
{3CDA6495-4FB2-4F07-8B2F-15BFD2A35181}.Debug|Any CPU.Build.0 = Debug|Any CPU
{3CDA6495-4FB2-4F07-8B2F-15BFD2A35181}.Release|Any CPU.ActiveCfg = Release|Any CPU
{3CDA6495-4FB2-4F07-8B2F-15BFD2A35181}.Release|Any CPU.Build.0 = Release|Any CPU
{DA10F9E0-2682-438E-BC2B-C22B6BBD13CB}.Debug|Any CPU.ActiveCfg = Debug|Any CPU
{DA10F9E0-2682-438E-BC2B-C22B6BBD13CB}.Debug|Any CPU.Build.0 = Debug|Any CPU
{DA10F9E0-2682-438E-BC2B-C22B6BBD13CB}.Release|Any CPU.ActiveCfg = Release|Any CPU
{DA10F9E0-2682-438E-BC2B-C22B6BBD13CB}.Release|Any CPU.Build.0 = Release|Any CPU
EndGlobalSection
EndGlobal

@ -1 +1 @@
Subproject commit d9f16ee761e31bb7af7a067c38f5fa02083f9d6c
Subproject commit aae9d32397b784f115111f6a05c6dcdd1fc4f91f