chore: adding postgre support [wip - batch 1]
All checks were successful
continuous-integration/drone/push Build is passing

This commit is contained in:
surtur 2020-12-06 04:46:36 +01:00
parent 8a628a890b
commit e5b871c275
Signed by: wanderer
GPG Key ID: 19CE1EC1D9E0486D
10 changed files with 142 additions and 1 deletions

View File

@ -1,14 +1,26 @@
using Microsoft.AspNetCore.Http;
using System.ComponentModel.DataAnnotations;
using System.ComponentModel.DataAnnotations.Schema;
namespace pwt_0x01_ng.Models
{
[Table("Carousel")]
public class Carousel
{
[Key]
[Required]
public int id { get; set; }
[Required]
public string DataTarget { get; set; }
[NotMapped]
public IFormFile Image { get; set; }
[Required]
[StringLength(255)]
public string ImageSrc { get; set; }
[Required]
[StringLength(50)]
public string ImageAlt { get; set; }
[Required]
public string CarouselContent { get; set; }
}
}

View File

@ -0,0 +1,20 @@
using System.Collections.Generic;
using Microsoft.IdentityModel.Tokens;
namespace pwt_0x01_ng.Models.Database
{
public static class CarouselHelper
{
public static IList<Carousel> GenerateCarousel()
{
IList<Carousel> carousels = new List<Carousel>()
{
new Carousel() { DataTarget = "#myCarousel", ImageSrc = "/images/banner1.svg", ImageAlt = "ASP.NET", CarouselContent = "Learn how to build ASP.NET apps that can run anywhere.<a class=\"btn btn-default\" href=\"https://go.microsoft.com/fwlink/?LinkID=525028&clcid=0x409\">Learn More</a>"},
new Carousel() { DataTarget = "#myCarousel", ImageSrc = "/images/banner2.svg", ImageAlt = "ASP.NET", CarouselContent = "There are powerful new features in Visual Studio for building modern web apps.<a class=\"btn btn-default\" href=\"https://go.microsoft.com/fwlink/?LinkID=525030&clcid=0x409\">Learn More</a>"},
new Carousel() { DataTarget = "#myCarousel", ImageSrc = "/images/banner3.svg", ImageAlt = "ASP.NET", CarouselContent = "Learn how Microsoft's Azure cloud platform allows you to build, deploy, and scale web apps.<a class=\"btn btn-default\" href=\"https://go.microsoft.com/fwlink/?LinkID=525027&clcid=0x409\">Learn More</a>"},
new Carousel() { DataTarget = "#myCarousel", ImageSrc = "/images/ms_loves_linux.jpeg", ImageAlt = "msloveslinux", CarouselContent = "ms loves linux"}
};
return carousels;
}
}
}

View File

@ -0,0 +1,17 @@
using System;
using Microsoft.EntityFrameworkCore;
using Npgsql.EntityFrameworkCore;
using pwt_0x01_ng.Models;
namespace pwt0x01ng.Models.Database
{
public class DBContext : DbContext
{
public DBContext(DbContextOptions<DBContext> options) : base(options)
{
}
public DbSet<Carousel> Carousel { get; set; }
}
}

24
Models/Database/DBInit.cs Normal file
View File

@ -0,0 +1,24 @@
using System;
using pwt_0x01_ng.Models.Database;
using System.Collections.Generic;
using pwt_0x01_ng.Models;
namespace pwt0x01ng.Models.Database
{
public static class DBInit
{
public static void Init(DBContext dbContext)
{
if (dbContext.Database.EnsureCreated())
{
IList<Carousel> carousels = CarouselHelper.GenerateCarousel();
foreach (var c in carousels)
{
dbContext.Carousel.Add(c);
}
dbContext.SaveChanges();
}
}
}
}

View File

