forked from ddrilling/AsbCloudServer
CS2-1: Исправлена возможность получения диапазона дат сообщений
This commit is contained in:
parent
0bfec5dcb1
commit
335792ae94
10
AsbCloudApp/Data/DatesRangeDto.cs
Normal file
10
AsbCloudApp/Data/DatesRangeDto.cs
Normal file
@ -0,0 +1,10 @@
|
||||
using System;
|
||||
|
||||
namespace AsbCloudApp.Data
|
||||
{
|
||||
public class DatesRangeDto
|
||||
{
|
||||
public DateTime From { get; set; }
|
||||
public DateTime To { get; set; }
|
||||
}
|
||||
}
|
@ -7,7 +7,7 @@ namespace AsbCloudApp.Services
|
||||
public interface IMessageService
|
||||
{
|
||||
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);
|
||||
}
|
||||
}
|
@ -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<WellDto> GetWellsByCustomer(int idCustomer);
|
||||
IEnumerable<WellDto> GetTransmittingWells(int idCustomer);
|
||||
bool CheckWellOwnership(int idCustomer, int wellId);
|
||||
}
|
||||
}
|
||||
|
@ -31,7 +31,7 @@ namespace AsbCloudInfrastructure.Services
|
||||
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);
|
||||
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<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);
|
||||
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<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>();
|
||||
|
||||
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<TelemetryMessageDto> dtos)
|
||||
|
@ -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<Well> cacheWells;
|
||||
|
||||
public WellService(IAsbCloudDbContext db, ITelemetryTracker telemetryTracker)
|
||||
public WellService(IAsbCloudDbContext db, ITelemetryTracker telemetryTracker, CacheDb cacheDb)
|
||||
{
|
||||
this.db = db;
|
||||
this.telemetryTracker = telemetryTracker;
|
||||
cacheWells = cacheDb.GetCachedTable<Well>((AsbCloudDbContext)db);
|
||||
}
|
||||
|
||||
public IEnumerable<WellDto> 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
|
||||
|
@ -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;
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
@ -44,10 +46,20 @@ namespace AsbCloudWebApi.Controllers
|
||||
|
||||
[HttpGet]
|
||||
[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)
|
||||
{
|
||||
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);
|
||||
}
|
||||
|
Loading…
Reference in New Issue
Block a user