128 lines
3.0 KiB
C#
128 lines
3.0 KiB
C#
|
using Bogus;
|
|||
|
using BulletinBoardContracts.BindingModels;
|
|||
|
using BulletinBoardContracts.SearchModels;
|
|||
|
using BulletinBoardContracts.StoragesContracts;
|
|||
|
using BulletinBoardContracts.ViewModels;
|
|||
|
using BulletinBoardDatabase.Models;
|
|||
|
using Microsoft.EntityFrameworkCore;
|
|||
|
using Microsoft.Win32;
|
|||
|
using System;
|
|||
|
using System.Collections.Generic;
|
|||
|
using System.Diagnostics;
|
|||
|
using System.Linq;
|
|||
|
using System.Numerics;
|
|||
|
using System.Text;
|
|||
|
using System.Threading.Tasks;
|
|||
|
|
|||
|
namespace BulletinBoardDatabase.Implements
|
|||
|
{
|
|||
|
public class RegionStorage : IRegionStorage
|
|||
|
{
|
|||
|
public void ClearEntity()
|
|||
|
{
|
|||
|
using var context = new BulletinBoardDatabase();
|
|||
|
|
|||
|
string deleteAllQuery = "DELETE FROM \"Regions\"";
|
|||
|
context.Database.ExecuteSqlRaw(deleteAllQuery);
|
|||
|
}
|
|||
|
|
|||
|
public void RegionInsertList(int num)
|
|||
|
{
|
|||
|
using var context = new BulletinBoardDatabase();
|
|||
|
|
|||
|
var faker = new Faker("ru");
|
|||
|
for (int i = 0; i < num; ++i)
|
|||
|
{
|
|||
|
var model = new RegionBindingModel
|
|||
|
{
|
|||
|
RegionName = faker.Address.Country(),
|
|||
|
};
|
|||
|
context.Regions.Add(Region.Create(model));
|
|||
|
}
|
|||
|
context.SaveChanges();
|
|||
|
}
|
|||
|
|
|||
|
public RegionViewModel? GetElement(RegionSearchModel model)
|
|||
|
{
|
|||
|
if (string.IsNullOrEmpty(model.RegionName) && !model.Id.HasValue)
|
|||
|
{
|
|||
|
return null;
|
|||
|
}
|
|||
|
|
|||
|
using var context = new BulletinBoardDatabase();
|
|||
|
|
|||
|
return context.Regions
|
|||
|
.FirstOrDefault(x =>
|
|||
|
(!string.IsNullOrEmpty(model.RegionName) && x.RegionName ==
|
|||
|
model.RegionName) ||
|
|||
|
(model.Id.HasValue && x.Id == model.Id))
|
|||
|
?.GetViewModel;
|
|||
|
}
|
|||
|
|
|||
|
public List<RegionViewModel> GetFilteredList(RegionSearchModel model)
|
|||
|
{
|
|||
|
if (string.IsNullOrEmpty(model.RegionName))
|
|||
|
{
|
|||
|
return new();
|
|||
|
}
|
|||
|
|
|||
|
using var context = new BulletinBoardDatabase();
|
|||
|
|
|||
|
return context.Regions
|
|||
|
.Where(x => x.RegionName.Contains(model.RegionName))
|
|||
|
.Select(x => x.GetViewModel)
|
|||
|
.ToList();
|
|||
|
}
|
|||
|
|
|||
|
public List<RegionViewModel> GetFullList()
|
|||
|
{
|
|||
|
using var context = new BulletinBoardDatabase();
|
|||
|
return context.Regions
|
|||
|
.Select(x => x.GetViewModel)
|
|||
|
.ToList();
|
|||
|
}
|
|||
|
|
|||
|
public RegionViewModel? Insert(RegionBindingModel model)
|
|||
|
{
|
|||
|
var newRegion = Region.Create(model);
|
|||
|
if (newRegion == null)
|
|||
|
{
|
|||
|
return null;
|
|||
|
}
|
|||
|
using var context = new BulletinBoardDatabase();
|
|||
|
|
|||
|
context.Regions.Add(newRegion);
|
|||
|
context.SaveChanges();
|
|||
|
return newRegion.GetViewModel;
|
|||
|
}
|
|||
|
|
|||
|
public RegionViewModel? Update(RegionBindingModel model)
|
|||
|
{
|
|||
|
using var context = new BulletinBoardDatabase();
|
|||
|
|
|||
|
var component = context.Regions.FirstOrDefault(x => x.Id == model.Id);
|
|||
|
if (component == null)
|
|||
|
{
|
|||
|
return null;
|
|||
|
}
|
|||
|
component.Update(model);
|
|||
|
context.SaveChanges();
|
|||
|
return component.GetViewModel;
|
|||
|
}
|
|||
|
|
|||
|
public RegionViewModel? Delete(RegionBindingModel model)
|
|||
|
{
|
|||
|
using var context = new BulletinBoardDatabase();
|
|||
|
|
|||
|
var element = context.Regions.FirstOrDefault(rec => rec.Id == model.Id);
|
|||
|
if (element != null)
|
|||
|
{
|
|||
|
context.Regions.Remove(element);
|
|||
|
context.SaveChanges();
|
|||
|
return element.GetViewModel;
|
|||
|
}
|
|||
|
return null;
|
|||
|
}
|
|||
|
}
|
|||
|
}
|