Skip to content

Commit

Permalink
Add Entity, Database, and DB Initializer
Browse files Browse the repository at this point in the history
  • Loading branch information
Christian-Schou committed Jan 17, 2023
1 parent 360ee99 commit a5c943c
Show file tree
Hide file tree
Showing 4 changed files with 111 additions and 1 deletion.
1 change: 0 additions & 1 deletion AzureRedisCacheApi/AzureRedisCacheApi.csproj
Expand Up @@ -2,7 +2,6 @@

<PropertyGroup>
<TargetFramework>net7.0</TargetFramework>
<Nullable>enable</Nullable>
<ImplicitUsings>enable</ImplicitUsings>
</PropertyGroup>

Expand Down
17 changes: 17 additions & 0 deletions AzureRedisCacheApi/Entities/Book.cs
@@ -0,0 +1,17 @@
namespace AzureRedisCacheApi.Entities
{
public class Book
{
public int Id { get; set; }
public string Title { get; set; }
public string Description { get; set; }
public string Author { get; set; }
public string Publisher { get; set; }
public string Language { get; set; }
public string Edition { get; set; }
public DateTime Published { get; set; }
public string ISBN10 { get; set; }
public string ISBN13 { get; set; }
public double Rating { get; set; }
}
}
24 changes: 24 additions & 0 deletions AzureRedisCacheApi/Persistence/AppDbContext.cs
@@ -0,0 +1,24 @@
using AzureRedisCacheApi.Entities;
using AzureRedisCacheApi.Persistence.Seed;
using Microsoft.EntityFrameworkCore;

namespace AzureRedisCacheApi.Persistence
{
public class AppDbContext : DbContext
{
public AppDbContext(DbContextOptions<AppDbContext> options) : base(options)
{
}

// Add the books entity to be added in our database
public DbSet<Book> Books { get; set; }

protected override void OnModelCreating(ModelBuilder modelBuilder)
{
// Seed database with new data if its not already present
modelBuilder.Seed();

base.OnModelCreating(modelBuilder);
}
}
}
70 changes: 70 additions & 0 deletions AzureRedisCacheApi/Persistence/Seed/DbInitializer.cs
@@ -0,0 +1,70 @@
using AzureRedisCacheApi.Entities;
using Microsoft.EntityFrameworkCore;

namespace AzureRedisCacheApi.Persistence.Seed
{
public static class DbInitializer
{
public static void Seed(this ModelBuilder modelBuilder)
{
modelBuilder.Entity<Book>(x =>
{
x.HasData(new Book
{
Id = 1,
Title = "Clean Code: A Handbook of Agile Software Craftsmanship",
Description = "Even bad code can function. But if code isn’t clean, it can bring a development organization to its knees. Every year, countless hours and significant resources are lost because of poorly written code. But it doesn’t have to be that way.",
Author = "Robert C. Martin (Author)",
Publisher = "",
Language = "English",
Edition = "1st",
ISBN10 = "9780132350884",
ISBN13 = "978-0132350884",
Published = DateTime.Parse("Aug 1, 2008")
});
x.HasData(new Book
{
Id = 2,
Title = "Design Patterns: Elements of Reusable Object-Oriented Software",
Description = "Capturing a wealth of experience about the design of object-oriented software, four top-notch designers present a catalog of simple and succinct solutions to commonly occurring design problems. Previously undocumented, these 23 patterns allow designers to create more flexible, elegant, and ultimately reusable designs without having to rediscover the design solutions themselves.",
Author = "Erich Gamma (Author), Richard Helm (Author), Ralph Johnson (Author), John Vlissides (Author), Grady Booch (Foreword)",
Publisher = "Pearson",
Language = "English",
Edition = "1st",
ISBN10 = "0201633612",
ISBN13 = "978-0201633610",
Published = DateTime.Parse("Oct 31, 1994")
});
x.HasData(new Book
{
Id = 3,
Title = "Head First Design Patterns: A Brain-Friendly Guide",
Description = "At any given moment, someone struggles with the same software design problems you have. And, chances are, someone else has already solved your problem. This edition of Head First Design Patterns—now updated for Java 8—shows you the tried-and-true, road-tested patterns used by developers to create functional, elegant, reusable, and flexible software.",
Author = "Eric Freeman (Author), Bert Bates (Author), Kathy Sierra (Author), Elisabeth Robson (Author)",
Publisher = "O'Reilly Media",
Language = "English",
Edition = "1st",
ISBN10 = "9780596007126",
ISBN13 = "978-0596007126",
Published = DateTime.Parse("Oct 1, 2004")
});
x.HasData(new Book
{
Id = 4,
Title = "The Pragmatic Programmer: From Journeyman to Master",
Description = "Ward Cunningham Straight from the programming trenches, The Pragmatic Programmer cuts through the increasing specialization and technicalities of modern software development to examine the core process--taking a requirement and producing working, maintainable code that delights its users.",
Author = "Andrew Hunt (Author), David Thomas (Author)",
Publisher = "Addison-Wesley Professional",
Language = "English",
Edition = "1st",
ISBN10 = "9780201616224",
ISBN13 = "978-0201616224",
Published = DateTime.Parse("Oct 30, 1999")
});
});
}
}
}

0 comments on commit a5c943c

Please sign in to comment.