LabSubd/Subd/MongoDB/VoyageStorage.cs

78 lines
2.8 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))
};
}
}
}