initial load
This commit is contained in:
@ -0,0 +1,38 @@
|
||||
using System;
|
||||
using System.Collections.Generic;
|
||||
using System.Linq;
|
||||
using System.Text;
|
||||
using System.Threading.Tasks;
|
||||
using YouTubeViewers.Domain.Commands;
|
||||
using YouTubeViewers.Domain.Models;
|
||||
using YouTubeViewers.EntityFramework.DTOs;
|
||||
|
||||
namespace YouTubeViewers.EntityFramework.Commands
|
||||
{
|
||||
public class CreateYouTubeViewerCommand :ICreateYouTubeViewerCommand
|
||||
{
|
||||
private readonly YouTubeViewersDbContextFactory _contextFactory;
|
||||
|
||||
public CreateYouTubeViewerCommand(YouTubeViewersDbContextFactory contextFactory)
|
||||
{
|
||||
_contextFactory = contextFactory;
|
||||
}
|
||||
|
||||
public async Task Execute(YouTubeViewer youTubeViewer)
|
||||
{
|
||||
using (YouTubeViewersDbContext context = _contextFactory.Create())
|
||||
{
|
||||
YouTubeViewerDto youTubeViewerDto = new YouTubeViewerDto()
|
||||
{
|
||||
Id = youTubeViewer.Id,
|
||||
Username = youTubeViewer.Username,
|
||||
IsSubscribed = youTubeViewer.IsSubscribed,
|
||||
IsMember = youTubeViewer.IsMember,
|
||||
};
|
||||
|
||||
context.YouTubeViewers.Add(youTubeViewerDto);
|
||||
await context.SaveChangesAsync();
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
@ -0,0 +1,35 @@
|
||||
using System;
|
||||
using System.Collections.Generic;
|
||||
using System.Linq;
|
||||
using System.Text;
|
||||
using System.Threading.Tasks;
|
||||
using YouTubeViewers.Domain.Commands;
|
||||
using YouTubeViewers.EntityFramework.DTOs;
|
||||
|
||||
namespace YouTubeViewers.EntityFramework.Commands
|
||||
{
|
||||
public class DeleteYouTubeViewerCommand : IDeleteYouTubeViewerCommand
|
||||
{
|
||||
private readonly YouTubeViewersDbContextFactory _contextFactory;
|
||||
|
||||
public DeleteYouTubeViewerCommand(YouTubeViewersDbContextFactory contextFactory)
|
||||
{
|
||||
_contextFactory = contextFactory;
|
||||
}
|
||||
|
||||
public async Task Execute(Guid id)
|
||||
{
|
||||
using (YouTubeViewersDbContext context = _contextFactory.Create())
|
||||
{
|
||||
await Task.Delay(5000);
|
||||
YouTubeViewerDto youTubeViewerDto = new YouTubeViewerDto()
|
||||
{
|
||||
Id = id,
|
||||
};
|
||||
|
||||
context.YouTubeViewers.Remove(youTubeViewerDto);
|
||||
await context.SaveChangesAsync();
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
@ -0,0 +1,38 @@
|
||||
using System;
|
||||
using System.Collections.Generic;
|
||||
using System.Linq;
|
||||
using System.Text;
|
||||
using System.Threading.Tasks;
|
||||
using YouTubeViewers.Domain.Commands;
|
||||
using YouTubeViewers.Domain.Models;
|
||||
using YouTubeViewers.EntityFramework.DTOs;
|
||||
|
||||
namespace YouTubeViewers.EntityFramework.Commands
|
||||
{
|
||||
public class UpdateYouTubeViewerCommand : IUpdateYouTubeViewerCommand
|
||||
{
|
||||
private readonly YouTubeViewersDbContextFactory _contextFactory;
|
||||
|
||||
public UpdateYouTubeViewerCommand(YouTubeViewersDbContextFactory contextFactory)
|
||||
{
|
||||
_contextFactory = contextFactory;
|
||||
}
|
||||
|
||||
public async Task Execute(YouTubeViewer youTubeViewer)
|
||||
{
|
||||
using (YouTubeViewersDbContext context = _contextFactory.Create())
|
||||
{
|
||||
YouTubeViewerDto youTubeViewerDto = new YouTubeViewerDto()
|
||||
{
|
||||
Id = youTubeViewer.Id,
|
||||
Username = youTubeViewer.Username,
|
||||
IsSubscribed = youTubeViewer.IsSubscribed,
|
||||
IsMember = youTubeViewer.IsMember,
|
||||
};
|
||||
|
||||
context.YouTubeViewers.Update(youTubeViewerDto);
|
||||
await context.SaveChangesAsync();
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
16
YouTubeViewers.EntityFramework/DTOs/YouTubeViewerDto.cs
Normal file
16
YouTubeViewers.EntityFramework/DTOs/YouTubeViewerDto.cs
Normal file
@ -0,0 +1,16 @@
|
||||
using System;
|
||||
using System.Collections.Generic;
|
||||
using System.Linq;
|
||||
using System.Text;
|
||||
using System.Threading.Tasks;
|
||||
|
||||
namespace YouTubeViewers.EntityFramework.DTOs
|
||||
{
|
||||
public class YouTubeViewerDto
|
||||
{
|
||||
public Guid Id { get; set; }
|
||||
public string Username { get; set; }
|
||||
public bool IsSubscribed { get; set; }
|
||||
public bool IsMember { get; set; }
|
||||
}
|
||||
}
|
||||
45
YouTubeViewers.EntityFramework/Migrations/20220819131646_Initial.Designer.cs
generated
Normal file
45
YouTubeViewers.EntityFramework/Migrations/20220819131646_Initial.Designer.cs
generated
Normal file
@ -0,0 +1,45 @@
|
||||
// <auto-generated />
|
||||
using System;
|
||||
using Microsoft.EntityFrameworkCore;
|
||||
using Microsoft.EntityFrameworkCore.Infrastructure;
|
||||
using Microsoft.EntityFrameworkCore.Migrations;
|
||||
using Microsoft.EntityFrameworkCore.Storage.ValueConversion;
|
||||
using YouTubeViewers.EntityFramework;
|
||||
|
||||
#nullable disable
|
||||
|
||||
namespace YouTubeViewers.EntityFramework.Migrations
|
||||
{
|
||||
[DbContext(typeof(YouTubeViewersDbContext))]
|
||||
[Migration("20220819131646_Initial")]
|
||||
partial class Initial
|
||||
{
|
||||
protected override void BuildTargetModel(ModelBuilder modelBuilder)
|
||||
{
|
||||
#pragma warning disable 612, 618
|
||||
modelBuilder.HasAnnotation("ProductVersion", "6.0.8");
|
||||
|
||||
modelBuilder.Entity("YouTubeViewers.EntityFramework.DTOs.YouTubeViewerDto", b =>
|
||||
{
|
||||
b.Property<Guid>("Id")
|
||||
.ValueGeneratedOnAdd()
|
||||
.HasColumnType("TEXT");
|
||||
|
||||
b.Property<bool>("IsMember")
|
||||
.HasColumnType("INTEGER");
|
||||
|
||||
b.Property<bool>("IsSubscribed")
|
||||
.HasColumnType("INTEGER");
|
||||
|
||||
b.Property<string>("Username")
|
||||
.IsRequired()
|
||||
.HasColumnType("TEXT");
|
||||
|
||||
b.HasKey("Id");
|
||||
|
||||
b.ToTable("YouTubeViewers");
|
||||
});
|
||||
#pragma warning restore 612, 618
|
||||
}
|
||||
}
|
||||
}
|
||||
@ -0,0 +1,33 @@
|
||||
using System;
|
||||
using Microsoft.EntityFrameworkCore.Migrations;
|
||||
|
||||
#nullable disable
|
||||
|
||||
namespace YouTubeViewers.EntityFramework.Migrations
|
||||
{
|
||||
public partial class Initial : Migration
|
||||
{
|
||||
protected override void Up(MigrationBuilder migrationBuilder)
|
||||
{
|
||||
migrationBuilder.CreateTable(
|
||||
name: "YouTubeViewers",
|
||||
columns: table => new
|
||||
{
|
||||
Id = table.Column<Guid>(type: "TEXT", nullable: false),
|
||||
Username = table.Column<string>(type: "TEXT", nullable: false),
|
||||
IsSubscribed = table.Column<bool>(type: "INTEGER", nullable: false),
|
||||
IsMember = table.Column<bool>(type: "INTEGER", nullable: false)
|
||||
},
|
||||
constraints: table =>
|
||||
{
|
||||
table.PrimaryKey("PK_YouTubeViewers", x => x.Id);
|
||||
});
|
||||
}
|
||||
|
||||
protected override void Down(MigrationBuilder migrationBuilder)
|
||||
{
|
||||
migrationBuilder.DropTable(
|
||||
name: "YouTubeViewers");
|
||||
}
|
||||
}
|
||||
}
|
||||
@ -0,0 +1,43 @@
|
||||
// <auto-generated />
|
||||
using System;
|
||||
using Microsoft.EntityFrameworkCore;
|
||||
using Microsoft.EntityFrameworkCore.Infrastructure;
|
||||
using Microsoft.EntityFrameworkCore.Storage.ValueConversion;
|
||||
using YouTubeViewers.EntityFramework;
|
||||
|
||||
#nullable disable
|
||||
|
||||
namespace YouTubeViewers.EntityFramework.Migrations
|
||||
{
|
||||
[DbContext(typeof(YouTubeViewersDbContext))]
|
||||
partial class YouTubeViewersDbContextModelSnapshot : ModelSnapshot
|
||||
{
|
||||
protected override void BuildModel(ModelBuilder modelBuilder)
|
||||
{
|
||||
#pragma warning disable 612, 618
|
||||
modelBuilder.HasAnnotation("ProductVersion", "6.0.8");
|
||||
|
||||
modelBuilder.Entity("YouTubeViewers.EntityFramework.DTOs.YouTubeViewerDto", b =>
|
||||
{
|
||||
b.Property<Guid>("Id")
|
||||
.ValueGeneratedOnAdd()
|
||||
.HasColumnType("TEXT");
|
||||
|
||||
b.Property<bool>("IsMember")
|
||||
.HasColumnType("INTEGER");
|
||||
|
||||
b.Property<bool>("IsSubscribed")
|
||||
.HasColumnType("INTEGER");
|
||||
|
||||
b.Property<string>("Username")
|
||||
.IsRequired()
|
||||
.HasColumnType("TEXT");
|
||||
|
||||
b.HasKey("Id");
|
||||
|
||||
b.ToTable("YouTubeViewers");
|
||||
});
|
||||
#pragma warning restore 612, 618
|
||||
}
|
||||
}
|
||||
}
|
||||
@ -0,0 +1,34 @@
|
||||
using Microsoft.EntityFrameworkCore;
|
||||
using System;
|
||||
using System.Collections.Generic;
|
||||
using System.Linq;
|
||||
using System.Text;
|
||||
using System.Threading.Tasks;
|
||||
using YouTubeViewers.Domain.Models;
|
||||
using YouTubeViewers.Domain.Queries;
|
||||
using YouTubeViewers.EntityFramework.DTOs;
|
||||
|
||||
namespace YouTubeViewers.EntityFramework.Queries
|
||||
{
|
||||
public class GetAllYouTubeViewersQuery : IGetAllYouTubeViewersQuery
|
||||
{
|
||||
private readonly YouTubeViewersDbContextFactory _contextFactory;
|
||||
|
||||
public GetAllYouTubeViewersQuery(YouTubeViewersDbContextFactory contextFactory)
|
||||
{
|
||||
_contextFactory = contextFactory;
|
||||
}
|
||||
|
||||
public async Task<IEnumerable<YouTubeViewer>> Execute()
|
||||
{
|
||||
using (YouTubeViewersDbContext context = _contextFactory.Create())
|
||||
{
|
||||
//await Task.Delay(5000);
|
||||
|
||||
IEnumerable<YouTubeViewerDto> youTubeViewerDtos = await context.YouTubeViewers.ToListAsync();
|
||||
|
||||
return youTubeViewerDtos.Select(y => new YouTubeViewer(y.Id, y.Username, y.IsSubscribed, y.IsMember));
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
@ -0,0 +1,22 @@
|
||||
<Project Sdk="Microsoft.NET.Sdk">
|
||||
|
||||
<PropertyGroup>
|
||||
<TargetFramework>net6.0</TargetFramework>
|
||||
<ImplicitUsings>enable</ImplicitUsings>
|
||||
<Nullable>enable</Nullable>
|
||||
</PropertyGroup>
|
||||
|
||||
<ItemGroup>
|
||||
<PackageReference Include="Microsoft.EntityFrameworkCore" Version="6.0.8" />
|
||||
<PackageReference Include="Microsoft.EntityFrameworkCore.Design" Version="6.0.8">
|
||||
<PrivateAssets>all</PrivateAssets>
|
||||
<IncludeAssets>runtime; build; native; contentfiles; analyzers; buildtransitive</IncludeAssets>
|
||||
</PackageReference>
|
||||
<PackageReference Include="Microsoft.EntityFrameworkCore.Sqlite" Version="6.0.8" />
|
||||
</ItemGroup>
|
||||
|
||||
<ItemGroup>
|
||||
<ProjectReference Include="..\YouTubeViewers.Domain\YouTubeViewers.Domain.csproj" />
|
||||
</ItemGroup>
|
||||
|
||||
</Project>
|
||||
24
YouTubeViewers.EntityFramework/YouTubeViewersDbContext.cs
Normal file
24
YouTubeViewers.EntityFramework/YouTubeViewersDbContext.cs
Normal file
@ -0,0 +1,24 @@
|
||||
using Microsoft.EntityFrameworkCore;
|
||||
using System;
|
||||
using System.Collections.Generic;
|
||||
using System.Linq;
|
||||
using System.Text;
|
||||
using System.Threading.Tasks;
|
||||
using YouTubeViewers.EntityFramework.DTOs;
|
||||
|
||||
namespace YouTubeViewers.EntityFramework
|
||||
{
|
||||
public class YouTubeViewersDbContext : DbContext
|
||||
{
|
||||
public YouTubeViewersDbContext(DbContextOptions options) : base(options) { }
|
||||
|
||||
public DbSet<YouTubeViewerDto> YouTubeViewers { get; set; }
|
||||
|
||||
//DbContextOptions options = new DbContextOptionsBuilder()
|
||||
// .UseSqlite(_connectionString)
|
||||
// .Options;
|
||||
|
||||
|
||||
}
|
||||
|
||||
}
|
||||
@ -0,0 +1,25 @@
|
||||
using Microsoft.EntityFrameworkCore;
|
||||
using System;
|
||||
using System.Collections.Generic;
|
||||
using System.Linq;
|
||||
using System.Text;
|
||||
using System.Threading.Tasks;
|
||||
|
||||
namespace YouTubeViewers.EntityFramework
|
||||
{
|
||||
public class YouTubeViewersDbContextFactory
|
||||
{
|
||||
private readonly DbContextOptions _options;
|
||||
|
||||
public YouTubeViewersDbContextFactory(DbContextOptions options)
|
||||
{
|
||||
_options = options;
|
||||
}
|
||||
|
||||
public YouTubeViewersDbContext Create()
|
||||
{
|
||||
|
||||
return new YouTubeViewersDbContext(_options);
|
||||
}
|
||||
}
|
||||
}
|
||||
@ -0,0 +1,18 @@
|
||||
using Microsoft.EntityFrameworkCore;
|
||||
using Microsoft.EntityFrameworkCore.Design;
|
||||
using System;
|
||||
using System.Collections.Generic;
|
||||
using System.Linq;
|
||||
using System.Text;
|
||||
using System.Threading.Tasks;
|
||||
|
||||
namespace YouTubeViewers.EntityFramework
|
||||
{
|
||||
public class YouTubeViewersDesignTimeDbContextFactory : IDesignTimeDbContextFactory<YouTubeViewersDbContext>
|
||||
{
|
||||
public YouTubeViewersDbContext CreateDbContext(string[] args = null)
|
||||
{
|
||||
return new YouTubeViewersDbContext(new DbContextOptionsBuilder().UseSqlite("Data Source=YouTubeViewers.db").Options);
|
||||
}
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user