Merge branch 'master' into dev

This commit is contained in:
ngfrolov 2024-02-05 10:40:43 +05:00
commit 27e65d1b33
Signed by untrusted user who does not match committer: ng.frolov
GPG Key ID: E99907A0357B29A7
5 changed files with 88 additions and 58 deletions

View File

@ -10,6 +10,8 @@ namespace AsbCloudDb
{
public static void EnsureCreatedAndMigrated(this DatabaseFacade db)
{
var connectionString = db.GetConnectionString();
Trace.TraceInformation($"connectionString: {connectionString}");
db.SetCommandTimeout(TimeSpan.FromMinutes(5));
if (db.EnsureCreated())
{

View File

@ -223,8 +223,10 @@ public class WellOperationRepository : IWellOperationRepository
{
var query = BuildQuery(request)
.AsNoTracking();
var result = await query.ToArrayAsync(token);
return result;
var dtos = await query.ToArrayAsync(token);
return dtos;
}
/// <inheritdoc/>
@ -383,13 +385,6 @@ public class WellOperationRepository : IWellOperationRepository
new[] { nameof(wellOperationDtos) });
}
//if (previousDateEnd > currentDateStart)
//{
// yield return new ValidationResult(
// "Предыдущая операция не завершена",
// new[] { nameof(wellOperationDtos) });
//}
previous = current;
}
}
@ -411,8 +406,9 @@ public class WellOperationRepository : IWellOperationRepository
{
var entity = dto.Adapt<WellOperation>();
entity.Id = default;
entity.DateStart = dto.DateStart.ToUniversalTime();
entity.DateStart = dto.DateStart.DateTime.ToUtcDateTimeOffset(timezone.Hours);
entity.IdWell = idWell;
entity.LastUpdateDate = DateTimeOffset.UtcNow;
db.WellOperations.Add(entity);
}
@ -429,7 +425,8 @@ public class WellOperationRepository : IWellOperationRepository
{
var timezone = wellService.GetTimezone(dto.IdWell);
var entity = dto.Adapt<WellOperation>();
entity.DateStart = dto.DateStart.ToUniversalTime();
entity.DateStart = dto.DateStart.DateTime.ToUtcDateTimeOffset(timezone.Hours);
entity.LastUpdateDate = DateTimeOffset.UtcNow;
db.WellOperations.Update(entity);
var result = await db.SaveChangesAsync(token);
@ -501,7 +498,7 @@ public class WellOperationRepository : IWellOperationRepository
.Where(subOp => subOp.IdType == 1)
.Where(subOp => WellOperationCategory.NonProductiveTimeSubIds.Contains(subOp.IdCategory));
var result = query.Select(o => new WellOperationDto
var dtos = query.Select(o => new WellOperationDto
{
Id = o.Id,
IdPlan = o.IdPlan,
@ -531,21 +528,21 @@ public class WellOperationRepository : IWellOperationRepository
.Min(subOp => subOp.DateStart))
.TotalDays,
IdUser = o.IdUser,
LastUpdateDate = o.LastUpdateDate.ToOffset(TimeSpan.FromHours(timezone.Hours))
LastUpdateDate = DateTime.SpecifyKind(o.LastUpdateDate.UtcDateTime + timeZoneOffset, DateTimeKind.Unspecified)
});
if (request.SortFields?.Any() == true)
{
result = result.SortBy(request.SortFields);
dtos = dtos.SortBy(request.SortFields);
}
else
{
result = result
dtos = dtos
.OrderBy(e => e.DateStart)
.ThenBy(e => e.DepthEnd)
.ThenBy(e => e.Id);
};
return result;
}
return dtos;
}
}

View File

@ -1,5 +1,5 @@
using AsbCloudApp.Data;
using Microsoft.AspNetCore.Mvc;
using AsbCloudApp.Requests;
using Refit;
namespace AsbCloudWebApi.IntegrationTests.Clients;
@ -12,6 +12,10 @@ public interface IWellOperationClient
Task<IApiResponse<int>> InsertRangeAsync(int idWell, int idType, bool deleteBeforeInsert, [Body] IEnumerable<WellOperationDto> dtos);
[Put(BaseRoute + "/{idOperation}")]
Task<IApiResponse<int>> UpdateAsync(int idWell, int idOperation, [FromBody] WellOperationDto value, CancellationToken token);
Task<IApiResponse<int>> UpdateAsync(int idWell, int idOperation, [Body] WellOperationDto value, CancellationToken token);
[Get(BaseRoute + "/plan")]
Task<IApiResponse<PaginationContainer<WellOperationDto>>> GetPageOperationsPlanAsync(int idWell,
[Query] WellOperationRequestBase request,
CancellationToken token);
}

View File

