166 lines
3.6 KiB
C#
166 lines
3.6 KiB
C#
using System;
|
|
using System.Collections.Generic;
|
|
using System.Data.Entity;
|
|
using System.Linq;
|
|
using LeafWeb.Core.Entities;
|
|
|
|
namespace LeafWeb.Core.DAL
|
|
{
|
|
public class DataService : IDisposable
|
|
{
|
|
public static void RegisterInitializer()
|
|
{
|
|
Database.SetInitializer(new LeafWebInitializer());
|
|
new LeafWebContext().Database.Initialize(true);
|
|
}
|
|
|
|
private readonly LeafWebContext _db = new LeafWebContext();
|
|
|
|
public void Dispose()
|
|
{
|
|
_db.Dispose();
|
|
}
|
|
|
|
#region Fluxnet Sites
|
|
|
|
public IQueryable<FluxnetSite> GetFluxnetSites()
|
|
{
|
|
return _db.FluxnetSites;
|
|
}
|
|
|
|
public IQueryable<FluxnetSite> GetFluxnetSitesAutocomplete(string term)
|
|
{
|
|
return
|
|
from fs in GetFluxnetSites()
|
|
where fs.FluxnetId.StartsWith(term)
|
|
|| fs.SiteName.Contains(term)
|
|
select fs;
|
|
}
|
|
#endregion
|
|
|
|
#region LeafInput
|
|
|
|
public IQueryable<LeafInput> GetLeafInputs()
|
|
{
|
|
return _db.LeafInputs;
|
|
}
|
|
|
|
public LeafInput GetLeafInput(int id)
|
|
{
|
|
return _db.LeafInputs.FirstOrDefault(li => li.Id == id);
|
|
}
|
|
|
|
public IQueryable<LeafInput> GetLeafInputs(LeafInputStatusType status)
|
|
{
|
|
return
|
|
from file in _db.LeafInputs
|
|
where file.CurrentStatus == status
|
|
select file;
|
|
}
|
|
|
|
public IQueryable<LeafInput> GetRunningLeafInputs()
|
|
{
|
|
return
|
|
from file in _db.LeafInputs
|
|
where
|
|
file.CurrentStatus == LeafInputStatusType.Running ||
|
|
file.CurrentStatus == LeafInputStatusType.Starting ||
|
|
file.CurrentStatus == LeafInputStatusType.Finishing
|
|
select file;
|
|
}
|
|
|
|
public void AddLeafInput(LeafInput leafInput)
|
|
{
|
|
leafInput.Added = DateTime.Now;
|
|
_db.LeafInputs.Add(leafInput);
|
|
SetLeafInputStatusNoUpdate(leafInput, LeafInputStatusType.Pending);
|
|
_db.SaveChanges();
|
|
}
|
|
|
|
private void SetLeafInputStatusNoUpdate(LeafInput leafInputFile, LeafInputStatusType status, string description = null, string details = null)
|
|
{
|
|
leafInputFile.CurrentStatus = status;
|
|
var leafInputFileStatus = new LeafInputStatus
|
|
{
|
|
Status = status,
|
|
DateTime = DateTime.Now,
|
|
Description = description,
|
|
Details = details,
|
|
LeafInput = leafInputFile
|
|
};
|
|
if (leafInputFile.StatusHistory == null)
|
|
leafInputFile.StatusHistory = new List<LeafInputStatus>();
|
|
leafInputFile.StatusHistory.Add(leafInputFileStatus);
|
|
}
|
|
|
|
public void SetLeafInputStatus(LeafInput leafInput, LeafInputStatusType status, string description = null, string details = null)
|
|
{
|
|
SetLeafInputStatusNoUpdate(leafInput, status, description, details);
|
|
UpdateLeafInput(leafInput);
|
|
}
|
|
|
|
public void UpdateLeafInput(LeafInput leafInput)
|
|
{
|
|
_db.Entry(leafInput).State = EntityState.Modified;
|
|
_db.SaveChanges();
|
|
}
|
|
|
|
#endregion
|
|
|
|
#region LeafInputFile
|
|
|
|
public IQueryable<LeafInputFile> GetLeafInputFiles()
|
|
{
|
|
return _db.LeafInputFiles;
|
|
}
|
|
|
|
public LeafInputFile GetLeafInputFile(int id)
|
|
{
|
|
return _db.LeafInputFiles.Find(id);
|
|
}
|
|
|
|
public void UpdateLeafInputFile(LeafInputFile leafInputFile)
|
|
{
|
|
_db.Entry(leafInputFile).State = EntityState.Modified;
|
|
_db.SaveChanges();
|
|
}
|
|
|
|
|
|
#endregion
|
|
|
|
#region Photosynthesis Types
|
|
|
|
public IQueryable<PhotosynthesisType> GetPhotosynthesisTypes()
|
|
{
|
|
return _db.PhotosynthesisTypes.OrderBy(pt => pt.SortOrder);
|
|
}
|
|
|
|
public PhotosynthesisType GetPhotosynthesisType(string id)
|
|
{
|
|
return _db.PhotosynthesisTypes.Find(id);
|
|
}
|
|
|
|
#endregion
|
|
|
|
#region LeafOutputFile
|
|
|
|
public void AddLeafOutputFile(LeafOutputFile leafOutput)
|
|
{
|
|
_db.LeafOutputFiles.Add(leafOutput);
|
|
_db.SaveChanges();
|
|
}
|
|
|
|
public IQueryable<LeafOutputFile> GetLeafOutputFiles()
|
|
{
|
|
return _db.LeafOutputFiles;
|
|
}
|
|
|
|
public LeafOutputFile GetLeafOutputFile(int id)
|
|
{
|
|
return _db.LeafOutputFiles.Find(id);
|
|
}
|
|
|
|
#endregion
|
|
}
|
|
}
|