105 lines
3.6 KiB
C#
105 lines
3.6 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 RouteStorage : IRouteStorage
|
|
{
|
|
public MongoClient contex;
|
|
public RouteStorage()
|
|
{
|
|
contex = MongoDateBase.getInstance().client;
|
|
}
|
|
public RouteVM? Delete(RouteBM model)
|
|
{
|
|
IMongoDatabase db = contex.GetDatabase("logistic");
|
|
var collection = db.GetCollection<BsonDocument>("Route");
|
|
var res = collection.FindOneAndDelete(new BsonDocument("name", model.Title));
|
|
if (res == null)
|
|
return null;
|
|
return new RouteVM
|
|
{
|
|
Title = res["name"].ToString(),
|
|
|
|
};
|
|
}
|
|
|
|
public RouteVM? GetElement(RouteSM model)
|
|
{
|
|
IMongoDatabase db = contex.GetDatabase("logistic");
|
|
var collection = db.GetCollection<BsonDocument>("Route");
|
|
BsonDocument route = collection.Find(new BsonDocument("name", model.Title)).FirstOrDefault();
|
|
if (route == null)
|
|
return null;
|
|
return new RouteVM
|
|
{
|
|
Title = route["name"].ToString(),
|
|
};
|
|
}
|
|
|
|
public List<RouteVM> GetFilteredList(RouteSM model)
|
|
{
|
|
IMongoDatabase db = contex.GetDatabase("logistic");
|
|
var collection = db.GetCollection<BsonDocument>("Route");
|
|
List<BsonDocument> statuses = collection.Find(new BsonDocument { { "$regex", $"^.{model.Title}.$" } }).ToList();
|
|
List<RouteVM> res = new List<RouteVM>();
|
|
foreach (var status in statuses)
|
|
{
|
|
res.Add(new RouteVM
|
|
{
|
|
Title = status["name"].ToString(),
|
|
});
|
|
}
|
|
return res;
|
|
}
|
|
|
|
public List<RouteVM> GetFullList()
|
|
{
|
|
IMongoDatabase db = contex.GetDatabase("logistic");
|
|
var collection = db.GetCollection<BsonDocument>("Route");
|
|
List<BsonDocument> statuses = collection.Find(new BsonDocument()).ToList();
|
|
List<RouteVM> res = new List<RouteVM>();
|
|
foreach (var status in statuses)
|
|
{
|
|
res.Add(new RouteVM
|
|
{
|
|
Title = status["name"].ToString(),
|
|
Length = status["length"].ToInt32()
|
|
});
|
|
}
|
|
return res;
|
|
}
|
|
|
|
public RouteVM? Insert(RouteBM model)
|
|
{
|
|
IMongoDatabase db = contex.GetDatabase("logistic");
|
|
var collection = db.GetCollection<BsonDocument>("Route");
|
|
BsonDocument status = new BsonDocument { { "name", model.Title }, { "length", model.Length }, { "place_From" , model.PlaceStartName}, { "place_To", model.PlaceEndName } };
|
|
collection.InsertOne(status);
|
|
return new RouteVM { Title = model.Title, Length = model.Length };
|
|
}
|
|
|
|
public RouteVM? Update(RouteBM model)
|
|
{
|
|
IMongoDatabase db = contex.GetDatabase("logistic");
|
|
var collection = db.GetCollection<BsonDocument>("Route");
|
|
var result = collection.UpdateOneAsync(new BsonDocument("name", model.oldTitle), new BsonDocument("$set", new BsonDocument("name", model.Title)));
|
|
if (result == null)
|
|
return null;
|
|
return new RouteVM
|
|
{
|
|
Title = model.Title,
|
|
};
|
|
}
|
|
}
|
|
}
|