From 2d597b782aacdff46f76c7370fe4ee67450cb5f2 Mon Sep 17 00:00:00 2001 From: krishna <vamsi.sirigirisetty@pal.tech> Date: Sat, 9 Nov 2024 01:38:03 +0530 Subject: [PATCH 1/5] Added Required Packegs and Configrations --- FoodTracker.Api/FoodTracker.Api.csproj | 5 +++ FoodTracker.Api/Program.cs | 36 +++++++------------ .../Data/ApplicationDbContext.cs | 7 ++-- FoodTracker.Database/Entity/UserEntity.cs | 6 ---- .../FoodTracker.Database.csproj | 11 +++++- 5 files changed, 32 insertions(+), 33 deletions(-) delete mode 100644 FoodTracker.Database/Entity/UserEntity.cs diff --git a/FoodTracker.Api/FoodTracker.Api.csproj b/FoodTracker.Api/FoodTracker.Api.csproj index 47416a5..3904411 100644 --- a/FoodTracker.Api/FoodTracker.Api.csproj +++ b/FoodTracker.Api/FoodTracker.Api.csproj @@ -7,7 +7,12 @@ </PropertyGroup> <ItemGroup> + <PackageReference Include="Microsoft.AspNetCore.Identity.EntityFrameworkCore" Version="8.0.10" /> <PackageReference Include="Microsoft.AspNetCore.OpenApi" Version="8.0.10" /> + <PackageReference Include="Microsoft.EntityFrameworkCore.Design" Version="8.0.10"> + <IncludeAssets>runtime; build; native; contentfiles; analyzers; buildtransitive</IncludeAssets> + <PrivateAssets>all</PrivateAssets> + </PackageReference> <PackageReference Include="Swashbuckle.AspNetCore" Version="6.6.2" /> </ItemGroup> diff --git a/FoodTracker.Api/Program.cs b/FoodTracker.Api/Program.cs index 00ff539..cf2b8a9 100644 --- a/FoodTracker.Api/Program.cs +++ b/FoodTracker.Api/Program.cs @@ -1,12 +1,23 @@ +using FoodTracker.Database.Data; +using Microsoft.EntityFrameworkCore; + var builder = WebApplication.CreateBuilder(args); // Add services to the container. // Learn more about configuring Swagger/OpenAPI at https://aka.ms/aspnetcore/swashbuckle builder.Services.AddEndpointsApiExplorer(); builder.Services.AddSwaggerGen(); +builder.Services.AddControllers(); + +builder.Services.AddDbContext<ApplicationDbContext>(options => +{ + options.UseSqlServer(builder.Configuration.GetConnectionString("DefaultConnectionString")); +}); + var app = builder.Build(); + // Configure the HTTP request pipeline. if (app.Environment.IsDevelopment()) { @@ -15,30 +26,7 @@ if (app.Environment.IsDevelopment()) } app.UseHttpsRedirection(); +app.MapControllers(); -var summaries = new[] -{ - "Freezing", "Bracing", "Chilly", "Cool", "Mild", "Warm", "Balmy", "Hot", "Sweltering", "Scorching" -}; - -app.MapGet("/weatherforecast", () => -{ - var forecast = Enumerable.Range(1, 5).Select(index => - new WeatherForecast - ( - DateOnly.FromDateTime(DateTime.Now.AddDays(index)), - Random.Shared.Next(-20, 55), - summaries[Random.Shared.Next(summaries.Length)] - )) - .ToArray(); - return forecast; -}) -.WithName("GetWeatherForecast") -.WithOpenApi(); app.Run(); - -record WeatherForecast(DateOnly Date, int TemperatureC, string? Summary) -{ - public int TemperatureF => 32 + (int)(TemperatureC / 0.5556); -} diff --git a/FoodTracker.Database/Data/ApplicationDbContext.cs b/FoodTracker.Database/Data/ApplicationDbContext.cs index 62675ad..b46a828 100644 --- a/FoodTracker.Database/Data/ApplicationDbContext.cs +++ b/FoodTracker.Database/Data/ApplicationDbContext.cs @@ -1,8 +1,11 @@ using System; +using Microsoft.AspNetCore.Identity; +using Microsoft.AspNetCore.Identity.EntityFrameworkCore; +using Microsoft.EntityFrameworkCore; namespace FoodTracker.Database.Data; -public class ApplicationDbContext +public class ApplicationDbContext : IdentityDbContext<IdentityUser> { - + public ApplicationDbContext(DbContextOptions<ApplicationDbContext> options) : base(options) { } } diff --git a/FoodTracker.Database/Entity/UserEntity.cs b/FoodTracker.Database/Entity/UserEntity.cs deleted file mode 100644 index 2f9bce0..0000000 --- a/FoodTracker.Database/Entity/UserEntity.cs +++ /dev/null @@ -1,6 +0,0 @@ -using System; - -namespace FoodTracker.Database.Entity; - -public class UserEntity -{} diff --git a/FoodTracker.Database/FoodTracker.Database.csproj b/FoodTracker.Database/FoodTracker.Database.csproj index 255127e..563b354 100644 --- a/FoodTracker.Database/FoodTracker.Database.csproj +++ b/FoodTracker.Database/FoodTracker.Database.csproj @@ -7,9 +7,18 @@ </PropertyGroup> <ItemGroup> - <Folder Include="Entity\" /> + <Folder Include="Configurations\" /> <Folder Include="Implementation\" /> <Folder Include="Interface\" /> </ItemGroup> + <ItemGroup> + <PackageReference Include="Microsoft.AspNetCore.Identity.EntityFrameworkCore" Version="8.0.10" /> + <PackageReference Include="Microsoft.EntityFrameworkCore.SqlServer" Version="8.0.10" /> + <PackageReference Include="Microsoft.EntityFrameworkCore.Tools" Version="8.0.10"> + <IncludeAssets>runtime; build; native; contentfiles; analyzers; buildtransitive</IncludeAssets> + <PrivateAssets>all</PrivateAssets> + </PackageReference> + </ItemGroup> + </Project> -- GitLab From dc2266f3c009ee7931099cb99823811d5699d87a Mon Sep 17 00:00:00 2001 From: krishna <vamsi.sirigirisetty@pal.tech> Date: Sat, 9 Nov 2024 01:38:57 +0530 Subject: [PATCH 2/5] added FoodLog Entity --- FoodTracker.Database/Entity/FoodLogEntry.cs | 17 +++++++++++++++++ FoodTracker.Database/Entity/MacroNutrients.cs | 12 ++++++++++++ 2 files changed, 29 insertions(+) create mode 100644 FoodTracker.Database/Entity/FoodLogEntry.cs create mode 100644 FoodTracker.Database/Entity/MacroNutrients.cs diff --git a/FoodTracker.Database/Entity/FoodLogEntry.cs b/FoodTracker.Database/Entity/FoodLogEntry.cs new file mode 100644 index 0000000..6b8e617 --- /dev/null +++ b/FoodTracker.Database/Entity/FoodLogEntry.cs @@ -0,0 +1,17 @@ +using System; + +namespace FoodTracker.Database.Entity; + +public class FoodLogEntry +{ + public int Id { get; set; } + public string? FoodItem { get; set; } + public int PortionSize { get; set; } + public int Calories { get; set; } + //For soft deletion + public bool IsAvailable { get; set; } + public DateTime dateTime { get; set; } + + + +} diff --git a/FoodTracker.Database/Entity/MacroNutrients.cs b/FoodTracker.Database/Entity/MacroNutrients.cs new file mode 100644 index 0000000..1dde9d2 --- /dev/null +++ b/FoodTracker.Database/Entity/MacroNutrients.cs @@ -0,0 +1,12 @@ +using System; + +namespace FoodTracker.Database.Entity; + +public class MacroNutrients +{ + public int Id { get; set; } + public int Fats { get; set; } + public int Protein { get; set; } + public int Potassium { get; set; } + public int Sugars { get; set; } +} -- GitLab From 7cb377fca567ca81cbdb874ad3503a3e48dbe3d6 Mon Sep 17 00:00:00 2001 From: krishna <vamsi.sirigirisetty@pal.tech> Date: Sat, 9 Nov 2024 12:17:41 +0530 Subject: [PATCH 3/5] Created Entites and Established Relations --- .../Data/ApplicationDbContext.cs | 15 + .../Entity/ActivityTracking.cs | 16 + .../Entity/ApplicationUser.cs | 16 + FoodTracker.Database/Entity/FoodLogEntry.cs | 11 +- FoodTracker.Database/Entity/GroupChallenge.cs | 11 + FoodTracker.Database/Entity/MacroNutrients.cs | 4 + FoodTracker.Database/Entity/UserDetails.cs | 17 ++ .../ServiceCollectionExtension.cs | 20 ++ ...odEntryTable_And_AddedIdentity.Designer.cs | 279 ++++++++++++++++++ ...CreatedFoodEntryTable_And_AddedIdentity.cs | 224 ++++++++++++++ .../ApplicationDbContextModelSnapshot.cs | 276 +++++++++++++++++ 11 files changed, 887 insertions(+), 2 deletions(-) create mode 100644 FoodTracker.Database/Entity/ActivityTracking.cs create mode 100644 FoodTracker.Database/Entity/ApplicationUser.cs create mode 100644 FoodTracker.Database/Entity/GroupChallenge.cs create mode 100644 FoodTracker.Database/Entity/UserDetails.cs create mode 100644 FoodTracker.Database/ExtensionMethods/ServiceCollectionExtension.cs create mode 100644 FoodTracker.Database/Migrations/20241109044639_CreatedFoodEntryTable_And_AddedIdentity.Designer.cs create mode 100644 FoodTracker.Database/Migrations/20241109044639_CreatedFoodEntryTable_And_AddedIdentity.cs create mode 100644 FoodTracker.Database/Migrations/ApplicationDbContextModelSnapshot.cs diff --git a/FoodTracker.Database/Data/ApplicationDbContext.cs b/FoodTracker.Database/Data/ApplicationDbContext.cs index b46a828..eb1554a 100644 --- a/FoodTracker.Database/Data/ApplicationDbContext.cs +++ b/FoodTracker.Database/Data/ApplicationDbContext.cs @@ -1,4 +1,5 @@ using System; +using FoodTracker.Database.Entity; using Microsoft.AspNetCore.Identity; using Microsoft.AspNetCore.Identity.EntityFrameworkCore; using Microsoft.EntityFrameworkCore; @@ -8,4 +9,18 @@ namespace FoodTracker.Database.Data; public class ApplicationDbContext : IdentityDbContext<IdentityUser> { public ApplicationDbContext(DbContextOptions<ApplicationDbContext> options) : base(options) { } + + protected override void OnModelCreating(ModelBuilder builder) + { + base.OnModelCreating(builder); + //One-to-Many Relation between FoodLogEntry and ApplicationUser + builder.Entity<FoodLogEntry>().HasOne(user=>user.ApplicationUser).WithMany(food=>food.FoodEntries).HasForeignKey(id=>id.UserId); + //One-to-one Relation between UserDetails and ApplicationUser + builder.Entity<ApplicationUser>().HasOne(user=>user.UserDetails).WithOne(userDetails=>userDetails.User).HasForeignKey<UserDetails>(id=>id.UserId); + //One-to-One Relation between ApplicationUser and ActivityTracking + builder.Entity<ApplicationUser>().HasOne(activity=>activity.UserActivityTracking).WithOne(user=>user.ApplicationUser).HasForeignKey<ActivityTracking>(id=>id.UserId); + //one-to-one relation between FoodLogEntry and Macronutrients + builder.Entity<FoodLogEntry>().HasOne(nutrients=>nutrients.MacroNutrients).WithOne(foodlog=>foodlog.FoodLogEntry).HasForeignKey<MacroNutrients>(id=>id.FoodLogId); + + } } diff --git a/FoodTracker.Database/Entity/ActivityTracking.cs b/FoodTracker.Database/Entity/ActivityTracking.cs new file mode 100644 index 0000000..421913a --- /dev/null +++ b/FoodTracker.Database/Entity/ActivityTracking.cs @@ -0,0 +1,16 @@ +using System; + +namespace FoodTracker.Database.Entity; + +public class ActivityTracking +{ + public int Id { get; set; } + public int StepCount { get; set; } + public int SleepingHours { get; set; } + public int WaterIntake { get; set; } + //Foreign Key of ApplicationUser + public string UserId { get; set; } + //one-to-one relation with ApplicationUser(Navigation Reference Property) + public ApplicationUser ApplicationUser { get; set; } + +} diff --git a/FoodTracker.Database/Entity/ApplicationUser.cs b/FoodTracker.Database/Entity/ApplicationUser.cs new file mode 100644 index 0000000..cc4b868 --- /dev/null +++ b/FoodTracker.Database/Entity/ApplicationUser.cs @@ -0,0 +1,16 @@ +using System; +using Microsoft.AspNetCore.Identity; + +namespace FoodTracker.Database.Entity; + +public class ApplicationUser:IdentityUser +{ + //One-to-Many realtion with FoodLogEntry(Navigation Collection Property) + public ICollection<FoodLogEntry> FoodEntries { get; set; } + + //One-to-one relation with ActivityTracking(Navigation Reference Property) + public ActivityTracking UserActivityTracking { get; set; } + + //One-to-one Relation with UserDetails(Navigation Reference Property) + public UserDetails UserDetails { get; set; } +} diff --git a/FoodTracker.Database/Entity/FoodLogEntry.cs b/FoodTracker.Database/Entity/FoodLogEntry.cs index 6b8e617..a83b006 100644 --- a/FoodTracker.Database/Entity/FoodLogEntry.cs +++ b/FoodTracker.Database/Entity/FoodLogEntry.cs @@ -10,8 +10,15 @@ public class FoodLogEntry public int Calories { get; set; } //For soft deletion public bool IsAvailable { get; set; } - public DateTime dateTime { get; set; } + public DateTime DateTime { get; set; } + public string MealType { get; set; } - + //ForeignKey With Application User + public string UserId { get; set; } + //Navigation Property + public ApplicationUser ApplicationUser { get; set; } + //Navigation Property to MacroNutrients Class + public MacroNutrients MacroNutrients { get; set; } + } diff --git a/FoodTracker.Database/Entity/GroupChallenge.cs b/FoodTracker.Database/Entity/GroupChallenge.cs new file mode 100644 index 0000000..eb600e7 --- /dev/null +++ b/FoodTracker.Database/Entity/GroupChallenge.cs @@ -0,0 +1,11 @@ +using System; + +namespace FoodTracker.Database.Entity; + +public class GroupChallenge +{ + public int Id { get; set; } + public string? ChallengeName { get; set; } + public DateTime StartDate { get; set; } + public DateTime EndDate { get; set; } +} diff --git a/FoodTracker.Database/Entity/MacroNutrients.cs b/FoodTracker.Database/Entity/MacroNutrients.cs index 1dde9d2..602dd76 100644 --- a/FoodTracker.Database/Entity/MacroNutrients.cs +++ b/FoodTracker.Database/Entity/MacroNutrients.cs @@ -9,4 +9,8 @@ public class MacroNutrients public int Protein { get; set; } public int Potassium { get; set; } public int Sugars { get; set; } + //Foreign-Key(one-to-one relation with FoodLogEntries) + public int FoodLogId { get; set; } + //Navigation Reference Proeprty + public FoodLogEntry FoodLogEntry { get; set; } } diff --git a/FoodTracker.Database/Entity/UserDetails.cs b/FoodTracker.Database/Entity/UserDetails.cs new file mode 100644 index 0000000..a6b18a0 --- /dev/null +++ b/FoodTracker.Database/Entity/UserDetails.cs @@ -0,0 +1,17 @@ +using System; + +namespace FoodTracker.Database.Entity; + +public class UserDetails +{ + public int Id { get; set; } + public string Gender { get; set; } + public int Height { get; set; } + public int Weight { get; set; } + public DateOnly DateOfBirth { get; set; } + + //One-to-one Realtion with ApplicationUser + public string UserId { get; set; } + //Navigation Reference Property + public ApplicationUser User { get; set; } +} diff --git a/FoodTracker.Database/ExtensionMethods/ServiceCollectionExtension.cs b/FoodTracker.Database/ExtensionMethods/ServiceCollectionExtension.cs new file mode 100644 index 0000000..260ea5a --- /dev/null +++ b/FoodTracker.Database/ExtensionMethods/ServiceCollectionExtension.cs @@ -0,0 +1,20 @@ +using System; +using Microsoft.AspNetCore.Identity; +using Microsoft.Extensions.DependencyInjection; + +namespace FoodTracker.Database.ExtensionMethods; + +public static class ServiceCollectionExtension +{ + public static async Task<IServiceCollection> CreateRoles(this IServiceCollection services){ + var serviceProvider = services.BuildServiceProvider(); + var roleManager = serviceProvider.GetRequiredService<RoleManager<IdentityRole>>(); + var roles = new []{"Admin","User"}; + foreach(var role in roles){ + if(!await roleManager.RoleExistsAsync(role)){ + await roleManager.CreateAsync(new IdentityRole(role)); + } + } + return services; + } +} diff --git a/FoodTracker.Database/Migrations/20241109044639_CreatedFoodEntryTable_And_AddedIdentity.Designer.cs b/FoodTracker.Database/Migrations/20241109044639_CreatedFoodEntryTable_And_AddedIdentity.Designer.cs new file mode 100644 index 0000000..26b97d8 --- /dev/null +++ b/FoodTracker.Database/Migrations/20241109044639_CreatedFoodEntryTable_And_AddedIdentity.Designer.cs @@ -0,0 +1,279 @@ +// <auto-generated /> +using System; +using FoodTracker.Database.Data; +using Microsoft.EntityFrameworkCore; +using Microsoft.EntityFrameworkCore.Infrastructure; +using Microsoft.EntityFrameworkCore.Metadata; +using Microsoft.EntityFrameworkCore.Migrations; +using Microsoft.EntityFrameworkCore.Storage.ValueConversion; + +#nullable disable + +namespace FoodTracker.Database.Migrations +{ + [DbContext(typeof(ApplicationDbContext))] + [Migration("20241109044639_CreatedFoodEntryTable_And_AddedIdentity")] + partial class CreatedFoodEntryTable_And_AddedIdentity + { + /// <inheritdoc /> + protected override void BuildTargetModel(ModelBuilder modelBuilder) + { +#pragma warning disable 612, 618 + modelBuilder + .HasAnnotation("ProductVersion", "8.0.10") + .HasAnnotation("Relational:MaxIdentifierLength", 128); + + SqlServerModelBuilderExtensions.UseIdentityColumns(modelBuilder); + + modelBuilder.Entity("Microsoft.AspNetCore.Identity.IdentityRole", b => + { + b.Property<string>("Id") + .HasColumnType("nvarchar(450)"); + + b.Property<string>("ConcurrencyStamp") + .IsConcurrencyToken() + .HasColumnType("nvarchar(max)"); + + b.Property<string>("Name") + .HasMaxLength(256) + .HasColumnType("nvarchar(256)"); + + b.Property<string>("NormalizedName") + .HasMaxLength(256) + .HasColumnType("nvarchar(256)"); + + b.HasKey("Id"); + + b.HasIndex("NormalizedName") + .IsUnique() + .HasDatabaseName("RoleNameIndex") + .HasFilter("[NormalizedName] IS NOT NULL"); + + b.ToTable("AspNetRoles", (string)null); + }); + + modelBuilder.Entity("Microsoft.AspNetCore.Identity.IdentityRoleClaim<string>", b => + { + b.Property<int>("Id") + .ValueGeneratedOnAdd() + .HasColumnType("int"); + + SqlServerPropertyBuilderExtensions.UseIdentityColumn(b.Property<int>("Id")); + + b.Property<string>("ClaimType") + .HasColumnType("nvarchar(max)"); + + b.Property<string>("ClaimValue") + .HasColumnType("nvarchar(max)"); + + b.Property<string>("RoleId") + .IsRequired() + .HasColumnType("nvarchar(450)"); + + b.HasKey("Id"); + + b.HasIndex("RoleId"); + + b.ToTable("AspNetRoleClaims", (string)null); + }); + + modelBuilder.Entity("Microsoft.AspNetCore.Identity.IdentityUser", b => + { + b.Property<string>("Id") + .HasColumnType("nvarchar(450)"); + + b.Property<int>("AccessFailedCount") + .HasColumnType("int"); + + b.Property<string>("ConcurrencyStamp") + .IsConcurrencyToken() + .HasColumnType("nvarchar(max)"); + + b.Property<string>("Email") + .HasMaxLength(256) + .HasColumnType("nvarchar(256)"); + + b.Property<bool>("EmailConfirmed") + .HasColumnType("bit"); + + b.Property<bool>("LockoutEnabled") + .HasColumnType("bit"); + + b.Property<DateTimeOffset?>("LockoutEnd") + .HasColumnType("datetimeoffset"); + + b.Property<string>("NormalizedEmail") + .HasMaxLength(256) + .HasColumnType("nvarchar(256)"); + + b.Property<string>("NormalizedUserName") + .HasMaxLength(256) + .HasColumnType("nvarchar(256)"); + + b.Property<string>("PasswordHash") + .HasColumnType("nvarchar(max)"); + + b.Property<string>("PhoneNumber") + .HasColumnType("nvarchar(max)"); + + b.Property<bool>("PhoneNumberConfirmed") + .HasColumnType("bit"); + + b.Property<string>("SecurityStamp") + .HasColumnType("nvarchar(max)"); + + b.Property<bool>("TwoFactorEnabled") + .HasColumnType("bit"); + + b.Property<string>("UserName") + .HasMaxLength(256) + .HasColumnType("nvarchar(256)"); + + b.HasKey("Id"); + + b.HasIndex("NormalizedEmail") + .HasDatabaseName("EmailIndex"); + + b.HasIndex("NormalizedUserName") + .IsUnique() + .HasDatabaseName("UserNameIndex") + .HasFilter("[NormalizedUserName] IS NOT NULL"); + + b.ToTable("AspNetUsers", (string)null); + }); + + modelBuilder.Entity("Microsoft.AspNetCore.Identity.IdentityUserClaim<string>", b => + { + b.Property<int>("Id") + .ValueGeneratedOnAdd() + .HasColumnType("int"); + + SqlServerPropertyBuilderExtensions.UseIdentityColumn(b.Property<int>("Id")); + + b.Property<string>("ClaimType") + .HasColumnType("nvarchar(max)"); + + b.Property<string>("ClaimValue") + .HasColumnType("nvarchar(max)"); + + b.Property<string>("UserId") + .IsRequired() + .HasColumnType("nvarchar(450)"); + + b.HasKey("Id"); + + b.HasIndex("UserId"); + + b.ToTable("AspNetUserClaims", (string)null); + }); + + modelBuilder.Entity("Microsoft.AspNetCore.Identity.IdentityUserLogin<string>", b => + { + b.Property<string>("LoginProvider") + .HasColumnType("nvarchar(450)"); + + b.Property<string>("ProviderKey") + .HasColumnType("nvarchar(450)"); + + b.Property<string>("ProviderDisplayName") + .HasColumnType("nvarchar(max)"); + + b.Property<string>("UserId") + .IsRequired() + .HasColumnType("nvarchar(450)"); + + b.HasKey("LoginProvider", "ProviderKey"); + + b.HasIndex("UserId"); + + b.ToTable("AspNetUserLogins", (string)null); + }); + + modelBuilder.Entity("Microsoft.AspNetCore.Identity.IdentityUserRole<string>", b => + { + b.Property<string>("UserId") + .HasColumnType("nvarchar(450)"); + + b.Property<string>("RoleId") + .HasColumnType("nvarchar(450)"); + + b.HasKey("UserId", "RoleId"); + + b.HasIndex("RoleId"); + + b.ToTable("AspNetUserRoles", (string)null); + }); + + modelBuilder.Entity("Microsoft.AspNetCore.Identity.IdentityUserToken<string>", b => + { + b.Property<string>("UserId") + .HasColumnType("nvarchar(450)"); + + b.Property<string>("LoginProvider") + .HasColumnType("nvarchar(450)"); + + b.Property<string>("Name") + .HasColumnType("nvarchar(450)"); + + b.Property<string>("Value") + .HasColumnType("nvarchar(max)"); + + b.HasKey("UserId", "LoginProvider", "Name"); + + b.ToTable("AspNetUserTokens", (string)null); + }); + + modelBuilder.Entity("Microsoft.AspNetCore.Identity.IdentityRoleClaim<string>", b => + { + b.HasOne("Microsoft.AspNetCore.Identity.IdentityRole", null) + .WithMany() + .HasForeignKey("RoleId") + .OnDelete(DeleteBehavior.Cascade) + .IsRequired(); + }); + + modelBuilder.Entity("Microsoft.AspNetCore.Identity.IdentityUserClaim<string>", b => + { + b.HasOne("Microsoft.AspNetCore.Identity.IdentityUser", null) + .WithMany() + .HasForeignKey("UserId") + .OnDelete(DeleteBehavior.Cascade) + .IsRequired(); + }); + + modelBuilder.Entity("Microsoft.AspNetCore.Identity.IdentityUserLogin<string>", b => + { + b.HasOne("Microsoft.AspNetCore.Identity.IdentityUser", null) + .WithMany() + .HasForeignKey("UserId") + .OnDelete(DeleteBehavior.Cascade) + .IsRequired(); + }); + + modelBuilder.Entity("Microsoft.AspNetCore.Identity.IdentityUserRole<string>", b => + { + b.HasOne("Microsoft.AspNetCore.Identity.IdentityRole", null) + .WithMany() + .HasForeignKey("RoleId") + .OnDelete(DeleteBehavior.Cascade) + .IsRequired(); + + b.HasOne("Microsoft.AspNetCore.Identity.IdentityUser", null) + .WithMany() + .HasForeignKey("UserId") + .OnDelete(DeleteBehavior.Cascade) + .IsRequired(); + }); + + modelBuilder.Entity("Microsoft.AspNetCore.Identity.IdentityUserToken<string>", b => + { + b.HasOne("Microsoft.AspNetCore.Identity.IdentityUser", null) + .WithMany() + .HasForeignKey("UserId") + .OnDelete(DeleteBehavior.Cascade) + .IsRequired(); + }); +#pragma warning restore 612, 618 + } + } +} diff --git a/FoodTracker.Database/Migrations/20241109044639_CreatedFoodEntryTable_And_AddedIdentity.cs b/FoodTracker.Database/Migrations/20241109044639_CreatedFoodEntryTable_And_AddedIdentity.cs new file mode 100644 index 0000000..4fdb858 --- /dev/null +++ b/FoodTracker.Database/Migrations/20241109044639_CreatedFoodEntryTable_And_AddedIdentity.cs @@ -0,0 +1,224 @@ +using System; +using Microsoft.EntityFrameworkCore.Migrations; + +#nullable disable + +namespace FoodTracker.Database.Migrations +{ + /// <inheritdoc /> + public partial class CreatedFoodEntryTable_And_AddedIdentity : Migration + { + /// <inheritdoc /> + protected override void Up(MigrationBuilder migrationBuilder) + { + migrationBuilder.CreateTable( + name: "AspNetRoles", + columns: table => new + { + Id = table.Column<string>(type: "nvarchar(450)", nullable: false), + Name = table.Column<string>(type: "nvarchar(256)", maxLength: 256, nullable: true), + NormalizedName = table.Column<string>(type: "nvarchar(256)", maxLength: 256, nullable: true), + ConcurrencyStamp = table.Column<string>(type: "nvarchar(max)", nullable: true) + }, + constraints: table => + { + table.PrimaryKey("PK_AspNetRoles", x => x.Id); + }); + + migrationBuilder.CreateTable( + name: "AspNetUsers", + columns: table => new + { + Id = table.Column<string>(type: "nvarchar(450)", nullable: false), + UserName = table.Column<string>(type: "nvarchar(256)", maxLength: 256, nullable: true), + NormalizedUserName = table.Column<string>(type: "nvarchar(256)", maxLength: 256, nullable: true), + Email = table.Column<string>(type: "nvarchar(256)", maxLength: 256, nullable: true), + NormalizedEmail = table.Column<string>(type: "nvarchar(256)", maxLength: 256, nullable: true), + EmailConfirmed = table.Column<bool>(type: "bit", nullable: false), + PasswordHash = table.Column<string>(type: "nvarchar(max)", nullable: true), + SecurityStamp = table.Column<string>(type: "nvarchar(max)", nullable: true), + ConcurrencyStamp = table.Column<string>(type: "nvarchar(max)", nullable: true), + PhoneNumber = table.Column<string>(type: "nvarchar(max)", nullable: true), + PhoneNumberConfirmed = table.Column<bool>(type: "bit", nullable: false), + TwoFactorEnabled = table.Column<bool>(type: "bit", nullable: false), + LockoutEnd = table.Column<DateTimeOffset>(type: "datetimeoffset", nullable: true), + LockoutEnabled = table.Column<bool>(type: "bit", nullable: false), + AccessFailedCount = table.Column<int>(type: "int", nullable: false) + }, + constraints: table => + { + table.PrimaryKey("PK_AspNetUsers", x => x.Id); + }); + + migrationBuilder.CreateTable( + name: "AspNetRoleClaims", + columns: table => new + { + Id = table.Column<int>(type: "int", nullable: false) + .Annotation("SqlServer:Identity", "1, 1"), + RoleId = table.Column<string>(type: "nvarchar(450)", nullable: false), + ClaimType = table.Column<string>(type: "nvarchar(max)", nullable: true), + ClaimValue = table.Column<string>(type: "nvarchar(max)", nullable: true) + }, + constraints: table => + { + table.PrimaryKey("PK_AspNetRoleClaims", x => x.Id); + table.ForeignKey( + name: "FK_AspNetRoleClaims_AspNetRoles_RoleId", + column: x => x.RoleId, + principalTable: "AspNetRoles", + principalColumn: "Id", + onDelete: ReferentialAction.Cascade); + }); + + migrationBuilder.CreateTable( + name: "AspNetUserClaims", + columns: table => new + { + Id = table.Column<int>(type: "int", nullable: false) + .Annotation("SqlServer:Identity", "1, 1"), + UserId = table.Column<string>(type: "nvarchar(450)", nullable: false), + ClaimType = table.Column<string>(type: "nvarchar(max)", nullable: true), + ClaimValue = table.Column<string>(type: "nvarchar(max)", nullable: true) + }, + constraints: table => + { + table.PrimaryKey("PK_AspNetUserClaims", x => x.Id); + table.ForeignKey( + name: "FK_AspNetUserClaims_AspNetUsers_UserId", + column: x => x.UserId, + principalTable: "AspNetUsers", + principalColumn: "Id", + onDelete: ReferentialAction.Cascade); + }); + + migrationBuilder.CreateTable( + name: "AspNetUserLogins", + columns: table => new + { + LoginProvider = table.Column<string>(type: "nvarchar(450)", nullable: false), + ProviderKey = table.Column<string>(type: "nvarchar(450)", nullable: false), + ProviderDisplayName = table.Column<string>(type: "nvarchar(max)", nullable: true), + UserId = table.Column<string>(type: "nvarchar(450)", nullable: false) + }, + constraints: table => + { + table.PrimaryKey("PK_AspNetUserLogins", x => new { x.LoginProvider, x.ProviderKey }); + table.ForeignKey( + name: "FK_AspNetUserLogins_AspNetUsers_UserId", + column: x => x.UserId, + principalTable: "AspNetUsers", + principalColumn: "Id", + onDelete: ReferentialAction.Cascade); + }); + + migrationBuilder.CreateTable( + name: "AspNetUserRoles", + columns: table => new + { + UserId = table.Column<string>(type: "nvarchar(450)", nullable: false), + RoleId = table.Column<string>(type: "nvarchar(450)", nullable: false) + }, + constraints: table => + { + table.PrimaryKey("PK_AspNetUserRoles", x => new { x.UserId, x.RoleId }); + table.ForeignKey( + name: "FK_AspNetUserRoles_AspNetRoles_RoleId", + column: x => x.RoleId, + principalTable: "AspNetRoles", + principalColumn: "Id", + onDelete: ReferentialAction.Cascade); + table.ForeignKey( + name: "FK_AspNetUserRoles_AspNetUsers_UserId", + column: x => x.UserId, + principalTable: "AspNetUsers", + principalColumn: "Id", + onDelete: ReferentialAction.Cascade); + }); + + migrationBuilder.CreateTable( + name: "AspNetUserTokens", + columns: table => new + { + UserId = table.Column<string>(type: "nvarchar(450)", nullable: false), + LoginProvider = table.Column<string>(type: "nvarchar(450)", nullable: false), + Name = table.Column<string>(type: "nvarchar(450)", nullable: false), + Value = table.Column<string>(type: "nvarchar(max)", nullable: true) + }, + constraints: table => + { + table.PrimaryKey("PK_AspNetUserTokens", x => new { x.UserId, x.LoginProvider, x.Name }); + table.ForeignKey( + name: "FK_AspNetUserTokens_AspNetUsers_UserId", + column: x => x.UserId, + principalTable: "AspNetUsers", + principalColumn: "Id", + onDelete: ReferentialAction.Cascade); + }); + + migrationBuilder.CreateIndex( + name: "IX_AspNetRoleClaims_RoleId", + table: "AspNetRoleClaims", + column: "RoleId"); + + migrationBuilder.CreateIndex( + name: "RoleNameIndex", + table: "AspNetRoles", + column: "NormalizedName", + unique: true, + filter: "[NormalizedName] IS NOT NULL"); + + migrationBuilder.CreateIndex( + name: "IX_AspNetUserClaims_UserId", + table: "AspNetUserClaims", + column: "UserId"); + + migrationBuilder.CreateIndex( + name: "IX_AspNetUserLogins_UserId", + table: "AspNetUserLogins", + column: "UserId"); + + migrationBuilder.CreateIndex( + name: "IX_AspNetUserRoles_RoleId", + table: "AspNetUserRoles", + column: "RoleId"); + + migrationBuilder.CreateIndex( + name: "EmailIndex", + table: "AspNetUsers", + column: "NormalizedEmail"); + + migrationBuilder.CreateIndex( + name: "UserNameIndex", + table: "AspNetUsers", + column: "NormalizedUserName", + unique: true, + filter: "[NormalizedUserName] IS NOT NULL"); + } + + /// <inheritdoc /> + protected override void Down(MigrationBuilder migrationBuilder) + { + migrationBuilder.DropTable( + name: "AspNetRoleClaims"); + + migrationBuilder.DropTable( + name: "AspNetUserClaims"); + + migrationBuilder.DropTable( + name: "AspNetUserLogins"); + + migrationBuilder.DropTable( + name: "AspNetUserRoles"); + + migrationBuilder.DropTable( + name: "AspNetUserTokens"); + + migrationBuilder.DropTable( + name: "AspNetRoles"); + + migrationBuilder.DropTable( + name: "AspNetUsers"); + } + } +} diff --git a/FoodTracker.Database/Migrations/ApplicationDbContextModelSnapshot.cs b/FoodTracker.Database/Migrations/ApplicationDbContextModelSnapshot.cs new file mode 100644 index 0000000..581fdf6 --- /dev/null +++ b/FoodTracker.Database/Migrations/ApplicationDbContextModelSnapshot.cs @@ -0,0 +1,276 @@ +// <auto-generated /> +using System; +using FoodTracker.Database.Data; +using Microsoft.EntityFrameworkCore; +using Microsoft.EntityFrameworkCore.Infrastructure; +using Microsoft.EntityFrameworkCore.Metadata; +using Microsoft.EntityFrameworkCore.Storage.ValueConversion; + +#nullable disable + +namespace FoodTracker.Database.Migrations +{ + [DbContext(typeof(ApplicationDbContext))] + partial class ApplicationDbContextModelSnapshot : ModelSnapshot + { + protected override void BuildModel(ModelBuilder modelBuilder) + { +#pragma warning disable 612, 618 + modelBuilder + .HasAnnotation("ProductVersion", "8.0.10") + .HasAnnotation("Relational:MaxIdentifierLength", 128); + + SqlServerModelBuilderExtensions.UseIdentityColumns(modelBuilder); + + modelBuilder.Entity("Microsoft.AspNetCore.Identity.IdentityRole", b => + { + b.Property<string>("Id") + .HasColumnType("nvarchar(450)"); + + b.Property<string>("ConcurrencyStamp") + .IsConcurrencyToken() + .HasColumnType("nvarchar(max)"); + + b.Property<string>("Name") + .HasMaxLength(256) + .HasColumnType("nvarchar(256)"); + + b.Property<string>("NormalizedName") + .HasMaxLength(256) + .HasColumnType("nvarchar(256)"); + + b.HasKey("Id"); + + b.HasIndex("NormalizedName") + .IsUnique() + .HasDatabaseName("RoleNameIndex") + .HasFilter("[NormalizedName] IS NOT NULL"); + + b.ToTable("AspNetRoles", (string)null); + }); + + modelBuilder.Entity("Microsoft.AspNetCore.Identity.IdentityRoleClaim<string>", b => + { + b.Property<int>("Id") + .ValueGeneratedOnAdd() + .HasColumnType("int"); + + SqlServerPropertyBuilderExtensions.UseIdentityColumn(b.Property<int>("Id")); + + b.Property<string>("ClaimType") + .HasColumnType("nvarchar(max)"); + + b.Property<string>("ClaimValue") + .HasColumnType("nvarchar(max)"); + + b.Property<string>("RoleId") + .IsRequired() + .HasColumnType("nvarchar(450)"); + + b.HasKey("Id"); + + b.HasIndex("RoleId"); + + b.ToTable("AspNetRoleClaims", (string)null); + }); + + modelBuilder.Entity("Microsoft.AspNetCore.Identity.IdentityUser", b => + { + b.Property<string>("Id") + .HasColumnType("nvarchar(450)"); + + b.Property<int>("AccessFailedCount") + .HasColumnType("int"); + + b.Property<string>("ConcurrencyStamp") + .IsConcurrencyToken() + .HasColumnType("nvarchar(max)"); + + b.Property<string>("Email") + .HasMaxLength(256) + .HasColumnType("nvarchar(256)"); + + b.Property<bool>("EmailConfirmed") + .HasColumnType("bit"); + + b.Property<bool>("LockoutEnabled") + .HasColumnType("bit"); + + b.Property<DateTimeOffset?>("LockoutEnd") + .HasColumnType("datetimeoffset"); + + b.Property<string>("NormalizedEmail") + .HasMaxLength(256) + .HasColumnType("nvarchar(256)"); + + b.Property<string>("NormalizedUserName") + .HasMaxLength(256) + .HasColumnType("nvarchar(256)"); + + b.Property<string>("PasswordHash") + .HasColumnType("nvarchar(max)"); + + b.Property<string>("PhoneNumber") + .HasColumnType("nvarchar(max)"); + + b.Property<bool>("PhoneNumberConfirmed") + .HasColumnType("bit"); + + b.Property<string>("SecurityStamp") + .HasColumnType("nvarchar(max)"); + + b.Property<bool>("TwoFactorEnabled") + .HasColumnType("bit"); + + b.Property<string>("UserName") + .HasMaxLength(256) + .HasColumnType("nvarchar(256)"); + + b.HasKey("Id"); + + b.HasIndex("NormalizedEmail") + .HasDatabaseName("EmailIndex"); + + b.HasIndex("NormalizedUserName") + .IsUnique() + .HasDatabaseName("UserNameIndex") + .HasFilter("[NormalizedUserName] IS NOT NULL"); + + b.ToTable("AspNetUsers", (string)null); + }); + + modelBuilder.Entity("Microsoft.AspNetCore.Identity.IdentityUserClaim<string>", b => + { + b.Property<int>("Id") + .ValueGeneratedOnAdd() + .HasColumnType("int"); + + SqlServerPropertyBuilderExtensions.UseIdentityColumn(b.Property<int>("Id")); + + b.Property<string>("ClaimType") + .HasColumnType("nvarchar(max)"); + + b.Property<string>("ClaimValue") + .HasColumnType("nvarchar(max)"); + + b.Property<string>("UserId") + .IsRequired() + .HasColumnType("nvarchar(450)"); + + b.HasKey("Id"); + + b.HasIndex("UserId"); + + b.ToTable("AspNetUserClaims", (string)null); + }); + + modelBuilder.Entity("Microsoft.AspNetCore.Identity.IdentityUserLogin<string>", b => + { + b.Property<string>("LoginProvider") + .HasColumnType("nvarchar(450)"); + + b.Property<string>("ProviderKey") + .HasColumnType("nvarchar(450)"); + + b.Property<string>("ProviderDisplayName") + .HasColumnType("nvarchar(max)"); + + b.Property<string>("UserId") + .IsRequired() + .HasColumnType("nvarchar(450)"); + + b.HasKey("LoginProvider", "ProviderKey"); + + b.HasIndex("UserId"); + + b.ToTable("AspNetUserLogins", (string)null); + }); + + modelBuilder.Entity("Microsoft.AspNetCore.Identity.IdentityUserRole<string>", b => + { + b.Property<string>("UserId") + .HasColumnType("nvarchar(450)"); + + b.Property<string>("RoleId") + .HasColumnType("nvarchar(450)"); + + b.HasKey("UserId", "RoleId"); + + b.HasIndex("RoleId"); + + b.ToTable("AspNetUserRoles", (string)null); + }); + + modelBuilder.Entity("Microsoft.AspNetCore.Identity.IdentityUserToken<string>", b => + { + b.Property<string>("UserId") + .HasColumnType("nvarchar(450)"); + + b.Property<string>("LoginProvider") + .HasColumnType("nvarchar(450)"); + + b.Property<string>("Name") + .HasColumnType("nvarchar(450)"); + + b.Property<string>("Value") + .HasColumnType("nvarchar(max)"); + + b.HasKey("UserId", "LoginProvider", "Name"); + + b.ToTable("AspNetUserTokens", (string)null); + }); + + modelBuilder.Entity("Microsoft.AspNetCore.Identity.IdentityRoleClaim<string>", b => + { + b.HasOne("Microsoft.AspNetCore.Identity.IdentityRole", null) + .WithMany() + .HasForeignKey("RoleId") + .OnDelete(DeleteBehavior.Cascade) + .IsRequired(); + }); + + modelBuilder.Entity("Microsoft.AspNetCore.Identity.IdentityUserClaim<string>", b => + { + b.HasOne("Microsoft.AspNetCore.Identity.IdentityUser", null) + .WithMany() + .HasForeignKey("UserId") + .OnDelete(DeleteBehavior.Cascade) + .IsRequired(); + }); + + modelBuilder.Entity("Microsoft.AspNetCore.Identity.IdentityUserLogin<string>", b => + { + b.HasOne("Microsoft.AspNetCore.Identity.IdentityUser", null) + .WithMany() + .HasForeignKey("UserId") + .OnDelete(DeleteBehavior.Cascade) + .IsRequired(); + }); + + modelBuilder.Entity("Microsoft.AspNetCore.Identity.IdentityUserRole<string>", b => + { + b.HasOne("Microsoft.AspNetCore.Identity.IdentityRole", null) + .WithMany() + .HasForeignKey("RoleId") + .OnDelete(DeleteBehavior.Cascade) + .IsRequired(); + + b.HasOne("Microsoft.AspNetCore.Identity.IdentityUser", null) + .WithMany() + .HasForeignKey("UserId") + .OnDelete(DeleteBehavior.Cascade) + .IsRequired(); + }); + + modelBuilder.Entity("Microsoft.AspNetCore.Identity.IdentityUserToken<string>", b => + { + b.HasOne("Microsoft.AspNetCore.Identity.IdentityUser", null) + .WithMany() + .HasForeignKey("UserId") + .OnDelete(DeleteBehavior.Cascade) + .IsRequired(); + }); +#pragma warning restore 612, 618 + } + } +} -- GitLab From 13011632b1953c38d96405ccb34a9b1ea04e19a5 Mon Sep 17 00:00:00 2001 From: krishna <vamsi.sirigirisetty@pal.tech> Date: Sat, 9 Nov 2024 12:31:27 +0530 Subject: [PATCH 4/5] Added IdentityTables and CreatedRoles --- FoodTracker.Api/FoodTracker.Api.http | 6 ------ FoodTracker.Api/Program.cs | 7 +++++++ 2 files changed, 7 insertions(+), 6 deletions(-) delete mode 100644 FoodTracker.Api/FoodTracker.Api.http diff --git a/FoodTracker.Api/FoodTracker.Api.http b/FoodTracker.Api/FoodTracker.Api.http deleted file mode 100644 index e58587d..0000000 --- a/FoodTracker.Api/FoodTracker.Api.http +++ /dev/null @@ -1,6 +0,0 @@ -@FoodTracker.Api_HostAddress = http://localhost:5139 - -GET {{FoodTracker.Api_HostAddress}}/weatherforecast/ -Accept: application/json - -### diff --git a/FoodTracker.Api/Program.cs b/FoodTracker.Api/Program.cs index cf2b8a9..01c3543 100644 --- a/FoodTracker.Api/Program.cs +++ b/FoodTracker.Api/Program.cs @@ -1,5 +1,7 @@ using FoodTracker.Database.Data; +using Microsoft.AspNetCore.Identity; using Microsoft.EntityFrameworkCore; +using FoodTracker.Database.ExtensionMethods; var builder = WebApplication.CreateBuilder(args); @@ -14,6 +16,11 @@ builder.Services.AddDbContext<ApplicationDbContext>(options => options.UseSqlServer(builder.Configuration.GetConnectionString("DefaultConnectionString")); }); +builder.Services.AddIdentity<IdentityUser,IdentityRole>() +.AddEntityFrameworkStores<ApplicationDbContext>(); + +await builder.Services.CreateRoles(); + var app = builder.Build(); -- GitLab From c67f461c569956b3ae364907bceb92d435740760 Mon Sep 17 00:00:00 2001 From: krishna <vamsi.sirigirisetty@pal.tech> Date: Sat, 9 Nov 2024 12:32:13 +0530 Subject: [PATCH 5/5] Added MealType Enumerations --- FoodTracker.Utilites/Enums/MealType.cs | 14 ++++++++++++++ 1 file changed, 14 insertions(+) create mode 100644 FoodTracker.Utilites/Enums/MealType.cs diff --git a/FoodTracker.Utilites/Enums/MealType.cs b/FoodTracker.Utilites/Enums/MealType.cs new file mode 100644 index 0000000..88ca053 --- /dev/null +++ b/FoodTracker.Utilites/Enums/MealType.cs @@ -0,0 +1,14 @@ +using System.Text.Json.Serialization; + +namespace FoodTracker.Utilites.Enums; +public enum MealType +{ + BreakFast, + Lunch, + Dinner, + MorningSnack, + EveningSnack, + NightSnack, + Other + +} -- GitLab