From 165a47f37a29bf577e47b6c15d5cde05a5af5e61 Mon Sep 17 00:00:00 2001 From: BabychMaksym Date: Thu, 4 Apr 2024 11:19:55 +0300 Subject: [PATCH] Add HomeWork 23 (Modul) --- HomeWork23/HomeWork23.sln | 25 +++ HomeWork23/HomeWork23/App.cs | 185 ++++++++++++++++ .../HomeWork23/Datas/ApplicatDbContext.cs | 26 +++ .../Datas/ApplicatDbContextFactory.cs | 29 +++ .../HomeWork23/Datas/Entities/BreedEntity.cs | 11 + .../Datas/Entities/CategoryEntity.cs | 10 + .../Datas/Entities/LocationEntity.cs | 9 + .../HomeWork23/Datas/Entities/PetEntity.cs | 18 ++ .../BreedEntityConffiguration.cs | 28 +++ .../CategoryEntityConfiguration.cs | 21 ++ .../LocationEntityConfiguration.cs | 21 ++ .../EntityConfigure/PetEntityConfiguration.cs | 53 +++++ .../HomeWork23/Dto/Requests/PetRequest.cs | 8 + HomeWork23/HomeWork23/Dto/Responses.cs | 7 + HomeWork23/HomeWork23/HomeWork23.csproj | 36 ++++ ...20240403173228_CreateMigration.Designer.cs | 195 +++++++++++++++++ .../20240403173228_CreateMigration.cs | 135 ++++++++++++ .../ApplicatDbContextModelSnapshot.cs | 192 +++++++++++++++++ HomeWork23/HomeWork23/Models/Breed.cs | 10 + HomeWork23/HomeWork23/Models/Category.cs | 8 + HomeWork23/HomeWork23/Models/Location.cs | 8 + HomeWork23/HomeWork23/Models/Pet.cs | 17 ++ HomeWork23/HomeWork23/Program.cs | 60 ++++++ .../Abstracts/IBreedRepository.cs | 12 ++ .../Abstracts/ICategoryRepository.cs | 12 ++ .../Abstracts/ILocationRepository.cs | 12 ++ .../Repositories/Abstracts/IPetRepository.cs | 15 ++ .../Repositories/BreedRepository.cs | 68 ++++++ .../Repositories/CategoryRepository.cs | 67 ++++++ .../Repositories/LocationRepositorycs.cs | 72 +++++++ .../HomeWork23/Repositories/PetRepository.cs | 103 +++++++++ .../Services/Abstracts/IBreedService.cs | 12 ++ .../Services/Abstracts/ICategoryService.cs | 12 ++ .../Services/Abstracts/ILocationService.cs | 12 ++ .../Services/Abstracts/IPetService.cs | 15 ++ .../HomeWork23/Services/BreedService.cs | 114 ++++++++++ .../HomeWork23/Services/CategoryService.cs | 88 ++++++++ .../HomeWork23/Services/LocationService.cs | 87 ++++++++ HomeWork23/HomeWork23/Services/PetService.cs | 199 ++++++++++++++++++ .../Wrapper/Abstracts/BaseDataService.cs | 69 ++++++ .../Wrapper/Abstracts/IBaseDataService.cs | 6 + .../Wrapper/Abstracts/IDbContextWrapper.cs | 12 ++ .../HomeWork23/Wrapper/DbContextWrapper.cs | 23 ++ HomeWork23/HomeWork23/config.json | 8 + 44 files changed, 2130 insertions(+) create mode 100644 HomeWork23/HomeWork23.sln create mode 100644 HomeWork23/HomeWork23/App.cs create mode 100644 HomeWork23/HomeWork23/Datas/ApplicatDbContext.cs create mode 100644 HomeWork23/HomeWork23/Datas/ApplicatDbContextFactory.cs create mode 100644 HomeWork23/HomeWork23/Datas/Entities/BreedEntity.cs create mode 100644 HomeWork23/HomeWork23/Datas/Entities/CategoryEntity.cs create mode 100644 HomeWork23/HomeWork23/Datas/Entities/LocationEntity.cs create mode 100644 HomeWork23/HomeWork23/Datas/Entities/PetEntity.cs create mode 100644 HomeWork23/HomeWork23/Datas/EntityConfigure/BreedEntityConffiguration.cs create mode 100644 HomeWork23/HomeWork23/Datas/EntityConfigure/CategoryEntityConfiguration.cs create mode 100644 HomeWork23/HomeWork23/Datas/EntityConfigure/LocationEntityConfiguration.cs create mode 100644 HomeWork23/HomeWork23/Datas/EntityConfigure/PetEntityConfiguration.cs create mode 100644 HomeWork23/HomeWork23/Dto/Requests/PetRequest.cs create mode 100644 HomeWork23/HomeWork23/Dto/Responses.cs create mode 100644 HomeWork23/HomeWork23/HomeWork23.csproj create mode 100644 HomeWork23/HomeWork23/Migrations/20240403173228_CreateMigration.Designer.cs create mode 100644 HomeWork23/HomeWork23/Migrations/20240403173228_CreateMigration.cs create mode 100644 HomeWork23/HomeWork23/Migrations/ApplicatDbContextModelSnapshot.cs create mode 100644 HomeWork23/HomeWork23/Models/Breed.cs create mode 100644 HomeWork23/HomeWork23/Models/Category.cs create mode 100644 HomeWork23/HomeWork23/Models/Location.cs create mode 100644 HomeWork23/HomeWork23/Models/Pet.cs create mode 100644 HomeWork23/HomeWork23/Program.cs create mode 100644 HomeWork23/HomeWork23/Repositories/Abstracts/IBreedRepository.cs create mode 100644 HomeWork23/HomeWork23/Repositories/Abstracts/ICategoryRepository.cs create mode 100644 HomeWork23/HomeWork23/Repositories/Abstracts/ILocationRepository.cs create mode 100644 HomeWork23/HomeWork23/Repositories/Abstracts/IPetRepository.cs create mode 100644 HomeWork23/HomeWork23/Repositories/BreedRepository.cs create mode 100644 HomeWork23/HomeWork23/Repositories/CategoryRepository.cs create mode 100644 HomeWork23/HomeWork23/Repositories/LocationRepositorycs.cs create mode 100644 HomeWork23/HomeWork23/Repositories/PetRepository.cs create mode 100644 HomeWork23/HomeWork23/Services/Abstracts/IBreedService.cs create mode 100644 HomeWork23/HomeWork23/Services/Abstracts/ICategoryService.cs create mode 100644 HomeWork23/HomeWork23/Services/Abstracts/ILocationService.cs create mode 100644 HomeWork23/HomeWork23/Services/Abstracts/IPetService.cs create mode 100644 HomeWork23/HomeWork23/Services/BreedService.cs create mode 100644 HomeWork23/HomeWork23/Services/CategoryService.cs create mode 100644 HomeWork23/HomeWork23/Services/LocationService.cs create mode 100644 HomeWork23/HomeWork23/Services/PetService.cs create mode 100644 HomeWork23/HomeWork23/Wrapper/Abstracts/BaseDataService.cs create mode 100644 HomeWork23/HomeWork23/Wrapper/Abstracts/IBaseDataService.cs create mode 100644 HomeWork23/HomeWork23/Wrapper/Abstracts/IDbContextWrapper.cs create mode 100644 HomeWork23/HomeWork23/Wrapper/DbContextWrapper.cs create mode 100644 HomeWork23/HomeWork23/config.json diff --git a/HomeWork23/HomeWork23.sln b/HomeWork23/HomeWork23.sln new file mode 100644 index 0000000..2c9a847 --- /dev/null +++ b/HomeWork23/HomeWork23.sln @@ -0,0 +1,25 @@ + +Microsoft Visual Studio Solution File, Format Version 12.00 +# Visual Studio Version 17 +VisualStudioVersion = 17.8.34525.116 +MinimumVisualStudioVersion = 10.0.40219.1 +Project("{FAE04EC0-301F-11D3-BF4B-00C04F79EFBC}") = "HomeWork23", "HomeWork23\HomeWork23.csproj", "{ABED2E1C-4F88-4125-AC6E-1A7F90D56C24}" +EndProject +Global + GlobalSection(SolutionConfigurationPlatforms) = preSolution + Debug|Any CPU = Debug|Any CPU + Release|Any CPU = Release|Any CPU + EndGlobalSection + GlobalSection(ProjectConfigurationPlatforms) = postSolution + {ABED2E1C-4F88-4125-AC6E-1A7F90D56C24}.Debug|Any CPU.ActiveCfg = Debug|Any CPU + {ABED2E1C-4F88-4125-AC6E-1A7F90D56C24}.Debug|Any CPU.Build.0 = Debug|Any CPU + {ABED2E1C-4F88-4125-AC6E-1A7F90D56C24}.Release|Any CPU.ActiveCfg = Release|Any CPU + {ABED2E1C-4F88-4125-AC6E-1A7F90D56C24}.Release|Any CPU.Build.0 = Release|Any CPU + EndGlobalSection + GlobalSection(SolutionProperties) = preSolution + HideSolutionNode = FALSE + EndGlobalSection + GlobalSection(ExtensibilityGlobals) = postSolution + SolutionGuid = {828E821C-2FAF-45D5-BBDE-31034FD737C0} + EndGlobalSection +EndGlobal diff --git a/HomeWork23/HomeWork23/App.cs b/HomeWork23/HomeWork23/App.cs new file mode 100644 index 0000000..3d2c6af --- /dev/null +++ b/HomeWork23/HomeWork23/App.cs @@ -0,0 +1,185 @@ +using HomeWork23.Dto.Requests; +using HomeWork23.Models; +using HomeWork23.Services.Abstracts; + +namespace HomeWork23 +{ + internal class App + { + private readonly IPetService _petService; + private readonly ILocationService _locationService; + private readonly ICategoryService _categoryService; + private readonly IBreedService _breedService; + + public App( + IPetService petService, + ICategoryService categoryService, + IBreedService breedService, + ILocationService locationService) + { + _petService = petService; + _categoryService = categoryService; + _breedService = breedService; + _locationService = locationService; + } + public async Task Start() + { + var locationId = await _locationService.AddLocationAsync("Ukraine"); + var location = await _locationService.GetLocationAsync(locationId); + + var catId = await _categoryService.AddCategoryAsync("Cat"); + var dogId = await _categoryService.AddCategoryAsync("Dog"); + var pigId = await _categoryService.AddCategoryAsync("Pig"); + + var cat = await _categoryService.GetCategoryAsync(catId); + var dog = await _categoryService.GetCategoryAsync(dogId); + var pig = await _categoryService.GetCategoryAsync(pigId); + + var pichId = await _breedService.AddBreedAsync(new Breed() + { + Name = "Pich", + Category = cat, + }); + + var pyshokId = await _breedService.AddBreedAsync(new Breed() + { + Name = "Pyshok", + Category = dog + }); + + var potId = await _breedService.AddBreedAsync(new Breed() + { + Name = "Pot", + Category = cat, + }); + + var dotId = await _breedService.AddBreedAsync(new Breed() + { + Name = "Dot", + Category = cat, + }); + + var vedId = await _breedService.AddBreedAsync(new Breed() + { + Name = "Ved", + Category = dog, + }); + + var pigiId = await _breedService.AddBreedAsync(new Breed() + { + Name = "Pigi", + Category = pig, + }); + + var pich = await _breedService.GetBreedAsync(pichId); + var pyshok = await _breedService.GetBreedAsync(pyshokId); + var pot = await _breedService.GetBreedAsync(potId); + var dot = await _breedService.GetBreedAsync(dotId); + var ved = await _breedService.GetBreedAsync(vedId); + var pigi = await _breedService.GetBreedAsync(pigiId); + + var petIdPi = await _petService.AddPetAsync(new Pet() + { + Name = "Pi", + Category = cat, + CategoryId = cat.Id, + Location = location, + LocationId = location.Id, + Breed = pich, + BreedId = pich.Id, + Age = 9 + }); + + var pyshokIdPy = await _petService.AddPetAsync(new Pet() + { + Name = "Py", + Category = dog, + CategoryId = dog.Id, + Location = location, + LocationId = location.Id, + Breed = pyshok, + BreedId = pyshok.Id, + Age = 5 + }); + + var potIdPo = await _petService.AddPetAsync(new Pet() + { + Name = "Po", + Category = cat, + CategoryId = cat.Id, + Location = location, + LocationId = location.Id, + Breed = pot, + BreedId = pot.Id, + Age = 6 + }); + + var dotIdDo = await _petService.AddPetAsync(new Pet() + { + Name = "Do", + Category = cat, + CategoryId = cat.Id, + Location = location, + LocationId = location.Id, + Breed = dot, + BreedId = dot.Id, + Age = 4 + }) ; + + var vedIdVe = await _petService.AddPetAsync(new Pet() + { + Name = "Ve", + Category = dog, + CategoryId = dog.Id, + Location = location, + LocationId = location.Id, + Breed = ved, + BreedId = ved.Id, + Age = 4 + }); + + var PigiIdpi = await _petService.AddPetAsync(new Pet() + { + Name = "Pig", + Category = pig, + CategoryId = pig.Id, + Location = location, + LocationId = location.Id, + Breed = pigi, + BreedId = pigi.Id, + Age = 7 + }); + + var petPi = await _petService.GetPetAsync(petIdPi); + var pyshokPy = await _petService.GetPetAsync(pyshokIdPy); + + var petRequest = new PetRequest() + { + Age = 3, + LocationCondition = "Ukraine" + }; + + var set = await _petService.GetPetPageAsync(petRequest); + + foreach (var item in set.FiltrData) + { + Console.WriteLine($"{item.Key} {item.Value}"); + } + + var errorId = await _petService.GetPetAsync(12131); + + var delete = await _petService.DeletePetAsync(petIdPi); + + Console.WriteLine(delete); + + pigi.Name = "Pigi2"; + pigi.Category = dog; + pigi.CategoryId = dog.Id; + + var upDateBreed = await _breedService.UpdateBreedAsync(pigi); + + Console.WriteLine(upDateBreed.Name); + Console.WriteLine(upDateBreed.Category.Name); + } + } +} diff --git a/HomeWork23/HomeWork23/Datas/ApplicatDbContext.cs b/HomeWork23/HomeWork23/Datas/ApplicatDbContext.cs new file mode 100644 index 0000000..14f3c7c --- /dev/null +++ b/HomeWork23/HomeWork23/Datas/ApplicatDbContext.cs @@ -0,0 +1,26 @@ +using HomeWork23.Datas.Entities; +using HomeWork23.Datas.EntityConfigure; +using Microsoft.EntityFrameworkCore; + +namespace HomeWork23.Datas +{ + internal class ApplicatDbContext : DbContext + { + public ApplicatDbContext(DbContextOptions option) : base(option) + {} + + public DbSet Breeds { get; set; } = null!; + public DbSet Categories { get; set; } = null!; + public DbSet Locations { get; set; } = null!; + public DbSet Pets { get; set; } = null!; + + protected override void OnModelCreating(ModelBuilder modelBuilder) + { + modelBuilder.ApplyConfiguration(new PetEntityConfiguration()); + modelBuilder.ApplyConfiguration(new CategoryEntityConfiguration()); + modelBuilder.ApplyConfiguration(new LocationEntityConfiguration()); + modelBuilder.ApplyConfiguration(new BreedEntityConffiguration()); + modelBuilder.UseHiLo(); + } + } +} diff --git a/HomeWork23/HomeWork23/Datas/ApplicatDbContextFactory.cs b/HomeWork23/HomeWork23/Datas/ApplicatDbContextFactory.cs new file mode 100644 index 0000000..1f63e4e --- /dev/null +++ b/HomeWork23/HomeWork23/Datas/ApplicatDbContextFactory.cs @@ -0,0 +1,29 @@ +using Microsoft.EntityFrameworkCore; +using Microsoft.EntityFrameworkCore.Design; +using Microsoft.Extensions.Configuration; + +namespace HomeWork23.Datas +{ + internal class ApplicatDbContextFactory : IDesignTimeDbContextFactory + { + public ApplicatDbContext CreateDbContext(string[] args) + { + var optionsBuilder = new DbContextOptionsBuilder(); + + var builder = new ConfigurationBuilder(); + builder.SetBasePath(Directory.GetCurrentDirectory()); + var config = builder.AddJsonFile("config.json") + .Build(); + + var connectionString = config.GetConnectionString("DefaultConnection"); + optionsBuilder.UseSqlServer(connectionString, option => + option + .CommandTimeout( + (int)TimeSpan + .FromMinutes(10) + .TotalSeconds)); + + return new ApplicatDbContext(optionsBuilder.Options); + } + } +} diff --git a/HomeWork23/HomeWork23/Datas/Entities/BreedEntity.cs b/HomeWork23/HomeWork23/Datas/Entities/BreedEntity.cs new file mode 100644 index 0000000..7d8e39b --- /dev/null +++ b/HomeWork23/HomeWork23/Datas/Entities/BreedEntity.cs @@ -0,0 +1,11 @@ +namespace HomeWork23.Datas.Entities +{ + internal class BreedEntity + { + public int Id { get; set; } + public string Name { get; set; } = null!; + public int CategoryId { get; set; } + public CategoryEntity? Category { get; set; } + public ICollection Pets { get; set; } = new List(); + } +} diff --git a/HomeWork23/HomeWork23/Datas/Entities/CategoryEntity.cs b/HomeWork23/HomeWork23/Datas/Entities/CategoryEntity.cs new file mode 100644 index 0000000..9c60958 --- /dev/null +++ b/HomeWork23/HomeWork23/Datas/Entities/CategoryEntity.cs @@ -0,0 +1,10 @@ +namespace HomeWork23.Datas.Entities +{ + internal class CategoryEntity + { + public int Id { get; set; } + public string Name { get; set; } = null!; + public ICollection Pets { get; set; } = new List(); + public ICollection Breeds { get; set; } = new List(); + } +} diff --git a/HomeWork23/HomeWork23/Datas/Entities/LocationEntity.cs b/HomeWork23/HomeWork23/Datas/Entities/LocationEntity.cs new file mode 100644 index 0000000..9962ee7 --- /dev/null +++ b/HomeWork23/HomeWork23/Datas/Entities/LocationEntity.cs @@ -0,0 +1,9 @@ +namespace HomeWork23.Datas.Entities +{ + internal class LocationEntity + { + public int Id { get; set; } + public string Name { get; set; } = null!; + public ICollection Pets { get; set; } = new List(); + } +} diff --git a/HomeWork23/HomeWork23/Datas/Entities/PetEntity.cs b/HomeWork23/HomeWork23/Datas/Entities/PetEntity.cs new file mode 100644 index 0000000..536d195 --- /dev/null +++ b/HomeWork23/HomeWork23/Datas/Entities/PetEntity.cs @@ -0,0 +1,18 @@ +namespace HomeWork23.Datas.Entities +{ + internal class PetEntity + { + public int Id { get; set; } + public string? Name { get; set; } + public int CategoryId { get; set; } + public CategoryEntity? Category { get; set; } + public int BreedId { get; set; } + public BreedEntity? Breed { get; set; } + public int Age { get; set; } = -1; + public int LocationId { get; set; } + public LocationEntity? Location { get; set; } + public string? ImageUrl { get; set; } + public string? Description { get; set; } + + } +} diff --git a/HomeWork23/HomeWork23/Datas/EntityConfigure/BreedEntityConffiguration.cs b/HomeWork23/HomeWork23/Datas/EntityConfigure/BreedEntityConffiguration.cs new file mode 100644 index 0000000..d621c65 --- /dev/null +++ b/HomeWork23/HomeWork23/Datas/EntityConfigure/BreedEntityConffiguration.cs @@ -0,0 +1,28 @@ +using HomeWork23.Datas.Entities; +using Microsoft.EntityFrameworkCore; +using Microsoft.EntityFrameworkCore.Metadata.Builders; + +namespace HomeWork23.Datas.EntityConfigure +{ + internal class BreedEntityConffiguration : IEntityTypeConfiguration + { + public void Configure(EntityTypeBuilder builder) + { + builder + .HasKey(x => x.Id); + + + builder + .Property(x => x.Name) + .IsRequired() + .HasColumnName("breed_name"); + + builder.Property(x => x.CategoryId).HasColumnName("category_id"); + + builder.HasOne(x => x.Category) + .WithMany(x => x.Breeds) + .HasForeignKey(x => x.CategoryId) + .OnDelete(DeleteBehavior.Cascade); + } + } +} diff --git a/HomeWork23/HomeWork23/Datas/EntityConfigure/CategoryEntityConfiguration.cs b/HomeWork23/HomeWork23/Datas/EntityConfigure/CategoryEntityConfiguration.cs new file mode 100644 index 0000000..799f1e7 --- /dev/null +++ b/HomeWork23/HomeWork23/Datas/EntityConfigure/CategoryEntityConfiguration.cs @@ -0,0 +1,21 @@ +using HomeWork23.Datas.Entities; +using Microsoft.EntityFrameworkCore; +using Microsoft.EntityFrameworkCore.Metadata.Builders; + +namespace HomeWork23.Datas.EntityConfigure +{ + internal class CategoryEntityConfiguration : IEntityTypeConfiguration + { + public void Configure(EntityTypeBuilder builder) + { + builder + .HasKey(x => x.Id); + + + builder + .Property(x => x.Name) + .IsRequired() + .HasColumnName("category_name"); + } + } +} diff --git a/HomeWork23/HomeWork23/Datas/EntityConfigure/LocationEntityConfiguration.cs b/HomeWork23/HomeWork23/Datas/EntityConfigure/LocationEntityConfiguration.cs new file mode 100644 index 0000000..22811e7 --- /dev/null +++ b/HomeWork23/HomeWork23/Datas/EntityConfigure/LocationEntityConfiguration.cs @@ -0,0 +1,21 @@ +using HomeWork23.Datas.Entities; +using Microsoft.EntityFrameworkCore; +using Microsoft.EntityFrameworkCore.Metadata.Builders; + +namespace HomeWork23.Datas.EntityConfigure +{ + internal class LocationEntityConfiguration : IEntityTypeConfiguration + { + public void Configure(EntityTypeBuilder builder) + { + builder + .HasKey(x => x.Id); + + + builder + .Property(x => x.Name) + .IsRequired() + .HasColumnName("location_name"); + } + } +} diff --git a/HomeWork23/HomeWork23/Datas/EntityConfigure/PetEntityConfiguration.cs b/HomeWork23/HomeWork23/Datas/EntityConfigure/PetEntityConfiguration.cs new file mode 100644 index 0000000..386c6d1 --- /dev/null +++ b/HomeWork23/HomeWork23/Datas/EntityConfigure/PetEntityConfiguration.cs @@ -0,0 +1,53 @@ +using HomeWork23.Datas.Entities; +using Microsoft.EntityFrameworkCore; +using Microsoft.EntityFrameworkCore.Metadata.Builders; + +namespace HomeWork23.Datas.EntityConfigure +{ + internal class PetEntityConfiguration : IEntityTypeConfiguration + { + public void Configure(EntityTypeBuilder builder) + { + builder.HasKey(x => x.Id); + + builder + .Property(x => x.Name) + .IsRequired() + .HasColumnName("name"); + + builder + .Property(x => x.Age) + .IsRequired() + .HasColumnName("age"); + + builder + .Property(x => x.Description) + .HasColumnName("discription"); + + builder + .Property(x => x.ImageUrl) + .HasColumnName("image_url"); + + builder.Property(x => x.BreedId).HasColumnName("breed_id"); + builder + .HasOne(x=>x.Breed) + .WithMany(x=>x.Pets) + .HasForeignKey(x=>x.BreedId) + .OnDelete(DeleteBehavior.NoAction); + + builder.Property(x => x.CategoryId).HasColumnName("category_id"); + builder + .HasOne(x => x.Category) + .WithMany(x => x.Pets) + .HasForeignKey(x => x.CategoryId) + .OnDelete(DeleteBehavior.Cascade); + + builder.Property(x => x.LocationId).HasColumnName("location_id"); + builder + .HasOne(x => x.Location) + .WithMany(x => x.Pets) + .HasForeignKey(x => x.LocationId) + .OnDelete(DeleteBehavior.Cascade); + } + } +} diff --git a/HomeWork23/HomeWork23/Dto/Requests/PetRequest.cs b/HomeWork23/HomeWork23/Dto/Requests/PetRequest.cs new file mode 100644 index 0000000..6303fc0 --- /dev/null +++ b/HomeWork23/HomeWork23/Dto/Requests/PetRequest.cs @@ -0,0 +1,8 @@ +namespace HomeWork23.Dto.Requests +{ + internal class PetRequest + { + public int Age { get; set; } + public string? LocationCondition { get; set; } + } +} diff --git a/HomeWork23/HomeWork23/Dto/Responses.cs b/HomeWork23/HomeWork23/Dto/Responses.cs new file mode 100644 index 0000000..005d821 --- /dev/null +++ b/HomeWork23/HomeWork23/Dto/Responses.cs @@ -0,0 +1,7 @@ +namespace HomeWork23.Dto +{ + internal class Responses + { + public Dictionary FiltrData { get; set; } + } +} diff --git a/HomeWork23/HomeWork23/HomeWork23.csproj b/HomeWork23/HomeWork23/HomeWork23.csproj new file mode 100644 index 0000000..28adc04 --- /dev/null +++ b/HomeWork23/HomeWork23/HomeWork23.csproj @@ -0,0 +1,36 @@ + + + + Exe + net8.0 + enable + enable + + + + + + all + runtime; build; native; contentfiles; analyzers; buildtransitive + + + + + + + + + + + + + + Always + + + + + + + + diff --git a/HomeWork23/HomeWork23/Migrations/20240403173228_CreateMigration.Designer.cs b/HomeWork23/HomeWork23/Migrations/20240403173228_CreateMigration.Designer.cs new file mode 100644 index 0000000..35663ee --- /dev/null +++ b/HomeWork23/HomeWork23/Migrations/20240403173228_CreateMigration.Designer.cs @@ -0,0 +1,195 @@ +// +using HomeWork23.Datas; +using Microsoft.EntityFrameworkCore; +using Microsoft.EntityFrameworkCore.Infrastructure; +using Microsoft.EntityFrameworkCore.Metadata; +using Microsoft.EntityFrameworkCore.Migrations; +using Microsoft.EntityFrameworkCore.Storage.ValueConversion; + +#nullable disable + +namespace HomeWork23.Migrations +{ + [DbContext(typeof(ApplicatDbContext))] + [Migration("20240403173228_CreateMigration")] + partial class CreateMigration + { + /// + protected override void BuildTargetModel(ModelBuilder modelBuilder) + { +#pragma warning disable 612, 618 + modelBuilder + .HasAnnotation("ProductVersion", "8.0.3") + .HasAnnotation("Relational:MaxIdentifierLength", 128); + + SqlServerModelBuilderExtensions.UseHiLo(modelBuilder, "EntityFrameworkHiLoSequence"); + + modelBuilder.HasSequence("EntityFrameworkHiLoSequence") + .IncrementsBy(10); + + modelBuilder.Entity("HomeWork23.Datas.Entities.BreedEntity", b => + { + b.Property("Id") + .ValueGeneratedOnAdd() + .HasColumnType("int"); + + SqlServerPropertyBuilderExtensions.UseHiLo(b.Property("Id")); + + b.Property("CategoryId") + .HasColumnType("int") + .HasColumnName("category_id"); + + b.Property("Name") + .IsRequired() + .HasColumnType("nvarchar(max)") + .HasColumnName("breed_name"); + + b.HasKey("Id"); + + b.HasIndex("CategoryId"); + + b.ToTable("Breeds"); + }); + + modelBuilder.Entity("HomeWork23.Datas.Entities.CategoryEntity", b => + { + b.Property("Id") + .ValueGeneratedOnAdd() + .HasColumnType("int"); + + SqlServerPropertyBuilderExtensions.UseHiLo(b.Property("Id")); + + b.Property("Name") + .IsRequired() + .HasColumnType("nvarchar(max)") + .HasColumnName("category_name"); + + b.HasKey("Id"); + + b.ToTable("Categories"); + }); + + modelBuilder.Entity("HomeWork23.Datas.Entities.LocationEntity", b => + { + b.Property("Id") + .ValueGeneratedOnAdd() + .HasColumnType("int"); + + SqlServerPropertyBuilderExtensions.UseHiLo(b.Property("Id")); + + b.Property("Name") + .IsRequired() + .HasColumnType("nvarchar(max)") + .HasColumnName("location_name"); + + b.HasKey("Id"); + + b.ToTable("Locations"); + }); + + modelBuilder.Entity("HomeWork23.Datas.Entities.PetEntity", b => + { + b.Property("Id") + .ValueGeneratedOnAdd() + .HasColumnType("int"); + + SqlServerPropertyBuilderExtensions.UseHiLo(b.Property("Id")); + + b.Property("Age") + .HasColumnType("int") + .HasColumnName("age"); + + b.Property("BreedId") + .HasColumnType("int") + .HasColumnName("breed_id"); + + b.Property("CategoryId") + .HasColumnType("int") + .HasColumnName("category_id"); + + b.Property("Description") + .HasColumnType("nvarchar(max)") + .HasColumnName("discription"); + + b.Property("ImageUrl") + .HasColumnType("nvarchar(max)") + .HasColumnName("image_url"); + + b.Property("LocationId") + .HasColumnType("int") + .HasColumnName("location_id"); + + b.Property("Name") + .IsRequired() + .HasColumnType("nvarchar(max)") + .HasColumnName("name"); + + b.HasKey("Id"); + + b.HasIndex("BreedId"); + + b.HasIndex("CategoryId"); + + b.HasIndex("LocationId"); + + b.ToTable("Pets"); + }); + + modelBuilder.Entity("HomeWork23.Datas.Entities.BreedEntity", b => + { + b.HasOne("HomeWork23.Datas.Entities.CategoryEntity", "Category") + .WithMany("Breeds") + .HasForeignKey("CategoryId") + .OnDelete(DeleteBehavior.Cascade) + .IsRequired(); + + b.Navigation("Category"); + }); + + modelBuilder.Entity("HomeWork23.Datas.Entities.PetEntity", b => + { + b.HasOne("HomeWork23.Datas.Entities.BreedEntity", "Breed") + .WithMany("Pets") + .HasForeignKey("BreedId") + .OnDelete(DeleteBehavior.NoAction) + .IsRequired(); + + b.HasOne("HomeWork23.Datas.Entities.CategoryEntity", "Category") + .WithMany("Pets") + .HasForeignKey("CategoryId") + .OnDelete(DeleteBehavior.Cascade) + .IsRequired(); + + b.HasOne("HomeWork23.Datas.Entities.LocationEntity", "Location") + .WithMany("Pets") + .HasForeignKey("LocationId") + .OnDelete(DeleteBehavior.Cascade) + .IsRequired(); + + b.Navigation("Breed"); + + b.Navigation("Category"); + + b.Navigation("Location"); + }); + + modelBuilder.Entity("HomeWork23.Datas.Entities.BreedEntity", b => + { + b.Navigation("Pets"); + }); + + modelBuilder.Entity("HomeWork23.Datas.Entities.CategoryEntity", b => + { + b.Navigation("Breeds"); + + b.Navigation("Pets"); + }); + + modelBuilder.Entity("HomeWork23.Datas.Entities.LocationEntity", b => + { + b.Navigation("Pets"); + }); +#pragma warning restore 612, 618 + } + } +} diff --git a/HomeWork23/HomeWork23/Migrations/20240403173228_CreateMigration.cs b/HomeWork23/HomeWork23/Migrations/20240403173228_CreateMigration.cs new file mode 100644 index 0000000..86290a5 --- /dev/null +++ b/HomeWork23/HomeWork23/Migrations/20240403173228_CreateMigration.cs @@ -0,0 +1,135 @@ +using Microsoft.EntityFrameworkCore.Migrations; + +#nullable disable + +namespace HomeWork23.Migrations +{ + /// + public partial class CreateMigration : Migration + { + /// + protected override void Up(MigrationBuilder migrationBuilder) + { + migrationBuilder.CreateSequence( + name: "EntityFrameworkHiLoSequence", + incrementBy: 10); + + migrationBuilder.CreateTable( + name: "Categories", + columns: table => new + { + Id = table.Column(type: "int", nullable: false), + category_name = table.Column(type: "nvarchar(max)", nullable: false) + }, + constraints: table => + { + table.PrimaryKey("PK_Categories", x => x.Id); + }); + + migrationBuilder.CreateTable( + name: "Locations", + columns: table => new + { + Id = table.Column(type: "int", nullable: false), + location_name = table.Column(type: "nvarchar(max)", nullable: false) + }, + constraints: table => + { + table.PrimaryKey("PK_Locations", x => x.Id); + }); + + migrationBuilder.CreateTable( + name: "Breeds", + columns: table => new + { + Id = table.Column(type: "int", nullable: false), + breed_name = table.Column(type: "nvarchar(max)", nullable: false), + category_id = table.Column(type: "int", nullable: false) + }, + constraints: table => + { + table.PrimaryKey("PK_Breeds", x => x.Id); + table.ForeignKey( + name: "FK_Breeds_Categories_category_id", + column: x => x.category_id, + principalTable: "Categories", + principalColumn: "Id", + onDelete: ReferentialAction.Cascade); + }); + + migrationBuilder.CreateTable( + name: "Pets", + columns: table => new + { + Id = table.Column(type: "int", nullable: false), + name = table.Column(type: "nvarchar(max)", nullable: false), + category_id = table.Column(type: "int", nullable: false), + breed_id = table.Column(type: "int", nullable: false), + age = table.Column(type: "int", nullable: false), + location_id = table.Column(type: "int", nullable: false), + image_url = table.Column(type: "nvarchar(max)", nullable: true), + discription = table.Column(type: "nvarchar(max)", nullable: true) + }, + constraints: table => + { + table.PrimaryKey("PK_Pets", x => x.Id); + table.ForeignKey( + name: "FK_Pets_Breeds_breed_id", + column: x => x.breed_id, + principalTable: "Breeds", + principalColumn: "Id"); + table.ForeignKey( + name: "FK_Pets_Categories_category_id", + column: x => x.category_id, + principalTable: "Categories", + principalColumn: "Id", + onDelete: ReferentialAction.Cascade); + table.ForeignKey( + name: "FK_Pets_Locations_location_id", + column: x => x.location_id, + principalTable: "Locations", + principalColumn: "Id", + onDelete: ReferentialAction.Cascade); + }); + + migrationBuilder.CreateIndex( + name: "IX_Breeds_category_id", + table: "Breeds", + column: "category_id"); + + migrationBuilder.CreateIndex( + name: "IX_Pets_breed_id", + table: "Pets", + column: "breed_id"); + + migrationBuilder.CreateIndex( + name: "IX_Pets_category_id", + table: "Pets", + column: "category_id"); + + migrationBuilder.CreateIndex( + name: "IX_Pets_location_id", + table: "Pets", + column: "location_id"); + } + + /// + protected override void Down(MigrationBuilder migrationBuilder) + { + migrationBuilder.DropTable( + name: "Pets"); + + migrationBuilder.DropTable( + name: "Breeds"); + + migrationBuilder.DropTable( + name: "Locations"); + + migrationBuilder.DropTable( + name: "Categories"); + + migrationBuilder.DropSequence( + name: "EntityFrameworkHiLoSequence"); + } + } +} diff --git a/HomeWork23/HomeWork23/Migrations/ApplicatDbContextModelSnapshot.cs b/HomeWork23/HomeWork23/Migrations/ApplicatDbContextModelSnapshot.cs new file mode 100644 index 0000000..9afca26 --- /dev/null +++ b/HomeWork23/HomeWork23/Migrations/ApplicatDbContextModelSnapshot.cs @@ -0,0 +1,192 @@ +// +using HomeWork23.Datas; +using Microsoft.EntityFrameworkCore; +using Microsoft.EntityFrameworkCore.Infrastructure; +using Microsoft.EntityFrameworkCore.Metadata; +using Microsoft.EntityFrameworkCore.Storage.ValueConversion; + +#nullable disable + +namespace HomeWork23.Migrations +{ + [DbContext(typeof(ApplicatDbContext))] + partial class ApplicatDbContextModelSnapshot : ModelSnapshot + { + protected override void BuildModel(ModelBuilder modelBuilder) + { +#pragma warning disable 612, 618 + modelBuilder + .HasAnnotation("ProductVersion", "8.0.3") + .HasAnnotation("Relational:MaxIdentifierLength", 128); + + SqlServerModelBuilderExtensions.UseHiLo(modelBuilder, "EntityFrameworkHiLoSequence"); + + modelBuilder.HasSequence("EntityFrameworkHiLoSequence") + .IncrementsBy(10); + + modelBuilder.Entity("HomeWork23.Datas.Entities.BreedEntity", b => + { + b.Property("Id") + .ValueGeneratedOnAdd() + .HasColumnType("int"); + + SqlServerPropertyBuilderExtensions.UseHiLo(b.Property("Id")); + + b.Property("CategoryId") + .HasColumnType("int") + .HasColumnName("category_id"); + + b.Property("Name") + .IsRequired() + .HasColumnType("nvarchar(max)") + .HasColumnName("breed_name"); + + b.HasKey("Id"); + + b.HasIndex("CategoryId"); + + b.ToTable("Breeds"); + }); + + modelBuilder.Entity("HomeWork23.Datas.Entities.CategoryEntity", b => + { + b.Property("Id") + .ValueGeneratedOnAdd() + .HasColumnType("int"); + + SqlServerPropertyBuilderExtensions.UseHiLo(b.Property("Id")); + + b.Property("Name") + .IsRequired() + .HasColumnType("nvarchar(max)") + .HasColumnName("category_name"); + + b.HasKey("Id"); + + b.ToTable("Categories"); + }); + + modelBuilder.Entity("HomeWork23.Datas.Entities.LocationEntity", b => + { + b.Property("Id") + .ValueGeneratedOnAdd() + .HasColumnType("int"); + + SqlServerPropertyBuilderExtensions.UseHiLo(b.Property("Id")); + + b.Property("Name") + .IsRequired() + .HasColumnType("nvarchar(max)") + .HasColumnName("location_name"); + + b.HasKey("Id"); + + b.ToTable("Locations"); + }); + + modelBuilder.Entity("HomeWork23.Datas.Entities.PetEntity", b => + { + b.Property("Id") + .ValueGeneratedOnAdd() + .HasColumnType("int"); + + SqlServerPropertyBuilderExtensions.UseHiLo(b.Property("Id")); + + b.Property("Age") + .HasColumnType("int") + .HasColumnName("age"); + + b.Property("BreedId") + .HasColumnType("int") + .HasColumnName("breed_id"); + + b.Property("CategoryId") + .HasColumnType("int") + .HasColumnName("category_id"); + + b.Property("Description") + .HasColumnType("nvarchar(max)") + .HasColumnName("discription"); + + b.Property("ImageUrl") + .HasColumnType("nvarchar(max)") + .HasColumnName("image_url"); + + b.Property("LocationId") + .HasColumnType("int") + .HasColumnName("location_id"); + + b.Property("Name") + .IsRequired() + .HasColumnType("nvarchar(max)") + .HasColumnName("name"); + + b.HasKey("Id"); + + b.HasIndex("BreedId"); + + b.HasIndex("CategoryId"); + + b.HasIndex("LocationId"); + + b.ToTable("Pets"); + }); + + modelBuilder.Entity("HomeWork23.Datas.Entities.BreedEntity", b => + { + b.HasOne("HomeWork23.Datas.Entities.CategoryEntity", "Category") + .WithMany("Breeds") + .HasForeignKey("CategoryId") + .OnDelete(DeleteBehavior.Cascade) + .IsRequired(); + + b.Navigation("Category"); + }); + + modelBuilder.Entity("HomeWork23.Datas.Entities.PetEntity", b => + { + b.HasOne("HomeWork23.Datas.Entities.BreedEntity", "Breed") + .WithMany("Pets") + .HasForeignKey("BreedId") + .OnDelete(DeleteBehavior.NoAction) + .IsRequired(); + + b.HasOne("HomeWork23.Datas.Entities.CategoryEntity", "Category") + .WithMany("Pets") + .HasForeignKey("CategoryId") + .OnDelete(DeleteBehavior.Cascade) + .IsRequired(); + + b.HasOne("HomeWork23.Datas.Entities.LocationEntity", "Location") + .WithMany("Pets") + .HasForeignKey("LocationId") + .OnDelete(DeleteBehavior.Cascade) + .IsRequired(); + + b.Navigation("Breed"); + + b.Navigation("Category"); + + b.Navigation("Location"); + }); + + modelBuilder.Entity("HomeWork23.Datas.Entities.BreedEntity", b => + { + b.Navigation("Pets"); + }); + + modelBuilder.Entity("HomeWork23.Datas.Entities.CategoryEntity", b => + { + b.Navigation("Breeds"); + + b.Navigation("Pets"); + }); + + modelBuilder.Entity("HomeWork23.Datas.Entities.LocationEntity", b => + { + b.Navigation("Pets"); + }); +#pragma warning restore 612, 618 + } + } +} diff --git a/HomeWork23/HomeWork23/Models/Breed.cs b/HomeWork23/HomeWork23/Models/Breed.cs new file mode 100644 index 0000000..d8aec22 --- /dev/null +++ b/HomeWork23/HomeWork23/Models/Breed.cs @@ -0,0 +1,10 @@ +namespace HomeWork23.Models +{ + internal class Breed + { + public int Id { get; set; } + public string? Name { get; set; } + public int CategoryId { get; set; } + public Category? Category { get; set; } + } +} diff --git a/HomeWork23/HomeWork23/Models/Category.cs b/HomeWork23/HomeWork23/Models/Category.cs new file mode 100644 index 0000000..2fe57b1 --- /dev/null +++ b/HomeWork23/HomeWork23/Models/Category.cs @@ -0,0 +1,8 @@ +namespace HomeWork23.Models +{ + internal class Category + { + public int Id { get; set; } + public string? Name { get; set; } + } +} diff --git a/HomeWork23/HomeWork23/Models/Location.cs b/HomeWork23/HomeWork23/Models/Location.cs new file mode 100644 index 0000000..5dc8391 --- /dev/null +++ b/HomeWork23/HomeWork23/Models/Location.cs @@ -0,0 +1,8 @@ +namespace HomeWork23.Models +{ + internal class Location + { + public int Id { get; set; } + public string Name { get; set; } + } +} diff --git a/HomeWork23/HomeWork23/Models/Pet.cs b/HomeWork23/HomeWork23/Models/Pet.cs new file mode 100644 index 0000000..08e4924 --- /dev/null +++ b/HomeWork23/HomeWork23/Models/Pet.cs @@ -0,0 +1,17 @@ +namespace HomeWork23.Models +{ + internal class Pet + { + public int Id { get; set; } + public string? Name { get; set; } + public int CategoryId { get; set; } + public Category? Category { get; set; } + public int BreedId { get; set; } + public Breed? Breed { get; set; } + public int Age { get; set; } + public int LocationId { get; set; } + public Location? Location { get; set; } + public string? ImageUrl { get; set; } + public string? Description { get; set; } + } +} diff --git a/HomeWork23/HomeWork23/Program.cs b/HomeWork23/HomeWork23/Program.cs new file mode 100644 index 0000000..e8b1dde --- /dev/null +++ b/HomeWork23/HomeWork23/Program.cs @@ -0,0 +1,60 @@ +using HomeWork23.Datas; +using HomeWork23.Repositories; +using HomeWork23.Repositories.Abstracts; +using HomeWork23.Services; +using HomeWork23.Services.Abstracts; +using HomeWork23.Wrapper; +using HomeWork23.Wrapper.Abstracts; +using Microsoft.EntityFrameworkCore; +using Microsoft.Extensions.Configuration; +using Microsoft.Extensions.DependencyInjection; +using Microsoft.Extensions.Logging; + +namespace HomeWork23 +{ + class Program + { + static async Task Main(string[] args) + { + void ConfigureService(ServiceCollection serviceCollection, IConfiguration configuration) + { + var connectionString = configuration.GetConnectionString("DefaultConnection"); + serviceCollection + .AddDbContextFactory(option => + option.UseSqlServer(connectionString)); + + serviceCollection + .AddLogging(loggin => loggin.AddConsole()) + .AddScoped, DbContextWrapper>() + .AddTransient() + .AddTransient() + .AddTransient() + .AddTransient() + .AddTransient() + .AddTransient() + .AddTransient() + .AddTransient() + .AddTransient(); + } + + IConfiguration configur = new ConfigurationBuilder() + .AddJsonFile("config.json") + .Build(); + + var serviceCollection = new ServiceCollection(); + ConfigureService(serviceCollection, configur); + var provaider = serviceCollection.BuildServiceProvider(); + + var isNeedMigration = configur.GetSection("Migration").GetSection("IsNeedMigration"); + + if (bool.Parse(isNeedMigration.Value)) + { + var dbContext = provaider.GetService(); + await dbContext!.Database.MigrateAsync(); + } + var app = provaider.GetService(); + await app!.Start(); + + } + } +} \ No newline at end of file diff --git a/HomeWork23/HomeWork23/Repositories/Abstracts/IBreedRepository.cs b/HomeWork23/HomeWork23/Repositories/Abstracts/IBreedRepository.cs new file mode 100644 index 0000000..656f29e --- /dev/null +++ b/HomeWork23/HomeWork23/Repositories/Abstracts/IBreedRepository.cs @@ -0,0 +1,12 @@ +using HomeWork23.Datas.Entities; + +namespace HomeWork23.Repositories.Abstracts +{ + internal interface IBreedRepository + { + public Task UpdateBreedAsync(BreedEntity breed); + public Task DeleteBreedAsync(int id); + public Task GetBreedAsync(int id); + public Task AddBreedAsync(BreedEntity breedEntity); + } +} diff --git a/HomeWork23/HomeWork23/Repositories/Abstracts/ICategoryRepository.cs b/HomeWork23/HomeWork23/Repositories/Abstracts/ICategoryRepository.cs new file mode 100644 index 0000000..2befccc --- /dev/null +++ b/HomeWork23/HomeWork23/Repositories/Abstracts/ICategoryRepository.cs @@ -0,0 +1,12 @@ +using HomeWork23.Datas.Entities; + +namespace HomeWork23.Repositories.Abstracts +{ + internal interface ICategoryRepository + { + public Task UpdateCategoryAsync(int id, string name); + public Task DeleteCategory(int id); + public Task GetCategoryAsync(int id); + public Task AddCategory(string name); + } +} diff --git a/HomeWork23/HomeWork23/Repositories/Abstracts/ILocationRepository.cs b/HomeWork23/HomeWork23/Repositories/Abstracts/ILocationRepository.cs new file mode 100644 index 0000000..57a9779 --- /dev/null +++ b/HomeWork23/HomeWork23/Repositories/Abstracts/ILocationRepository.cs @@ -0,0 +1,12 @@ +using HomeWork23.Datas.Entities; + +namespace HomeWork23.Repositories.Abstracts +{ + internal interface ILocationRepository + { + public Task AddLocationAsync(string name); + public Task GetLocationAsync(int id); + public Task UpdateLocationAsync(int id, string name); + public Task DeleteLocationAsync(int id); + } +} diff --git a/HomeWork23/HomeWork23/Repositories/Abstracts/IPetRepository.cs b/HomeWork23/HomeWork23/Repositories/Abstracts/IPetRepository.cs new file mode 100644 index 0000000..debd505 --- /dev/null +++ b/HomeWork23/HomeWork23/Repositories/Abstracts/IPetRepository.cs @@ -0,0 +1,15 @@ +using HomeWork23.Datas.Entities; +using HomeWork23.Dto.Requests; +using HomeWork23.Dto; + +namespace HomeWork23.Repositories.Abstracts +{ + internal interface IPetRepository + { + public Task AddPetAsync(PetEntity pet); + public Task GetPetAsync(int id); + public Task DeletePetAsync(int id); + public Task UpdatePetAsync(PetEntity petEntity); + public Task> GetPetPageAsync(PetRequest request); + } +} diff --git a/HomeWork23/HomeWork23/Repositories/BreedRepository.cs b/HomeWork23/HomeWork23/Repositories/BreedRepository.cs new file mode 100644 index 0000000..63e4d55 --- /dev/null +++ b/HomeWork23/HomeWork23/Repositories/BreedRepository.cs @@ -0,0 +1,68 @@ +using HomeWork23.Datas; +using HomeWork23.Datas.Entities; +using HomeWork23.Repositories.Abstracts; +using HomeWork23.Wrapper.Abstracts; +using Microsoft.EntityFrameworkCore; + +namespace HomeWork23.Repositories +{ + internal class BreedRepository : IBreedRepository + { + private readonly ApplicatDbContext _dbContext; + + public BreedRepository(IDbContextWrapper dbContextWrapper) + { + _dbContext = dbContextWrapper.DbContext; + } + + public async Task AddBreedAsync(BreedEntity breedEntity) + { + var breed = await _dbContext.Breeds.AddAsync(breedEntity); + await _dbContext.SaveChangesAsync(); + + return breed.Entity.Id; + } + + public async Task GetBreedAsync(int id) + { + var entity = await _dbContext.Breeds + .Include(x => x.Category) + .FirstOrDefaultAsync(x => x.Id == id); + + return entity is null ? null : entity; + } + + public async Task DeleteBreedAsync(int id) + { + var entity = await GetBreedAsync(id); + + if (entity is null) + { + return null; + } + + var status = _dbContext.Breeds.Remove(entity); + + await _dbContext.SaveChangesAsync(); + return status.State.ToString(); + } + + public async Task UpdateBreedAsync(BreedEntity breed) + { + var entity = await GetBreedAsync(breed.Id); + + if (entity is null) + { + return null; + } + + entity.Name = breed.Name is null ? entity.Name : breed.Name; + entity.Category = breed.Category is null ? entity.Category : breed.Category; + entity.CategoryId = breed.Category is null ? entity.CategoryId : breed.CategoryId; + + await _dbContext.SaveChangesAsync(); + + return entity; + } + } +} diff --git a/HomeWork23/HomeWork23/Repositories/CategoryRepository.cs b/HomeWork23/HomeWork23/Repositories/CategoryRepository.cs new file mode 100644 index 0000000..da37439 --- /dev/null +++ b/HomeWork23/HomeWork23/Repositories/CategoryRepository.cs @@ -0,0 +1,67 @@ +using HomeWork23.Datas; +using HomeWork23.Datas.Entities; +using HomeWork23.Repositories.Abstracts; +using HomeWork23.Wrapper.Abstracts; +using Microsoft.EntityFrameworkCore; + +namespace HomeWork23.Repositories +{ + internal class CategoryRepository : ICategoryRepository + { + private readonly ApplicatDbContext _dbContext; + + public CategoryRepository(IDbContextWrapper dbContextWrapper) + { + _dbContext = dbContextWrapper.DbContext; + } + + public async Task AddCategory(string name) + { + var entity = await _dbContext.Categories.AddAsync(new CategoryEntity() + { + Name = name + }); + + await _dbContext.SaveChangesAsync(); + return entity.Entity.Id; + } + + public async Task GetCategoryAsync(int id) + { + var entity = await _dbContext.Categories + .FirstOrDefaultAsync(x=>x.Id == id); + + return entity is null ? null : entity; + } + + public async Task DeleteCategory(int id) + { + var entity = await GetCategoryAsync(id); + + if (entity is null) + { + return null; + } + + var status = _dbContext.Categories.Remove(entity); + await _dbContext.SaveChangesAsync(); + + return status.State.ToString(); + } + + public async Task UpdateCategoryAsync(int id, string name) + { + var entity = await GetCategoryAsync(id); + + if (entity is null) + { + return null; + } + + entity.Name = name; + + await _dbContext.SaveChangesAsync(); + return entity; + } + } +} diff --git a/HomeWork23/HomeWork23/Repositories/LocationRepositorycs.cs b/HomeWork23/HomeWork23/Repositories/LocationRepositorycs.cs new file mode 100644 index 0000000..c086967 --- /dev/null +++ b/HomeWork23/HomeWork23/Repositories/LocationRepositorycs.cs @@ -0,0 +1,72 @@ +using HomeWork23.Datas; +using HomeWork23.Datas.Entities; +using HomeWork23.Repositories.Abstracts; +using HomeWork23.Wrapper.Abstracts; +using Microsoft.EntityFrameworkCore; + +namespace HomeWork23.Repositories +{ + internal class LocationRepository : ILocationRepository + { + private readonly ApplicatDbContext _dbContext; + + public LocationRepository(IDbContextWrapper dbContextWrapper) + { + _dbContext = dbContextWrapper.DbContext; + } + + public async Task AddLocationAsync(string name) + { + var location = await _dbContext + .Locations + .AddAsync(new LocationEntity() + { + Name = name, + }); + + await _dbContext.SaveChangesAsync(); + + return location.Entity.Id; + } + + public async Task GetLocationAsync(int id) + { + var entity = await _dbContext.Locations + .FirstOrDefaultAsync(x => x.Id == id); + + return entity is null ? null : entity; + } + + public async Task UpdateLocationAsync(int id, string name) + { + var entity = await GetLocationAsync(id); + + if (entity is null) + { + return entity!; + } + + entity.Name = name; + + await _dbContext.SaveChangesAsync(); + + return entity; + } + + public async Task DeleteLocationAsync(int id) + { + var entity = await GetLocationAsync(id); + + if (entity is null) + { + return null; + } + + var status = _dbContext.Locations.Remove(entity!); + + await _dbContext.SaveChangesAsync(); + + return status.State.ToString(); + } + } +} diff --git a/HomeWork23/HomeWork23/Repositories/PetRepository.cs b/HomeWork23/HomeWork23/Repositories/PetRepository.cs new file mode 100644 index 0000000..628f9aa --- /dev/null +++ b/HomeWork23/HomeWork23/Repositories/PetRepository.cs @@ -0,0 +1,103 @@ +using HomeWork23.Datas; +using HomeWork23.Datas.Entities; +using HomeWork23.Dto; +using HomeWork23.Dto.Requests; +using HomeWork23.Repositories.Abstracts; +using HomeWork23.Wrapper.Abstracts; +using Microsoft.EntityFrameworkCore; + +namespace HomeWork23.Repositories +{ + internal class PetRepository : IPetRepository + { + private readonly ApplicatDbContext _dbContext; + + public PetRepository(IDbContextWrapper dbContextWrapper) + { + _dbContext = dbContextWrapper.DbContext; + } + + public async Task AddPetAsync(PetEntity pet) + { + var entity = await _dbContext.Pets.AddAsync(pet); + + await _dbContext.SaveChangesAsync(); + + return entity.Entity.Id; + } + + public async Task GetPetAsync(int id) + { + var entity = await _dbContext.Pets + .Include(x=>x.Breed!.Category) + .Include(x=>x.Category) + .Include (x=>x.Location) + .FirstOrDefaultAsync(x => x.Id == id); + + return entity is null ? null : entity; + } + + public async Task DeletePetAsync(int id) + { + var entity = await GetPetAsync(id); + + if(entity is null) + { + return null!; + } + + var state = _dbContext.Pets.Remove(entity!); + await _dbContext.SaveChangesAsync(); + + return state.State.ToString(); + } + + public async Task UpdatePetAsync(PetEntity petEntity) + { + var entity = await _dbContext.Pets + .Include(x=>x.Breed!.Category) + .Include(x=>x.Category) + .Include (x=>x.Location) + .FirstOrDefaultAsync(x => x.Id == petEntity.Id); + + if (entity is null) + { + return entity!; + } + + entity.Age = petEntity.Age == -1 ? entity.Age : petEntity.Age; + entity.Description = petEntity?.Description is null ? entity.Description : petEntity.Description; + entity.ImageUrl = petEntity?.ImageUrl is null ? entity.ImageUrl : petEntity.ImageUrl; + entity.Name = petEntity?.Name is null ? entity.Name : petEntity.Name; + + entity.Location = petEntity?.Location is null ? entity.Location : petEntity.Location; + entity.LocationId = petEntity?.Location is null ? entity.LocationId : petEntity.LocationId; + + + entity.BreedId = petEntity?.Breed is null ? entity.BreedId : petEntity.BreedId; + entity.Breed = petEntity?.Breed is null ? entity.Breed : petEntity.Breed; + entity.Breed!.Category = petEntity?.Breed?.Category is null ? entity.Breed.Category : petEntity.Breed.Category; + + entity.CategoryId = petEntity?.Category is null ? entity.CategoryId : petEntity.CategoryId; + entity.Category = petEntity?.Category is null ? entity.Category : petEntity.Category; + + await _dbContext.SaveChangesAsync(); + + return entity; + } + + public async Task> GetPetPageAsync(PetRequest request) + { + var respons = new Responses(); + + respons.FiltrData = await _dbContext.Pets + .Where(x => x.Location.Name == request.LocationCondition) + .Where(x => x.Age > request.Age) + .GroupBy(x=> x.Category.Name) + .Select(g=> new {CategoryName = g.Key, BreedCount = g.Select(x => x.Breed.Name).Distinct().Count()}) + .ToDictionaryAsync(x=>x.CategoryName,y=>y.BreedCount); + + return respons; + } + } +} diff --git a/HomeWork23/HomeWork23/Services/Abstracts/IBreedService.cs b/HomeWork23/HomeWork23/Services/Abstracts/IBreedService.cs new file mode 100644 index 0000000..2e4b6cc --- /dev/null +++ b/HomeWork23/HomeWork23/Services/Abstracts/IBreedService.cs @@ -0,0 +1,12 @@ +using HomeWork23.Models; + +namespace HomeWork23.Services.Abstracts +{ + internal interface IBreedService + { + public Task UpdateBreedAsync(Breed breed); + public Task DeleteBreedAsync(int id); + public Task GetBreedAsync(int id); + public Task AddBreedAsync(Breed breed); + } +} diff --git a/HomeWork23/HomeWork23/Services/Abstracts/ICategoryService.cs b/HomeWork23/HomeWork23/Services/Abstracts/ICategoryService.cs new file mode 100644 index 0000000..cef8e8d --- /dev/null +++ b/HomeWork23/HomeWork23/Services/Abstracts/ICategoryService.cs @@ -0,0 +1,12 @@ +using HomeWork23.Models; + +namespace HomeWork23.Services.Abstracts +{ + internal interface ICategoryService + { + public Task AddCategoryAsync(string name); + public Task GetCategoryAsync(int id); + public Task DeleteCategoryAsync(int id); + public Task UpdateCategory(int id, string name); + } +} diff --git a/HomeWork23/HomeWork23/Services/Abstracts/ILocationService.cs b/HomeWork23/HomeWork23/Services/Abstracts/ILocationService.cs new file mode 100644 index 0000000..55b9195 --- /dev/null +++ b/HomeWork23/HomeWork23/Services/Abstracts/ILocationService.cs @@ -0,0 +1,12 @@ +using HomeWork23.Models; + +namespace HomeWork23.Services.Abstracts +{ + internal interface ILocationService + { + public Task AddLocationAsync(string name); + public Task GetLocationAsync(int id); + public Task UpdateLocationAsync(int id, string name); + public Task DeleteLocationAsync(int id); + } +} diff --git a/HomeWork23/HomeWork23/Services/Abstracts/IPetService.cs b/HomeWork23/HomeWork23/Services/Abstracts/IPetService.cs new file mode 100644 index 0000000..389dab2 --- /dev/null +++ b/HomeWork23/HomeWork23/Services/Abstracts/IPetService.cs @@ -0,0 +1,15 @@ +using HomeWork23.Dto.Requests; +using HomeWork23.Dto; +using HomeWork23.Models; + +namespace HomeWork23.Services.Abstracts +{ + internal interface IPetService + { + public Task AddPetAsync(Pet pet); + public Task GetPetAsync(int id); + public Task DeletePetAsync(int id); + public Task UpdateAsync(Pet pet); + public Task> GetPetPageAsync(PetRequest request); + } +} diff --git a/HomeWork23/HomeWork23/Services/BreedService.cs b/HomeWork23/HomeWork23/Services/BreedService.cs new file mode 100644 index 0000000..fc96995 --- /dev/null +++ b/HomeWork23/HomeWork23/Services/BreedService.cs @@ -0,0 +1,114 @@ +using HomeWork23.Datas; +using HomeWork23.Datas.Entities; +using HomeWork23.Models; +using HomeWork23.Repositories.Abstracts; +using HomeWork23.Services.Abstracts; +using HomeWork23.Wrapper.Abstracts; +using Microsoft.Extensions.Logging; + +namespace HomeWork23.Services +{ + internal class BreedService : BaseDataService, IBreedService + { + private readonly ILogger _loggerService; + private readonly IBreedRepository _repository; + public BreedService( + IDbContextWrapper dbContextWrapper, + ILogger logger, + IBreedRepository breedRepository, + ILogger loggerService) + : base(dbContextWrapper, logger) + { + _loggerService = loggerService; + _repository = breedRepository; + } + + public async Task AddBreedAsync(Breed breed) + { + var id = await _repository.AddBreedAsync(new BreedEntity() + { + Name = breed.Name, + Category = new CategoryEntity() + { + Name = breed.Category.Name + } + }); + + _loggerService.LogInformation($"Breed seccusfull create id: {id}"); + return id; + } + + public async Task GetBreedAsync(int id) + { + var entity = await _repository.GetBreedAsync(id); + + if (entity is null) + { + _loggerService.LogWarning("Breed not finded"); + return null!; + } + + _loggerService.LogInformation($"Breed seccusfull get id: {id}"); + + return new Breed() + { + Id = entity.Id, + Name = entity.Name, + CategoryId = entity.CategoryId, + Category = new Category() + { + Id = entity.Category.Id, + Name = entity.Category.Name + } + }; + } + + public async Task DeleteBreedAsync(int id) + { + var message = await _repository.DeleteBreedAsync(id); + if (message is null) + { + _loggerService.LogWarning("Breed not finded"); + return null!; + } + _loggerService.LogInformation($"Breed seccusfull get id: {id}"); + + return message; + } + + public async Task UpdateBreedAsync(Breed breed) + { + var entity = new BreedEntity() + { + Id = breed.Id, + Name = breed.Name, + CategoryId = breed.CategoryId, + Category = new CategoryEntity() + { + Id = breed.Category.Id, + Name = breed.Category.Name + } + }; + + var entityUpdate = await _repository.UpdateBreedAsync(entity); + + if (entity is null) + { + _loggerService.LogWarning("Breed not finded"); + return null!; + } + + return new Breed() + { + Id = entityUpdate.Id, + Name = entityUpdate.Name, + CategoryId = entityUpdate.CategoryId, + Category = new Category() + { + Id = breed.Category.Id, + Name = breed.Category.Name + } + }; + } + } +} diff --git a/HomeWork23/HomeWork23/Services/CategoryService.cs b/HomeWork23/HomeWork23/Services/CategoryService.cs new file mode 100644 index 0000000..9e81c0c --- /dev/null +++ b/HomeWork23/HomeWork23/Services/CategoryService.cs @@ -0,0 +1,88 @@ +using HomeWork23.Datas; +using HomeWork23.Models; +using HomeWork23.Repositories.Abstracts; +using HomeWork23.Services.Abstracts; +using HomeWork23.Wrapper.Abstracts; +using Microsoft.Extensions.Logging; + +namespace HomeWork23.Services +{ + internal class CategoryService : BaseDataService, ICategoryService + { + private readonly ICategoryRepository _repository; + private readonly ILogger _loggerService; + + public CategoryService( + IDbContextWrapper dbContextWrapper, + ILogger loggerService, + ILogger> loggerBase, + ICategoryRepository categoryRepository) + : base(dbContextWrapper, loggerBase) + { + _repository = categoryRepository; + _loggerService = loggerService; + } + + public async Task AddCategoryAsync(string name) + { + var id = await _repository.AddCategory(name); + + _loggerService.LogInformation($"Category seccusfull create wuth id: {id}"); + + return id; + } + + public async Task GetCategoryAsync(int id) + { + var entity = await _repository.GetCategoryAsync(id); + + if (entity is null) + { + _loggerService.LogWarning("Category not finded"); + return null!; + } + + _loggerService.LogInformation($"Category seccusfull get wuth id: {id}"); + + return new Category() + { + Id = entity.Id, + Name = entity.Name + }; + } + + public async Task DeleteCategoryAsync(int id) + { + var message = await _repository.DeleteCategory(id); + + if (message is null) + { + _loggerService.LogWarning("Category not finded"); + return null!; + } + + _loggerService.LogInformation($"Deleted category: {id}"); + + return message; + } + + public async Task UpdateCategory(int id, string name) + { + var entity = await _repository.UpdateCategoryAsync(id, name); + + if (entity is null) + { + _loggerService.LogWarning("Category not finded"); + return null!; + } + + _loggerService.LogInformation($"Update succesfull category: {id}"); + + return new Category() + { + Id = entity.Id, + Name = entity.Name + }; + } + } +} diff --git a/HomeWork23/HomeWork23/Services/LocationService.cs b/HomeWork23/HomeWork23/Services/LocationService.cs new file mode 100644 index 0000000..44da91d --- /dev/null +++ b/HomeWork23/HomeWork23/Services/LocationService.cs @@ -0,0 +1,87 @@ +using HomeWork23.Datas; +using HomeWork23.Models; +using HomeWork23.Repositories.Abstracts; +using HomeWork23.Services.Abstracts; +using HomeWork23.Wrapper.Abstracts; +using Microsoft.Extensions.Logging; + +namespace HomeWork23.Services +{ + internal class LocationService : BaseDataService, ILocationService + { + private readonly ILogger _loggerService; + private readonly ILocationRepository _repository; + public LocationService( + IDbContextWrapper dbContextWrapper, + ILogger logger, + ILogger loggerService, + ILocationRepository locationRepository) + : base(dbContextWrapper, logger) + { + _loggerService = loggerService; + _repository = locationRepository; + } + + public async Task AddLocationAsync(string name) + { + var id = await _repository.AddLocationAsync(name); + + _loggerService.LogInformation($"Location seccusfull create id {id}"); + + return id; + } + + public async Task GetLocationAsync(int id) + { + var entity = await _repository.GetLocationAsync(id); + + if (entity is null) + { + _loggerService.LogWarning("Location not finded"); + return null!; + } + + _loggerService.LogInformation($"Location seccusfull get id {id}"); + + return new Location() + { + Id = entity.Id, + Name = entity.Name + }; + } + + public async Task UpdateLocationAsync(int id, string name) + { + var entity = await _repository.UpdateLocationAsync(id, name); + + if (entity is null) + { + _loggerService.LogWarning("Location not finded"); + return null!; + } + + _loggerService.LogInformation($"Location seccusfull update id {id}"); + + return new Location() + { + Id = entity.Id, + Name = entity.Name + }; + } + + public async Task DeleteLocationAsync(int id) + { + var message = await _repository.DeleteLocationAsync(id); + + if (message is null) + { + _loggerService.LogWarning("Location not finded"); + return null!; + } + + _loggerService.LogInformation($"Location seccusfull delete id {id}"); + + return message; + } + } +} diff --git a/HomeWork23/HomeWork23/Services/PetService.cs b/HomeWork23/HomeWork23/Services/PetService.cs new file mode 100644 index 0000000..fcbfe45 --- /dev/null +++ b/HomeWork23/HomeWork23/Services/PetService.cs @@ -0,0 +1,199 @@ +using HomeWork23.Datas; +using HomeWork23.Datas.Entities; +using HomeWork23.Dto; +using HomeWork23.Dto.Requests; +using HomeWork23.Models; +using HomeWork23.Repositories.Abstracts; +using HomeWork23.Services.Abstracts; +using HomeWork23.Wrapper.Abstracts; +using Microsoft.Extensions.Logging; + +namespace HomeWork23.Services +{ + internal class PetService : BaseDataService, IPetService + { + private readonly ILogger _loggerService; + private readonly IPetRepository _repository; + public PetService( + IDbContextWrapper dbContextWrapper, + ILogger logger, + ILogger loggerService, + IPetRepository petRepository) + : base(dbContextWrapper, logger) + { + _loggerService = loggerService; + _repository = petRepository; + } + + public async Task AddPetAsync(Pet pet) + { + return await ExecuteSafeAsync(async () => + { + var id = await _repository.AddPetAsync(MapingToEntityAdd(pet)); + + _loggerService.LogInformation($"Pet seccusfull create id : {id}"); + + return id; + }); + } + + public async Task GetPetAsync(int id) + { + return await ExecuteSafeAsync(async () => + { + var entity = await _repository.GetPetAsync(id); + + if (entity is null) + { + _loggerService.LogWarning($"Id not valid"); + return null!; + } + + _loggerService.LogInformation($"Pet seccusfull get id : {id}"); + return MapingToModel(entity); + }); + } + + public async Task DeletePetAsync(int id) + { + return await ExecuteSafeAsync(async () => + { + var message = await _repository.DeletePetAsync(id); + if (message is null) + { + _loggerService.LogWarning("id not valid"); + return null!; + } + + _loggerService.LogInformation($"Pet seccusfull delete id : {id}"); + return message; + }); + } + + public async Task UpdateAsync(Pet pet) + { + return await ExecuteSafeAsync(async() => + { + var entity = MapingToEntityUpdate(pet); + + + var entityUpdate = await _repository.UpdatePetAsync(entity); + + if (entityUpdate is null) + { + _loggerService.LogInformation($"Pet id : {pet.Id} not valid"); + return null!; + } + + _loggerService.LogInformation($"Pet seccusfull update id : {pet.Id}"); + return MapingToModel(entityUpdate); + }); + } + + public async Task> GetPetPageAsync(PetRequest request) + { + return await ExecuteSafeAsync(async() => + { + return await _repository.GetPetPageAsync(request); + }); + } + + private Pet MapingToModel(PetEntity entity) + { + return new Pet() + { + Id = entity.Id, + Name = entity.Name, + Description = entity.Description, + Age = entity.Age, + ImageUrl = entity.ImageUrl, + CategoryId = entity.CategoryId, + LocationId = entity.LocationId, + BreedId = entity.BreedId, + Category = new Category() + { + Id = entity.Category.Id, + Name = entity.Category.Name + }, + Breed = new Breed() + { + Id = entity.Breed.Id, + Name = entity.Breed.Name, + Category = new Category() + { + Id = entity.Breed.Category.Id, + Name = entity.Breed.Category.Name + } + }, + Location = new Location() + { + Name = entity.Location.Name, + Id = entity.Location.Id + } + }; + } + + private PetEntity MapingToEntityUpdate(Pet pet) + { + return new PetEntity() + { + Id = pet.Id, + Name = pet.Name, + Description = pet.Description, + Age = pet.Age, + ImageUrl = pet.ImageUrl, + CategoryId= pet.CategoryId, + LocationId = pet.LocationId, + BreedId = pet.BreedId, + Category = new CategoryEntity() + { + Id = pet.Category.Id, + Name = pet.Category.Name + }, + Breed = new BreedEntity() + { + Id = pet.Breed.Id, + Name = pet.Breed.Name, + Category = new CategoryEntity() + { + Id = pet.Breed.Category.Id, + Name = pet.Breed.Category.Name + } + }, + Location = new LocationEntity() + { + Id = pet.Location.Id, + Name = pet.Location.Name + } + }; + } + + private PetEntity MapingToEntityAdd(Pet pet) + { + return new PetEntity() + { + Name = pet.Name, + Description = pet.Description, + Age = pet.Age, + ImageUrl = pet.ImageUrl, + + Category = new CategoryEntity() + { + Name = pet.Category.Name + }, + Breed = new BreedEntity() + { + Name = pet.Breed.Name, + Category = new CategoryEntity() + { + Name = pet.Breed.Category.Name + } + }, + Location = new LocationEntity() + { + Name = pet.Location.Name + } + }; + } + } +} diff --git a/HomeWork23/HomeWork23/Wrapper/Abstracts/BaseDataService.cs b/HomeWork23/HomeWork23/Wrapper/Abstracts/BaseDataService.cs new file mode 100644 index 0000000..b894e85 --- /dev/null +++ b/HomeWork23/HomeWork23/Wrapper/Abstracts/BaseDataService.cs @@ -0,0 +1,69 @@ +using Microsoft.EntityFrameworkCore; +using Microsoft.Extensions.Logging; + +namespace HomeWork23.Wrapper.Abstracts +{ + internal abstract class BaseDataService : IBaseDataService + where T : DbContext + { + private readonly IDbContextWrapper _dbContextWrapper; + private readonly ILogger _logger; + + public BaseDataService( + IDbContextWrapper dbContextWrapper, + ILogger logger) + { + _dbContextWrapper = dbContextWrapper; + _logger = logger; + } + + protected Task ExecuteSafeAsync( + Func func, + CancellationToken cancellationToken = default) => + ExecuteSafeAsync(token => func(), cancellationToken); + + protected Task ExecuteSafeAsync( + Func> func, + CancellationToken cancellationToken = default) => + ExecuteSafeAsync(token => func(), cancellationToken); + + private async Task ExecuteSafeAsync( + Func func, + CancellationToken cancellationToken = default) + { + await using var transaction = await _dbContextWrapper.BeginTransactionAsync(cancellationToken); + + try + { + await func(cancellationToken); + await transaction.CommitAsync(cancellationToken); + } + catch (Exception ex) + { + await transaction.RollbackAsync(cancellationToken); + _logger.LogError(ex, $"transaction is rollbacked"); + throw; + } + } + + private async Task ExecuteSafeAsync( + Func> func, + CancellationToken cancellationToken = default) + { + await using var transaction = await _dbContextWrapper.BeginTransactionAsync(cancellationToken); + + try + { + var result = await func(cancellationToken); + await transaction.CommitAsync(cancellationToken); + return result; + } + catch (Exception ex) + { + await transaction.RollbackAsync(cancellationToken); + _logger.LogError(ex, $"transaction is rollbacked"); + throw; + } + } + } +} diff --git a/HomeWork23/HomeWork23/Wrapper/Abstracts/IBaseDataService.cs b/HomeWork23/HomeWork23/Wrapper/Abstracts/IBaseDataService.cs new file mode 100644 index 0000000..2aa116a --- /dev/null +++ b/HomeWork23/HomeWork23/Wrapper/Abstracts/IBaseDataService.cs @@ -0,0 +1,6 @@ +namespace HomeWork23.Wrapper.Abstracts +{ + internal interface IBaseDataService + { + } +} diff --git a/HomeWork23/HomeWork23/Wrapper/Abstracts/IDbContextWrapper.cs b/HomeWork23/HomeWork23/Wrapper/Abstracts/IDbContextWrapper.cs new file mode 100644 index 0000000..5ed9e9c --- /dev/null +++ b/HomeWork23/HomeWork23/Wrapper/Abstracts/IDbContextWrapper.cs @@ -0,0 +1,12 @@ +using Microsoft.EntityFrameworkCore; +using Microsoft.EntityFrameworkCore.Storage; + +namespace HomeWork23.Wrapper.Abstracts +{ + internal interface IDbContextWrapper + where T : DbContext + { + public T DbContext { get; } + public Task BeginTransactionAsync(CancellationToken cancellationToken); + } +} diff --git a/HomeWork23/HomeWork23/Wrapper/DbContextWrapper.cs b/HomeWork23/HomeWork23/Wrapper/DbContextWrapper.cs new file mode 100644 index 0000000..3564658 --- /dev/null +++ b/HomeWork23/HomeWork23/Wrapper/DbContextWrapper.cs @@ -0,0 +1,23 @@ +using HomeWork23.Wrapper.Abstracts; +using Microsoft.EntityFrameworkCore; +using Microsoft.EntityFrameworkCore.Storage; + +namespace HomeWork23.Wrapper +{ + internal class DbContextWrapper : IDbContextWrapper + where T : DbContext + { + private readonly T _dbContext; + public DbContextWrapper(IDbContextFactory dbContextFactory) + { + _dbContext = dbContextFactory.CreateDbContext(); + } + + public T DbContext => _dbContext; + + public async Task BeginTransactionAsync(CancellationToken cancellationToken) + { + return await _dbContext.Database.BeginTransactionAsync(cancellationToken); + } + } +} diff --git a/HomeWork23/HomeWork23/config.json b/HomeWork23/HomeWork23/config.json new file mode 100644 index 0000000..fbdce29 --- /dev/null +++ b/HomeWork23/HomeWork23/config.json @@ -0,0 +1,8 @@ +{ + "Migration": { + "IsNeedMigration": true + }, + "ConnectionStrings": { + "DefaultConnection": "Server=127.0.0.1,1434;Database=AdventureWorks;User ID=sa;Password=1My_password;TrustServerCertificate=true" + } +} \ No newline at end of file