91 lines
3.3 KiB
C#
91 lines
3.3 KiB
C#
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;
|
|
|
|
namespace SportCompetitionsMongo.Implements
|
|
{
|
|
public class ResultStorage : IResultStorage
|
|
{
|
|
|
|
public void ClearEntity()
|
|
{
|
|
using var context = new SportCompetitionsMongoDB();
|
|
context.GetCollection<Result>("Results")
|
|
.DeleteMany(Builders<Result>.Filter.Empty);
|
|
}
|
|
|
|
public List<ResultViewModel> GetFullList()
|
|
{
|
|
using var context = new SportCompetitionsMongoDB();
|
|
var results = context.GetCollection<Result>("Results").Find(Builders<Result>.Filter.Empty).ToList();
|
|
|
|
return results.Select(x => x.GetViewModel).ToList();
|
|
}
|
|
public List<ResultViewModel> GetFilteredList(ResultSearchModel model)
|
|
{
|
|
using var context = new SportCompetitionsMongoDB();
|
|
if (!model.Id.HasValue) return new List<ResultViewModel>();
|
|
|
|
var filterBuilder = Builders<Result>.Filter;
|
|
var filter = filterBuilder.Empty;
|
|
filter = filterBuilder.Eq("Id", model.Id);
|
|
|
|
var results = context.GetCollection<Result>("Results").Find(filter).ToList();
|
|
|
|
return results.Select(x => x.GetViewModel).ToList();
|
|
}
|
|
|
|
public ResultViewModel? GetElement(ResultSearchModel model)
|
|
{
|
|
using var context = new SportCompetitionsMongoDB();
|
|
var results = context.GetCollection<Result>("Results");
|
|
if (!model.Id.HasValue) return null;
|
|
return results.Find(x => x.Id == model.Id).FirstOrDefault()?.GetViewModel;
|
|
}
|
|
public ResultViewModel? Insert(ResultBindingModel model)
|
|
{
|
|
using var context = new SportCompetitionsMongoDB();
|
|
|
|
|
|
var results = context.GetCollection<Result>("Results");
|
|
|
|
model.Id = (int)results.CountDocuments(FilterDefinition<Result>.Empty);
|
|
|
|
var newResult = Result.Create(model);
|
|
|
|
if (newResult == null) return null;
|
|
|
|
|
|
results.InsertOne(newResult);
|
|
return newResult.GetViewModel;
|
|
}
|
|
public ResultViewModel? Update(ResultBindingModel model)
|
|
{
|
|
using var context = new SportCompetitionsMongoDB();
|
|
var result = context.GetCollection<Result>("Results").Find(x => x.Id == model.Id).FirstOrDefault();
|
|
if (result == null) return null;
|
|
result.Update(model);
|
|
context.GetCollection<Result>("Results").ReplaceOne(x => x.Id == model.Id, result);
|
|
return result.GetViewModel;
|
|
}
|
|
public ResultViewModel? Delete(ResultBindingModel model)
|
|
{
|
|
using var context = new SportCompetitionsMongoDB();
|
|
var element = context.GetCollection<Result>("Results").Find(x => x.Id == model.Id).FirstOrDefault();
|
|
if (element != null)
|
|
{
|
|
context.GetCollection<Result>("Results").DeleteOne(x => x.Id == model.Id);
|
|
return element.GetViewModel;
|
|
}
|
|
return null;
|
|
}
|
|
}
|
|
}
|