197 lines
6.6 KiB
C#
197 lines
6.6 KiB
C#
using AutoMapper;
|
|
using Microsoft.EntityFrameworkCore;
|
|
using MiniSoftContracts.ContractStorage;
|
|
using MiniSoftContracts.Data_Models;
|
|
using MiniSoftContracts.Exceptions;
|
|
using MiniSoftDatabase.Models;
|
|
using Npgsql;
|
|
using System;
|
|
using System.Collections.Generic;
|
|
using System.Linq;
|
|
using System.Text;
|
|
using System.Threading.Tasks;
|
|
|
|
namespace MiniSoftDatabase.Implementations
|
|
{
|
|
internal class PostStorageContract : IPostStorageContract
|
|
{
|
|
private readonly MiniSoftDbContext _dbContext;
|
|
private readonly Mapper _mapper;
|
|
|
|
public PostStorageContract(MiniSoftDbContext dbContext)
|
|
{
|
|
_dbContext = dbContext;
|
|
var config = new MapperConfiguration(cfg =>
|
|
{
|
|
cfg.CreateMap<PostDataModel, Post>()
|
|
.ForMember(x => x.Id, x => x.Ignore())
|
|
.ForMember(x => x.PostId, x => x.MapFrom(src => src.Id))
|
|
.ForMember(x => x.IsActual, x => x.MapFrom(src => true))
|
|
.ForMember(x => x.ChangeDate, x => x.MapFrom(src => DateTime.UtcNow));
|
|
cfg.CreateMap<Post, PostDataModel>()
|
|
.ForMember(x => x.Id, x => x.MapFrom(src => src.PostId)); // проверить айди на совпадения
|
|
});
|
|
_mapper = new Mapper(config);
|
|
}
|
|
public List<PostDataModel> GetList(bool onlyActual = true)
|
|
{
|
|
try
|
|
{
|
|
var query = _dbContext.Posts.AsQueryable();
|
|
if (onlyActual)
|
|
{
|
|
query = query.Where(x => x.IsActual);
|
|
}
|
|
return [.. query.Select(x => _mapper.Map<PostDataModel>(x))];
|
|
}
|
|
catch (Exception ex)
|
|
{
|
|
_dbContext.ChangeTracker.Clear();
|
|
throw new StorageException(ex);
|
|
}
|
|
}
|
|
|
|
|
|
public PostDataModel? GetElementById(string id)
|
|
{
|
|
try
|
|
{
|
|
return _mapper.Map<PostDataModel>(_dbContext.Posts.FirstOrDefault(x => x.PostId == id && x.IsActual));
|
|
}
|
|
catch (Exception ex)
|
|
{
|
|
_dbContext.ChangeTracker.Clear();
|
|
throw new StorageException(ex);
|
|
}
|
|
}
|
|
|
|
public PostDataModel? GetElementByName(string name)
|
|
{
|
|
try
|
|
{
|
|
return _mapper.Map<PostDataModel>(_dbContext.Posts.FirstOrDefault(x => x.PostName == name && x.IsActual));
|
|
}
|
|
catch (Exception ex)
|
|
{
|
|
_dbContext.ChangeTracker.Clear();
|
|
throw new StorageException(ex);
|
|
}
|
|
}
|
|
|
|
|
|
public List<PostDataModel> GetPostWithHistory(string postId)
|
|
{
|
|
try
|
|
{
|
|
return [.. _dbContext.Posts.Where(x => x.PostId == postId).Select(x => _mapper.Map<PostDataModel>(x))];
|
|
}
|
|
catch (Exception ex)
|
|
{
|
|
_dbContext.ChangeTracker.Clear();
|
|
throw new StorageException(ex);
|
|
}
|
|
}
|
|
|
|
public void RestoreElement(string id)
|
|
{
|
|
try
|
|
{
|
|
var element = GetPostById(id) ?? throw new ElementNotFoundException(id);
|
|
element.IsActual = true;
|
|
_dbContext.SaveChanges();
|
|
}
|
|
catch
|
|
{
|
|
_dbContext.ChangeTracker.Clear();
|
|
throw;
|
|
}
|
|
}
|
|
|
|
public void AddElement(PostDataModel model)
|
|
{
|
|
try
|
|
{
|
|
_dbContext.Posts.Add(_mapper.Map<Post>(model));
|
|
_dbContext.SaveChanges();
|
|
}
|
|
catch (DbUpdateException ex) when (ex.InnerException is PostgresException { ConstraintName: "IX_Posts_PostName_IsActual" })
|
|
{
|
|
_dbContext.ChangeTracker.Clear();
|
|
throw new ElementExistsException("PostName", model.PostName);
|
|
}
|
|
catch (DbUpdateException ex) when (ex.InnerException is PostgresException { ConstraintName: "IX_Posts_PostId_IsActual" })
|
|
{
|
|
_dbContext.ChangeTracker.Clear();
|
|
throw new ElementExistsException("PostId", model.Id);
|
|
}
|
|
catch (Exception ex)
|
|
{
|
|
_dbContext.ChangeTracker.Clear();
|
|
throw new StorageException(ex);
|
|
}
|
|
}
|
|
public void UpdateElement(PostDataModel model)
|
|
{
|
|
try
|
|
{
|
|
var transaction = _dbContext.Database.BeginTransaction();
|
|
try
|
|
{
|
|
var element = GetPostById(model.Id) ?? throw new ElementNotFoundException(model.Id);
|
|
if (!element.IsActual)
|
|
{
|
|
throw new ElementDeletedException(model.Id);
|
|
}
|
|
element.IsActual = false;
|
|
_dbContext.SaveChanges();
|
|
var newElement = _mapper.Map<Post>(model);
|
|
_dbContext.Posts.Add(newElement);
|
|
_dbContext.SaveChanges();
|
|
transaction.Commit();
|
|
}
|
|
catch
|
|
{
|
|
transaction.Rollback();
|
|
throw;
|
|
}
|
|
}
|
|
catch (DbUpdateException ex) when (ex.InnerException is PostgresException { ConstraintName: "IX_Posts_PostName_IsActual" })
|
|
{
|
|
_dbContext.ChangeTracker.Clear();
|
|
throw new ElementExistsException("PostName", model.PostName);
|
|
}
|
|
catch (Exception ex) when (ex is ElementDeletedException || ex is ElementNotFoundException)
|
|
{
|
|
_dbContext.ChangeTracker.Clear();
|
|
throw;
|
|
}
|
|
catch (Exception ex)
|
|
{
|
|
_dbContext.ChangeTracker.Clear();
|
|
throw new StorageException(ex);
|
|
}
|
|
}
|
|
public void DeleteElement(string id)
|
|
{
|
|
try
|
|
{
|
|
var element = GetPostById(id) ?? throw new ElementNotFoundException(id);
|
|
if (!element.IsActual)
|
|
{
|
|
throw new ElementDeletedException(id);
|
|
}
|
|
element.IsActual = false;
|
|
_dbContext.SaveChanges();
|
|
}
|
|
catch
|
|
{
|
|
_dbContext.ChangeTracker.Clear();
|
|
throw;
|
|
}
|
|
}
|
|
|
|
|
|
private Post? GetPostById(string id) => _dbContext.Posts.Where(x => x.PostId == id).OrderByDescending(x => x.ChangeDate).FirstOrDefault();
|
|
}
|
|
}
|