Правки по PR

This commit is contained in:
Lyudmila Romanova 2022-05-25 20:19:08 +05:00
parent 8ca313a18a
commit 80bad5bbe2
10 changed files with 102 additions and 103 deletions

View File

@ -8,6 +8,6 @@ namespace AsbCloudApp.Services
{
public interface IScheduleService : ICrudService<ScheduleDto>
{
Task<IEnumerable<ScheduleDto>> GetSchedule(int idWell,DateTimeOffset dateStart, DateTimeOffset dateEnd, CancellationToken token = default);
Task<DrillerDto> GetSchedule(int idWell,DateTimeOffset workTime, CancellationToken token = default);
}
}

View File

@ -285,6 +285,14 @@ namespace AsbCloudDb.Model
.ToView("mw_telemetry_datas_saub_stat");
});
modelBuilder.Entity<ScheduleItem>(entity =>
{
entity.HasOne(d => d.Driller)
.WithMany(p => p.Schedule)
.HasForeignKey(d => d.IdDriller)
.HasConstraintName("t_schedule_t_driller_id_driller");
});
FillData(modelBuilder);
}

View File

@ -5,6 +5,7 @@ using System.ComponentModel.DataAnnotations;
using System.ComponentModel.DataAnnotations.Schema;
using System.Linq;
using System.Text;
using System.Text.Json.Serialization;
using System.Threading.Tasks;
namespace AsbCloudDb.Model
@ -27,5 +28,9 @@ namespace AsbCloudDb.Model
[Column("patronymic"), Comment("Отчество")]
[StringLength(255)]
public string Patronymic { get; set; }
[JsonIgnore]
[InverseProperty(nameof(ScheduleItem.Driller))]
public virtual ICollection<ScheduleItem> Schedule { get; set; } = null!;
}
}

View File

@ -35,9 +35,10 @@ namespace AsbCloudDb.Model
public DateTimeOffset DrillEnd { get; set; }
[ForeignKey(nameof(IdDriller))]
[InverseProperty(nameof(Model.Driller.Schedule))]
public virtual Driller Driller { get; set; }
[ForeignKey(nameof(IdWell))]
public virtual Well well { get; set; }
public virtual Well Well { get; set; }
}
}

View File