@ -2,45 +2,43 @@ using AsbCloudApp.Data;
using AsbCloudDb.Model;
using AsbCloudWebApi.IntegrationTests.Clients;
using System.Net;
using AsbCloudApp.Requests;
using Xunit;
namespace AsbCloudWebApi.IntegrationTests.Controllers;
public class WellOperationControllerTest : BaseIntegrationTest
{
private static int idWell = 1;
private readonly WellOperationDto[] dtos = new WellOperationDto[]
{
new WellOperationDto()
{
Id = 2,
IdWell = idWell,
IdType = 1,
DateStart = DateTimeOffset.Now,
CategoryInfo = "1",
CategoryName = "1",
Comment = "1",
Day = 1,
DepthEnd = 20,
DepthStart = 10,
DurationHours = 1,
IdCategory = 5000,
IdParentCategory = null,
IdPlan = null,
IdUser = 1,
IdWellSectionType = 1,
LastUpdateDate = DateTimeOffset.Now,
NptHours = 1,
WellSectionTypeName = null,
UserName = null
}
};
private readonly WellOperationDto[] dtos = new WellOperationDto[]
{
new()
{
IdWell = 1,
IdWellSectionType = 1,
WellSectionTypeName = "Пилотный ствол",
IdCategory = 5000,
IdPlan = null,
CategoryName = "Разборка КНБК",
IdParentCategory = 4000,
CategoryInfo = "1",
IdType = 0,
DepthStart = 10.0,
DepthEnd = 20.0,
Day = 0.0,
NptHours = 0.0,
DateStart = new DateTimeOffset(new DateTime(2023, 02, 03, 1, 0, 0, DateTimeKind.Unspecified)),
DurationHours = 1.0,
Comment = "1",
IdUser = 1,
UserName = null,
}
};
private readonly WellOperationDto[] dtosWithError = new WellOperationDto[]
{
new WellOperationDto()
new()
{
Id = 3,
IdWell = idWell,
@ -63,7 +61,7 @@ public class WellOperationControllerTest : BaseIntegrationTest
WellSectionTypeName = null,
UserName = null
},
new WellOperationDto()
new()
{
Id = 4,
IdWell = idWell,
@ -88,12 +86,12 @@ public class WellOperationControllerTest : BaseIntegrationTest
}
};
private IWellOperationClient wellOperationClient;
private IWellOperationClient client;
public WellOperationControllerTest(WebAppFactoryFixture factory)
: base(factory)
{
wellOperationClient = factory.GetAuthorizedHttpClient<IWellOperationClient>();
client = factory.GetAuthorizedHttpClient<IWellOperationClient>();
}
/// <summary>
@ -105,7 +103,7 @@ public class WellOperationControllerTest : BaseIntegrationTest
{
dbContext.CleanupDbSet<WellOperation>();
//act
var response = await wellOperationClient.InsertRangeAsync(idWell, 1, false, dtos);
var response = await client.InsertRangeAsync(idWell, 1, false, dtos);
//assert
Assert.Equal(HttpStatusCode.OK, response.StatusCode);
@ -120,7 +118,7 @@ public class WellOperationControllerTest : BaseIntegrationTest
public async Task InsertRange_returns_error()
{
//act
var response = await wellOperationClient.InsertRangeAsync(idWell, 1, false, dtosWithError);
var response = await client.InsertRangeAsync(idWell, 1, false, dtosWithError);
//assert
Assert.Equal(HttpStatusCode.BadRequest, response.StatusCode);
@ -134,7 +132,7 @@ public class WellOperationControllerTest : BaseIntegrationTest
public async Task InsertRangeWithDeleteBefore_returns_success()
{
//act
var response = await wellOperationClient.InsertRangeAsync(idWell, 1, true, dtos);
var response = await client.InsertRangeAsync(idWell, 1, true, dtos);
//assert
Assert.Equal(HttpStatusCode.OK, response.StatusCode);
@ -148,7 +146,7 @@ public class WellOperationControllerTest : BaseIntegrationTest
public async Task InsertRangeWithDeleteBefore_returns_error()
{
//act
var response = await wellOperationClient.InsertRangeAsync(idWell, 1, true, dtosWithError);
var response = await client.InsertRangeAsync(idWell, 1, true, dtosWithError);
//assert
Assert.Equal(HttpStatusCode.BadRequest, response.StatusCode);
@ -163,7 +161,7 @@ public class WellOperationControllerTest : BaseIntegrationTest
{
//act
var dto = dtos.FirstOrDefault()!;
var response = await wellOperationClient.UpdateAsync(idWell, 1, dto, CancellationToken.None);
var response = await client.UpdateAsync(idWell, 1, dto, CancellationToken.None);
//assert
Assert.Equal(HttpStatusCode.OK, response.StatusCode);
@ -178,9 +176,39 @@ public class WellOperationControllerTest : BaseIntegrationTest
{
//act
var dto = dtosWithError.LastOrDefault()!;
var response = await wellOperationClient.UpdateAsync(idWell, 1, dto, CancellationToken.None);
var response = await client.UpdateAsync(idWell, 1, dto, CancellationToken.None);
//assert
Assert.Equal(HttpStatusCode.BadRequest, response.StatusCode);
}
/// <summary>
/// Получение плановых операций
/// </summary>
/// <returns></returns>
[Fact]
public async Task GetPageOperationsPlanAsync_returns_success()
{
//arrange
dbContext.CleanupDbSet<WellOperation>();
await client.InsertRangeAsync(idWell, WellOperation.IdOperationTypePlan, false, dtos);
var request = new WellOperationRequestBase
{
OperationType = WellOperation.IdOperationTypePlan
};
//act
var response = await client.GetPageOperationsPlanAsync(idWell, request, CancellationToken.None);
//assert
Assert.NotNull(response.Content);
Assert.Single(response.Content.Items);
var dto = dtos[0];
var wellOperation = response.Content.Items.First();
var excludeProps = new[] { nameof(WellOperationDto.Id) };
MatchHelper.Match(dto, wellOperation, excludeProps);
}
}

View File

@ -233,7 +233,6 @@ namespace AsbCloudWebApi.Controllers
return Forbid();
wellOperation.IdWell = idWell;
wellOperation.LastUpdateDate = DateTimeOffset.UtcNow;
wellOperation.IdUser = User.GetUserId();
wellOperation.IdType = idType;