diff --git a/AsbCloudInfrastructure/Services/WellService.cs b/AsbCloudInfrastructure/Services/WellService.cs index 97bf9e06..6a805560 100644 --- a/AsbCloudInfrastructure/Services/WellService.cs +++ b/AsbCloudInfrastructure/Services/WellService.cs @@ -33,7 +33,8 @@ namespace AsbCloudInfrastructure.Services .Include(w => w.Telemetry) .Include(w => w.WellType) .Include(w => w.RelationCompaniesWells) - .ThenInclude(r => r.Company); + .ThenInclude(r => r.Company) + .AsNoTracking(); public WellService(IAsbCloudDbContext db, IMemoryCache memoryCache, ITelemetryService telemetryService, ITimezoneService timezoneService, WellInfoService wellInfoService) : base(db, memoryCache, MakeQueryWell) @@ -105,19 +106,18 @@ namespace AsbCloudInfrastructure.Services public async Task GetOrDefaultStatAsync(int idWell, CancellationToken token) { - var dto = wellInfoService.FirstOrDefault(well => well.Id == idWell); - if (dto is not null) - return dto; - - var request = new WellRequest{Ids = new[] { idWell }}; - var entities = await GetEntitiesAsync(request, token); - var entity = entities.FirstOrDefault(); - if (entity is null) + var well = await GetOrDefaultAsync(idWell, token); + + if (well is null) return null; + + var wellInfo = wellInfoService.FirstOrDefault(well => well.Id == idWell); - dto = entity.Adapt(); - - return dto; + if (wellInfo is null) + return well.Adapt(); + + wellInfo.IdState = well.IdState; + return wellInfo; } public async Task> GetAsync(WellRequest request, CancellationToken token) diff --git a/AsbCloudWebApi/Controllers/WellController.cs b/AsbCloudWebApi/Controllers/WellController.cs index 3ed37d65..2a0ad437 100644 --- a/AsbCloudWebApi/Controllers/WellController.cs +++ b/AsbCloudWebApi/Controllers/WellController.cs @@ -1,4 +1,5 @@ using AsbCloudApp.Data; +using AsbCloudApp.Exceptions; using AsbCloudApp.Services; using AsbCloudDb.Model; using Microsoft.AspNetCore.Authorization; @@ -135,7 +136,11 @@ namespace AsbCloudWebApi.Controllers idWell, token).ConfigureAwait(false)) return Forbid(); - var dto = wellService.GetOrDefault(idWell)!; + var dto = await wellService.GetOrDefaultAsync(idWell, token); + + if (dto is null) + return this.ValidationBadRequest(nameof(idWell), $"Скважина с id: {idWell} недоступна"); + dto.IdState = idState; var result = await wellService.UpdateAsync(dto, token)