169 lines
5.8 KiB
C#
169 lines
5.8 KiB
C#
using System;
|
|
using System.Linq;
|
|
using LeafWeb.Core.Entities;
|
|
using LeafWeb.WebCms.Models;
|
|
|
|
namespace LeafWeb.WebCms.Utility
|
|
{
|
|
internal static class QueryFilter
|
|
{
|
|
public static IQueryable<LeafInput> Search(IQueryable<LeafInput> resultItems, LeafDataQuery query, string currentUserEmail)
|
|
{
|
|
// search functionality
|
|
if (!string.IsNullOrEmpty(query.q))
|
|
{
|
|
foreach (var piece in query.q.Split(' ').Select(p => p.Trim()))
|
|
{
|
|
resultItems =
|
|
from li in resultItems
|
|
where li.Name.Contains(piece)
|
|
|| li.Identifier.Contains(piece)
|
|
|| li.SiteId.Contains(piece)
|
|
select li;
|
|
}
|
|
}
|
|
|
|
if (!string.IsNullOrEmpty(query.species))
|
|
{
|
|
resultItems =
|
|
from li in resultItems
|
|
where li.LeafInputData.Any(lid => lid.MajorSpecies.Contains(query.species))
|
|
select li;
|
|
}
|
|
|
|
if (!string.IsNullOrEmpty(query.siteid))
|
|
{
|
|
resultItems =
|
|
from li in resultItems
|
|
where li.SiteId.Contains(query.siteid)
|
|
select li;
|
|
}
|
|
|
|
if (!string.IsNullOrEmpty(query.lat))
|
|
{
|
|
var latitude = int.Parse(query.lat);
|
|
var range = 0;
|
|
if (!string.IsNullOrEmpty(query.latr))
|
|
{
|
|
range = int.Parse(query.latr);
|
|
}
|
|
|
|
resultItems =
|
|
from li in resultItems
|
|
where li.LeafInputData.Any(lid =>
|
|
lid.Site.Latitude >= latitude - range &&
|
|
lid.Site.Latitude <= latitude + range)
|
|
select li;
|
|
}
|
|
|
|
if (!string.IsNullOrEmpty(query.lon))
|
|
{
|
|
var longitude = int.Parse(query.lon);
|
|
var range = 0;
|
|
if (!string.IsNullOrEmpty(query.lonr))
|
|
{
|
|
range = int.Parse(query.lonr);
|
|
}
|
|
|
|
resultItems =
|
|
from li in resultItems
|
|
where li.LeafInputData.Any(lid =>
|
|
lid.Site.Longitude >= longitude - range &&
|
|
lid.Site.Longitude <= longitude + range)
|
|
select li;
|
|
}
|
|
|
|
|
|
// co2s
|
|
if (!string.IsNullOrEmpty(query.co2s))
|
|
{
|
|
var p = double.Parse(query.co2s);
|
|
|
|
resultItems =
|
|
from li in resultItems
|
|
where
|
|
// maximum range inside LeafInputFiles
|
|
li.LeafInputData.Max(lid =>
|
|
// range of LeafInputData
|
|
lid.Data.Max(d => d.CO2S)
|
|
- lid.Data.Min(d => d.CO2S))
|
|
>= p
|
|
select li;
|
|
}
|
|
|
|
|
|
// pari
|
|
if (!string.IsNullOrEmpty(query.pari))
|
|
{
|
|
var p = double.Parse(query.pari);
|
|
|
|
resultItems =
|
|
from li in resultItems
|
|
where
|
|
// maximum range inside LeafInputFiles
|
|
li.LeafInputData.Max(lid =>
|
|
// range of LeafInputData
|
|
lid.Data.Max(d => d.PARi)
|
|
- lid.Data.Min(d => d.PARi))
|
|
>= p
|
|
select li;
|
|
}
|
|
|
|
// tleaf
|
|
if (!string.IsNullOrEmpty(query.tleaf))
|
|
{
|
|
var p = double.Parse(query.tleaf);
|
|
|
|
resultItems =
|
|
from li in resultItems
|
|
where
|
|
// maximum range inside LeafInputFiles
|
|
li.LeafInputData.Max(lid =>
|
|
// range of LeafInputData
|
|
lid.Data.Max(d => d.Tleaf)
|
|
- lid.Data.Min(d => d.Tleaf))
|
|
>= p
|
|
select li;
|
|
}
|
|
|
|
// phips2
|
|
if (!string.IsNullOrEmpty(query.phips2))
|
|
{
|
|
var p = double.Parse(query.phips2);
|
|
|
|
resultItems =
|
|
from li in resultItems
|
|
where
|
|
// maximum range inside LeafInputFiles
|
|
li.LeafInputData.Max(lid =>
|
|
// range of LeafInputData
|
|
lid.Data.Max(d => d.PhiPS2)
|
|
- lid.Data.Min(d => d.PhiPS2))
|
|
>= p
|
|
select li;
|
|
}
|
|
|
|
if (query.compl.HasValue && query.compl.Value)
|
|
{
|
|
resultItems =
|
|
from li in resultItems
|
|
where
|
|
li.CurrentStatus == LeafInputStatusType.Complete
|
|
// didn't have an error message
|
|
&& ! li.OutputFiles.Any(f => f.Filename.Contains(LeafOutputFile.Filename_ErrorMessage))
|
|
select li;
|
|
}
|
|
|
|
if (query.usr.HasValue && query.usr.Value && !string.IsNullOrEmpty(currentUserEmail))
|
|
{
|
|
resultItems =
|
|
from li in resultItems
|
|
where
|
|
li.Email.Equals(currentUserEmail, StringComparison.OrdinalIgnoreCase)
|
|
select li;
|
|
}
|
|
|
|
return resultItems;
|
|
}
|
|
}
|
|
} |