diff --git a/AsbCloudApp/Services/IScheduleService.cs b/AsbCloudApp/Services/IScheduleService.cs index e4234ac6..e3296eb9 100644 --- a/AsbCloudApp/Services/IScheduleService.cs +++ b/AsbCloudApp/Services/IScheduleService.cs @@ -8,6 +8,6 @@ namespace AsbCloudApp.Services { public interface IScheduleService : ICrudService { - Task> GetSchedule(int idWell,DateTimeOffset dateStart, DateTimeOffset dateEnd, CancellationToken token = default); + Task GetSchedule(int idWell,DateTimeOffset workTime, CancellationToken token = default); } } diff --git a/AsbCloudDb/Model/AsbCloudDbContext.cs b/AsbCloudDb/Model/AsbCloudDbContext.cs index d5066196..dfb164be 100644 --- a/AsbCloudDb/Model/AsbCloudDbContext.cs +++ b/AsbCloudDb/Model/AsbCloudDbContext.cs @@ -285,6 +285,14 @@ namespace AsbCloudDb.Model .ToView("mw_telemetry_datas_saub_stat"); }); + modelBuilder.Entity(entity => + { + entity.HasOne(d => d.Driller) + .WithMany(p => p.Schedule) + .HasForeignKey(d => d.IdDriller) + .HasConstraintName("t_schedule_t_driller_id_driller"); + }); + FillData(modelBuilder); } diff --git a/AsbCloudDb/Model/Driller.cs b/AsbCloudDb/Model/Driller.cs index 9d675e8c..7aa39b21 100644 --- a/AsbCloudDb/Model/Driller.cs +++ b/AsbCloudDb/Model/Driller.cs @@ -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 Schedule { get; set; } = null!; } } diff --git a/AsbCloudDb/Model/ScheduleItem.cs b/AsbCloudDb/Model/ScheduleItem.cs index 6e381af5..a5708fe9 100644 --- a/AsbCloudDb/Model/ScheduleItem.cs +++ b/AsbCloudDb/Model/ScheduleItem.cs @@ -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; } } } diff --git a/AsbCloudInfrastructure/Services/DrillerService.cs b/AsbCloudInfrastructure/Services/DrillerService.cs index 03846c1e..28788abb 100644 --- a/AsbCloudInfrastructure/Services/DrillerService.cs +++ b/AsbCloudInfrastructure/Services/DrillerService.cs @@ -9,49 +9,36 @@ using System.Threading.Tasks; namespace AsbCloudInfrastructure.Services { - public class DrillerService : CrudCacheServiceBase, IDrillerService + public class DrillerService : CrudServiceBase, 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 DeleteAsync(int id, CancellationToken dto) + public async Task 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> GetAllAsync(CancellationToken token) + public async Task> GetAllAsync(CancellationToken token) { return await base.GetAllAsync(token); } - public override async Task GetAsync(int id, CancellationToken token) + public async Task 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 InsertAsync(DrillerDto dto, CancellationToken token) + public async Task 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 UpdateAsync(int id, DrillerDto dto, CancellationToken token) + public async Task 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); } diff --git a/AsbCloudInfrastructure/Services/ScheduleService.cs b/AsbCloudInfrastructure/Services/ScheduleService.cs index d260264a..41be8f9e 100644 --- a/AsbCloudInfrastructure/Services/ScheduleService.cs +++ b/AsbCloudInfrastructure/Services/ScheduleService.cs @@ -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, IScheduleService + public class ScheduleService : CrudServiceBase, 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 DeleteAsync(int id, CancellationToken dto) - { - var result = await Cache.RemoveAsync(o => o.Id == id); - return result; + public async Task DeleteAsync(int id, CancellationToken dto) + { + return await base.DeleteAsync(id,dto); } - public override async Task> GetAllAsync(CancellationToken token) + public async Task> GetAllAsync(CancellationToken token) { return await base.GetAllAsync(token); } - public override async Task GetAsync(int id, CancellationToken token) + public async Task 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> 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 InsertAsync(ScheduleDto dto, CancellationToken token) + public async Task GetSchedule(int idWell, DateTimeOffset workTime, CancellationToken token = default) { - var entity = Convert(dto); - var result = await Cache.InsertAsync(entity, token); - return result.Id; + IQueryable query = context.Set(); + 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(); } - public override async Task UpdateAsync(int id, ScheduleDto dto, CancellationToken token) + public async Task InsertAsync(ScheduleDto dto, CancellationToken token) + { + return await base.InsertAsync(dto,token); + } + + public async Task 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); } diff --git a/AsbCloudWebApi.Tests/ServicesTests/DrillerServiceTest.cs b/AsbCloudWebApi.Tests/ServicesTests/DrillerServiceTest.cs index 2b5e378d..93401fd1 100644 --- a/AsbCloudWebApi.Tests/ServicesTests/DrillerServiceTest.cs +++ b/AsbCloudWebApi.Tests/ServicesTests/DrillerServiceTest.cs @@ -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); diff --git a/AsbCloudWebApi.Tests/ServicesTests/ScheduleServiceTest.cs b/AsbCloudWebApi.Tests/ServicesTests/ScheduleServiceTest.cs index d9122da8..6e6c08ee 100644 --- a/AsbCloudWebApi.Tests/ServicesTests/ScheduleServiceTest.cs +++ b/AsbCloudWebApi.Tests/ServicesTests/ScheduleServiceTest.cs @@ -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); diff --git a/AsbCloudWebApi/Controllers/AdminDrillerController.cs b/AsbCloudWebApi/Controllers/DrillerController.cs similarity index 69% rename from AsbCloudWebApi/Controllers/AdminDrillerController.cs rename to AsbCloudWebApi/Controllers/DrillerController.cs index b31ddd27..a2c4cada 100644 --- a/AsbCloudWebApi/Controllers/AdminDrillerController.cs +++ b/AsbCloudWebApi/Controllers/DrillerController.cs @@ -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); } - /// - /// Получить бурильщика по идентификатору - /// - /// Идентификатор - /// - /// Объект, описывающий бурильщика - [HttpGet("{DrillerId}")] - public async Task GetAsync(int DrillerId, CancellationToken token = default) + /// + /// Получить бурильщика по идентификатору + /// + /// Идентификатор + /// + /// Объект, описывающий бурильщика + [HttpGet("{drillerId}")] + public async Task 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 /// /// Обновить данные о бурильщике /// - /// Идентификатор + /// Идентификатор /// Объект с измененными данными /// /// Количество измененых записей - [HttpPut("{DrillerId}")] - public async Task UpdateAsync(int DrillerId, DrillerDto dto, + [HttpPut("{drillerId}")] + public async Task 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 /// /// Удалить бурильщика /// - /// Идентификатор бурильщика + /// Идентификатор бурильщика /// /// Количество удаленных записей - [HttpDelete("{DrillerId}")] - public async Task> DeleteAsync(int DrillerId, CancellationToken token = default) + [HttpDelete("{drillerId}")] + public async Task> 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); } diff --git a/AsbCloudWebApi/Controllers/AdminScheduleController.cs b/AsbCloudWebApi/Controllers/ScheduleController.cs similarity index 76% rename from AsbCloudWebApi/Controllers/AdminScheduleController.cs rename to AsbCloudWebApi/Controllers/ScheduleController.cs index 05f9db7b..f8e90db5 100644 --- a/AsbCloudWebApi/Controllers/AdminScheduleController.cs +++ b/AsbCloudWebApi/Controllers/ScheduleController.cs @@ -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 /// Получить список записей графика для конкретной скважины /// /// Идентификатор скважины - /// Начало периода - /// Конец периода + /// Рабочее время /// /// Список записей графика [HttpGet] - public async Task> GetScheduleAsync(int idWell, DateTimeOffset dateStart, DateTimeOffset dateEnd, CancellationToken token = default) + public async Task> 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); } /// /// Обновить график /// - /// Идентификатор записи + /// Идентификатор записи /// Элемент графика /// /// Количнство обновленных записей - [HttpPut("{ScheduleId}")] - public async Task UpdateAsync(int ScheduleId, ScheduleDto dto, + [HttpPut("{scheduleId}")] + public async Task 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 /// /// Удалить запись из графика /// - /// Идентификатор записи + /// Идентификатор записи /// /// - [HttpDelete("{ScheduleId}")] - public async Task> DeleteAsync(int ScheduleId, CancellationToken token = default) + [HttpDelete("{scheduleId}")] + public async Task> 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); }