@ -9,49 +9,36 @@ using System.Threading.Tasks;
namespace AsbCloudInfrastructure.Services
{
public class DrillerService : CrudCacheServiceBase<DrillerDto, Driller>, IDrillerService
public class DrillerService : CrudServiceBase<DrillerDto, Driller>, IDrillerService
{
private IAsbCloudDbContext context;
public DrillerService(IAsbCloudDbContext db, CacheDb cacheDb) : base(db, cacheDb)
public DrillerService(IAsbCloudDbContext context) : base(context)
{
context= db;
}
public override async Task<int> DeleteAsync(int id, CancellationToken dto)
public async Task<int> DeleteAsync(int id, CancellationToken dto)
{
var result = await Cache.RemoveAsync(o => o.Id == id);
return result;
return await base.DeleteAsync(id, dto);
}
public override async Task<IEnumerable<DrillerDto>> GetAllAsync(CancellationToken token)
public async Task<IEnumerable<DrillerDto>> GetAllAsync(CancellationToken token)
{
return await base.GetAllAsync(token);
}
public override async Task<DrillerDto> GetAsync(int id, CancellationToken token)
public async Task<DrillerDto> GetAsync(int id, CancellationToken token)
{
var res= await Cache.FirstOrDefaultAsync(o=>o.Id==id,token)
return await base.GetAsync(id,token)
.ConfigureAwait(false);
if (res is null)
return null;
var dto = Convert(res);
return dto;
}
public override async Task<int> InsertAsync(DrillerDto dto, CancellationToken token)
public async Task<int> InsertAsync(DrillerDto dto, CancellationToken token)
{
var entity = Convert(dto);
var result = await Cache.InsertAsync(entity, token);
return result.Id;
return await base.InsertAsync(dto, token);
}
public override async Task<int> UpdateAsync(int id, DrillerDto dto, CancellationToken token)
public async Task<int> UpdateAsync(int id, DrillerDto dto, CancellationToken token)
{
var entity = Convert(dto);
var result = await Cache.UpsertAsync(entity, token);
return result;
return await base.UpdateAsync(id,dto, token);
}

View File

@ -2,6 +2,8 @@
using AsbCloudApp.Services;
using AsbCloudDb.Model;
using AsbCloudInfrastructure.Services.Cache;
using Mapster;
using Microsoft.EntityFrameworkCore;
using System;
using System.Collections.Generic;
using System.Linq;
@ -10,58 +12,55 @@ using System.Threading.Tasks;
namespace AsbCloudInfrastructure.Services
{
public class ScheduleService : CrudCacheServiceBase<ScheduleDto, ScheduleItem>, IScheduleService
public class ScheduleService : CrudServiceBase<ScheduleDto, ScheduleItem>, IScheduleService
{
private IAsbCloudDbContext context;
public ScheduleService(IAsbCloudDbContext db, CacheDb cacheDb) : base(db, cacheDb)
public ScheduleService(IAsbCloudDbContext context) : base(context)
{
context= db;
Includes.Add(nameof(ScheduleItem.Driller));
}
public override async Task<int> DeleteAsync(int id, CancellationToken dto)
{
var result = await Cache.RemoveAsync(o => o.Id == id);
return result;
public async Task<int> DeleteAsync(int id, CancellationToken dto)
{
return await base.DeleteAsync(id,dto);
}
public override async Task<IEnumerable<ScheduleDto>> GetAllAsync(CancellationToken token)
public async Task<IEnumerable<ScheduleDto>> GetAllAsync(CancellationToken token)
{
return await base.GetAllAsync(token);
}
public override async Task<ScheduleDto> GetAsync(int id, CancellationToken token)
public async Task<ScheduleDto> GetAsync(int id, CancellationToken token)
{
var res= await Cache.FirstOrDefaultAsync(o=>o.Id==id,token)
var res= await base.GetAsync(id,token)
.ConfigureAwait(false);
if (res is null)
return null;
var dto = Convert(res);
return dto;
}
public async Task<IEnumerable<ScheduleDto>> GetSchedule(int idWell, DateTimeOffset dateStart, DateTimeOffset dateEnd, CancellationToken token = default)
{
var entires=await Cache.WhereAsync(o=>o.IdWell==idWell
&&o.ShiftStart<=dateStart
&&o.ShiftEnd<=dateEnd).ConfigureAwait(false);
var res = entires?.Select(Convert);
return res;
}
public override async Task<int> InsertAsync(ScheduleDto dto, CancellationToken token)
public async Task<DrillerDto> GetSchedule(int idWell, DateTimeOffset workTime, CancellationToken token = default)
{
var entity = Convert(dto);
var result = await Cache.InsertAsync(entity, token);
return result.Id;
IQueryable<ScheduleItem> query = context.Set<ScheduleItem>();
foreach (var include in Includes)
query = query.Include(include);
var entires = query.Where(e => e.IdWell==idWell
&& e.DrillStart <= workTime
&& e.DrillEnd >= workTime)
.FirstOrDefault();
if (entires is null)
return null;
return entires.Driller.Adapt<DrillerDto>();
}
public override async Task<int> UpdateAsync(int id, ScheduleDto dto, CancellationToken token)
public async Task<int> InsertAsync(ScheduleDto dto, CancellationToken token)
{
return await base.InsertAsync(dto,token);
}
public async Task<int> UpdateAsync(int id, ScheduleDto dto, CancellationToken token)
{
var entity = Convert(dto);
var result = await Cache.UpsertAsync(entity, token);
return result;
return await base.UpdateAsync(id,dto,token);
}

View File

@ -39,7 +39,7 @@ namespace AsbCloudWebApi.Tests.ServicesTests
[Fact]
public async Task GetListAsync_count()
{
var service = new DrillerService(context, cacheDb);
var service = new DrillerService(context);
///Добавляем элемент
var id = await service.InsertAsync(drillerObj, CancellationToken.None);
@ -47,13 +47,13 @@ namespace AsbCloudWebApi.Tests.ServicesTests
id = await service.InsertAsync(drillerObj, CancellationToken.None);
var newCount = (await service.GetAllAsync(CancellationToken.None)).Count();
Assert.Equal(newCount, 3);
Assert.Equal(3, newCount);
}
[Fact]
public async Task InsertAsync_returns_id()
{
var service = new DrillerService(context, cacheDb);
var service = new DrillerService(context);
var id = await service.InsertAsync(drillerObj, CancellationToken.None);
Assert.NotEqual(0, id);
}
@ -61,7 +61,7 @@ namespace AsbCloudWebApi.Tests.ServicesTests
[Fact]
public async Task UpdateAsync_not_add_if_exists()
{
var service = new DrillerService(context, cacheDb);
var service = new DrillerService(context);
///Добавляем элемент
var id = await service.InsertAsync(drillerObj, CancellationToken.None);
@ -78,7 +78,7 @@ namespace AsbCloudWebApi.Tests.ServicesTests
[Fact]
public async Task DeleteAsync_decrement_count()
{
var service = new DrillerService(context, cacheDb);
var service = new DrillerService(context);
///Добавляем элемент
var id = await service.InsertAsync(drillerObj, CancellationToken.None);

View File

@ -39,7 +39,7 @@ namespace AsbCloudWebApi.Tests.ServicesTests
[Fact]
public async Task GetListAsync_count()
{
var service = new ScheduleService(context, cacheDb);
var service = new ScheduleService(context);
///Добавляем элемент
var id = await service.InsertAsync(entity, CancellationToken.None);
@ -53,7 +53,7 @@ namespace AsbCloudWebApi.Tests.ServicesTests
[Fact]
public async Task InsertAsync_returns_id()
{
var service = new ScheduleService(context, cacheDb);
var service = new ScheduleService(context);
var id = await service.InsertAsync(entity, CancellationToken.None);
Assert.NotEqual(0, id);
}
@ -61,7 +61,7 @@ namespace AsbCloudWebApi.Tests.ServicesTests
[Fact]
public async Task UpdateAsync_not_add_if_exists()
{
var service = new ScheduleService(context, cacheDb);
var service = new ScheduleService(context);
///Добавляем элемент
var id = await service.InsertAsync(entity, CancellationToken.None);
@ -78,7 +78,7 @@ namespace AsbCloudWebApi.Tests.ServicesTests
[Fact]
public async Task DeleteAsync_decrement_count()
{
var service = new ScheduleService(context, cacheDb);
var service = new ScheduleService(context);
///Добавляем элемент
var id = await service.InsertAsync(entity, CancellationToken.None);

View File

@ -7,13 +7,13 @@ using System.Threading.Tasks;
namespace AsbCloudWebApi.Controllers
{
[Route("api/admin/driller")]
[Route("api/driller")]
[ApiController]
[Authorize]
public class AdminDrillerController : ControllerBase
public class DrillerController : ControllerBase
{
private readonly IDrillerService drillerService;
public AdminDrillerController(IDrillerService drillerService)
public DrillerController(IDrillerService drillerService)
{
this.drillerService = drillerService;
}
@ -43,16 +43,16 @@ namespace AsbCloudWebApi.Controllers
return Ok(result);
}
/// <summary>
/// Получить бурильщика по идентификатору
/// </summary>
/// <param name="DrillerId">Идентификатор</param>
/// <param name="token"></param>
/// <returns>Объект, описывающий бурильщика</returns>
[HttpGet("{DrillerId}")]
public async Task<IActionResult> GetAsync(int DrillerId, CancellationToken token = default)
/// <summary>
/// Получить бурильщика по идентификатору
/// </summary>
/// <param name="drillerId">Идентификатор</param>
/// <param name="token"></param>
/// <returns>Объект, описывающий бурильщика</returns>
[HttpGet("{drillerId}")]
public async Task<IActionResult> GetAsync(int drillerId, CancellationToken token = default)
{
var driller = await drillerService.GetAsync(DrillerId, token)
var driller = await drillerService.GetAsync(drillerId, token)
.ConfigureAwait(false);
return Ok(driller);
}
@ -60,15 +60,15 @@ namespace AsbCloudWebApi.Controllers
/// <summary>
/// Обновить данные о бурильщике
/// </summary>
/// <param name="DrillerId">Идентификатор</param>
/// <param name="drillerId">Идентификатор</param>
/// <param name="dto">Объект с измененными данными</param>
/// <param name="token"></param>
/// <returns>Количество измененых записей</returns>
[HttpPut("{DrillerId}")]
public async Task<IActionResult> UpdateAsync(int DrillerId, DrillerDto dto,
[HttpPut("{drillerId}")]
public async Task<IActionResult> UpdateAsync(int drillerId, DrillerDto dto,
CancellationToken token = default)
{
var result = await drillerService.UpdateAsync(DrillerId, dto, token)
var result = await drillerService.UpdateAsync(drillerId, dto, token)
.ConfigureAwait(false);
return Ok(result);
@ -77,13 +77,13 @@ namespace AsbCloudWebApi.Controllers
/// <summary>
/// Удалить бурильщика
/// </summary>
/// <param name="DrillerId">Идентификатор бурильщика</param>
/// <param name="drillerId">Идентификатор бурильщика</param>
/// <param name="token"></param>
/// <returns>Количество удаленных записей</returns>
[HttpDelete("{DrillerId}")]
public async Task<ActionResult<int>> DeleteAsync(int DrillerId, CancellationToken token = default)
[HttpDelete("{drillerId}")]
public async Task<ActionResult<int>> DeleteAsync(int drillerId, CancellationToken token = default)
{
var result = await drillerService.DeleteAsync(DrillerId, token)
var result = await drillerService.DeleteAsync(drillerId, token)
.ConfigureAwait(false);
return Ok(result);
}

View File

@ -8,13 +8,13 @@ using System.Threading.Tasks;
namespace AsbCloudWebApi.Controllers
{
[Route("api/admin/schedule")]
[Route("api/schedule")]
[ApiController]
[Authorize]
public class AdminScheduleController : ControllerBase
public class ScheduleController : ControllerBase
{
private readonly IScheduleService scheduleService;
public AdminScheduleController(IScheduleService service)
public ScheduleController(IScheduleService service)
{
this.scheduleService = service;
}
@ -48,29 +48,28 @@ namespace AsbCloudWebApi.Controllers
/// Получить список записей графика для конкретной скважины
/// </summary>
/// <param name="idWell">Идентификатор скважины</param>
/// <param name="dateStart">Начало периода</param>
/// <param name="dateEnd">Конец периода</param>
/// <param name="workTime">Рабочее время</param>
/// <param name="token"></param>
/// <returns>Список записей графика</returns>
[HttpGet]
public async Task<ActionResult<int>> GetScheduleAsync(int idWell, DateTimeOffset dateStart, DateTimeOffset dateEnd, CancellationToken token = default)
public async Task<ActionResult<int>> GetScheduleAsync(int idWell, DateTimeOffset workTime, CancellationToken token = default)
{
var result = await scheduleService.GetSchedule(idWell,dateStart,dateEnd, token);
var result = await scheduleService.GetSchedule(idWell,workTime, token);
return Ok(result);
}
/// <summary>
/// Обновить график
/// </summary>
/// <param name="ScheduleId">Идентификатор записи</param>
/// <param name="scheduleId">Идентификатор записи</param>
/// <param name="dto">Элемент графика</param>
/// <param name="token"></param>
/// <returns>Количнство обновленных записей</returns>
[HttpPut("{ScheduleId}")]
public async Task<IActionResult> UpdateAsync(int ScheduleId, ScheduleDto dto,
[HttpPut("{scheduleId}")]
public async Task<IActionResult> UpdateAsync(int scheduleId, ScheduleDto dto,
CancellationToken token = default)
{
var result = await scheduleService.UpdateAsync(ScheduleId, dto, token)
var result = await scheduleService.UpdateAsync(scheduleId, dto, token)
.ConfigureAwait(false);
return Ok(result);
@ -79,13 +78,13 @@ namespace AsbCloudWebApi.Controllers
/// <summary>
/// Удалить запись из графика
/// </summary>
/// <param name="ScheduleId">Идентификатор записи</param>
/// <param name="scheduleId">Идентификатор записи</param>
/// <param name="token"></param>
/// <returns></returns>
[HttpDelete("{ScheduleId}")]
public async Task<ActionResult<int>> DeleteAsync(int ScheduleId, CancellationToken token = default)
[HttpDelete("{scheduleId}")]
public async Task<ActionResult<int>> DeleteAsync(int scheduleId, CancellationToken token = default)
{
var result = await scheduleService.DeleteAsync(ScheduleId, token)
var result = await scheduleService.DeleteAsync(scheduleId, token)
.ConfigureAwait(false);
return Ok(result);
}