@ -6,7 +6,9 @@ using System.Threading.Tasks;
using Microsoft.AspNetCore;
using Microsoft.AspNetCore.Hosting;
using Microsoft.Extensions.Configuration;
using Microsoft.Extensions.DependencyInjection;
using Microsoft.Extensions.Logging;
using pwt0x01ng.Models.Database;
namespace pwt_0x01_ng
{
@ -14,7 +16,16 @@ namespace pwt_0x01_ng
{
public static void Main(string[] args)
{
CreateWebHostBuilder(args).Build().Run();
IWebHost webHost = CreateWebHostBuilder(args).Build();
using (var scope = webHost.Services.CreateScope())
{
var serviceProvider = scope.ServiceProvider;
var dbctx = serviceProvider.GetRequiredService<DBContext>();
DBInit.Init(dbctx);
}
webHost.Run();
}
public static IWebHostBuilder CreateWebHostBuilder(string[] args) =>

View File

@ -10,6 +10,8 @@ using Microsoft.AspNetCore.HttpsPolicy;
using Microsoft.AspNetCore.Mvc;
using Microsoft.Extensions.Configuration;
using Microsoft.Extensions.DependencyInjection;
using pwt0x01ng.Models.Database;
using Microsoft.EntityFrameworkCore;
namespace pwt_0x01_ng
{
@ -35,6 +37,14 @@ namespace pwt_0x01_ng
});
var connectionString = Environment.GetEnvironmentVariable("DB_CONNECTION_STRING");
services.AddDbContext<DBContext>(options =>
options.UseNpgsql(
connectionString
)
);
services.AddMvc().SetCompatibilityVersion(CompatibilityVersion.Version_2_1);
}

View File

@ -1,4 +1,8 @@
{
"db": {
"Postgre": "host=db,port=5432,database=pwt;username=postgres;password=679968312e029a806c1905c40ec331aa199a1eb86bd0b9eb04057933e449bdc9ef8ef292a39b68cafa5689c901a17266",
"MySQL": "server=db,Database=pwt-0x01-ng;Port=3306;user=root;password=679968312e029a806c1905c40ec331aa199a1eb86bd0b9eb04057933e449bdc9ef8ef292a39b68cafa5689c901a17266;"
},
"Logging": {
"LogLevel": {
"Default": "Debug",

View File

@ -1,4 +1,8 @@
{
"db": {
"Postgre": "host=db,port=5432;database=pwt;username=postgres;password=679968312e029a806c1905c40ec331aa199a1eb86bd0b9eb04057933e449bdc9ef8ef292a39b68cafa5689c901a17266",
"MySQL": "server=db,Database=pwt-0x01-ng;Port=3306;user=root;password=679968312e029a806c1905c40ec331aa199a1eb86bd0b9eb04057933e449bdc9ef8ef292a39b68cafa5689c901a17266;"
},
"Logging": {
"LogLevel": {
"Default": "Warning"

36
docker-compose.yml Normal file
View File

@ -0,0 +1,36 @@
version: '3.8'
services:
netcoreultimateapp-dev:
build:
context: .
dockerfile: Dockerfile.dev
ports:
- 127.0.0.1:8001:5000
environment:
ASPNETCORE_ENVIRONMENT: Development
DB_CONNECTION_STRING: "host=db,port=5432;database=pwt;username=postgres;password=679968312e029a806c1905c40ec331aa199a1eb86bd0b9eb04057933e449bdc9ef8ef292a39b68cafa5689c901a17266"
restart: always
cap_drop:
- NET_ADMIN
- SYS_ADMIN
depends_on:
- db
db:
image: postgres:13.1-alpine
ports:
- 5432:5432
volumes:
- dbdata:/var/lib/postgresql/data
environment:
POSTGRES_USER: postgres
POSTGRES_PASSWORD: 679968312e029a806c1905c40ec331aa199a1eb86bd0b9eb04057933e449bdc9ef8ef292a39b68cafa5689c901a17266
POSTGRES_INITDB_ARGS: "--data-checksums"
restart: always
cap_drop:
- NET_ADMIN
- SYS_ADMIN
volumes:
dbdata:

View File

@ -15,11 +15,14 @@
<ItemGroup>
<PackageReference Include="Microsoft.AspNetCore.App" />
<PackageReference Include="Microsoft.AspNetCore.Razor.Design" Version="2.1.2" PrivateAssets="All" />
<PackageReference Include="Microsoft.EntityFrameworkCore" Version="2.1.14" />
<PackageReference Include="Npgsql.EntityFrameworkCore.PostgreSQL" Version="2.1.2" />
</ItemGroup>
<ItemGroup>
<Folder Include="Areas\Admin\Data" />
<Folder Include="Areas\Admin\Models" />
<Folder Include="Models\Database\" />
</ItemGroup>
</Project>