CS2-1: Исправлена возможность получения диапазона дат сообщений

This commit is contained in:
KharchenkoVV 2021-05-14 17:02:29 +05:00
parent 0bfec5dcb1
commit 335792ae94
6 changed files with 51 additions and 26 deletions

View File

@ -0,0 +1,10 @@
using System;
namespace AsbCloudApp.Data
{
public class DatesRangeDto
{
public DateTime From { get; set; }
public DateTime To { get; set; }
}
}

View File

@ -7,7 +7,7 @@ namespace AsbCloudApp.Services
public interface IMessageService public interface IMessageService
{ {
PaginationContainer<MessageDto> GetMessages(int wellId, IEnumerable<int> categoryids = null, DateTime begin = default, DateTime end = default, int skip = 0, int take = 32); PaginationContainer<MessageDto> GetMessages(int wellId, IEnumerable<int> categoryids = null, DateTime begin = default, DateTime end = default, int skip = 0, int take = 32);
IDictionary<string, DateTime> GetMessagesDatesRange(int wellId); DatesRangeDto GetMessagesDatesRange(int wellId);
void Insert(string uid, IEnumerable<TelemetryMessageDto> dtos); void Insert(string uid, IEnumerable<TelemetryMessageDto> dtos);
} }
} }

View File

@ -1,5 +1,6 @@
using AsbCloudApp.Data; using AsbCloudApp.Data;
using System.Collections.Generic; using System.Collections.Generic;
using System.Security.Claims;
namespace AsbCloudApp.Services namespace AsbCloudApp.Services
{ {
@ -7,5 +8,6 @@ namespace AsbCloudApp.Services
{ {
IEnumerable<WellDto> GetWellsByCustomer(int idCustomer); IEnumerable<WellDto> GetWellsByCustomer(int idCustomer);
IEnumerable<WellDto> GetTransmittingWells(int idCustomer); IEnumerable<WellDto> GetTransmittingWells(int idCustomer);
bool CheckWellOwnership(int idCustomer, int wellId);
} }
} }

View File

@ -31,7 +31,7 @@ namespace AsbCloudInfrastructure.Services
cacheWells = cacheDb.GetCachedTable<Well>((AsbCloudDbContext)db); cacheWells = cacheDb.GetCachedTable<Well>((AsbCloudDbContext)db);
} }
private IEnumerable<Message> GetMessagesByWell(int wellId) private Telemetry GetTelemetryByWellId(int wellId)
{ {
var well = cacheWells.FirstOrDefault(w => w.Id == wellId); var well = cacheWells.FirstOrDefault(w => w.Id == wellId);
if (well is null) if (well is null)
@ -41,20 +41,12 @@ namespace AsbCloudInfrastructure.Services
if (telemetry is null) if (telemetry is null)
return null; return null;
var messages = from m in db.Messages return telemetry;
where m.IdTelemetry == telemetry.Id
select m;
return messages;
} }
public PaginationContainer<MessageDto> GetMessages(int wellId, IEnumerable<int> categoryids = default, DateTime begin = default, DateTime end = default, int skip = 0, int take = 32) public PaginationContainer<MessageDto> GetMessages(int wellId, IEnumerable<int> categoryids = default, DateTime begin = default, DateTime end = default, int skip = 0, int take = 32)
{ {
var well = cacheWells.FirstOrDefault(w => w.Id == wellId); var telemetry = GetTelemetryByWellId(wellId);
if (well is null)
return null;
var telemetry = cacheTelemetry.FirstOrDefault(t => t.Id == well.Id);
if (telemetry is null) if (telemetry is null)
return null; return null;
@ -123,19 +115,21 @@ namespace AsbCloudInfrastructure.Services
return result; return result;
} }
public IDictionary<string, DateTime> GetMessagesDatesRange(int wellId) public DatesRangeDto GetMessagesDatesRange(int wellId)
{ {
IEnumerable<Message> wellMessages = GetMessagesByWell(wellId) ?? new List<Message>(); var telemetry = GetTelemetryByWellId(wellId);
if (telemetry is null)
return null;
IEnumerable<DateTime> 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<string, DateTime> messagesDatesRange = new Dictionary<string, DateTime>(); return messagesDatesRange ?? new DatesRangeDto { From = DateTime.MinValue, To = DateTime.MaxValue};
messagesDatesRange.Add("From", wellMessagesDates.Any() ? wellMessagesDates.Min() : DateTime.MinValue);
messagesDatesRange.Add("To", wellMessagesDates.Any() ? wellMessagesDates.Max() : DateTime.MaxValue);
return messagesDatesRange;
} }
public void Insert(string uid, IEnumerable<TelemetryMessageDto> dtos) public void Insert(string uid, IEnumerable<TelemetryMessageDto> dtos)

View File

@ -1,6 +1,7 @@
using AsbCloudApp.Data; using AsbCloudApp.Data;
using AsbCloudApp.Services; using AsbCloudApp.Services;
using AsbCloudDb.Model; using AsbCloudDb.Model;
using AsbCloudInfrastructure.Services.Cache;
using System.Collections.Generic; using System.Collections.Generic;
using System.Linq; using System.Linq;
using System; using System;
@ -11,11 +12,13 @@ namespace AsbCloudInfrastructure.Services
{ {
private readonly IAsbCloudDbContext db; private readonly IAsbCloudDbContext db;
private readonly ITelemetryTracker telemetryTracker; private readonly ITelemetryTracker telemetryTracker;
private readonly ICacheTable<Well> cacheWells;
public WellService(IAsbCloudDbContext db, ITelemetryTracker telemetryTracker) public WellService(IAsbCloudDbContext db, ITelemetryTracker telemetryTracker, CacheDb cacheDb)
{ {
this.db = db; this.db = db;
this.telemetryTracker = telemetryTracker; this.telemetryTracker = telemetryTracker;
cacheWells = cacheDb.GetCachedTable<Well>((AsbCloudDbContext)db);
} }
public IEnumerable<WellDto> GetTransmittingWells(int idCustomer) public IEnumerable<WellDto> GetTransmittingWells(int idCustomer)
@ -37,6 +40,10 @@ namespace AsbCloudInfrastructure.Services
return wells.Select(w => From(w)); 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) private static WellDto From(Well well)
{ {
var wellDto = new WellDto var wellDto = new WellDto

View File

@ -11,10 +11,12 @@ namespace AsbCloudWebApi.Controllers
public class MessageController : ControllerBase public class MessageController : ControllerBase
{ {
private readonly IMessageService messageService; private readonly IMessageService messageService;
private readonly IWellService wellService;
public MessageController(IMessageService messageService) public MessageController(IMessageService messageService, IWellService wellService)
{ {
this.messageService = messageService; this.messageService = messageService;
this.wellService = wellService;
} }
/// <summary> /// <summary>
@ -44,10 +46,20 @@ namespace AsbCloudWebApi.Controllers
[HttpGet] [HttpGet]
[Route("{wellId}/messagesDatesRange")] [Route("{wellId}/messagesDatesRange")]
[ProducesResponseType(typeof(IDictionary<string, DateTime>), (int)System.Net.HttpStatusCode.OK)] [ProducesResponseType(typeof(DatesRangeDto), (int)System.Net.HttpStatusCode.OK)]
public IActionResult GetMessagesDateRange(int wellId) public IActionResult GetMessagesDateRange(int wellId)
{ {
IDictionary<string, DateTime> 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); return Ok(wellMessagesDatesRange);
} }