133 lines
3.1 KiB
C#
133 lines
3.1 KiB
C#
using Bogus;
|
|
using BulletinBoardContracts.BindingModels;
|
|
using BulletinBoardContracts.SearchModels;
|
|
using BulletinBoardContracts.StoragesContracts;
|
|
using BulletinBoardContracts.ViewModels;
|
|
using BulletinBoardDatabase.Models;
|
|
using Microsoft.EntityFrameworkCore;
|
|
using System;
|
|
using System.Collections.Generic;
|
|
using System.Linq;
|
|
using System.Text;
|
|
using System.Threading.Tasks;
|
|
|
|
namespace BulletinBoardDatabase.Implements
|
|
{
|
|
public class CityStorage : ICityStorage
|
|
{
|
|
public void ClearEntity()
|
|
{
|
|
using var context = new BulletinBoardDatabase();
|
|
string deleteAllQuery = "DELETE FROM \"Cities\"";
|
|
context.Database.ExecuteSqlRaw(deleteAllQuery);
|
|
}
|
|
|
|
public CityViewModel? GetElement(CitySearchModel model)
|
|
{
|
|
using var context = new BulletinBoardDatabase();
|
|
|
|
return context.Cities.FirstOrDefault(x =>
|
|
(model.Id.HasValue && x.Id == model.Id))
|
|
?.GetViewModel;
|
|
}
|
|
|
|
public void CityInsertList(int num, List<RegionViewModel> regions)
|
|
{
|
|
Random rnd = new Random();
|
|
var faker = new Faker("ru");
|
|
using var context = new BulletinBoardDatabase();
|
|
|
|
for (int i = 0; i < num; ++i)
|
|
{
|
|
var model = new CityBindingModel
|
|
{
|
|
CityName = faker.Address.City(),
|
|
RegionId = regions[rnd.Next(regions.Count)].Id,
|
|
};
|
|
context.Cities.Add(City.Create(model));
|
|
}
|
|
context.SaveChanges();
|
|
}
|
|
|
|
public List<CityViewModel> GetFilteredList(CitySearchModel model)
|
|
{
|
|
using var context = new BulletinBoardDatabase();
|
|
|
|
if (model.Id.HasValue)
|
|
{
|
|
return context.Cities
|
|
.Where(x => x.Id == model.Id)
|
|
.Select(x => x.GetViewModel)
|
|
.ToList();
|
|
}
|
|
else if (model.CityName != string.Empty)
|
|
{
|
|
return context.Cities
|
|
.Where(x => x.CityName == model.CityName)
|
|
.Select(x => x.GetViewModel)
|
|
.ToList();
|
|
}
|
|
else if (model.RegionId.HasValue)
|
|
{
|
|
return context.Cities
|
|
.Where(x => x.RegionId <= model.RegionId)
|
|
.Select(x => x.GetViewModel)
|
|
.ToList();
|
|
}
|
|
|
|
return null;
|
|
}
|
|
|
|
public List<CityViewModel> GetFullList()
|
|
{
|
|
using var context = new BulletinBoardDatabase();
|
|
return context.Cities
|
|
.Include(x => x.Region)
|
|
.Select(x => x.GetViewModel)
|
|
.ToList();
|
|
}
|
|
|
|
public CityViewModel? Insert(CityBindingModel model)
|
|
{
|
|
var newCity = City.Create(model);
|
|
if (newCity == null)
|
|
{
|
|
return null;
|
|
}
|
|
using var context = new BulletinBoardDatabase();
|
|
|
|
context.Cities.Add(newCity);
|
|
context.SaveChanges();
|
|
return newCity.GetViewModel;
|
|
}
|
|
|
|
public CityViewModel? Update(CityBindingModel model)
|
|
{
|
|
using var context = new BulletinBoardDatabase();
|
|
|
|
var component = context.Cities.FirstOrDefault(x => x.Id == model.Id);
|
|
if (component == null)
|
|
{
|
|
return null;
|
|
}
|
|
component.Update(model);
|
|
context.SaveChanges();
|
|
return component.GetViewModel;
|
|
}
|
|
|
|
public CityViewModel? Delete(CityBindingModel model)
|
|
{
|
|
using var context = new BulletinBoardDatabase();
|
|
|
|
var element = context.Cities.FirstOrDefault(rec => rec.Id == model.Id);
|
|
if (element != null)
|
|
{
|
|
context.Cities.Remove(element);
|
|
context.SaveChanges();
|
|
return element.GetViewModel;
|
|
}
|
|
return null;
|
|
}
|
|
}
|
|
}
|