diff --git a/AsbCloudApp/Data/DatesRangeDto.cs b/AsbCloudApp/Data/DatesRangeDto.cs new file mode 100644 index 00000000..8df4776b --- /dev/null +++ b/AsbCloudApp/Data/DatesRangeDto.cs @@ -0,0 +1,10 @@ +using System; + +namespace AsbCloudApp.Data +{ + public class DatesRangeDto + { + public DateTime From { get; set; } + public DateTime To { get; set; } + } +} diff --git a/AsbCloudApp/Services/IMessageService.cs b/AsbCloudApp/Services/IMessageService.cs index 9f3b13d0..0ca4b916 100644 --- a/AsbCloudApp/Services/IMessageService.cs +++ b/AsbCloudApp/Services/IMessageService.cs @@ -7,7 +7,7 @@ namespace AsbCloudApp.Services public interface IMessageService { PaginationContainer GetMessages(int wellId, IEnumerable categoryids = null, DateTime begin = default, DateTime end = default, int skip = 0, int take = 32); - IDictionary GetMessagesDatesRange(int wellId); + DatesRangeDto GetMessagesDatesRange(int wellId); void Insert(string uid, IEnumerable dtos); } } \ No newline at end of file diff --git a/AsbCloudApp/Services/IWellService.cs b/AsbCloudApp/Services/IWellService.cs index e3d111c7..88378e6f 100644 --- a/AsbCloudApp/Services/IWellService.cs +++ b/AsbCloudApp/Services/IWellService.cs @@ -1,5 +1,6 @@ using AsbCloudApp.Data; using System.Collections.Generic; +using System.Security.Claims; namespace AsbCloudApp.Services { @@ -7,5 +8,6 @@ namespace AsbCloudApp.Services { IEnumerable GetWellsByCustomer(int idCustomer); IEnumerable GetTransmittingWells(int idCustomer); + bool CheckWellOwnership(int idCustomer, int wellId); } } diff --git a/AsbCloudInfrastructure/Services/MessageService.cs b/AsbCloudInfrastructure/Services/MessageService.cs index f1f0f0e0..ce840653 100644 --- a/AsbCloudInfrastructure/Services/MessageService.cs +++ b/AsbCloudInfrastructure/Services/MessageService.cs @@ -31,7 +31,7 @@ namespace AsbCloudInfrastructure.Services cacheWells = cacheDb.GetCachedTable((AsbCloudDbContext)db); } - private IEnumerable GetMessagesByWell(int wellId) + private Telemetry GetTelemetryByWellId(int wellId) { var well = cacheWells.FirstOrDefault(w => w.Id == wellId); if (well is null) @@ -41,20 +41,12 @@ namespace AsbCloudInfrastructure.Services if (telemetry is null) return null; - var messages = from m in db.Messages - where m.IdTelemetry == telemetry.Id - select m; - - return messages; + return telemetry; } public PaginationContainer GetMessages(int wellId, IEnumerable categoryids = default, DateTime begin = default, DateTime end = default, int skip = 0, int take = 32) { - var well = cacheWells.FirstOrDefault(w => w.Id == wellId); - if (well is null) - return null; - - var telemetry = cacheTelemetry.FirstOrDefault(t => t.Id == well.Id); + var telemetry = GetTelemetryByWellId(wellId); if (telemetry is null) return null; @@ -123,19 +115,21 @@ namespace AsbCloudInfrastructure.Services return result; } - public IDictionary GetMessagesDatesRange(int wellId) + public DatesRangeDto GetMessagesDatesRange(int wellId) { - IEnumerable wellMessages = GetMessagesByWell(wellId) ?? new List(); + var telemetry = GetTelemetryByWellId(wellId); + if (telemetry is null) + return null; - IEnumerable wellMessagesDates = wellMessages.Select(wm => wm.Date); + var messagesDatesRange = (from m in db.Messages + where m.IdTelemetry == telemetry.Id + group m by m.IdTelemetry into g + select new DatesRangeDto { + From = g.Min(d => d.Date), + To = g.Max(d => d.Date) + }).FirstOrDefault(); - IDictionary messagesDatesRange = new Dictionary(); - - messagesDatesRange.Add("From", wellMessagesDates.Any() ? wellMessagesDates.Min() : DateTime.MinValue); - - messagesDatesRange.Add("To", wellMessagesDates.Any() ? wellMessagesDates.Max() : DateTime.MaxValue); - - return messagesDatesRange; + return messagesDatesRange ?? new DatesRangeDto { From = DateTime.MinValue, To = DateTime.MaxValue}; } public void Insert(string uid, IEnumerable dtos) diff --git a/AsbCloudInfrastructure/Services/WellService.cs b/AsbCloudInfrastructure/Services/WellService.cs index 83df1397..a358c97b 100644 --- a/AsbCloudInfrastructure/Services/WellService.cs +++ b/AsbCloudInfrastructure/Services/WellService.cs @@ -1,6 +1,7 @@ using AsbCloudApp.Data; using AsbCloudApp.Services; using AsbCloudDb.Model; +using AsbCloudInfrastructure.Services.Cache; using System.Collections.Generic; using System.Linq; using System; @@ -11,11 +12,13 @@ namespace AsbCloudInfrastructure.Services { private readonly IAsbCloudDbContext db; private readonly ITelemetryTracker telemetryTracker; + private readonly ICacheTable cacheWells; - public WellService(IAsbCloudDbContext db, ITelemetryTracker telemetryTracker) + public WellService(IAsbCloudDbContext db, ITelemetryTracker telemetryTracker, CacheDb cacheDb) { this.db = db; this.telemetryTracker = telemetryTracker; + cacheWells = cacheDb.GetCachedTable((AsbCloudDbContext)db); } public IEnumerable GetTransmittingWells(int idCustomer) @@ -37,6 +40,10 @@ namespace AsbCloudInfrastructure.Services return wells.Select(w => From(w)); } + public bool CheckWellOwnership(int idCustomer, int wellId) + => cacheWells.Contains(w => w.Id == wellId && w.IdCustomer == idCustomer); + + private static WellDto From(Well well) { var wellDto = new WellDto diff --git a/AsbCloudWebApi/Controllers/MessageController.cs b/AsbCloudWebApi/Controllers/MessageController.cs index e3c1fbbd..3331eb04 100644 --- a/AsbCloudWebApi/Controllers/MessageController.cs +++ b/AsbCloudWebApi/Controllers/MessageController.cs @@ -11,10 +11,12 @@ namespace AsbCloudWebApi.Controllers public class MessageController : ControllerBase { private readonly IMessageService messageService; + private readonly IWellService wellService; - public MessageController(IMessageService messageService) + public MessageController(IMessageService messageService, IWellService wellService) { this.messageService = messageService; + this.wellService = wellService; } /// @@ -44,10 +46,20 @@ namespace AsbCloudWebApi.Controllers [HttpGet] [Route("{wellId}/messagesDatesRange")] - [ProducesResponseType(typeof(IDictionary), (int)System.Net.HttpStatusCode.OK)] + [ProducesResponseType(typeof(DatesRangeDto), (int)System.Net.HttpStatusCode.OK)] public IActionResult GetMessagesDateRange(int wellId) { - IDictionary wellMessagesDatesRange = messageService.GetMessagesDatesRange(wellId); + int? idCustomer = User.GetCustomerId(); + + if (idCustomer is null) + return BadRequest(); + + bool isCustomerOwnsWell = wellService.CheckWellOwnership((int)idCustomer, wellId); + + if (!isCustomerOwnsWell) + return Forbid(); + + DatesRangeDto wellMessagesDatesRange = messageService.GetMessagesDatesRange(wellId); return Ok(wellMessagesDatesRange); }