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

122 lines
4.1 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;
namespace MongoDB
{
public class CarStorage : ICarStorage
{
public MongoClient contex;
public CarStorage()
{
contex = MongoDateBase.getInstance().client;
}
public CarVM? Delete(CarBM model)
{
IMongoDatabase db = contex.GetDatabase("logistic");
var collection = db.GetCollection<BsonDocument>("Car");
var res = collection.FindOneAndDelete(new BsonDocument("model", model.Model));
if (res == null)
return null;
return new CarVM
{
Model = res["model"].ToString(),
StatusTitle = res["Status"].ToString(),
};
}
public CarVM? GetElement(CarSM model)
{
IMongoDatabase db = contex.GetDatabase("logistic");
var collection = db.GetCollection<BsonDocument>("Car");
BsonDocument car = collection.Find(new BsonDocument{ {"model",model.Model } }).FirstOrDefault();
if (car == null)
return null;
return new CarVM
{
Tonnage = car["tonnage"].ToInt32(),
StatusTitle = car["Status"].ToString()
};
}
public List<CarVM> GetFilteredList(CarSM model)
{
IMongoDatabase db = contex.GetDatabase("logistic");
var collection = db.GetCollection<BsonDocument>("Car");
List<BsonDocument> cars = collection.Find(new BsonDocument { { "$regex", $"^.{model.Model}.$" } }).ToList();
List<CarVM> res = new List<CarVM>();
foreach (var car in cars)
{
res.Add(new CarVM
{
Model = car["name"].ToString(),
number = car["phone"].ToString(),
Tonnage = car["tonnage"].ToInt32(),
StatusTitle = car["Status"].ToString()
});
}
return res;
}
public List<CarVM> GetFullList()
{
IMongoDatabase db = contex.GetDatabase("logistic");
var collection = db.GetCollection<BsonDocument>("Car");
List<BsonDocument> cars = collection.Find(new BsonDocument()).ToList();
List<CarVM> res = new List<CarVM>();
foreach (var car in cars)
{
res.Add(new CarVM
{
Model = car["model"].ToString(),
Tonnage = car["tonnage"].ToInt32(),
StatusTitle = car["Status"].ToString()
});
}
return res;
}
public CarVM? Insert(CarBM model)
{
IMongoDatabase db = contex.GetDatabase("logistic");
var collection = db.GetCollection<BsonDocument>("Car");
BsonDocument car = new BsonDocument { { "model", model.Model }, { "Status", model.StatusTitle }, { "tonnage", model.Tonnage } };
collection.InsertOne(car);
return new CarVM {
Model = car["model"].ToString(),
Tonnage = car["tonnage"].ToInt32(),
StatusTitle = car["Status"].ToString()
};
}
public CarVM? Update(CarBM model)
{
IMongoDatabase db = contex.GetDatabase("logistic");
var collection = db.GetCollection<BsonDocument>("Car");
var result = collection.UpdateOneAsync(new BsonDocument("name", model.oldModel), new BsonDocument("$set", new BsonDocument("name", model.Model)));
if (result == null)
return null;
return new CarVM
{
Model = model.Model,
StatusTitle = model.StatusTitle,
Tonnage = model.Tonnage,
};
}
}
}