LabSubd/Subd/MongoDB/VoyageStorage.cs
2023-05-08 15:15:10 +03:00

127 lines
4.9 KiB
C#

using Contracts.BindingModels;
using Contracts.SearchModel;
using Contracts.Storage;
using Contracts.ViewModels;
using MongoDB.Bson;
using MongoDB.Driver;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using static System.Net.Mime.MediaTypeNames;
namespace MongoDB
{
public class VoyageStorage : IVoyageStorage
{
public MongoClient contex;
public VoyageStorage()
{
contex = MongoDateBase.getInstance().client;
}
public VoyageVM? Delete(VoyageBM model)
{
throw new Exception("DONT DO IT!!!");
}
public VoyageVM? GetElement(VoyageSM model)
{
throw new Exception("DONT DO IT!!!");
}
public List<VoyageVM> GetFilteredList(VoyageSM model)
{
throw new Exception("DONT DO IT!!!");
}
public List<VoyageVM> GetFullList()
{
IMongoDatabase db = contex.GetDatabase("logistic");
var collection = db.GetCollection<BsonDocument>("Voyage");
List<BsonDocument> humans = collection.Find(new BsonDocument()).ToList();
List<VoyageVM> res = new List<VoyageVM>();
foreach (var human in humans)
{
res.Add(new VoyageVM
{
CarName = human["car"].ToString(),
HumanName = human["human"].ToString(),
RouteName = human["route"].ToString(),
CompanyName = human["company"].ToString(),
DateStart = DateOnly.Parse(human["date_From"].ToString().Substring(0,10)),
DateEnd = DateOnly.Parse(human["date_To"].ToString().Substring(0,10)),
});
}
return res;
}
public VoyageVM? Insert(VoyageBM model)
{
IMongoDatabase db = contex.GetDatabase("logistic");
var collection = db.GetCollection<BsonDocument>("Voyage");
BsonDocument human = new BsonDocument { { "car", model.CarName }, { "human", model.HumanName }, { "route", model.RouteName }, {"company", model.CompanyName } ,{ "date_From", DateTime.Parse(model.DateStart.ToString()) }, { "date_To", DateTime.Parse(model.DateEnd.ToString()) } };
collection.InsertOne(human);
return new VoyageVM {
CarName = human["car"].ToString(),
HumanName = human["human"].ToString(),
RouteName = human["route"].ToString(),
CompanyName = human["company"].ToString(),
DateStart = DateOnly.Parse(human["date_From"].ToString().Substring(0, 10)),
DateEnd = DateOnly.Parse(human["date_To"].ToString().Substring(0, 10))
};
}
public List<ReportCompany> ReportCompany()
{
IMongoDatabase db = contex.GetDatabase("logistic");
var collection = db.GetCollection<BsonDocument>("Voyage");
var builder = Builders<BsonDocument>.Filter;
DateTime date = DateTime.Now;
var filter = builder.Gt("date_From", date.AddYears(-1));
var res = collection.Aggregate().Match(filter).Group(
new BsonDocument {
{ "_id", new BsonDocument{{"company" , "$company"},{"route" , "$route" } } },
{ "total_count", new BsonDocument("$count", new BsonDocument{}) } }).ToList();
List<ReportCompany> list = new List<ReportCompany>();
foreach (var el in res)
{
var mid = el.GetValue("_id");
list.Add(new ReportCompany
{
Title = mid["company"].ToString(),
Details = mid["route"].ToString(),
Count = el["total_count"].ToInt32()
}); ;
}
return list;
}
public List<ReportHuman> ReportHuman()
{
IMongoDatabase db = contex.GetDatabase("logistic");
var collection = db.GetCollection<BsonDocument>("Voyage");
var builder = Builders<BsonDocument>.Filter;
DateTime date = DateTime.Now;
var filter = builder.Gt("date_From", date.AddMonths(-1));
var res = collection.Aggregate().Match(filter).Lookup("Route", "route", "name", "RouteObj").Unwind("RouteObj").Group(
new BsonDocument {
{ "_id", "$human" },
{ "total_length", new BsonDocument("$sum", "$RouteObj.length") } }).ToList();
List<ReportHuman> list = new List<ReportHuman>();
foreach(var el in res)
{
list.Add(new ReportHuman
{
Name = el["_id"].ToString(),
Length = el["total_length"].ToInt32()
});
}
return list;
}
}
}