Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
25 changes: 25 additions & 0 deletions HomeWork23/HomeWork23.sln
Original file line number Diff line number Diff line change
@@ -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
185 changes: 185 additions & 0 deletions HomeWork23/HomeWork23/App.cs
Original file line number Diff line number Diff line change
@@ -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);
}
}
}
26 changes: 26 additions & 0 deletions HomeWork23/HomeWork23/Datas/ApplicatDbContext.cs
Original file line number Diff line number Diff line change
@@ -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<ApplicatDbContext> option) : base(option)
{}

public DbSet<BreedEntity> Breeds { get; set; } = null!;
public DbSet<CategoryEntity> Categories { get; set; } = null!;
public DbSet<LocationEntity> Locations { get; set; } = null!;
public DbSet<PetEntity> 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();
}
}
}
29 changes: 29 additions & 0 deletions HomeWork23/HomeWork23/Datas/ApplicatDbContextFactory.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,29 @@
using Microsoft.EntityFrameworkCore;
using Microsoft.EntityFrameworkCore.Design;
using Microsoft.Extensions.Configuration;

namespace HomeWork23.Datas
{
internal class ApplicatDbContextFactory : IDesignTimeDbContextFactory<ApplicatDbContext>
{
public ApplicatDbContext CreateDbContext(string[] args)
{
var optionsBuilder = new DbContextOptionsBuilder<ApplicatDbContext>();

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);
}
}
}
11 changes: 11 additions & 0 deletions HomeWork23/HomeWork23/Datas/Entities/BreedEntity.cs
Original file line number Diff line number Diff line change
@@ -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<PetEntity> Pets { get; set; } = new List<PetEntity>();
}
}
10 changes: 10 additions & 0 deletions HomeWork23/HomeWork23/Datas/Entities/CategoryEntity.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,10 @@
namespace HomeWork23.Datas.Entities
{
internal class CategoryEntity
{
public int Id { get; set; }
public string Name { get; set; } = null!;
public ICollection<PetEntity> Pets { get; set; } = new List<PetEntity>();
public ICollection<BreedEntity> Breeds { get; set; } = new List<BreedEntity>();
}
}
9 changes: 9 additions & 0 deletions HomeWork23/HomeWork23/Datas/Entities/LocationEntity.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
namespace HomeWork23.Datas.Entities
{
internal class LocationEntity
{
public int Id { get; set; }
public string Name { get; set; } = null!;
public ICollection<PetEntity> Pets { get; set; } = new List<PetEntity>();
}
}
18 changes: 18 additions & 0 deletions HomeWork23/HomeWork23/Datas/Entities/PetEntity.cs
Original file line number Diff line number Diff line change
@@ -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; }

}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,28 @@
using HomeWork23.Datas.Entities;
using Microsoft.EntityFrameworkCore;
using Microsoft.EntityFrameworkCore.Metadata.Builders;

namespace HomeWork23.Datas.EntityConfigure
{
internal class BreedEntityConffiguration : IEntityTypeConfiguration<BreedEntity>
{
public void Configure(EntityTypeBuilder<BreedEntity> 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);
}
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,21 @@
using HomeWork23.Datas.Entities;
using Microsoft.EntityFrameworkCore;
using Microsoft.EntityFrameworkCore.Metadata.Builders;

namespace HomeWork23.Datas.EntityConfigure
{
internal class CategoryEntityConfiguration : IEntityTypeConfiguration<CategoryEntity>
{
public void Configure(EntityTypeBuilder<CategoryEntity> builder)
{
builder
.HasKey(x => x.Id);


builder
.Property(x => x.Name)
.IsRequired()
.HasColumnName("category_name");
}
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,21 @@
using HomeWork23.Datas.Entities;
using Microsoft.EntityFrameworkCore;
using Microsoft.EntityFrameworkCore.Metadata.Builders;

namespace HomeWork23.Datas.EntityConfigure
{
internal class LocationEntityConfiguration : IEntityTypeConfiguration<LocationEntity>
{
public void Configure(EntityTypeBuilder<LocationEntity> builder)
{
builder
.HasKey(x => x.Id);


builder
.Property(x => x.Name)
.IsRequired()
.HasColumnName("location_name");
}
}
}
Loading