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 GetFilteredList(VoyageSM model) { throw new Exception("DONT DO IT!!!"); } public List GetFullList() { IMongoDatabase db = contex.GetDatabase("logistic"); var collection = db.GetCollection("Voyage"); List humans = collection.Find(new BsonDocument()).ToList(); List res = new List(); 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("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() { IMongoDatabase db = contex.GetDatabase("logistic"); var collection = db.GetCollection("Voyage"); var builder = Builders.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 list = new List(); 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() { IMongoDatabase db = contex.GetDatabase("logistic"); var collection = db.GetCollection("Voyage"); var builder = Builders.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 list = new List(); foreach(var el in res) { list.Add(new ReportHuman { Name = el["_id"].ToString(), Length = el["total_length"].ToInt32() }); } return list; } } }