Merge pull request 'Контактная информация по скважине' (#60) from feature/well-contacts into dev

Reviewed-on: http://test.digitaldrilling.ru:8080/DDrilling/AsbCloudServer/pulls/60
This commit is contained in:
Никита Фролов 2023-06-23 13:27:26 +05:00
commit 0594eb84ea
45 changed files with 8553 additions and 19 deletions

View File

@ -1,4 +1,7 @@
using System.ComponentModel.DataAnnotations;
using System.Collections.Generic;
using System.ComponentModel.DataAnnotations;
using System.Linq;
using AsbCloudApp.Data.User;
namespace AsbCloudApp.Data
{
@ -27,5 +30,17 @@ namespace AsbCloudApp.Data
/// </summary>
[StringLength(30, MinimumLength = 1, ErrorMessage = "Допустимое имя типа компании от 1 до 30 символов")]
public string? CompanyTypeCaption { get; set; } = null!;
}
/// <summary>
/// DTO компании с пользователями
/// </summary>
public class CompanyWithUsersDto: CompanyDto
{
/// <summary>
/// Пользователи компании
/// </summary>
public IEnumerable<UserContactDto> Users { get; set; } = Enumerable.Empty<UserContactDto>();
}
}

View File

@ -1,5 +1,6 @@
using System.Collections.Generic;
using System.Linq;
using AsbCloudApp.Data.User;
namespace AsbCloudApp.Data
{

View File

@ -2,6 +2,7 @@
using System.Collections.Generic;
using System.ComponentModel.DataAnnotations;
using System.Linq;
using AsbCloudApp.Data.User;
namespace AsbCloudApp.Data
{

View File

@ -1,5 +1,6 @@
using System;
using System.ComponentModel.DataAnnotations;
using AsbCloudApp.Data.User;
namespace AsbCloudApp.Data
{

View File

@ -1,4 +1,5 @@
using System;
using AsbCloudApp.Data.User;
namespace AsbCloudApp.Data
{

View File

@ -1,5 +1,6 @@
using System;
using System.Collections.Generic;
using AsbCloudApp.Data.User;
namespace AsbCloudApp.Data.SAUB
{

View File

@ -0,0 +1,14 @@
namespace AsbCloudApp.Data.User
{
/// <summary>
/// Пользователь - контакт
/// </summary>
public class UserContactDto : UserDto
{
/// <summary>
/// является ли пользователь контактом
/// </summary>
public bool IsContact { get; set; }
}
}

View File

@ -1,6 +1,6 @@
using System.ComponentModel.DataAnnotations;
namespace AsbCloudApp.Data
namespace AsbCloudApp.Data.User
{
/// <summary>
/// DTO пользователя платформы

View File

@ -1,7 +1,7 @@
using System.Collections.Generic;
using System.Linq;
namespace AsbCloudApp.Data
namespace AsbCloudApp.Data.User
{
/// <inheritdoc/>
public class UserExtendedDto : UserDto

View File

@ -1,6 +1,6 @@
using System.ComponentModel.DataAnnotations;
namespace AsbCloudApp.Data
namespace AsbCloudApp.Data.User
{
/// <inheritdoc/>
public class UserRegistrationDto : UserDto

View File

@ -2,7 +2,7 @@
using System.ComponentModel.DataAnnotations;
using System.Linq;
namespace AsbCloudApp.Data
namespace AsbCloudApp.Data.User
{
/// <summary>
/// Роль пользователя платформы
@ -16,7 +16,7 @@ namespace AsbCloudApp.Data
/// название
/// </summary>
[Required]
[StringLength(50, MinimumLength = 1,ErrorMessage = "Допустимая длина названия роли от 1 до 50 символов")]
[StringLength(50, MinimumLength = 1, ErrorMessage = "Допустимая длина названия роли от 1 до 50 символов")]
public string Caption { get; set; } = null!;
/// <summary>

View File

@ -1,7 +1,7 @@
using System.Collections.Generic;
using System.Linq;
namespace AsbCloudApp.Data
namespace AsbCloudApp.Data.User
{
/// <inheritdoc/>
public class UserTokenDto : UserExtendedDto

View File

@ -1,5 +1,6 @@
using System.Collections.Generic;
using System.Linq;
using AsbCloudApp.Data.User;
namespace AsbCloudApp.Data
{

View File

@ -1,4 +1,5 @@
using AsbCloudApp.Data;
using AsbCloudApp.Data.User;
using AsbCloudApp.Services;
using System.Collections.Generic;

View File

@ -1,8 +1,8 @@
using AsbCloudApp.Data;
using AsbCloudApp.Services;
using AsbCloudApp.Services;
using System.Collections.Generic;
using System.Threading.Tasks;
using System.Threading;
using AsbCloudApp.Data.User;
namespace AsbCloudApp.Repositories
{

View File

@ -1,4 +1,5 @@
using AsbCloudApp.Data;
using AsbCloudApp.Data.User;
using System.Collections.Generic;
using System.Threading;
using System.Threading.Tasks;

View File

@ -1,4 +1,4 @@
using AsbCloudApp.Data;
using AsbCloudApp.Data.User;
using System.Security.Claims;
using System.Threading;
using System.Threading.Tasks;

View File

@ -1,4 +1,5 @@
using AsbCloudApp.Data;
using AsbCloudApp.Data.User;
using System.Collections.Generic;
using System.Threading;
using System.Threading.Tasks;

View File

@ -0,0 +1,39 @@
using AsbCloudApp.Data;
using System.Collections.Generic;
using System.Threading;
using System.Threading.Tasks;
namespace AsbCloudApp.Services
{
/// <summary>
/// Сервис по работе с контактной информацией по скважине
/// </summary>
public interface IWellContactService
{
/// <summary>
/// Полуение пользователей по ключу скважины и типу контакта
/// </summary>
/// <param name="wellId">ключ скважины</param>
/// <param name="contactTypeId">тип контакта</param>
/// <param name="token"></param>
/// <returns></returns>
Task<IEnumerable<CompanyWithUsersDto>> GetAsync(int wellId, int contactTypeId, CancellationToken token);
/// <summary>
/// Получение типов контаков
/// </summary>
/// <param name="token"></param>
/// <returns></returns>
Task<IEnumerable<CompanyTypeDto>> GetTypesAsync(CancellationToken token);
/// <summary>
/// Обновление контактов по ключу скважины, типу контакта и ключам пользователей
/// </summary>
/// <param name="idWell">ключ скважины</param>
/// <param name="contactTypeId">ключ типа контакта</param>
/// <param name="userIds">ключи пользователей</param>
/// <param name="token"></param>
/// <returns></returns>
Task<int> UpdateRangeAsync(int idWell, int contactTypeId, IEnumerable<int> userIds, CancellationToken token);
}
}

View File

@ -0,0 +1,58 @@
using Microsoft.EntityFrameworkCore.Migrations;
#nullable disable
namespace AsbCloudDb.Migrations
{
public partial class Add_Table_Relation_Contact_Well_And_Field_IsContact_To_CompanyType : Migration
{
protected override void Up(MigrationBuilder migrationBuilder)
{
migrationBuilder.AddColumn<bool>(
name: "IsContact",
table: "t_company_type",
type: "boolean",
nullable: false,
defaultValue: false);
migrationBuilder.CreateTable(
name: "t_relation_contact_well",
columns: table => new
{
id_well = table.Column<int>(type: "integer", nullable: false),
id_user = table.Column<int>(type: "integer", nullable: false)
},
constraints: table =>
{
table.PrimaryKey("PK_t_relation_contact_well", x => new { x.id_well, x.id_user });
table.ForeignKey(
name: "FK_t_relation_contact_well_t_user_id_user",
column: x => x.id_user,
principalTable: "t_user",
principalColumn: "id",
onDelete: ReferentialAction.Cascade);
table.ForeignKey(
name: "FK_t_relation_contact_well_t_well_id_well",
column: x => x.id_well,
principalTable: "t_well",
principalColumn: "id",
onDelete: ReferentialAction.Cascade);
});
migrationBuilder.CreateIndex(
name: "IX_t_relation_contact_well_id_user",
table: "t_relation_contact_well",
column: "id_user");
}
protected override void Down(MigrationBuilder migrationBuilder)
{
migrationBuilder.DropTable(
name: "t_relation_contact_well");
migrationBuilder.DropColumn(
name: "IsContact",
table: "t_company_type");
}
}
}

View File

@ -119,6 +119,9 @@ namespace AsbCloudDb.Migrations
.HasColumnType("character varying(255)")
.HasColumnName("caption");
b.Property<bool>("IsContact")
.HasColumnType("boolean");
b.HasKey("Id");
b.ToTable("t_company_type");
@ -127,17 +130,20 @@ namespace AsbCloudDb.Migrations
new
{
Id = 1,
Caption = "Недрапользователь"
Caption = "Недрапользователь",
IsContact = false
},
new
{
Id = 2,
Caption = "Буровой подрядчик"
Caption = "Буровой подрядчик",
IsContact = false
},
new
{
Id = 3,
Caption = "Сервис автоматизации бурения"
Caption = "Сервис автоматизации бурения",
IsContact = false
});
});
@ -2253,6 +2259,23 @@ namespace AsbCloudDb.Migrations
b.HasComment("отношение скважин и компаний");
});
modelBuilder.Entity("AsbCloudDb.Model.RelationContactWell", b =>
{
b.Property<int>("IdWell")
.HasColumnType("integer")
.HasColumnName("id_well");
b.Property<int>("IdUser")
.HasColumnType("integer")
.HasColumnName("id_user");
b.HasKey("IdWell", "IdUser");
b.HasIndex("IdUser");
b.ToTable("t_relation_contact_well");
});
modelBuilder.Entity("AsbCloudDb.Model.RelationUserDrillingProgramPart", b =>
{
b.Property<int>("IdUser")
@ -7525,6 +7548,25 @@ namespace AsbCloudDb.Migrations
b.Navigation("Well");
});
modelBuilder.Entity("AsbCloudDb.Model.RelationContactWell", b =>
{
b.HasOne("AsbCloudDb.Model.User", "User")
.WithMany("RelationContactsWells")
.HasForeignKey("IdUser")
.OnDelete(DeleteBehavior.Cascade)
.IsRequired();
b.HasOne("AsbCloudDb.Model.Well", "Well")
.WithMany()
.HasForeignKey("IdWell")
.OnDelete(DeleteBehavior.Cascade)
.IsRequired();
b.Navigation("User");
b.Navigation("Well");
});
modelBuilder.Entity("AsbCloudDb.Model.RelationUserDrillingProgramPart", b =>
{
b.HasOne("AsbCloudDb.Model.DrillingProgramPart", "DrillingProgramPart")
@ -8046,6 +8088,8 @@ namespace AsbCloudDb.Migrations
b.Navigation("Files");
b.Navigation("RelationContactsWells");
b.Navigation("RelationUsersUserRoles");
});

View File

@ -28,6 +28,7 @@ namespace AsbCloudDb.Model
public virtual DbSet<RelationUserRolePermission> RelationUserRolePermissions => Set<RelationUserRolePermission>();
public virtual DbSet<RelationUserRoleUserRole> RelationUserRoleUserRoles => Set<RelationUserRoleUserRole>();
public virtual DbSet<RelationUserUserRole> RelationUserUserRoles => Set<RelationUserUserRole>();
public virtual DbSet<RelationContactWell> RelationContactsWells => Set<RelationContactWell>();
public virtual DbSet<ReportProperty> ReportProperties => Set<ReportProperty>();
public virtual DbSet<SetpointsRequest> SetpointsRequests => Set<SetpointsRequest>();
public virtual DbSet<Subsystem> Subsystems => Set<Subsystem>();
@ -379,6 +380,11 @@ namespace AsbCloudDb.Model
.HasName("t_well_final_documents_pk");
});
modelBuilder.Entity<RelationContactWell>(entity =>
{
entity.HasKey(x => new { x.IdWell, x.IdUser });
});
DefaultData.DefaultContextData.Fill(modelBuilder);
}

View File

@ -14,6 +14,7 @@ namespace AsbCloudDb.Model
[Column("caption")]
[StringLength(255)]
public string Caption { get; set; } = null!;
public bool IsContact { get; set; }
[InverseProperty(nameof(Company.CompanyType))]
public virtual ICollection<Company> Companies { get; set; } = null!;

View File

@ -13,6 +13,7 @@ namespace AsbCloudDb.Model
{
DbSet<Cluster> Clusters { get; }
DbSet<Company> Companies { get; }
DbSet<CompanyType> CompaniesTypes { get; }
DbSet<DailyReport.DailyReport> DailyReports { get; }
DbSet<Deposit> Deposits { get; }
DbSet<DetectedOperation> DetectedOperations { get; }
@ -30,6 +31,7 @@ namespace AsbCloudDb.Model
DbSet<RelationUserRolePermission> RelationUserRolePermissions { get; }
DbSet<RelationUserUserRole> RelationUserUserRoles { get; }
DbSet<RelationUserRoleUserRole> RelationUserRoleUserRoles { get; }
DbSet<RelationContactWell> RelationContactsWells { get; }
DbSet<ReportProperty> ReportProperties { get; }
DbSet<Subsystem> Subsystems { get; }
DbSet<SubsystemOperationTime> SubsystemOperationTimes { get; }
@ -58,7 +60,6 @@ namespace AsbCloudDb.Model
DbSet<LimitingParameter> LimitingParameter { get; }
DbSet<TelemetryWirelineRunOut> TelemetryWirelineRunOut { get; }
DbSet<Faq> Faqs { get; }
DbSet<Record1> Record1 { get; }
DbSet<Record7> Record7 { get; }
DbSet<Record8> Record8 { get; }

View File

@ -0,0 +1,20 @@
using System.ComponentModel.DataAnnotations.Schema;
namespace AsbCloudDb.Model
{
[Table("t_relation_contact_well")]
public partial class RelationContactWell
{
[Column("id_well")]
public int IdWell { get; set; }
[Column("id_user")]
public int IdUser { get; set; }
[ForeignKey(nameof(IdUser))]
public virtual User User { get; set; } = null!;
[ForeignKey(nameof(IdWell))]
public virtual Well Well { get; set; } = null!;
}
}

View File

@ -57,6 +57,8 @@ namespace AsbCloudDb.Model
[InverseProperty(nameof(RelationUserUserRole.User))]
public virtual ICollection<RelationUserUserRole> RelationUsersUserRoles { get; set; } = null!;
public virtual ICollection<RelationContactWell> RelationContactsWells { get; set; } = null!;
[InverseProperty(nameof(FileInfo.Author))]
public virtual ICollection<FileInfo> Files { get; set; } = null!;

View File

@ -180,6 +180,7 @@ namespace AsbCloudInfrastructure
services.AddTransient<ITrajectoryPlanRepository, TrajectoryPlanRepository>();
services.AddTransient<ITrajectoryFactRepository, TrajectoryFactRepository>();
services.AddTransient<IFaqRepository, FaqRepository>();
services.AddTransient<IWellContactService, WellContactService>();
// Subsystem service
services.AddTransient<ICrudRepository<SubsystemDto>, CrudCacheRepositoryBase<SubsystemDto, Subsystem>>();

View File

@ -10,6 +10,7 @@ using System.Linq;
using System.Threading;
using System.Threading.Tasks;
using Microsoft.Extensions.Caching.Memory;
using AsbCloudApp.Data.User;
namespace AsbCloudInfrastructure.Repository
{

View File

@ -1,5 +1,6 @@
using AsbCloudApp.Comparators;
using AsbCloudApp.Data;
using AsbCloudApp.Data.User;
using AsbCloudApp.Exceptions;
using AsbCloudApp.Repositories;
using AsbCloudDb;

View File

@ -1,4 +1,5 @@
using AsbCloudApp.Data;
using AsbCloudApp.Data.User;
using AsbCloudApp.Exceptions;
using AsbCloudApp.Repositories;
using AsbCloudApp.Requests;

View File

@ -1,4 +1,4 @@
using AsbCloudApp.Data;
using AsbCloudApp.Data.User;
using AsbCloudApp.Repositories;
using AsbCloudApp.Services;
using AsbCloudDb.Model;

View File

@ -1,5 +1,6 @@
using AsbCloudApp.Data;
using AsbCloudApp.Data.DailyReport;
using AsbCloudApp.Data.User;
using AsbCloudApp.Exceptions;
using AsbCloudApp.Repositories;
using AsbCloudApp.Requests;

View File

@ -1,4 +1,5 @@
using AsbCloudApp.Data;
using AsbCloudApp.Data.User;
using AsbCloudApp.Exceptions;
using AsbCloudApp.Repositories;
using AsbCloudApp.Services;

View File

@ -0,0 +1,84 @@
using AsbCloudApp.Data;
using AsbCloudApp.Data.User;
using AsbCloudApp.Services;
using AsbCloudDb.Model;
using Mapster;
using Microsoft.EntityFrameworkCore;
using System.Collections.Generic;
using System.Linq;
using System.Threading;
using System.Threading.Tasks;
namespace AsbCloudInfrastructure.Services
{
public class WellContactService : IWellContactService
{
private readonly IAsbCloudDbContext db;
public WellContactService(IAsbCloudDbContext db)
{
this.db = db;
}
public async Task<IEnumerable<CompanyWithUsersDto>> GetAsync(int wellId, int contactTypeId, CancellationToken token)
{
var query = db.Companies
.Where(c => c.IdCompanyType == contactTypeId)
.Where(c => c.RelationCompaniesWells.Any(rc => rc.IdWell == wellId))
.Select(c => new CompanyWithUsersDto()
{
Caption = c.Caption,
Id = c.Id,
Users = c.Users.Select(u => new UserContactDto()
{
Id = u.Id,
Name = u.Name,
Patronymic = u.Patronymic,
Surname = u.Surname,
Company = u.Company.Adapt<CompanyDto>(),
Email = u.Email,
Phone = u.Phone,
Position = u.Position,
IsContact = u.RelationContactsWells.Any(rel => rel.IdWell == wellId)
})
});
var entities = await query.AsNoTracking()
.ToArrayAsync(token)
.ConfigureAwait(false);
return entities;
}
public async Task<IEnumerable<CompanyTypeDto>> GetTypesAsync(CancellationToken token)
{
var query = db.CompaniesTypes.Where(t => t.IsContact);
var entities = await query.AsNoTracking()
.ToArrayAsync(token)
.ConfigureAwait(false);
var dtos = entities.Adapt<IEnumerable<CompanyTypeDto>>();
return dtos;
}
public async Task<int> UpdateRangeAsync(int idWell, int contactTypeId, IEnumerable<int> userIds, CancellationToken token)
{
var items = db.RelationContactsWells
.Where(x => x.IdWell == idWell && x.User.Company.IdCompanyType == contactTypeId);
var wellContacts = userIds.Select(userId => new RelationContactWell()
{
IdWell = idWell,
IdUser = userId,
});
db.RelationContactsWells.RemoveRange(items);
db.RelationContactsWells.AddRange(wellContacts);
return await db.SaveChangesAsync(token)
.ConfigureAwait(false);
}
}
}

View File

@ -1,4 +1,5 @@
using AsbCloudApp.Data;
using AsbCloudApp.Data.User;
using AsbCloudApp.Exceptions;
using AsbCloudApp.Repositories;
using AsbCloudApp.Requests;

View File

@ -1,4 +1,5 @@
using AsbCloudApp.Data;
using AsbCloudApp.Data.User;
using AsbCloudApp.Repositories;
using AsbCloudApp.Services;
using AsbCloudDb.Model;

View File

@ -1,4 +1,5 @@
using AsbCloudApp.Data;
using AsbCloudApp.Data.User;
using AsbCloudApp.Repositories;
using AsbCloudApp.Requests;
using AsbCloudApp.Services;

View File

@ -9,6 +9,7 @@ using System.IO;
using System.Linq;
using AsbCloudApp.Repositories;
using System.Collections.Generic;
using AsbCloudApp.Data.User;
namespace AsbCloudWebApi.Tests.ServicesTests
{

View File

@ -1,4 +1,4 @@
using AsbCloudApp.Data;
using AsbCloudApp.Data.User;
using AsbCloudApp.Repositories;
using AsbCloudApp.Services;
using AsbCloudInfrastructure.Repository;

View File

@ -1,6 +1,5 @@
using AsbCloudApp.Data;
using AsbCloudApp.Data.User;
using AsbCloudApp.Repositories;
using AsbCloudApp.Services;
using Microsoft.AspNetCore.Authorization;
using Microsoft.AspNetCore.Mvc;
using System.Threading.Tasks;

View File

@ -1,4 +1,5 @@
using AsbCloudApp.Data;
using AsbCloudApp.Data.User;
using AsbCloudApp.Repositories;
using AsbCloudApp.Services;
using AsbCloudInfrastructure.Repository;

View File

@ -0,0 +1,94 @@
using AsbCloudApp.Data;
using AsbCloudApp.Data.User;
using AsbCloudApp.Services;
using Microsoft.AspNetCore.Authorization;
using Microsoft.AspNetCore.Mvc;
using System;
using System.Collections.Generic;
using System.ComponentModel.DataAnnotations;
using System.Threading;
using System.Threading.Tasks;
namespace AsbCloudWebApi.Controllers
{
/// <summary>
/// контроллер с контактной информацией по скважине
/// </summary>
[ApiController]
[Authorize]
public class WellContactController : ControllerBase
{
private readonly IWellContactService wellContactsRepository;
private readonly IWellService wellService;
public WellContactController(IWellContactService wellContactsRepository, IWellService wellService)
{
this.wellContactsRepository = wellContactsRepository;
this.wellService = wellService;
}
/// <summary>
/// получение списка типов контактов
/// </summary>
/// <param name="token"></param>
/// <returns></returns>
[HttpGet("api/contacts/types")]
[ProducesResponseType(typeof(IEnumerable<CompanyTypeDto>), (int)System.Net.HttpStatusCode.OK)]
public async Task<IActionResult> GetTypesAsync(CancellationToken token)
{
var result = await wellContactsRepository.GetTypesAsync(token).ConfigureAwait(false);
return Ok(result);
}
/// <summary>
/// Получение пользователей по типу контакта и ключу скважины
/// </summary>
/// <param name="idWell">ключ скважины</param>
/// <param name="contactTypeId">тип контакта</param>
/// <param name="token"></param>
/// <returns></returns>
[HttpGet("api/well/{idWell}/contactType/{contactTypeId}")]
[ProducesResponseType(typeof(IEnumerable<CompanyWithUsersDto>), (int)System.Net.HttpStatusCode.OK)]
public async Task<IActionResult> GetAsync(int idWell, int contactTypeId, CancellationToken token)
{
if (!await CanUserAccessToWellAsync(idWell, token).ConfigureAwait(false))
return Forbid();
var result = await wellContactsRepository.GetAsync(idWell, contactTypeId, token).ConfigureAwait(false);
return Ok(result);
}
/// <summary>
/// Создание контактов по ключу скважины, типу контакта и ключам пользователей
/// </summary>
/// <param name="idWell">ключ скважины</param>
/// <param name="contactTypeId">ключ типа контакта</param>
/// <param name="userIds">ключи пользователей</param>
/// <param name="token"></param>
/// <returns></returns>
[HttpPost("api/well/{idWell}/contactType/{contactTypeId}")]
[Permission]
[ProducesResponseType(typeof(int), (int)System.Net.HttpStatusCode.OK)]
public async Task<IActionResult> UpdateRangeAsync(
[Range(1, int.MaxValue, ErrorMessage = "Id скважины не может быть меньше 1")] int idWell,
[Range(1, int.MaxValue, ErrorMessage = "Id типа контакта не может быть меньше 1")] int contactTypeId,
[FromBody] IEnumerable<int> userIds,
CancellationToken token)
{
if (!await CanUserAccessToWellAsync(idWell, token).ConfigureAwait(false))
return Forbid();
var result = await wellContactsRepository.UpdateRangeAsync(idWell, contactTypeId, userIds, token).ConfigureAwait(false);
return Ok(result);
}
private async Task<bool> CanUserAccessToWellAsync(int idWell, CancellationToken token)
{
int? idCompany = User.GetCompanyId();
return idCompany is not null && await wellService.IsCompanyInvolvedInWellAsync((int)idCompany,
idWell, token).ConfigureAwait(false);
}
}
}

View File

@ -8,6 +8,7 @@ using System.Collections.Generic;
using Microsoft.AspNetCore.Http;
using System.ComponentModel.DataAnnotations;
using AsbCloudApp.Repositories;
using AsbCloudApp.Data.User;
namespace AsbCloudWebApi.Controllers
{

View File

@ -1,4 +1,4 @@
using AsbCloudApp.Data;
using AsbCloudApp.Data.User;
using AsbCloudWebApi.Converters;
using System;
using System.ComponentModel;