diff --git a/PARR.DAL/Context/DataContext.cs b/PARR.DAL/Context/DataContext.cs new file mode 100644 index 00000000..cfb62e4f --- /dev/null +++ b/PARR.DAL/Context/DataContext.cs @@ -0,0 +1,16 @@ +using Microsoft.EntityFrameworkCore; +using PARR.DAL.Models; + +namespace PARR.DAL.Context +{ + internal class DataContext : DbContext + { + public DataContext(DbContextOptions options) : base(options) { } + + + //public DbSet Tests { get; set; } + //public DbSet Places { get; set; } + //public DbSet AreaCategories { get; set; } + //public DbSet Areas { get; set; } + } +} diff --git a/PARR.DAL/Migrations/20230517062913_init.Designer.cs b/PARR.DAL/Migrations/20230517062913_init.Designer.cs new file mode 100644 index 00000000..cf4b7350 --- /dev/null +++ b/PARR.DAL/Migrations/20230517062913_init.Designer.cs @@ -0,0 +1,29 @@ +// +using Microsoft.EntityFrameworkCore; +using Microsoft.EntityFrameworkCore.Infrastructure; +using Microsoft.EntityFrameworkCore.Migrations; +using Microsoft.EntityFrameworkCore.Storage.ValueConversion; +using Npgsql.EntityFrameworkCore.PostgreSQL.Metadata; +using PARR.DAL.Context; + +#nullable disable + +namespace PARR.DAL.Migrations +{ + [DbContext(typeof(DataContext))] + [Migration("20230517062913_init")] + partial class init + { + /// + protected override void BuildTargetModel(ModelBuilder modelBuilder) + { +#pragma warning disable 612, 618 + modelBuilder + .HasAnnotation("ProductVersion", "7.0.5") + .HasAnnotation("Relational:MaxIdentifierLength", 63); + + NpgsqlModelBuilderExtensions.UseIdentityByDefaultColumns(modelBuilder); +#pragma warning restore 612, 618 + } + } +} diff --git a/PARR.DAL/Migrations/20230517062913_init.cs b/PARR.DAL/Migrations/20230517062913_init.cs new file mode 100644 index 00000000..472b6151 --- /dev/null +++ b/PARR.DAL/Migrations/20230517062913_init.cs @@ -0,0 +1,22 @@ +using Microsoft.EntityFrameworkCore.Migrations; + +#nullable disable + +namespace PARR.DAL.Migrations +{ + /// + public partial class init : Migration + { + /// + protected override void Up(MigrationBuilder migrationBuilder) + { + + } + + /// + protected override void Down(MigrationBuilder migrationBuilder) + { + + } + } +} diff --git a/PARR.DAL/Migrations/DataContextModelSnapshot.cs b/PARR.DAL/Migrations/DataContextModelSnapshot.cs new file mode 100644 index 00000000..507e2996 --- /dev/null +++ b/PARR.DAL/Migrations/DataContextModelSnapshot.cs @@ -0,0 +1,26 @@ +// +using Microsoft.EntityFrameworkCore; +using Microsoft.EntityFrameworkCore.Infrastructure; +using Microsoft.EntityFrameworkCore.Storage.ValueConversion; +using Npgsql.EntityFrameworkCore.PostgreSQL.Metadata; +using PARR.DAL.Context; + +#nullable disable + +namespace PARR.DAL.Migrations +{ + [DbContext(typeof(DataContext))] + partial class DataContextModelSnapshot : ModelSnapshot + { + protected override void BuildModel(ModelBuilder modelBuilder) + { +#pragma warning disable 612, 618 + modelBuilder + .HasAnnotation("ProductVersion", "7.0.5") + .HasAnnotation("Relational:MaxIdentifierLength", 63); + + NpgsqlModelBuilderExtensions.UseIdentityByDefaultColumns(modelBuilder); +#pragma warning restore 612, 618 + } + } +} diff --git a/PARR.DAL/Models/Base/IBase.cs b/PARR.DAL/Models/Base/IBase.cs new file mode 100644 index 00000000..a0c17efa --- /dev/null +++ b/PARR.DAL/Models/Base/IBase.cs @@ -0,0 +1,11 @@ +namespace PARR.DAL.Models.Base +{ + public interface IBase + { + Guid Id { get; set; } + + DateTimeOffset DateCreated { get; set; } + + DateTimeOffset? DateModified { get; set; } + } +} diff --git a/PARR.DAL/Models/Test.cs b/PARR.DAL/Models/Test.cs new file mode 100644 index 00000000..84ecbbf6 --- /dev/null +++ b/PARR.DAL/Models/Test.cs @@ -0,0 +1,19 @@ +using PARR.DAL.Models.Base; +using System.ComponentModel.DataAnnotations; +using System.ComponentModel.DataAnnotations.Schema; + +namespace PARR.DAL.Models +{ + [Table("Tests")] + public class Test : IBase + { + [Key] + public Guid Id { get; set; } + public DateTimeOffset DateCreated { get; set; } + public DateTimeOffset? DateModified { get; set; } + + public required string Name { get; set; } + + public string? Description { get; set; } + } +} diff --git a/PARR.DAL/PARR.DAL.csproj b/PARR.DAL/PARR.DAL.csproj index cfadb03d..74a3dffd 100644 --- a/PARR.DAL/PARR.DAL.csproj +++ b/PARR.DAL/PARR.DAL.csproj @@ -6,4 +6,19 @@ enable + + + all + runtime; build; native; contentfiles; analyzers; buildtransitive + + + + + + + + + + + diff --git a/PARR.DAL/ParrDalInstaller.cs b/PARR.DAL/ParrDalInstaller.cs new file mode 100644 index 00000000..0d85ae01 --- /dev/null +++ b/PARR.DAL/ParrDalInstaller.cs @@ -0,0 +1,22 @@ +using Microsoft.EntityFrameworkCore; +using Microsoft.Extensions.Configuration; +using Microsoft.Extensions.DependencyInjection; +using PARR.DAL.Context; + +namespace PARR.DAL +{ + public static class ParrDalInstaller + { + public static void InstallDalServices(this IServiceCollection services, IConfiguration configuration) + { + services.AddDbContext(opt => + opt.UseNpgsql(configuration.GetConnectionString("DefaultConnection")) + ); + + //services.AddTransient(); + //services.AddTransient(); + //services.AddTransient(); + //services.AddTransient(); + } + } +} diff --git a/PARR.DAL/Services/Abstracts/BaseService.cs b/PARR.DAL/Services/Abstracts/BaseService.cs new file mode 100644 index 00000000..64e7b2d1 --- /dev/null +++ b/PARR.DAL/Services/Abstracts/BaseService.cs @@ -0,0 +1,119 @@ +using Microsoft.EntityFrameworkCore; +using Microsoft.Extensions.Logging; +using PARR.DAL.Context; +using PARR.DAL.Models.Base; +using PARR.DAL.Services.Interfaces.Base; + +namespace PARR.DAL.Services.Abstracts +{ + internal abstract class BaseService : IBaseService where T : class, IBase + { + private readonly ILogger> logger; + + protected abstract DbSet EntitySet { get; } + protected abstract DataContext EntitiContext { get; } + + public BaseService(ILogger> logger) + { + this.logger = logger; + } + + public virtual async Task AddRangeAsync(List objs) + { + objs.ForEach(item => item.DateCreated = DateTimeOffset.UtcNow); + + try + { + await EntitySet.AddRangeAsync(objs); + return true; + } + catch (Exception ex) + { + logger.LogError(ex, "Ошибка при добавлении диапазона в БД."); + return false; + } + } + + public async Task CommitAsync() + { + try + { + await EntitiContext.SaveChangesAsync(); + return true; + } + catch (Exception ex) + { + logger.LogError(ex, "Ошибка при Commit"); + return false; + } + } + + public virtual async Task CreateAsync(T obj) + { + obj.DateCreated = DateTimeOffset.UtcNow; + + try + { + await EntitySet.AddAsync(obj); + return true; + } + catch (Exception ex) + { + logger.LogError(ex, "Ошибка при добавлении в БД."); + return false; + } + } + + public virtual bool Delete(T obj) + { + try + { + EntitySet.Remove(obj); + return true; + } + catch (Exception ex) + { + logger.LogError(ex, "Ошибка при удалении из БД."); + return false; + } + } + + public virtual async Task DeleteAsync(Guid id) + { + try + { + var exist = await GetAsync(id); + if (exist == null) + { + logger.LogError($"Ошибка при удалении из БД. Не найдена запись в БД с id: {id}"); + return false; + } + + EntitySet.Remove(exist); + return true; + } + catch (Exception ex) + { + logger.LogError(ex, "Ошибка при удалении из БД."); + return false; + } + } + + public virtual IQueryable Get() + { + return EntitySet; + } + + public virtual async Task GetAsync(Guid id) + { + return await EntitySet.FirstOrDefaultAsync(t => t.Id == id); + } + + //public virtual IQueryable GetPage(IQueryable query, PaginationFilter paginationFilter) + //{ + // int skip = (paginationFilter.PageNumber - 1) * paginationFilter.PageSize; + + // return query.Skip(skip).Take(paginationFilter.PageSize); + //} + } +} diff --git a/PARR.DAL/Services/Interfaces/Base/IBaseService.cs b/PARR.DAL/Services/Interfaces/Base/IBaseService.cs new file mode 100644 index 00000000..1e9c442d --- /dev/null +++ b/PARR.DAL/Services/Interfaces/Base/IBaseService.cs @@ -0,0 +1,19 @@ +using PARR.DAL.Models.Base; + +namespace PARR.DAL.Services.Interfaces.Base +{ + public interface IBaseService where T : class, IBase + { + Task GetAsync(Guid id); + IQueryable Get(); + //IQueryable GetPage(IQueryable query, PaginationFilter paginationFilter); + + Task CreateAsync(T obj); + Task AddRangeAsync(List objs); + + Task DeleteAsync(Guid id); + bool Delete(T obj); + + Task CommitAsync(); + } +} diff --git a/PARR_API.sln b/PARR_API.sln index d381db2f..1da0e13d 100644 --- a/PARR_API.sln +++ b/PARR_API.sln @@ -3,7 +3,9 @@ Microsoft Visual Studio Solution File, Format Version 12.00 # Visual Studio Version 17 VisualStudioVersion = 17.4.33213.308 MinimumVisualStudioVersion = 10.0.40219.1 -Project("{FAE04EC0-301F-11D3-BF4B-00C04F79EFBC}") = "PARR_API", "PARR_API\PARR_API.csproj", "{82C57299-DA04-4A99-9C04-614FDDCE7AA1}" +Project("{FAE04EC0-301F-11D3-BF4B-00C04F79EFBC}") = "PARR.API", "PARR_API\PARR.API.csproj", "{82C57299-DA04-4A99-9C04-614FDDCE7AA1}" +EndProject +Project("{FAE04EC0-301F-11D3-BF4B-00C04F79EFBC}") = "PARR.DAL", "PARR.DAL\PARR.DAL.csproj", "{CBC557EE-382E-4386-B4D2-2EE3DC9A7CBE}" EndProject Global GlobalSection(SolutionConfigurationPlatforms) = preSolution @@ -15,6 +17,10 @@ Global {82C57299-DA04-4A99-9C04-614FDDCE7AA1}.Debug|Any CPU.Build.0 = Debug|Any CPU {82C57299-DA04-4A99-9C04-614FDDCE7AA1}.Release|Any CPU.ActiveCfg = Release|Any CPU {82C57299-DA04-4A99-9C04-614FDDCE7AA1}.Release|Any CPU.Build.0 = Release|Any CPU + {CBC557EE-382E-4386-B4D2-2EE3DC9A7CBE}.Debug|Any CPU.ActiveCfg = Debug|Any CPU + {CBC557EE-382E-4386-B4D2-2EE3DC9A7CBE}.Debug|Any CPU.Build.0 = Debug|Any CPU + {CBC557EE-382E-4386-B4D2-2EE3DC9A7CBE}.Release|Any CPU.ActiveCfg = Release|Any CPU + {CBC557EE-382E-4386-B4D2-2EE3DC9A7CBE}.Release|Any CPU.Build.0 = Release|Any CPU EndGlobalSection GlobalSection(SolutionProperties) = preSolution HideSolutionNode = FALSE diff --git a/PARR_API/Contracts/V1/ApiRoutes.cs b/PARR_API/Contracts/V1/ApiRoutes.cs index 17adcd4f..01024021 100644 --- a/PARR_API/Contracts/V1/ApiRoutes.cs +++ b/PARR_API/Contracts/V1/ApiRoutes.cs @@ -1,4 +1,4 @@ -namespace PARR_API.Contracts.V1 +namespace PARR.API.Contracts.V1 { // https://tproger.ru/translations/luchshie-praktiki-razrabotki-rest-api-20-sovetov/ diff --git a/PARR_API/Controllers/V1/ApiStatusController.cs b/PARR_API/Controllers/V1/ApiStatusController.cs index b9b9d8ea..d7db559d 100644 --- a/PARR_API/Controllers/V1/ApiStatusController.cs +++ b/PARR_API/Controllers/V1/ApiStatusController.cs @@ -1,6 +1,6 @@ -using PARR_API.Controllers.V1.Base; +using PARR.API.Controllers.V1.Base; -namespace PARR_API.Controllers.V1 +namespace PARR.API.Controllers.V1 { public class ApiStatusController : BaseApiController { diff --git a/PARR_API/Controllers/V1/Base/BaseApiController.cs b/PARR_API/Controllers/V1/Base/BaseApiController.cs index aa33350a..601f4a43 100644 --- a/PARR_API/Controllers/V1/Base/BaseApiController.cs +++ b/PARR_API/Controllers/V1/Base/BaseApiController.cs @@ -1,6 +1,6 @@ using Microsoft.AspNetCore.Mvc; -namespace PARR_API.Controllers.V1.Base +namespace PARR.API.Controllers.V1.Base { [ApiController] public class BaseApiController : ControllerBase diff --git a/PARR_API/Controllers/V1/HomeController.cs b/PARR_API/Controllers/V1/HomeController.cs new file mode 100644 index 00000000..7308ff88 --- /dev/null +++ b/PARR_API/Controllers/V1/HomeController.cs @@ -0,0 +1,12 @@ +using Microsoft.AspNetCore.Mvc; + +namespace PARR.API.Controllers.V1 +{ + public class HomeController : Controller + { + public IActionResult Index() + { + return View(); + } + } +} diff --git a/PARR_API/Installers/ApiServicesInstaller.cs b/PARR_API/Installers/ApiServicesInstaller.cs index 32078df9..daa0b8b7 100644 --- a/PARR_API/Installers/ApiServicesInstaller.cs +++ b/PARR_API/Installers/ApiServicesInstaller.cs @@ -1,7 +1,7 @@ -using PARR_API.Services.Implementations; -using PARR_API.Services.Interfaces; +using PARR.API.Services.Implementations; +using PARR.API.Services.Interfaces; -namespace PARR_API.Installers +namespace PARR.API.Installers { /// /// Самописные сервисы которые используются для АПИ diff --git a/PARR_API/Installers/CorsInstaller.cs b/PARR_API/Installers/CorsInstaller.cs index b760c21b..42e150c5 100644 --- a/PARR_API/Installers/CorsInstaller.cs +++ b/PARR_API/Installers/CorsInstaller.cs @@ -1,6 +1,6 @@ -using PARR_API.Settings; +using PARR.API.Settings; -namespace PARR_API.Installers +namespace PARR.API.Installers { public static class CorsInstaller { diff --git a/PARR_API/Installers/SettingsInstaller.cs b/PARR_API/Installers/SettingsInstaller.cs index 46c5420d..29117a34 100644 --- a/PARR_API/Installers/SettingsInstaller.cs +++ b/PARR_API/Installers/SettingsInstaller.cs @@ -1,4 +1,4 @@ -namespace PARR_API.Installers +namespace PARR.API.Installers { /// /// Биндинги из конфига appsettings diff --git a/PARR_API/Installers/SwaggerInstaller.cs b/PARR_API/Installers/SwaggerInstaller.cs index a53be633..e429eff3 100644 --- a/PARR_API/Installers/SwaggerInstaller.cs +++ b/PARR_API/Installers/SwaggerInstaller.cs @@ -1,7 +1,7 @@ using Microsoft.OpenApi.Models; using System.Reflection; -namespace PARR_API.Installers +namespace PARR.API.Installers { public static class SwaggerInstaller { diff --git a/PARR_API/MappingProfiles/DomainToResponseProfile.cs b/PARR_API/MappingProfiles/DomainToResponseProfile.cs index ec5db6c0..f64d38ef 100644 --- a/PARR_API/MappingProfiles/DomainToResponseProfile.cs +++ b/PARR_API/MappingProfiles/DomainToResponseProfile.cs @@ -1,6 +1,6 @@ using AutoMapper; -namespace PARR_API.MappingProfiles +namespace PARR.API.MappingProfiles { public class DomainToResponseProfile : Profile { diff --git a/PARR_API/MappingProfiles/RequestToDomainProfile.cs b/PARR_API/MappingProfiles/RequestToDomainProfile.cs index 6c9fae67..f387c048 100644 --- a/PARR_API/MappingProfiles/RequestToDomainProfile.cs +++ b/PARR_API/MappingProfiles/RequestToDomainProfile.cs @@ -1,6 +1,6 @@ using AutoMapper; -namespace PARR_API.MappingProfiles +namespace PARR.API.MappingProfiles { public class RequestToDomainProfile : Profile { diff --git a/PARR_API/PARR_API.csproj b/PARR_API/PARR.API.csproj similarity index 81% rename from PARR_API/PARR_API.csproj rename to PARR_API/PARR.API.csproj index 3616e162..bfee007d 100644 --- a/PARR_API/PARR_API.csproj +++ b/PARR_API/PARR.API.csproj @@ -19,6 +19,10 @@ + + all + runtime; build; native; contentfiles; analyzers; buildtransitive + @@ -33,4 +37,8 @@ + + + + diff --git a/PARR_API/Program.cs b/PARR_API/Program.cs index 54062b05..fc6a3509 100644 --- a/PARR_API/Program.cs +++ b/PARR_API/Program.cs @@ -1,5 +1,6 @@ using FluentValidation; -using PARR_API.Installers; +using PARR.API.Installers; +using PARR.DAL; using Serilog; using System.Reflection; @@ -13,6 +14,7 @@ builder.Host.UseSerilog((context, config) => }); // Add services to the container. +builder.Services.InstallDalServices(builder.Configuration); builder.Services.InstallApiServices(builder.Configuration); builder.Services.InstallSettings(builder.Configuration); builder.Services.AddAutoMapper(AppDomain.CurrentDomain.GetAssemblies()); diff --git a/PARR_API/Services/Implementations/UriService.cs b/PARR_API/Services/Implementations/UriService.cs index c42c35e7..6df5d62e 100644 --- a/PARR_API/Services/Implementations/UriService.cs +++ b/PARR_API/Services/Implementations/UriService.cs @@ -1,6 +1,6 @@ -using PARR_API.Services.Interfaces; +using PARR.API.Services.Interfaces; -namespace PARR_API.Services.Implementations +namespace PARR.API.Services.Implementations { public class UriService : IUriService { diff --git a/PARR_API/Services/Interfaces/IUriService.cs b/PARR_API/Services/Interfaces/IUriService.cs index 79aa67e0..bdb57831 100644 --- a/PARR_API/Services/Interfaces/IUriService.cs +++ b/PARR_API/Services/Interfaces/IUriService.cs @@ -1,4 +1,4 @@ -namespace PARR_API.Services.Interfaces +namespace PARR.API.Services.Interfaces { public interface IUriService { diff --git a/PARR_API/Settings/CorsSettings.cs b/PARR_API/Settings/CorsSettings.cs index 165882b5..3b969b51 100644 --- a/PARR_API/Settings/CorsSettings.cs +++ b/PARR_API/Settings/CorsSettings.cs @@ -1,4 +1,4 @@ -namespace PARR_API.Settings +namespace PARR.API.Settings { public class CorsSettings { diff --git a/PARR_API/appsettings.json b/PARR_API/appsettings.json index 2c17a37e..1dfce196 100644 --- a/PARR_API/appsettings.json +++ b/PARR_API/appsettings.json @@ -1,6 +1,6 @@ { "ConnectionStrings": { - "DefaultConnection": "Server=10.99.253.184;Database=geo;User Id=app_geo; Password=Khdlifg(G875904HJFfd@3;" + "DefaultConnection": "Server=10.99.253.184;Database=parr;User Id=app_parr; Password=PosdfkhT&)%sdfligL&%5546;" }, "Logging": { "LogLevel": {