111 lines
4.1 KiB
C#
111 lines
4.1 KiB
C#
|
using MongoDB.Bson;
|
|||
|
using MongoDB.Driver;
|
|||
|
using SportCompetitionsContracts.BindingModels;
|
|||
|
using SportCompetitionsContracts.SearchModels;
|
|||
|
using SportCompetitionsContracts.StoragesContracts;
|
|||
|
using SportCompetitionsContracts.ViewModels;
|
|||
|
using SportCompetitionsMongo.Models;
|
|||
|
using System;
|
|||
|
using System.Collections.Generic;
|
|||
|
using System.Linq;
|
|||
|
using System.Text;
|
|||
|
using System.Threading.Tasks;
|
|||
|
|
|||
|
namespace SportCompetitionsMongo.Implements
|
|||
|
{
|
|||
|
public class CompetitionStorage : ICompetitionStorage
|
|||
|
{
|
|||
|
|
|||
|
public void ClearEntity()
|
|||
|
{
|
|||
|
using var context = new SportCompetitionsMongoDB();
|
|||
|
context.GetCollection<Competition>("Competitions")
|
|||
|
.DeleteMany(Builders<Competition>.Filter.Empty);
|
|||
|
}
|
|||
|
|
|||
|
public List<CompetitionViewModel> GetFullList()
|
|||
|
{
|
|||
|
using var context = new SportCompetitionsMongoDB();
|
|||
|
var competitions = context.GetCollection<Competition>("Competitions");
|
|||
|
return competitions.Find(Builders<Competition>.Filter.Empty)
|
|||
|
.ToList()
|
|||
|
.Select(x => x.GetViewModel)
|
|||
|
.ToList();
|
|||
|
}
|
|||
|
public List<CompetitionViewModel> GetFilteredList(CompetitionSearchModel model)
|
|||
|
{
|
|||
|
if (string.IsNullOrEmpty(model.CompetitionName)) return new List<CompetitionViewModel>();
|
|||
|
using var context = new SportCompetitionsMongoDB();
|
|||
|
var competitions = context.GetCollection<Competition>("Competitions");
|
|||
|
|
|||
|
var filterBuilder = Builders<Competition>.Filter;
|
|||
|
var filter = filterBuilder.Regex(x => x.CompetitionName, new BsonRegularExpression(model.CompetitionName));
|
|||
|
return competitions
|
|||
|
.Find(filter)
|
|||
|
.ToList()
|
|||
|
.Select(x => x.GetViewModel)
|
|||
|
.ToList();
|
|||
|
}
|
|||
|
|
|||
|
public CompetitionViewModel? GetElement(CompetitionSearchModel model)
|
|||
|
{
|
|||
|
if (!model.Id.HasValue)
|
|||
|
{
|
|||
|
return null;
|
|||
|
}
|
|||
|
using (var context = new SportCompetitionsMongoDB())
|
|||
|
{
|
|||
|
var competitions = context.GetCollection<Competition>("Competitions");
|
|||
|
|
|||
|
var filterBuilder = Builders<Competition>.Filter;
|
|||
|
var filter = filterBuilder.Empty;
|
|||
|
|
|||
|
if (!model.Id.HasValue)
|
|||
|
{
|
|||
|
filter &= filterBuilder.Eq(x => x.Id, model.Id);
|
|||
|
}
|
|||
|
|
|||
|
return competitions.Find(filter)
|
|||
|
.FirstOrDefault()
|
|||
|
?.GetViewModel;
|
|||
|
}
|
|||
|
}
|
|||
|
public CompetitionViewModel? Insert(CompetitionBindingModel model)
|
|||
|
{
|
|||
|
using var context = new SportCompetitionsMongoDB();
|
|||
|
|
|||
|
var competitions = context.GetCollection<Competition>("Competitions");
|
|||
|
|
|||
|
model.Id = (int)competitions.CountDocuments(FilterDefinition<Competition>.Empty);
|
|||
|
|
|||
|
var competition = Competition.Create(model);
|
|||
|
competitions.InsertOne(competition);
|
|||
|
return competition.GetViewModel;
|
|||
|
}
|
|||
|
public CompetitionViewModel? Update(CompetitionBindingModel model)
|
|||
|
{
|
|||
|
using var context = new SportCompetitionsMongoDB();
|
|||
|
var competitions = context.GetCollection<Competition>("Competitions");
|
|||
|
|
|||
|
var filter = Builders<Competition>.Filter.Eq(x => x.Id, model.Id);
|
|||
|
var competition = competitions.Find(filter).FirstOrDefault();
|
|||
|
if (competition == null)
|
|||
|
{
|
|||
|
return null;
|
|||
|
}
|
|||
|
competition.Update(model);
|
|||
|
competitions.ReplaceOne(filter, competition);
|
|||
|
return competition.GetViewModel;
|
|||
|
}
|
|||
|
public CompetitionViewModel? Delete(CompetitionBindingModel model)
|
|||
|
{
|
|||
|
using var context = new SportCompetitionsMongoDB();
|
|||
|
var categories = context.GetCollection<Competition>("Competitions");
|
|||
|
|
|||
|
var filter = Builders<Competition>.Filter.Eq(x => x.Id, model.Id);
|
|||
|
var category = categories.FindOneAndDelete(filter);
|
|||
|
return category?.GetViewModel;
|
|||
|
}
|
|||
|
}
|
|||
|
}
|