From 06df9e2fa7f390d4ad296ff03bdacba6be8553b8 Mon Sep 17 00:00:00 2001 From: Josh Creek <8179928+jcreek@users.noreply.github.com> Date: Thu, 14 Sep 2023 22:14:08 +0100 Subject: [PATCH] chore(#8): Add sqlite database --- README.md | 6 ++ WhoAtX/Controllers/UserController.cs | 64 ++++++++++++------ WhoAtX/Data/DbContext.cs | 13 ++++ .../20230914205826_InitialCreate.Designer.cs | 67 +++++++++++++++++++ .../20230914205826_InitialCreate.cs | 41 ++++++++++++ .../ApplicationDbContextModelSnapshot.cs | 64 ++++++++++++++++++ WhoAtX/Models/UserProfile.cs | 14 ++++ WhoAtX/Program.cs | 9 +++ WhoAtX/WhoAtX.csproj | 13 ++++ WhoAtX/appsettings.json | 5 +- 10 files changed, 273 insertions(+), 23 deletions(-) create mode 100644 WhoAtX/Data/DbContext.cs create mode 100644 WhoAtX/Migrations/20230914205826_InitialCreate.Designer.cs create mode 100644 WhoAtX/Migrations/20230914205826_InitialCreate.cs create mode 100644 WhoAtX/Migrations/ApplicationDbContextModelSnapshot.cs create mode 100644 WhoAtX/Models/UserProfile.cs diff --git a/README.md b/README.md index e88a542..c5b0511 100644 --- a/README.md +++ b/README.md @@ -25,3 +25,9 @@ This is plugged in to an LLM (GPT) with search capability, so you can find the p - linking to calendars to make it clear if people are available or away - embedding into Jira to make it easy to assign the right people to the right tickets + +## Local development instructions + +1. Clone the repo +2. Run `dotnet tool install --global dotnet-ef` to install the Entity Framework CLI if you haven't already +3. Within the WhoAtX folder, run `dotnet ef migrations add InitialCreate` then `dotnet ef database update` to create the database diff --git a/WhoAtX/Controllers/UserController.cs b/WhoAtX/Controllers/UserController.cs index 8029501..d452cae 100644 --- a/WhoAtX/Controllers/UserController.cs +++ b/WhoAtX/Controllers/UserController.cs @@ -1,39 +1,59 @@ using Microsoft.AspNetCore.Mvc; +using WhoAtX.Data; using WhoAtX.Models; namespace WhoAtX.Controllers { public class UserController : Controller { - // TODO(#8): Get data from database rather than hard-coding it - private UserProfileViewModel GetUserProfile() - { - // Sample user profile data - var userProfile = new UserProfileViewModel - { - Name = "John Doe", - Pronouns = "He/Him", - NamePronunciationPath = "/audio/john_doe_pronunciation.mp3", - // NamePronunciationPath = null, - // Add more user profile data here - AreasOfKnowledge = "Web Development", - Projects = "Project X", - Team = "Development Team", - WorkingHours = "9:00 AM - 5:00 PM", - TimeZone = "UTC-05:00", - // Add more professional details here - }; + private readonly ApplicationDbContext _context; - return userProfile; + public UserController(ApplicationDbContext context) + { + _context = context; } public IActionResult Profile() { - // Retrieve user profile data - var userProfile = GetUserProfile(); + // Retrieve user profile data from the database + var userProfile = _context.UserProfiles.FirstOrDefault(); + + if (userProfile is null) + { + // throw new NullReferenceException("User not found"); + + // For now, just give the default data + userProfile = new UserProfile + { + Name = "John Doe", + Pronouns = "He/Him", + NamePronunciationPath = "/audio/john_doe_pronunciation.mp3", + // NamePronunciationPath = null, + // Add more user profile data here + AreasOfKnowledge = "Web Development", + Projects = "Project X", + Team = "Development Team", + WorkingHours = "9:00 AM - 5:00 PM", + TimeZone = "UTC-05:00", + // Add more professional details here + }; + } + + // Map UserProfile to UserProfileViewModel + var userProfileViewModel = new UserProfileViewModel + { + Name = userProfile.Name, + Pronouns = userProfile.Pronouns, + NamePronunciationPath = userProfile.NamePronunciationPath, + AreasOfKnowledge = userProfile.AreasOfKnowledge, + Projects = userProfile.Projects, + Team = userProfile.Team, + WorkingHours = userProfile.WorkingHours, + TimeZone = userProfile.TimeZone, + }; // Pass the user profile data to the view - return View(userProfile); + return View(userProfileViewModel); } } } diff --git a/WhoAtX/Data/DbContext.cs b/WhoAtX/Data/DbContext.cs new file mode 100644 index 0000000..5b4fe84 --- /dev/null +++ b/WhoAtX/Data/DbContext.cs @@ -0,0 +1,13 @@ +using Microsoft.EntityFrameworkCore; +using WhoAtX.Models; + +namespace WhoAtX.Data; + +public class ApplicationDbContext : DbContext +{ + public ApplicationDbContext(DbContextOptions options) : base(options) + { + } + + public DbSet UserProfiles { get; set; } +} diff --git a/WhoAtX/Migrations/20230914205826_InitialCreate.Designer.cs b/WhoAtX/Migrations/20230914205826_InitialCreate.Designer.cs new file mode 100644 index 0000000..47a6f03 --- /dev/null +++ b/WhoAtX/Migrations/20230914205826_InitialCreate.Designer.cs @@ -0,0 +1,67 @@ +// +using Microsoft.EntityFrameworkCore; +using Microsoft.EntityFrameworkCore.Infrastructure; +using Microsoft.EntityFrameworkCore.Migrations; +using Microsoft.EntityFrameworkCore.Storage.ValueConversion; +using WhoAtX.Data; + +#nullable disable + +namespace WhoAtX.Migrations +{ + [DbContext(typeof(ApplicationDbContext))] + [Migration("20230914205826_InitialCreate")] + partial class InitialCreate + { + /// + protected override void BuildTargetModel(ModelBuilder modelBuilder) + { +#pragma warning disable 612, 618 + modelBuilder.HasAnnotation("ProductVersion", "7.0.11"); + + modelBuilder.Entity("WhoAtX.Models.UserProfile", b => + { + b.Property("Id") + .ValueGeneratedOnAdd() + .HasColumnType("INTEGER"); + + b.Property("AreasOfKnowledge") + .IsRequired() + .HasColumnType("TEXT"); + + b.Property("Name") + .IsRequired() + .HasColumnType("TEXT"); + + b.Property("NamePronunciationPath") + .IsRequired() + .HasColumnType("TEXT"); + + b.Property("Projects") + .IsRequired() + .HasColumnType("TEXT"); + + b.Property("Pronouns") + .IsRequired() + .HasColumnType("TEXT"); + + b.Property("Team") + .IsRequired() + .HasColumnType("TEXT"); + + b.Property("TimeZone") + .IsRequired() + .HasColumnType("TEXT"); + + b.Property("WorkingHours") + .IsRequired() + .HasColumnType("TEXT"); + + b.HasKey("Id"); + + b.ToTable("UserProfiles"); + }); +#pragma warning restore 612, 618 + } + } +} diff --git a/WhoAtX/Migrations/20230914205826_InitialCreate.cs b/WhoAtX/Migrations/20230914205826_InitialCreate.cs new file mode 100644 index 0000000..4891400 --- /dev/null +++ b/WhoAtX/Migrations/20230914205826_InitialCreate.cs @@ -0,0 +1,41 @@ +using Microsoft.EntityFrameworkCore.Migrations; + +#nullable disable + +namespace WhoAtX.Migrations +{ + /// + public partial class InitialCreate : Migration + { + /// + protected override void Up(MigrationBuilder migrationBuilder) + { + migrationBuilder.CreateTable( + name: "UserProfiles", + columns: table => new + { + Id = table.Column(type: "INTEGER", nullable: false) + .Annotation("Sqlite:Autoincrement", true), + Name = table.Column(type: "TEXT", nullable: false), + Pronouns = table.Column(type: "TEXT", nullable: false), + NamePronunciationPath = table.Column(type: "TEXT", nullable: false), + AreasOfKnowledge = table.Column(type: "TEXT", nullable: false), + Projects = table.Column(type: "TEXT", nullable: false), + Team = table.Column(type: "TEXT", nullable: false), + WorkingHours = table.Column(type: "TEXT", nullable: false), + TimeZone = table.Column(type: "TEXT", nullable: false) + }, + constraints: table => + { + table.PrimaryKey("PK_UserProfiles", x => x.Id); + }); + } + + /// + protected override void Down(MigrationBuilder migrationBuilder) + { + migrationBuilder.DropTable( + name: "UserProfiles"); + } + } +} diff --git a/WhoAtX/Migrations/ApplicationDbContextModelSnapshot.cs b/WhoAtX/Migrations/ApplicationDbContextModelSnapshot.cs new file mode 100644 index 0000000..f1aafce --- /dev/null +++ b/WhoAtX/Migrations/ApplicationDbContextModelSnapshot.cs @@ -0,0 +1,64 @@ +// +using Microsoft.EntityFrameworkCore; +using Microsoft.EntityFrameworkCore.Infrastructure; +using Microsoft.EntityFrameworkCore.Storage.ValueConversion; +using WhoAtX.Data; + +#nullable disable + +namespace WhoAtX.Migrations +{ + [DbContext(typeof(ApplicationDbContext))] + partial class ApplicationDbContextModelSnapshot : ModelSnapshot + { + protected override void BuildModel(ModelBuilder modelBuilder) + { +#pragma warning disable 612, 618 + modelBuilder.HasAnnotation("ProductVersion", "7.0.11"); + + modelBuilder.Entity("WhoAtX.Models.UserProfile", b => + { + b.Property("Id") + .ValueGeneratedOnAdd() + .HasColumnType("INTEGER"); + + b.Property("AreasOfKnowledge") + .IsRequired() + .HasColumnType("TEXT"); + + b.Property("Name") + .IsRequired() + .HasColumnType("TEXT"); + + b.Property("NamePronunciationPath") + .IsRequired() + .HasColumnType("TEXT"); + + b.Property("Projects") + .IsRequired() + .HasColumnType("TEXT"); + + b.Property("Pronouns") + .IsRequired() + .HasColumnType("TEXT"); + + b.Property("Team") + .IsRequired() + .HasColumnType("TEXT"); + + b.Property("TimeZone") + .IsRequired() + .HasColumnType("TEXT"); + + b.Property("WorkingHours") + .IsRequired() + .HasColumnType("TEXT"); + + b.HasKey("Id"); + + b.ToTable("UserProfiles"); + }); +#pragma warning restore 612, 618 + } + } +} diff --git a/WhoAtX/Models/UserProfile.cs b/WhoAtX/Models/UserProfile.cs new file mode 100644 index 0000000..c7f41eb --- /dev/null +++ b/WhoAtX/Models/UserProfile.cs @@ -0,0 +1,14 @@ +namespace WhoAtX.Models; + +public class UserProfile +{ + public int Id { get; set; } + public string Name { get; set; } + public string Pronouns { get; set; } + public string NamePronunciationPath { get; set; } + public string AreasOfKnowledge { get; set; } + public string Projects { get; set; } + public string Team { get; set; } + public string WorkingHours { get; set; } + public string TimeZone { get; set; } +} diff --git a/WhoAtX/Program.cs b/WhoAtX/Program.cs index 4ac679a..8ebc935 100644 --- a/WhoAtX/Program.cs +++ b/WhoAtX/Program.cs @@ -1,8 +1,17 @@ +using WhoAtX.Data; +using Microsoft.EntityFrameworkCore; + var builder = WebApplication.CreateBuilder(args); // Add services to the container. builder.Services.AddControllersWithViews(); +var connectionString = builder.Configuration.GetConnectionString("DefaultConnection"); +builder.Services.AddDbContext(options => +{ + options.UseSqlite(connectionString); +}); + var app = builder.Build(); // Configure the HTTP request pipeline. diff --git a/WhoAtX/WhoAtX.csproj b/WhoAtX/WhoAtX.csproj index 9c486ca..43cfd22 100644 --- a/WhoAtX/WhoAtX.csproj +++ b/WhoAtX/WhoAtX.csproj @@ -7,7 +7,20 @@ + + + all + runtime; build; native; contentfiles; analyzers; buildtransitive + + + + + PreserveNewest + + + + diff --git a/WhoAtX/appsettings.json b/WhoAtX/appsettings.json index 4d56694..55d53c8 100644 --- a/WhoAtX/appsettings.json +++ b/WhoAtX/appsettings.json @@ -5,5 +5,8 @@ "Microsoft.AspNetCore": "Warning" } }, - "AllowedHosts": "*" + "AllowedHosts": "*", + "ConnectionStrings": { + "DefaultConnection": "Data Source=UserDatabase.sqlite" + } }