From 30a59d08096be29d3fb64ba8bbbde0a55422097a Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?=D0=A4=D1=80=D0=BE=D0=BB=D0=BE=D0=B2?= Date: Fri, 10 Sep 2021 11:28:57 +0500 Subject: [PATCH] cleanup --- AsbCloudApp/Data/CompanyDto.cs | 2 +- AsbCloudApp/Data/EventDto.cs | 2 +- AsbCloudApp/Data/PlanFactBase.cs | 8 +- AsbCloudApp/Data/PlanFactPredictBase.cs | 8 +- AsbCloudApp/Data/StatSectionDto.cs | 2 +- AsbCloudApp/Data/TelemetryAnalysisDto.cs | 2 +- AsbCloudApp/Data/TelemetryDto.cs | 9 ++ AsbCloudApp/Data/TelemetryMessageDto.cs | 2 +- AsbCloudApp/Data/UserRoleDto.cs | 11 +++ AsbCloudApp/Data/WellDto.cs | 1 + AsbCloudApp/Services/IAuthService.cs | 2 +- AsbCloudApp/Services/IClusterService.cs | 10 +- AsbCloudApp/Services/ICrudService.cs | 12 ++- AsbCloudApp/Services/IDataService.cs | 2 +- AsbCloudApp/Services/IEventService.cs | 2 +- AsbCloudApp/Services/IFileService.cs | 2 +- AsbCloudApp/Services/IMessageService.cs | 6 +- AsbCloudApp/Services/IWellOperationService.cs | 6 +- AsbCloudDb/Migrations/20210907085527_Init.cs | 6 +- AsbCloudDb/Model/AsbCloudDbContext.cs | 23 +++-- AsbCloudDb/Model/IIdWell.cs | 8 +- AsbCloudDb/Model/RelationCompanyWell.cs | 2 +- AsbCloudDb/Model/ReportProperty.cs | 2 +- AsbCloudDb/Model/Well.cs | 1 - AsbCloudDb/Model/WellOperation.cs | 4 +- AsbCloudDb/Readme.md | 4 + AsbCloudDbDemoData/Program.cs | 10 +- AsbCloudInfrastructure/DependencyInjection.cs | 9 ++ AsbCloudInfrastructure/Helper.cs | 2 - AsbCloudInfrastructure/MapsterExtension.cs | 2 +- .../Services/Cache/CacheTable.cs | 4 +- .../Services/CrudServiceBase.cs | 95 ++++++++++++++----- .../Services/DrillingProgramService.cs | 14 ++- .../Services/ICachedCrudService.cs | 8 ++ .../Services/MeasureService.cs | 2 +- .../Services/WellOperationsStatService.cs | 15 +-- .../Services/WellService.cs | 8 +- .../Services/_CachedCrudService.cs | 47 +++++++++ .../Controllers/AdminClusterController.cs | 19 ++++ .../Controllers/AdminCompanyController.cs | 19 ++++ .../Controllers/AdminDepositController.cs | 19 ++++ .../Controllers/AdminTelemetryController.cs | 19 ++++ .../Controllers/AdminUserController.cs | 19 ++++ .../Controllers/AdminUserRoleController.cs | 19 ++++ .../Controllers/AdminWellController.cs | 19 ++++ AsbCloudWebApi/Controllers/CrudController.cs | 88 ++++++++++++++--- 46 files changed, 447 insertions(+), 129 deletions(-) create mode 100644 AsbCloudApp/Data/TelemetryDto.cs create mode 100644 AsbCloudApp/Data/UserRoleDto.cs create mode 100644 AsbCloudDb/Readme.md create mode 100644 AsbCloudInfrastructure/Services/ICachedCrudService.cs create mode 100644 AsbCloudInfrastructure/Services/_CachedCrudService.cs create mode 100644 AsbCloudWebApi/Controllers/AdminClusterController.cs create mode 100644 AsbCloudWebApi/Controllers/AdminCompanyController.cs create mode 100644 AsbCloudWebApi/Controllers/AdminDepositController.cs create mode 100644 AsbCloudWebApi/Controllers/AdminTelemetryController.cs create mode 100644 AsbCloudWebApi/Controllers/AdminUserController.cs create mode 100644 AsbCloudWebApi/Controllers/AdminUserRoleController.cs create mode 100644 AsbCloudWebApi/Controllers/AdminWellController.cs diff --git a/AsbCloudApp/Data/CompanyDto.cs b/AsbCloudApp/Data/CompanyDto.cs index fd9ef0a8..572c4b24 100644 --- a/AsbCloudApp/Data/CompanyDto.cs +++ b/AsbCloudApp/Data/CompanyDto.cs @@ -1,6 +1,6 @@ namespace AsbCloudApp.Data { - public class CompanyDto: IId + public class CompanyDto : IId { public int Id { get; set; } public string Caption { get; set; } diff --git a/AsbCloudApp/Data/EventDto.cs b/AsbCloudApp/Data/EventDto.cs index e111a923..df4a3da3 100644 --- a/AsbCloudApp/Data/EventDto.cs +++ b/AsbCloudApp/Data/EventDto.cs @@ -1,6 +1,6 @@ namespace AsbCloudApp.Data { - public class EventDto: IId + public class EventDto : IId { public int Id { get; set; } public string Message { get; set; } diff --git a/AsbCloudApp/Data/PlanFactBase.cs b/AsbCloudApp/Data/PlanFactBase.cs index 033afeed..b4ba5ac0 100644 --- a/AsbCloudApp/Data/PlanFactBase.cs +++ b/AsbCloudApp/Data/PlanFactBase.cs @@ -1,10 +1,4 @@ -using System; -using System.Collections.Generic; -using System.Linq; -using System.Text; -using System.Threading.Tasks; - -namespace AsbCloudApp.Data +namespace AsbCloudApp.Data { public class PlanFactBase { diff --git a/AsbCloudApp/Data/PlanFactPredictBase.cs b/AsbCloudApp/Data/PlanFactPredictBase.cs index c27d476d..22ad9f5a 100644 --- a/AsbCloudApp/Data/PlanFactPredictBase.cs +++ b/AsbCloudApp/Data/PlanFactPredictBase.cs @@ -1,10 +1,4 @@ -using System; -using System.Collections.Generic; -using System.Linq; -using System.Text; -using System.Threading.Tasks; - -namespace AsbCloudApp.Data +namespace AsbCloudApp.Data { /// /// Lines container for Time Vs Depth chart diff --git a/AsbCloudApp/Data/StatSectionDto.cs b/AsbCloudApp/Data/StatSectionDto.cs index 7e20b594..b3563645 100644 --- a/AsbCloudApp/Data/StatSectionDto.cs +++ b/AsbCloudApp/Data/StatSectionDto.cs @@ -3,6 +3,6 @@ public class StatSectionDto : PlanFactBase, IId { public int Id { get; set; } - public string Caption { get; set; } + public string Caption { get; set; } } } diff --git a/AsbCloudApp/Data/TelemetryAnalysisDto.cs b/AsbCloudApp/Data/TelemetryAnalysisDto.cs index bb31ca87..aa9d938d 100644 --- a/AsbCloudApp/Data/TelemetryAnalysisDto.cs +++ b/AsbCloudApp/Data/TelemetryAnalysisDto.cs @@ -1,6 +1,6 @@ namespace AsbCloudApp.Data { - public class TelemetryAnalysisDto: IId + public class TelemetryAnalysisDto : IId { public int Id { get; set; } public int IdTelemetry { get; set; } diff --git a/AsbCloudApp/Data/TelemetryDto.cs b/AsbCloudApp/Data/TelemetryDto.cs new file mode 100644 index 00000000..ce79b5f8 --- /dev/null +++ b/AsbCloudApp/Data/TelemetryDto.cs @@ -0,0 +1,9 @@ +namespace AsbCloudApp.Data +{ + public class TelemetryDto : IId + { + public int Id { get; set; } + public string RemoteUid { get; set; } + public TelemetryInfoDto Info { get; set; } + } +} diff --git a/AsbCloudApp/Data/TelemetryMessageDto.cs b/AsbCloudApp/Data/TelemetryMessageDto.cs index 177e5867..54616cd0 100644 --- a/AsbCloudApp/Data/TelemetryMessageDto.cs +++ b/AsbCloudApp/Data/TelemetryMessageDto.cs @@ -5,7 +5,7 @@ namespace AsbCloudApp.Data /// /// Сообщение получаемое от телеметрии /// - public class TelemetryMessageDto: IId + public class TelemetryMessageDto : IId { public int Id { get; set; } public DateTime Date { get; set; } diff --git a/AsbCloudApp/Data/UserRoleDto.cs b/AsbCloudApp/Data/UserRoleDto.cs new file mode 100644 index 00000000..e4abea51 --- /dev/null +++ b/AsbCloudApp/Data/UserRoleDto.cs @@ -0,0 +1,11 @@ +using System.Collections.Generic; + +namespace AsbCloudApp.Data +{ + public class UserRoleDto : IId + { + public int Id { get; set; } + public string Caption { get; set; } + public virtual ICollection Users { get; set; } + } +} diff --git a/AsbCloudApp/Data/WellDto.cs b/AsbCloudApp/Data/WellDto.cs index 108404b7..ad156b45 100644 --- a/AsbCloudApp/Data/WellDto.cs +++ b/AsbCloudApp/Data/WellDto.cs @@ -7,5 +7,6 @@ public double? Latitude { get; set; } public double? Longitude { get; set; } public string WellType { get; set; } + public TelemetryDto Telemetry { get; set; } } } diff --git a/AsbCloudApp/Services/IAuthService.cs b/AsbCloudApp/Services/IAuthService.cs index 621f2c7e..4cf84371 100644 --- a/AsbCloudApp/Services/IAuthService.cs +++ b/AsbCloudApp/Services/IAuthService.cs @@ -9,7 +9,7 @@ namespace AsbCloudApp.Services { int ChangePassword(int idUser, string newPassword); int ChangePassword(string userLogin, string newPassword); - Task LoginAsync(string login, + Task LoginAsync(string login, string password, CancellationToken token = default); string Refresh(ClaimsPrincipal user); diff --git a/AsbCloudApp/Services/IClusterService.cs b/AsbCloudApp/Services/IClusterService.cs index 104e51c7..dacd8f4f 100644 --- a/AsbCloudApp/Services/IClusterService.cs +++ b/AsbCloudApp/Services/IClusterService.cs @@ -7,13 +7,13 @@ namespace AsbCloudApp.Services { public interface IClusterService { - Task> GetDepositsAsync(int idCompany, - CancellationToken token ); - Task> GetClustersAsync(int idCompany, - int depositId, CancellationToken token ); + Task> GetDepositsAsync(int idCompany, + CancellationToken token); + Task> GetClustersAsync(int idCompany, + int depositId, CancellationToken token); Task> GetClustersAsync(int idCompany, CancellationToken token); - Task> GetWellsAsync(int idCompany, + Task> GetWellsAsync(int idCompany, int clusterId, CancellationToken token); } } diff --git a/AsbCloudApp/Services/ICrudService.cs b/AsbCloudApp/Services/ICrudService.cs index 24e8de6f..1d9c7ff8 100644 --- a/AsbCloudApp/Services/ICrudService.cs +++ b/AsbCloudApp/Services/ICrudService.cs @@ -7,11 +7,15 @@ namespace AsbCloudApp.Services public interface ICrudService where Tdto : Data.IId { - Task GetAsync(int id, CancellationToken token = default); - Task InsertAsync(Tdto newItem, CancellationToken token = default); - Task> InsertRangeAsync(IEnumerable newItems, CancellationToken token = default); - Task UpdateAsync(Tdto item, CancellationToken token = default); + List Incledes { get; } + Task DeleteAsync(int id, CancellationToken token = default); Task DeleteAsync(IEnumerable ids, CancellationToken token = default); + Task> GetAllAsync(CancellationToken token = default); + Task GetAsync(int id, CancellationToken token = default); + Task> GetPageAsync(int skip = 0, int take = 32, CancellationToken token = default); + Task InsertAsync(Tdto newItem, CancellationToken token = default); + Task InsertRangeAsync(IEnumerable newItems, CancellationToken token = default); + Task UpdateAsync(int id, Tdto item, CancellationToken token = default); } } \ No newline at end of file diff --git a/AsbCloudApp/Services/IDataService.cs b/AsbCloudApp/Services/IDataService.cs index d9222fd3..ab9ad431 100644 --- a/AsbCloudApp/Services/IDataService.cs +++ b/AsbCloudApp/Services/IDataService.cs @@ -8,7 +8,7 @@ namespace AsbCloudApp.Services { public interface IDataService { - Task> GetAsync(int idWell, DateTime dateBegin = default, + Task> GetAsync(int idWell, DateTime dateBegin = default, double intervalSec = 600d, int approxPointsCount = 1024, CancellationToken token = default); diff --git a/AsbCloudApp/Services/IEventService.cs b/AsbCloudApp/Services/IEventService.cs index 26f03d16..54e8897a 100644 --- a/AsbCloudApp/Services/IEventService.cs +++ b/AsbCloudApp/Services/IEventService.cs @@ -7,7 +7,7 @@ namespace AsbCloudApp.Services { public interface IEventService { - Task UpsertAsync(string uid, IEnumerable dtos, + Task UpsertAsync(string uid, IEnumerable dtos, CancellationToken token = default); } } diff --git a/AsbCloudApp/Services/IFileService.cs b/AsbCloudApp/Services/IFileService.cs index ad9c201b..34a0f3fc 100644 --- a/AsbCloudApp/Services/IFileService.cs +++ b/AsbCloudApp/Services/IFileService.cs @@ -1,7 +1,7 @@ using AsbCloudApp.Data; using System; -using System.IO; using System.Collections.Generic; +using System.IO; using System.Threading; using System.Threading.Tasks; diff --git a/AsbCloudApp/Services/IMessageService.cs b/AsbCloudApp/Services/IMessageService.cs index 097a737c..e51b56df 100644 --- a/AsbCloudApp/Services/IMessageService.cs +++ b/AsbCloudApp/Services/IMessageService.cs @@ -8,9 +8,9 @@ namespace AsbCloudApp.Services { public interface IMessageService { - Task> GetMessagesAsync(int idWell, - IEnumerable categoryids = null, DateTime begin = default, - DateTime end = default, string searchString = default, + Task> GetMessagesAsync(int idWell, + IEnumerable categoryids = null, DateTime begin = default, + DateTime end = default, string searchString = default, int skip = 0, int take = 32, CancellationToken token = default); Task GetMessagesDatesRangeAsync(int idWell, CancellationToken token = default); diff --git a/AsbCloudApp/Services/IWellOperationService.cs b/AsbCloudApp/Services/IWellOperationService.cs index 37be7c0e..6844cd7f 100644 --- a/AsbCloudApp/Services/IWellOperationService.cs +++ b/AsbCloudApp/Services/IWellOperationService.cs @@ -1,8 +1,8 @@ -using System; +using AsbCloudApp.Data; +using System; using System.Collections.Generic; using System.Threading; using System.Threading.Tasks; -using AsbCloudApp.Data; namespace AsbCloudApp.Services { @@ -28,7 +28,7 @@ namespace AsbCloudApp.Services Task InsertRangeAsync(int idWell, IEnumerable wellOperationDtos, CancellationToken token); - Task UpdateAsync(int idWell, int idOperation, WellOperationDto item, + Task UpdateAsync(int idWell, int idOperation, WellOperationDto item, CancellationToken token); Task DeleteAsync(IEnumerable ids, CancellationToken token); diff --git a/AsbCloudDb/Migrations/20210907085527_Init.cs b/AsbCloudDb/Migrations/20210907085527_Init.cs index 8ef9032b..4588fca2 100644 --- a/AsbCloudDb/Migrations/20210907085527_Init.cs +++ b/AsbCloudDb/Migrations/20210907085527_Init.cs @@ -1,8 +1,8 @@ -using System; -using System.Collections.Generic; -using AsbCloudDb.Model; +using AsbCloudDb.Model; using Microsoft.EntityFrameworkCore.Migrations; using Npgsql.EntityFrameworkCore.PostgreSQL.Metadata; +using System; +using System.Collections.Generic; namespace AsbCloudDb.Migrations { diff --git a/AsbCloudDb/Model/AsbCloudDbContext.cs b/AsbCloudDb/Model/AsbCloudDbContext.cs index bdcb8443..7a8c027e 100644 --- a/AsbCloudDb/Model/AsbCloudDbContext.cs +++ b/AsbCloudDb/Model/AsbCloudDbContext.cs @@ -1,10 +1,10 @@ using Microsoft.EntityFrameworkCore; using System; using System.Collections.Generic; +using System.Data.Common; using System.Linq; using System.Threading; using System.Threading.Tasks; -using System.Data.Common; #nullable disable @@ -41,11 +41,15 @@ namespace AsbCloudDb.Model //var context = new AsbCloudDbContext(options); public AsbCloudDbContext() : base() - { } + { + Database.Migrate(); + } public AsbCloudDbContext(DbContextOptions options) : base(options) - { } + { + Database.Migrate(); + } protected override void OnConfiguring(DbContextOptionsBuilder optionsBuilder) { @@ -290,7 +294,8 @@ namespace AsbCloudDb.Model }); }); - modelBuilder.Entity(entity => { + modelBuilder.Entity(entity => + { entity.HasData(new List{ new WellSectionType{ Id = 1, Caption = "Пилотный ствол"}, new WellSectionType{ Id = 2, Caption = "Направление"}, @@ -301,15 +306,17 @@ namespace AsbCloudDb.Model }); }); - modelBuilder.Entity(entity => { - entity.HasData(new List { + modelBuilder.Entity(entity => + { + entity.HasData(new List { new WellType{ Id = 1, Caption = "Наклонно-направленная"}, new WellType{ Id = 2, Caption = "Горизонтальная"}, }); }); - modelBuilder.Entity(entity => { - entity.HasData(new List { + modelBuilder.Entity(entity => + { + entity.HasData(new List { new MeasureCategory{ Id = 1, Name = "Показатели бурового раствора", ShortName = "Раствор"}, new MeasureCategory{ Id = 2, Name = "Шламограмма", ShortName = "Шламограмма"}, new MeasureCategory{ Id = 3, Name = "ННБ", ShortName = "ННБ"}, diff --git a/AsbCloudDb/Model/IIdWell.cs b/AsbCloudDb/Model/IIdWell.cs index 322915da..d800f74b 100644 --- a/AsbCloudDb/Model/IIdWell.cs +++ b/AsbCloudDb/Model/IIdWell.cs @@ -1,10 +1,4 @@ -using System; -using System.Collections.Generic; -using System.Linq; -using System.Text; -using System.Threading.Tasks; - -namespace AsbCloudDb.Model +namespace AsbCloudDb.Model { /// /// For well related entities diff --git a/AsbCloudDb/Model/RelationCompanyWell.cs b/AsbCloudDb/Model/RelationCompanyWell.cs index d1f535af..e7f5c0b5 100644 --- a/AsbCloudDb/Model/RelationCompanyWell.cs +++ b/AsbCloudDb/Model/RelationCompanyWell.cs @@ -6,7 +6,7 @@ using System.ComponentModel.DataAnnotations.Schema; namespace AsbCloudDb.Model { [Table("t_relation_company_well"), Comment("отношение скважин и компаний")] - public partial class RelationCompanyWell: IIdWell + public partial class RelationCompanyWell : IIdWell { [Column("id_well")] public int IdWell { get; set; } diff --git a/AsbCloudDb/Model/ReportProperty.cs b/AsbCloudDb/Model/ReportProperty.cs index d6c5c354..2f6904f3 100644 --- a/AsbCloudDb/Model/ReportProperty.cs +++ b/AsbCloudDb/Model/ReportProperty.cs @@ -6,7 +6,7 @@ using System.ComponentModel.DataAnnotations.Schema; namespace AsbCloudDb.Model { [Table("t_report_property"), Comment("Отчеты с данными по буровым")] - public class ReportProperty: IId, IIdWell + public class ReportProperty : IId, IIdWell { [Key] [Column("id")] diff --git a/AsbCloudDb/Model/Well.cs b/AsbCloudDb/Model/Well.cs index bdadc398..e5f32af2 100644 --- a/AsbCloudDb/Model/Well.cs +++ b/AsbCloudDb/Model/Well.cs @@ -1,5 +1,4 @@ using Microsoft.EntityFrameworkCore; -using System; using System.Collections.Generic; using System.ComponentModel.DataAnnotations; using System.ComponentModel.DataAnnotations.Schema; diff --git a/AsbCloudDb/Model/WellOperation.cs b/AsbCloudDb/Model/WellOperation.cs index 1856c638..06ca3d49 100644 --- a/AsbCloudDb/Model/WellOperation.cs +++ b/AsbCloudDb/Model/WellOperation.cs @@ -1,5 +1,5 @@ -using System; -using Microsoft.EntityFrameworkCore; +using Microsoft.EntityFrameworkCore; +using System; using System.ComponentModel.DataAnnotations; using System.ComponentModel.DataAnnotations.Schema; using System.Text.Json.Serialization; diff --git a/AsbCloudDb/Readme.md b/AsbCloudDb/Readme.md new file mode 100644 index 00000000..107bb2dd --- /dev/null +++ b/AsbCloudDb/Readme.md @@ -0,0 +1,4 @@ +Применить миграции +``` + dotnet ef database update --project .\AsbCloudDb + ``` \ No newline at end of file diff --git a/AsbCloudDbDemoData/Program.cs b/AsbCloudDbDemoData/Program.cs index eb7a64ba..33f07197 100644 --- a/AsbCloudDbDemoData/Program.cs +++ b/AsbCloudDbDemoData/Program.cs @@ -1,7 +1,7 @@ -using System; -using System.Collections.Generic; +using AsbCloudDb.Model; using Microsoft.EntityFrameworkCore; -using AsbCloudDb.Model; +using System; +using System.Collections.Generic; namespace AsbCloudDbDemoData { @@ -12,7 +12,7 @@ namespace AsbCloudDbDemoData Console.WriteLine("Заполнить БД тестовыми данными? y/n"); string result = Console.ReadLine(); - if(result != "y") + if (result != "y") { Console.WriteLine("Хорошо, в другой раз."); return; @@ -299,7 +299,7 @@ namespace AsbCloudDbDemoData else Console.WriteLine("Ошибка при добавлении данных"); } - catch(Exception ex) + catch (Exception ex) { Console.WriteLine(ex.Message); } diff --git a/AsbCloudInfrastructure/DependencyInjection.cs b/AsbCloudInfrastructure/DependencyInjection.cs index a2c91224..a3d1c356 100644 --- a/AsbCloudInfrastructure/DependencyInjection.cs +++ b/AsbCloudInfrastructure/DependencyInjection.cs @@ -41,6 +41,15 @@ namespace AsbCloudInfrastructure services.AddTransient(); services.AddTransient(); + // admin crud services: + services.AddTransient, CrudServiceBase>(); + services.AddTransient, CrudServiceBase>(); + services.AddTransient, CrudServiceBase>(); + services.AddTransient, CrudServiceBase>(); + services.AddTransient, CrudServiceBase>(); + services.AddTransient, CrudServiceBase>(); + services.AddTransient, CrudServiceBase>(); + return services; } } diff --git a/AsbCloudInfrastructure/Helper.cs b/AsbCloudInfrastructure/Helper.cs index 29bcabdc..01fc97b5 100644 --- a/AsbCloudInfrastructure/Helper.cs +++ b/AsbCloudInfrastructure/Helper.cs @@ -1,6 +1,4 @@ using System; -using System.Collections.Generic; -using System.Linq; namespace AsbCloudInfrastructure { diff --git a/AsbCloudInfrastructure/MapsterExtension.cs b/AsbCloudInfrastructure/MapsterExtension.cs index 93c991ff..d0ee8141 100644 --- a/AsbCloudInfrastructure/MapsterExtension.cs +++ b/AsbCloudInfrastructure/MapsterExtension.cs @@ -37,7 +37,7 @@ namespace Mapster eachAfterMapAction(dest, item); yield return dest; } - + } } } diff --git a/AsbCloudInfrastructure/Services/Cache/CacheTable.cs b/AsbCloudInfrastructure/Services/Cache/CacheTable.cs index f7ee38bc..20a3acba 100644 --- a/AsbCloudInfrastructure/Services/Cache/CacheTable.cs +++ b/AsbCloudInfrastructure/Services/Cache/CacheTable.cs @@ -36,9 +36,9 @@ namespace AsbCloudInfrastructure.Services.Cache catch { // ignore - // TODO: figure out what the well + // TODO: figure out what the hell } - + } var dbEntities = context.Set().AsNoTracking().ToList(); cached.AddRange(dbEntities); diff --git a/AsbCloudInfrastructure/Services/CrudServiceBase.cs b/AsbCloudInfrastructure/Services/CrudServiceBase.cs index f7e4fed5..c8e7f69c 100644 --- a/AsbCloudInfrastructure/Services/CrudServiceBase.cs +++ b/AsbCloudInfrastructure/Services/CrudServiceBase.cs @@ -1,7 +1,9 @@ -using AsbCloudApp.Services; +using AsbCloudApp.Data; +using AsbCloudApp.Services; using AsbCloudDb.Model; using Mapster; using Microsoft.EntityFrameworkCore; +using System; using System.Collections.Generic; using System.Linq; using System.Threading; @@ -16,46 +18,79 @@ namespace AsbCloudInfrastructure.Services protected readonly IAsbCloudDbContext context; protected readonly DbSet dbSet; + public List Incledes { get; } = new List(); + public CrudServiceBase(IAsbCloudDbContext context) { this.context = context; dbSet = context.Set(); } - public virtual async Task GetAsync(int id, CancellationToken token = default) + public virtual async Task> GetPageAsync(int skip = 0, int take = 32, CancellationToken token = default) { - var entity = await dbSet.AsNoTracking() - .FirstOrDefaultAsync(e => e.Id == id, token).ConfigureAwait(false); - var dto = entity.Adapt(); + var query = GetQueryWithIncludes(); + var count = await query + .CountAsync(token) + .ConfigureAwait(false); + + var container = new PaginationContainer + { + Skip = skip, + Take = take, + Count = count, + }; + + if (skip >= count) + return container; + + var entities = await query + .ToListAsync(token) + .ConfigureAwait(false); + + container.Items = entities + .Select(entity => Convert(entity)) + .ToList(); + + return container; + } + + public virtual async Task> GetAllAsync(CancellationToken token = default) + { + var query = GetQueryWithIncludes(); + var entities = await query + .ToListAsync(token).ConfigureAwait(false); + var dto = entities.Select(entity => Convert(entity)); return dto; } - public virtual async Task InsertAsync(Tdto newItem, CancellationToken token = default) + public virtual async Task GetAsync(int id, CancellationToken token = default) { - var newEntity = newItem.Adapt(); - var dbEntity = dbSet.Add(newEntity); - await context.SaveChangesAsync(token).ConfigureAwait(false); - return dbEntity.Entity.Adapt(); + var query = GetQueryWithIncludes(); + var entity = await query + .FirstOrDefaultAsync(e => e.Id == id, token).ConfigureAwait(false); + var dto = Convert(entity); + return dto; } - public virtual async Task> InsertRangeAsync(IEnumerable newItems, CancellationToken token = default) + public virtual Task InsertAsync(Tdto item, CancellationToken token = default) { - var newEntities = newItems.Adapt>(); - var dbEntities = new Microsoft.EntityFrameworkCore.ChangeTracking.EntityEntry[newItems.Count()]; - - for (int i = 0; i < dbEntities.Length; i++) - dbEntities[i] = dbSet.Add(newEntities.ElementAt(i)); - - await context.SaveChangesAsync(token).ConfigureAwait(false); - return dbEntities.Select(e => e.Entity.Adapt()); + var entity = Convert(item); + dbSet.Add(entity); + return context.SaveChangesAsync(token); } - public virtual async Task UpdateAsync(Tdto item, CancellationToken token = default) + public virtual Task InsertRangeAsync(IEnumerable items, CancellationToken token = default) { - var newEntity = item.Adapt(); - var dbEntity = dbSet.Update(newEntity); - await context.SaveChangesAsync(token).ConfigureAwait(false); - return dbEntity.Entity.Adapt(); + var entities = items.Select(i => Convert(i)); + dbSet.AddRange(entities); + return context.SaveChangesAsync(token); + } + + public virtual Task UpdateAsync(int id, Tdto item, CancellationToken token = default) + { + var entity = Convert(item); + dbSet.Update(entity); + return context.SaveChangesAsync(token); } public virtual Task DeleteAsync(int id, CancellationToken token = default) @@ -76,5 +111,17 @@ namespace AsbCloudInfrastructure.Services dbSet.RemoveRange(entities); return context.SaveChangesAsync(token); } + + public virtual Tdto Convert(TModel src) => src.Adapt(); + + public virtual TModel Convert(Tdto src) => src.Adapt(); + + private IQueryable GetQueryWithIncludes() + { + IQueryable query = dbSet; + foreach (var include in Incledes) + query = query.Include(include); + return query; + } } } diff --git a/AsbCloudInfrastructure/Services/DrillingProgramService.cs b/AsbCloudInfrastructure/Services/DrillingProgramService.cs index d5e9843b..7197c4e9 100644 --- a/AsbCloudInfrastructure/Services/DrillingProgramService.cs +++ b/AsbCloudInfrastructure/Services/DrillingProgramService.cs @@ -1,15 +1,12 @@ -using System; +using AsbCloudApp.Data; +using AsbCloudApp.Services; +using NPOI.SS.UserModel; +using NPOI.XSSF.UserModel; using System.Collections.Generic; using System.IO; using System.Linq; using System.Threading; using System.Threading.Tasks; -using AsbCloudApp.Data; -using AsbCloudApp.Services; -using AsbCloudDb.Model; -using Microsoft.EntityFrameworkCore; -using NPOI.SS.UserModel; -using NPOI.XSSF.UserModel; namespace AsbCloudInfrastructure.Services { @@ -39,7 +36,8 @@ namespace AsbCloudInfrastructure.Services var matchFiles = await fileService.GetInfosByCategoryAsync(idWell, idFileCategoryDrillingProgram, token) .ConfigureAwait(false); - if (matchFiles is not null && matchFiles.Any()) { + if (matchFiles is not null && matchFiles.Any()) + { matchFiles = matchFiles.OrderByDescending(f => f.UploadDate); var matchFilesIterator = matchFiles.GetEnumerator(); matchFilesIterator.MoveNext(); diff --git a/AsbCloudInfrastructure/Services/ICachedCrudService.cs b/AsbCloudInfrastructure/Services/ICachedCrudService.cs new file mode 100644 index 00000000..1e6043d4 --- /dev/null +++ b/AsbCloudInfrastructure/Services/ICachedCrudService.cs @@ -0,0 +1,8 @@ +using AsbCloudApp.Data; + +namespace AsbCloudInfrastructure.Services +{ + public interface ICachedCrudService where Tdto : IId + { + } +} \ No newline at end of file diff --git a/AsbCloudInfrastructure/Services/MeasureService.cs b/AsbCloudInfrastructure/Services/MeasureService.cs index 8d86d07a..7735f3fe 100644 --- a/AsbCloudInfrastructure/Services/MeasureService.cs +++ b/AsbCloudInfrastructure/Services/MeasureService.cs @@ -26,7 +26,7 @@ namespace AsbCloudInfrastructure.Services public async Task> GetCategoriesAsync(CancellationToken token) { var entities = await cacheCategories.WhereAsync(token).ConfigureAwait(false); - var dto = entities.ToDictionary(e=>e.Id, e=>e.Name); + var dto = entities.ToDictionary(e => e.Id, e => e.Name); return dto; } diff --git a/AsbCloudInfrastructure/Services/WellOperationsStatService.cs b/AsbCloudInfrastructure/Services/WellOperationsStatService.cs index 23f6345f..a6dc0df7 100644 --- a/AsbCloudInfrastructure/Services/WellOperationsStatService.cs +++ b/AsbCloudInfrastructure/Services/WellOperationsStatService.cs @@ -2,13 +2,13 @@ using AsbCloudApp.Services; using AsbCloudDb.Model; using AsbCloudInfrastructure.Services.Cache; +using Mapster; using Microsoft.EntityFrameworkCore; using System; using System.Collections.Generic; using System.Linq; using System.Threading; using System.Threading.Tasks; -using Mapster; namespace AsbCloudInfrastructure.Services { @@ -138,7 +138,8 @@ namespace AsbCloudInfrastructure.Services if (!wellOperations.Any()) return new StatWellDto() - { + { + Id = idWell, Caption = well.Caption, WellType = wellType.Caption }; @@ -174,7 +175,7 @@ namespace AsbCloudInfrastructure.Services StatSectionDto section = new StatSectionDto { Id = id, - Caption = sectionType.Caption, + Caption = sectionType.Caption, Plan = CalcSectionStat(operationsPlan, id), Fact = CalcSectionStat(operationsFact, id), }; @@ -201,7 +202,7 @@ namespace AsbCloudInfrastructure.Services .Where(o => o.IdWellSectionType == idSectionType) .OrderBy(o => o.Start) .ThenBy(o => o.WellDepth); - + return CalcStat(sectionOperations); } @@ -329,7 +330,7 @@ namespace AsbCloudInfrastructure.Services foreach (var race in races) { dDepth += race.EndWellDepth; - for (var i = race.Operations.Count -1; i > 0 ; i--) + for (var i = race.Operations.Count - 1; i > 0; i--) { if (race.Operations[i].IdCategory == idOperationBhaUp) dHours += race.Operations[i].Hours; @@ -468,8 +469,8 @@ namespace AsbCloudInfrastructure.Services return m; } - private static readonly Action WellOperationDtoMutation = (WellOperationDto dest, WellOperation source) => - { + private static readonly Action WellOperationDtoMutation = (WellOperationDto dest, WellOperation source) => + { dest.CategoryName = source.OperationCategory?.Name; dest.WellSectionTypeName = source.WellSectionType?.Caption; }; diff --git a/AsbCloudInfrastructure/Services/WellService.cs b/AsbCloudInfrastructure/Services/WellService.cs index 75edf50a..a6382be0 100644 --- a/AsbCloudInfrastructure/Services/WellService.cs +++ b/AsbCloudInfrastructure/Services/WellService.cs @@ -80,8 +80,8 @@ namespace AsbCloudInfrastructure.Services public async Task GetAsync(int idWell, CancellationToken token) { var entity = await db.Wells - .Include(w=>w.Cluster) - .ThenInclude(c=>c.Deposit) + .Include(w => w.Cluster) + .ThenInclude(c => c.Deposit) .FirstOrDefaultAsync(w => w.Id == idWell, token) .ConfigureAwait(false); @@ -100,8 +100,8 @@ namespace AsbCloudInfrastructure.Services public async Task> GetCompaniesAsync(int idWell, CancellationToken token) { var well = await db.Wells - .Include(w=>w.RelationCompaniesWells) - .ThenInclude(r=>r.Company) + .Include(w => w.RelationCompaniesWells) + .ThenInclude(r => r.Company) .FirstOrDefaultAsync(w => w.Id == idWell, token) .ConfigureAwait(false); var companies = well.RelationCompaniesWells.Select(r => r.Company); diff --git a/AsbCloudInfrastructure/Services/_CachedCrudService.cs b/AsbCloudInfrastructure/Services/_CachedCrudService.cs new file mode 100644 index 00000000..8571e201 --- /dev/null +++ b/AsbCloudInfrastructure/Services/_CachedCrudService.cs @@ -0,0 +1,47 @@ +using AsbCloudApp.Data; +using AsbCloudDb.Model; +using AsbCloudInfrastructure.Services.Cache; +using Mapster; +using Microsoft.EntityFrameworkCore; +using System; +using System.Linq; +using System.Threading; +using System.Threading.Tasks; + +namespace AsbCloudInfrastructure.Services +{ + public class _CachedCrudService : ICachedCrudService + where TModel : class, AsbCloudDb.Model.IId + where Tdto : AsbCloudApp.Data.IId + { + private readonly CacheTable cache; + + public _CachedCrudService(IAsbCloudDbContext db, Cache.CacheDb cacheDb) + { + cache = cacheDb.GetCachedTable((DbContext)db); + } + + public virtual async Task> GetAsync(int skip = 0, int take = 32, CancellationToken token = default) + { + var count = cache.Count(); + var result = new PaginationContainer { Skip = skip, Take = take, Count = count }; + if (count <= skip) + return result; + + var items = await cache.WhereAsync(token).ConfigureAwait(false); + + result.Items.AddRange(items.OrderBy(i => i.Id).Skip(skip).Take(take).Select(i => Convert(i))); + + return result; + } + + public virtual Task GetAsync(int id, CancellationToken token = default) + { + throw new NotImplementedException(); + } + + public virtual Tdto Convert(TModel src) => src.Adapt(); + + public virtual TModel Convert(Tdto src) => src.Adapt(); + } +} diff --git a/AsbCloudWebApi/Controllers/AdminClusterController.cs b/AsbCloudWebApi/Controllers/AdminClusterController.cs new file mode 100644 index 00000000..7f2dd0e1 --- /dev/null +++ b/AsbCloudWebApi/Controllers/AdminClusterController.cs @@ -0,0 +1,19 @@ +using AsbCloudApp.Data; +using AsbCloudApp.Services; +using Microsoft.AspNetCore.Authorization; +using Microsoft.AspNetCore.Mvc; + +namespace AsbCloudWebApi.Controllers +{ + [Route("api/admin/cluster")] + [ApiController] + [Authorize] + public class AdminClusterController : CrudController> + { + public AdminClusterController(ICrudService service) + :base(service) + { + service.Incledes.Add("Wells"); + } + } +} diff --git a/AsbCloudWebApi/Controllers/AdminCompanyController.cs b/AsbCloudWebApi/Controllers/AdminCompanyController.cs new file mode 100644 index 00000000..b412120c --- /dev/null +++ b/AsbCloudWebApi/Controllers/AdminCompanyController.cs @@ -0,0 +1,19 @@ +using AsbCloudApp.Data; +using AsbCloudApp.Services; +using Microsoft.AspNetCore.Authorization; +using Microsoft.AspNetCore.Mvc; + +namespace AsbCloudWebApi.Controllers +{ + [Route("api/admin/company")] + [ApiController] + [Authorize] + public class AdminCompanyController : CrudController> + { + public AdminCompanyController(ICrudService service) + : base(service) + { + + } + } +} diff --git a/AsbCloudWebApi/Controllers/AdminDepositController.cs b/AsbCloudWebApi/Controllers/AdminDepositController.cs new file mode 100644 index 00000000..b4b4713c --- /dev/null +++ b/AsbCloudWebApi/Controllers/AdminDepositController.cs @@ -0,0 +1,19 @@ +using AsbCloudApp.Data; +using AsbCloudApp.Services; +using Microsoft.AspNetCore.Authorization; +using Microsoft.AspNetCore.Mvc; + +namespace AsbCloudWebApi.Controllers +{ + [Route("api/admin/deposit")] + [ApiController] + [Authorize] + public class AdminDepositController : CrudController> + { + public AdminDepositController(ICrudService service) + :base(service) + { + service.Incledes.Add("Clusters"); + } + } +} diff --git a/AsbCloudWebApi/Controllers/AdminTelemetryController.cs b/AsbCloudWebApi/Controllers/AdminTelemetryController.cs new file mode 100644 index 00000000..f6ade1af --- /dev/null +++ b/AsbCloudWebApi/Controllers/AdminTelemetryController.cs @@ -0,0 +1,19 @@ +using AsbCloudApp.Data; +using AsbCloudApp.Services; +using Microsoft.AspNetCore.Authorization; +using Microsoft.AspNetCore.Mvc; + +namespace AsbCloudWebApi.Controllers +{ + [Route("api/admin/telemetry")] + [ApiController] + [Authorize] + public class AdminTelemetryController : CrudController> + { + public AdminTelemetryController(ICrudService service) + : base(service) + { + service.Incledes.Add("Well"); + } + } +} diff --git a/AsbCloudWebApi/Controllers/AdminUserController.cs b/AsbCloudWebApi/Controllers/AdminUserController.cs new file mode 100644 index 00000000..6ed2b550 --- /dev/null +++ b/AsbCloudWebApi/Controllers/AdminUserController.cs @@ -0,0 +1,19 @@ +using AsbCloudApp.Data; +using AsbCloudApp.Services; +using Microsoft.AspNetCore.Authorization; +using Microsoft.AspNetCore.Mvc; + +namespace AsbCloudWebApi.Controllers +{ + [Route("api/admin/user")] + [ApiController] + [Authorize] + public class AdminUserController : CrudController> + { + public AdminUserController(ICrudService service) + : base(service) + { + + } + } +} diff --git a/AsbCloudWebApi/Controllers/AdminUserRoleController.cs b/AsbCloudWebApi/Controllers/AdminUserRoleController.cs new file mode 100644 index 00000000..62e2f546 --- /dev/null +++ b/AsbCloudWebApi/Controllers/AdminUserRoleController.cs @@ -0,0 +1,19 @@ +using AsbCloudApp.Data; +using AsbCloudApp.Services; +using Microsoft.AspNetCore.Authorization; +using Microsoft.AspNetCore.Mvc; + +namespace AsbCloudWebApi.Controllers +{ + [Route("api/admin/user/role")] + [ApiController] + [Authorize] + public class AdminUserRoleController : CrudController> + { + public AdminUserRoleController(ICrudService service) + : base(service) + { + + } + } +} diff --git a/AsbCloudWebApi/Controllers/AdminWellController.cs b/AsbCloudWebApi/Controllers/AdminWellController.cs new file mode 100644 index 00000000..1f7d9d17 --- /dev/null +++ b/AsbCloudWebApi/Controllers/AdminWellController.cs @@ -0,0 +1,19 @@ +using AsbCloudApp.Data; +using AsbCloudApp.Services; +using Microsoft.AspNetCore.Authorization; +using Microsoft.AspNetCore.Mvc; + +namespace AsbCloudWebApi.Controllers +{ + [Route("api/admin/well")] + [ApiController] + [Authorize] + public class AdminWellController : CrudController> + { + public AdminWellController(ICrudService service) + :base(service) + { + service.Incledes.Add("Telemetry"); + } + } +} diff --git a/AsbCloudWebApi/Controllers/CrudController.cs b/AsbCloudWebApi/Controllers/CrudController.cs index 1ba261c5..06bef091 100644 --- a/AsbCloudWebApi/Controllers/CrudController.cs +++ b/AsbCloudWebApi/Controllers/CrudController.cs @@ -1,54 +1,114 @@ using AsbCloudApp.Data; using AsbCloudApp.Services; +using Microsoft.AspNetCore.Authorization; using Microsoft.AspNetCore.Mvc; using System.Threading; +using System.Linq; +using System.Threading.Tasks; +using System.Collections.Generic; // For more information on enabling Web API for empty projects, visit https://go.microsoft.com/fwlink/?LinkID=397860 - namespace AsbCloudWebApi.Controllers { + /// + /// CRUD контроллер для админки. + /// + /// + /// [ApiController] + [Authorize] public abstract class CrudController : ControllerBase where T : IId where TService : ICrudService { protected readonly TService service; + public List Roles { get; } = new List { "Администратор" }; + public CrudController(TService service) { this.service = service; } - // GET api//5 + /// + /// Получить страницу с записями в PaginationContainer + /// + /// пропустить skip записей + /// получить take записей + /// CancellationToken + /// страница с записями в PaginationContainer + [HttpGet()] + public virtual async Task GetPage(int skip = 0, int take = 32, CancellationToken token = default) + { + if (!Roles.Any(role => User.IsInRole(role))) + return Forbid(); + + var result = await service.GetPageAsync(skip, take, token).ConfigureAwait(false); + return Ok(result); + } + + /// + /// Получить одну запись по Id + /// + /// id записи + /// + /// запись [HttpGet("{id}")] - - public virtual IActionResult Get(int id, CancellationToken token = default) + public virtual async Task Get(int id, CancellationToken token = default) { - var result = service.GetAsync(id, token).ConfigureAwait(false); + if (!Roles.Any(role => User.IsInRole(role))) + return Forbid(); + + var result = await service.GetAsync(id, token).ConfigureAwait(false); return Ok(result); } - // POST api/ + /// + /// Добавить запись + /// + /// запись + /// + /// 1 - добавлено, 0 - нет [HttpPost] - public virtual IActionResult Insert([FromBody] T value, CancellationToken token = default) + public virtual async Task Insert([FromBody] T value, CancellationToken token = default) { - var result = service.InsertAsync(value, token).ConfigureAwait(false); + if (!Roles.Any(role => User.IsInRole(role))) + return Forbid(); + + var result = await service.InsertAsync(value, token).ConfigureAwait(false); return Ok(result); } - // PUT api//5 + /// + /// Редактировать запись по id + /// + /// id записи + /// запись + /// + /// 1 - успешно отредактировано, 0 - нет [HttpPut("{id}")] - public virtual IActionResult Put(int id, [FromBody] T value, CancellationToken token = default) + public virtual async Task Put(int id, [FromBody] T value, CancellationToken token = default) { - var result = service.UpdateAsync(value, token).ConfigureAwait(false); + if (!Roles.Any(role => User.IsInRole(role))) + return Forbid(); + + var result = await service.UpdateAsync(id, value, token).ConfigureAwait(false); return Ok(result); } - // DELETE api//5 + /// + /// Удалить запись по id + /// + /// id записи + /// + /// 1 - успешно удалено, 0 - нет [HttpDelete("{id}")] - public virtual IActionResult Delete(int id, CancellationToken token = default) + public virtual async Task Delete(int id, CancellationToken token = default) { - var result = service.DeleteAsync(id, token).ConfigureAwait(false); + if (!Roles.Any(role => User.IsInRole(role))) + return Forbid(); + + var result = await service.DeleteAsync(id, token).ConfigureAwait(false); return Ok(result); } }