commit 2fa9c6fa801ee0611c5747c5d45e382a4a39f5d6 Author: majed adel Date: Sun Jan 19 12:45:19 2025 -0800 feat: Implement database schema, relationships and models for survey application - Designed an ERD (Entity-Relationship Diagram) for the survey system. - Defined entities: User, Survey, Question, Choice, Response, and Answer. - Established relationships: - Users can author multiple surveys. - Surveys contain multiple questions. - Questions have multiple choices. - Surveys can have multiple responses from participants. - Responses are linked to answers, questions, and choices. - Ensured compliance with best practices for relational database design. Note : no migration yet diff --git a/survey-beta.sln b/survey-beta.sln new file mode 100644 index 0000000..121ab4e --- /dev/null +++ b/survey-beta.sln @@ -0,0 +1,25 @@ + +Microsoft Visual Studio Solution File, Format Version 12.00 +# Visual Studio Version 17 +VisualStudioVersion = 17.11.35312.102 +MinimumVisualStudioVersion = 10.0.40219.1 +Project("{FAE04EC0-301F-11D3-BF4B-00C04F79EFBC}") = "survey-beta", "survey-beta\survey-beta.csproj", "{7A5087A4-5EFE-49C8-AD7A-A2AC9F815C32}" +EndProject +Global + GlobalSection(SolutionConfigurationPlatforms) = preSolution + Debug|Any CPU = Debug|Any CPU + Release|Any CPU = Release|Any CPU + EndGlobalSection + GlobalSection(ProjectConfigurationPlatforms) = postSolution + {7A5087A4-5EFE-49C8-AD7A-A2AC9F815C32}.Debug|Any CPU.ActiveCfg = Debug|Any CPU + {7A5087A4-5EFE-49C8-AD7A-A2AC9F815C32}.Debug|Any CPU.Build.0 = Debug|Any CPU + {7A5087A4-5EFE-49C8-AD7A-A2AC9F815C32}.Release|Any CPU.ActiveCfg = Release|Any CPU + {7A5087A4-5EFE-49C8-AD7A-A2AC9F815C32}.Release|Any CPU.Build.0 = Release|Any CPU + EndGlobalSection + GlobalSection(SolutionProperties) = preSolution + HideSolutionNode = FALSE + EndGlobalSection + GlobalSection(ExtensibilityGlobals) = postSolution + SolutionGuid = {AD3F914F-11E0-46E6-A86F-6C7743255DC4} + EndGlobalSection +EndGlobal diff --git a/survey-beta/DataBaseContext/AppDbContext.cs b/survey-beta/DataBaseContext/AppDbContext.cs new file mode 100644 index 0000000..efca097 --- /dev/null +++ b/survey-beta/DataBaseContext/AppDbContext.cs @@ -0,0 +1,56 @@ +using Microsoft.EntityFrameworkCore; +using survey_beta.Models; +namespace survey_beta.DataBaseContext +{ + public class AppDbContext : DbContext + { + public DbSet Users { get; set; } + public DbSet Surveys { get; set; } + public DbSet Questions { get; set; } + public DbSet Choices { get; set; } + public DbSet Responses { get; set; } + public DbSet Answers { get; set; } + + public AppDbContext(DbContextOptions options) : base(options) + { + } + protected override void OnModelCreating(ModelBuilder modelBuilder) + { + modelBuilder.Entity() + .HasOne(s => s.Author) + .WithMany(u => u.Surveys) + .HasForeignKey(s => s.AuthorId); + + modelBuilder.Entity() + .HasOne(q => q.Survey) + .WithMany(s => s.Questions) + .HasForeignKey(q => q.SurveyId); + + modelBuilder.Entity() + .HasOne(c => c.Question) + .WithMany(q => q.Choices) + .HasForeignKey(c => c.QuestionId); + + modelBuilder.Entity() + .HasOne(r => r.Survey) + .WithMany(s => s.Responses) + .HasForeignKey(r => r.SurveyId); + + modelBuilder.Entity() + .HasOne(a => a.Response) + .WithMany(r => r.Answers) + .HasForeignKey(a => a.ResponseId); + + modelBuilder.Entity() + .HasOne(a => a.Question) + .WithMany() + .HasForeignKey(a => a.QuestionId); + + modelBuilder.Entity() + .HasOne(a => a.Choice) + .WithMany() + .HasForeignKey(a => a.ChoiceId); + } + } +} + diff --git a/survey-beta/Models/Answer.cs b/survey-beta/Models/Answer.cs new file mode 100644 index 0000000..9e9b01b --- /dev/null +++ b/survey-beta/Models/Answer.cs @@ -0,0 +1,13 @@ +namespace survey_beta.Models +{ + public class Answer + { + public string Id { get; set; } + public string ResponseId { get; set; } + public Response Response { get; set; } + public string QuestionId { get; set; } + public Question Question { get; set; } + public string ChoiceId { get; set; } + public Choice Choice { get; set; } + } +} diff --git a/survey-beta/Models/Choice.cs b/survey-beta/Models/Choice.cs new file mode 100644 index 0000000..c2f6e2b --- /dev/null +++ b/survey-beta/Models/Choice.cs @@ -0,0 +1,11 @@ +namespace survey_beta.Models +{ + public class Choice + { + public string Id { get; set; } + public string Letter { get; set; } + public string Content { get; set; } + public string QuestionId { get; set; } + public Question Question { get; set; } + } +} diff --git a/survey-beta/Models/Question.cs b/survey-beta/Models/Question.cs new file mode 100644 index 0000000..1efc066 --- /dev/null +++ b/survey-beta/Models/Question.cs @@ -0,0 +1,11 @@ +namespace survey_beta.Models +{ + public class Question + { + public string Id { get; set; } + public string Content { get; set; } + public string SurveyId { get; set; } + public Survey Survey { get; set; } + public ICollection Choices { get; set; } + } +} diff --git a/survey-beta/Models/Response.cs b/survey-beta/Models/Response.cs new file mode 100644 index 0000000..9b06353 --- /dev/null +++ b/survey-beta/Models/Response.cs @@ -0,0 +1,11 @@ +namespace survey_beta.Models +{ + public class Response + { + public string Id { get; set; } + public string IpAddress { get; set; } + public string SurveyId { get; set; } + public Survey Survey { get; set; } + public ICollection Answers { get; set; } + } +} diff --git a/survey-beta/Models/Survey.cs b/survey-beta/Models/Survey.cs new file mode 100644 index 0000000..a9f6a4d --- /dev/null +++ b/survey-beta/Models/Survey.cs @@ -0,0 +1,16 @@ +namespace survey_beta.Models +{ + public class Survey + { + public string Id { get; set; } + public string Title { get; set; } + public string Description { get; set; } + public string Category { get; set; } + public DateTime ExpirationDate { get; set; } + public bool IsPublished { get; set; } + public string AuthorId { get; set; } + public User Author { get; set; } + public ICollection Questions { get; set; } + public ICollection Responses { get; set; } + } +} diff --git a/survey-beta/Models/User.cs b/survey-beta/Models/User.cs new file mode 100644 index 0000000..98691f7 --- /dev/null +++ b/survey-beta/Models/User.cs @@ -0,0 +1,13 @@ +namespace survey_beta.Models +{ + public class User + { + public string? Id { get; set; } + public string? Username { get; set; } + public string? Fullname { get; set; } + public string? Email { get; set; } + public string? PasswordHash { get; set; } + public ICollection Surveys { get; set; } = new List(); + + } +} diff --git a/survey-beta/Program.cs b/survey-beta/Program.cs new file mode 100644 index 0000000..e84aade --- /dev/null +++ b/survey-beta/Program.cs @@ -0,0 +1,23 @@ +using Microsoft.EntityFrameworkCore; +using survey_beta.DataBaseContext; + +var builder = WebApplication.CreateBuilder(args); + +// Add services to the container +builder.Services.AddDbContext(options => + options.UseNpgsql(builder.Configuration.GetConnectionString("DefaultConnection"))); + +builder.Services.AddControllers(); +builder.Services.AddEndpointsApiExplorer(); + +var app = builder.Build(); + +// Configure the HTTP request pipeline. + +app.UseHttpsRedirection(); + +app.UseAuthorization(); + +app.MapControllers(); + +app.Run(); \ No newline at end of file diff --git a/survey-beta/Properties/launchSettings.json b/survey-beta/Properties/launchSettings.json new file mode 100644 index 0000000..085c92c --- /dev/null +++ b/survey-beta/Properties/launchSettings.json @@ -0,0 +1,41 @@ +{ + "$schema": "http://json.schemastore.org/launchsettings.json", + "iisSettings": { + "windowsAuthentication": false, + "anonymousAuthentication": true, + "iisExpress": { + "applicationUrl": "http://localhost:5388", + "sslPort": 44392 + } + }, + "profiles": { + "http": { + "commandName": "Project", + "dotnetRunMessages": true, + "launchBrowser": true, + "launchUrl": "weatherforecast", + "applicationUrl": "http://localhost:5036", + "environmentVariables": { + "ASPNETCORE_ENVIRONMENT": "Development" + } + }, + "https": { + "commandName": "Project", + "dotnetRunMessages": true, + "launchBrowser": true, + "launchUrl": "weatherforecast", + "applicationUrl": "https://localhost:7002;http://localhost:5036", + "environmentVariables": { + "ASPNETCORE_ENVIRONMENT": "Development" + } + }, + "IIS Express": { + "commandName": "IISExpress", + "launchBrowser": true, + "launchUrl": "weatherforecast", + "environmentVariables": { + "ASPNETCORE_ENVIRONMENT": "Development" + } + } + } +} diff --git a/survey-beta/appsettings.Development.json b/survey-beta/appsettings.Development.json new file mode 100644 index 0000000..0c208ae --- /dev/null +++ b/survey-beta/appsettings.Development.json @@ -0,0 +1,8 @@ +{ + "Logging": { + "LogLevel": { + "Default": "Information", + "Microsoft.AspNetCore": "Warning" + } + } +} diff --git a/survey-beta/appsettings.json b/survey-beta/appsettings.json new file mode 100644 index 0000000..4254f60 --- /dev/null +++ b/survey-beta/appsettings.json @@ -0,0 +1,12 @@ +{ + "Logging": { + "LogLevel": { + "Default": "Information", + "Microsoft.AspNetCore": "Warning" + } + }, + "AllowedHosts": "*", + "ConnectionStrings": { + "DefaultConnection": "Host=localhost;Database=SurveyDB;Username=postgre;Password=MAJEDali645" + } +} \ No newline at end of file diff --git a/survey-beta/survey-beta.csproj b/survey-beta/survey-beta.csproj new file mode 100644 index 0000000..5715e09 --- /dev/null +++ b/survey-beta/survey-beta.csproj @@ -0,0 +1,24 @@ + + + + net8.0 + enable + enable + survey_beta + + + + + + all + runtime; build; native; contentfiles; analyzers; buildtransitive + + + + + + + + + + diff --git a/survey-beta/survey-beta.csproj.user b/survey-beta/survey-beta.csproj.user new file mode 100644 index 0000000..9ff5820 --- /dev/null +++ b/survey-beta/survey-beta.csproj.user @@ -0,0 +1,6 @@ + + + + https + + \ No newline at end of file diff --git a/survey-beta/survey-beta.http b/survey-beta/survey-beta.http new file mode 100644 index 0000000..391bf09 --- /dev/null +++ b/survey-beta/survey-beta.http @@ -0,0 +1,6 @@ +@survey_beta_HostAddress = http://localhost:5036 + +GET {{survey_beta_HostAddress}}/ +Accept: application/json + +###