удалила сущность

This commit is contained in:
Полина Чубыкина 2024-05-21 21:01:50 +04:00
parent 9e8e7a4f0e
commit af5041044f
23 changed files with 103 additions and 418 deletions

View File

@ -1,112 +0,0 @@
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using BookshopContracts.BindingModels;
using BookshopContracts.SearchModels;
using BookshopContracts.ViewModels;
using BookshopContracts.BusinessLogicsContracts;
using BookshopContracts.StorageContracts;
using Microsoft.Extensions.Logging;
namespace BookshopBusinessLogic.BusinessLogics
{
public class InstanceLogic : IInstanceLogic
{
private readonly ILogger _logger;
private readonly IInstanceStorage _InstanceStorage;
public InstanceLogic(ILogger<InstanceLogic> logger, IInstanceStorage InstanceStorage)
{
_logger = logger;
_InstanceStorage = InstanceStorage;
}
public List<InstanceViewModel>? ReadList(InstanceSearchModel? model)
{
_logger.LogInformation("ReadList. BookId:{BookId}. Id:{Id}", model?.BookId, model?.Id);
var list = model == null ? _InstanceStorage.GetFullList() : _InstanceStorage.GetFilteredList(model);
if (list == null)
{
_logger.LogWarning("ReadList return null list");
return null;
}
_logger.LogInformation("ReadList. Count:{Count}", list.Count);
return list;
}
public InstanceViewModel? ReadElement(InstanceSearchModel model)
{
if (model == null)
{
throw new ArgumentNullException(nameof(model));
}
_logger.LogInformation("ReadElement. BookId:{BookId}. Id:{Id}", model.BookId, model.Id);
var element = _InstanceStorage.GetElement(model);
if (element == null)
{
_logger.LogWarning("ReadElement element not found");
return null;
}
_logger.LogInformation("ReadElement find. Id:{Id}", element.Id);
return element;
}
public bool Create(InstanceBindingModel model)
{
CheckModel(model);
if (_InstanceStorage.Insert(model) == null)
{
_logger.LogWarning("Insert operation failed");
return false;
}
return true;
}
public bool Update(InstanceBindingModel model)
{
CheckModel(model);
if (_InstanceStorage.Update(model) == null)
{
_logger.LogWarning("Update operation failed");
return false;
}
return true;
}
public bool Delete(InstanceBindingModel model)
{
CheckModel(model, false);
_logger.LogInformation("Delete. Id:{Id}", model.Id);
if (_InstanceStorage.Delete(model) == null)
{
_logger.LogWarning("Delete operation failed");
return false;
}
return true;
}
private void CheckModel(InstanceBindingModel model, bool withParams = true)
{
if (model == null)
{
throw new ArgumentNullException(nameof(model));
}
if (!withParams)
{
return;
}
if (model.StockQuantity <= 0)
{
throw new ArgumentNullException("Количество экзмепляров должно быть больше 0", nameof(model.StockQuantity));
}
if (model.Price <= 0)
{
throw new ArgumentNullException("Цена книги должна быть больше 0", nameof(model.Price));
}
_logger.LogInformation("Instance. Id:{Id}", model.Id);
}
}
}

View File

@ -100,9 +100,9 @@ namespace BookshopBusinessLogic.BusinessLogics
{
throw new ArgumentNullException("Количество книг в заказе должно быть больше 0", nameof(model.Quantity));
}
if (model.InstanceId <= 0)
if (model.BookId <= 0)
{
throw new ArgumentNullException("Не указан идентификатор экземляра", nameof(model.InstanceId));
throw new ArgumentNullException("Не указан идентификатор книги", nameof(model.BookId));
}
if (model.UserId <= 0)
{

View File

@ -100,9 +100,9 @@ namespace BookshopBusinessLogic.BusinessLogics
{
throw new ArgumentNullException("Нет текста отзыва", nameof(model.ReviewText));
}
if (model.InstanceId <= 0)
if (model.BookId <= 0)
{
throw new ArgumentNullException("Не указан идентификатор экземляра", nameof(model.InstanceId));
throw new ArgumentNullException("Не указан идентификатор книги", nameof(model.BookId));
}
if (model.UserId <= 0)
{

View File

@ -1,19 +0,0 @@
using BookshopDataModels.Models;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
namespace BookshopContracts.BindingModels
{
public class InstanceBindingModel : IInstanceModel
{
public int Id { get; set; }
public bool Pictures { get; set; }
public int StockQuantity { get; set; }
public double Price { get; set; }
public int BookId { get; set; }
public int PublisherId { get; set; }
}
}

View File

@ -12,7 +12,7 @@ namespace BookshopContracts.BindingModels
public int Id { get; set; }
public DateTime OrderDate { get; set; }
public int Quantity { get; set; }
public int InstanceId { get; set; }
public int BookId { get; set; }
public int UserId { get; set; }
}
}

View File

@ -12,7 +12,7 @@ namespace BookshopContracts.BindingModels
public int Id { get; set; }
public string ReviewText { get; set; } = string.Empty;
public DateTime ReviewDate { get; set; }
public int InstanceId { get; set; }
public int BookId { get; set; }
public int UserId { get; set; }
}
}

View File

@ -1,20 +0,0 @@
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using BookshopContracts.BindingModels;
using BookshopContracts.ViewModels;
using BookshopContracts.SearchModels;
namespace BookshopContracts.BusinessLogicsContracts
{
public interface IInstanceLogic
{
List<InstanceViewModel>? ReadList(InstanceSearchModel? model);
InstanceViewModel? ReadElement(InstanceSearchModel model);
bool Create(InstanceBindingModel model);
bool Update(InstanceBindingModel model);
bool Delete(InstanceBindingModel model);
}
}

View File

@ -1,18 +0,0 @@
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
namespace BookshopContracts.SearchModels
{
public class InstanceSearchModel
{
public int? Id { get; set; }
public bool? Pictures { get; set; }
public int? StockQuantity { get; set; }
public double? Price { get; set; }
public int? BookId { get; set; }
public int? PublisherId { get; set; }
}
}

View File

@ -11,7 +11,7 @@ namespace BookshopContracts.SearchModels
public int? Id { get; set; }
public DateTime? OrderDate { get; set; }
public int? Quantity { get; set; }
public int? InstanceId { get; set; }
public int? BookId { get; set; }
public int? UserId { get; set; }
}
}

View File

@ -11,7 +11,7 @@ namespace BookshopContracts.SearchModels
public int? Id { get; set; }
public string? ReviewText { get; set; } = string.Empty;
public DateTime? ReviewDate { get; set; }
public int? InstanceId { get; set; }
public int? BookId { get; set; }
public int? UserId { get; set; }
}
}

View File

@ -1,21 +0,0 @@
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using BookshopContracts.BindingModels;
using BookshopContracts.ViewModels;
using BookshopContracts.SearchModels;
namespace BookshopContracts.StorageContracts
{
public interface IInstanceStorage
{
List<InstanceViewModel> GetFullList();
List<InstanceViewModel> GetFilteredList(InstanceSearchModel model);
InstanceViewModel? GetElement(InstanceSearchModel model);
InstanceViewModel? Insert(InstanceBindingModel model);
InstanceViewModel? Update(InstanceBindingModel model);
InstanceViewModel? Delete(InstanceBindingModel model);
}
}

View File

@ -1,22 +0,0 @@
using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
namespace BookshopContracts.ViewModels
{
public class InstanceViewModel
{
public int Id { get; set; }
[DisplayName("Наличие картинок")]
public bool Pictures { get; set; }
[DisplayName("Количество экземлпяров")]
public int StockQuantity { get; set; }
[DisplayName("Стоимость книги")]
public double Price { get; set; }
public int BookId { get; set; }
public int PublisherId { get; set; }
}
}

View File

@ -14,7 +14,7 @@ namespace BookshopContracts.ViewModels
public DateTime OrderDate { get; set; }
[DisplayName("Количество")]
public int Quantity { get; set; }
public int InstanceId { get; set; }
public int BookId { get; set; }
public int UserId { get; set; }
}
}

View File

@ -14,7 +14,7 @@ namespace BookshopContracts.ViewModels
public string ReviewText { get; set; } = string.Empty;
[DisplayName("Дата отзыва")]
public DateTime ReviewDate { get; set; }
public int InstanceId { get; set; }
public int BookId { get; set; }
public int UserId { get; set; }
}
}

View File

@ -1,17 +0,0 @@
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
namespace BookshopDataModels.Models
{
public interface IInstanceModel : IId
{
bool Pictures { get; }
int StockQuantity { get; }
double Price { get; }
int BookId { get; }
int PublisherId { get; }
}
}

View File

@ -10,7 +10,7 @@ namespace BookshopDataModels.Models
{
DateTime OrderDate { get; }
int Quantity { get; }
int InstanceId { get; }
int BookId { get; }
int UserId { get; }
}
}

View File

@ -10,7 +10,7 @@ namespace BookshopDataModels.Models
{
string ReviewText { get; }
DateTime ReviewDate { get; }
int InstanceId { get; }
int BookId { get; }
int UserId { get; }
}
}

View File

@ -21,7 +21,6 @@ namespace BookshopDatabaseImplement
public virtual DbSet<Author> Authors { set; get; }
public virtual DbSet<Book> Books { set; get; }
public virtual DbSet<Genre> Genres { set; get; }
public virtual DbSet<Instance> Instances { set; get; }
public virtual DbSet<Order> Orders { set; get; }
public virtual DbSet<Publisher> Publishers { set; get; }
public virtual DbSet<Review> Reviews { set; get; }

View File

@ -1,95 +0,0 @@
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using BookshopContracts.StorageContracts;
using BookshopContracts.ViewModels;
using BookshopContracts.SearchModels;
using BookshopContracts.BindingModels;
using BookshopDatabaseImplement.Models;
using Microsoft.EntityFrameworkCore;
namespace BookshopDatabaseImplement.Implemets
{
public class InstanceStorage : IInstanceStorage
{
public List<InstanceViewModel> GetFullList()
{
using var context = new BookshopDatabase();
return context.Instances
.Include(x => x.Book)
.Include(x => x.Publisher)
.Select(x => x.GetViewModel)
.ToList();
}
public List<InstanceViewModel> GetFilteredList(InstanceSearchModel model)
{
if (string.IsNullOrEmpty(model.Title))
{
return new();
}
using var context = new BookshopDatabase();
return context.Instances
.Include(x => x.Author)
.Include(x => x.Genre)
.Where(x => x.Title.Contains(model.Title))
.Select(x => x.GetViewModel)
.ToList();
}
public InstanceViewModel? GetElement(InstanceSearchModel model)
{
if (string.IsNullOrEmpty(model.Title) && !model.Id.HasValue)
{
return null;
}
using var context = new BookshopDatabase();
return context.Instances
.Include(x => x.Author)
.Include(x => x.Genre)
.FirstOrDefault(x => x.Id.Equals(model.Id))
?.GetViewModel;
}
public InstanceViewModel? Insert(InstanceBindingModel model)
{
var newInstance = Instance.Create(model);
if (newInstance == null)
{
return null;
}
using var context = new BookshopDatabase();
context.Instances.Add(newInstance);
context.SaveChanges();
return newInstance.GetViewModel;
}
public InstanceViewModel? Update(InstanceBindingModel model)
{
using var context = new BookshopDatabase();
var client = context.Instances.FirstOrDefault(x => x.Id == model.Id);
if (client == null)
{
return null;
}
client.Update(model);
context.SaveChanges();
return client.GetViewModel;
}
public InstanceViewModel? Delete(InstanceBindingModel model)
{
using var context = new BookshopDatabase();
var element = context.Instances.FirstOrDefault(x => x.Id == model.Id);
if (element != null)
{
context.Instances.Remove(element);
context.SaveChanges();
return element.GetViewModel;
}
return null;
}
}
}

View File

@ -8,10 +8,88 @@ using BookshopContracts.ViewModels;
using BookshopContracts.SearchModels;
using BookshopContracts.BindingModels;
using BookshopDatabaseImplement.Models;
using Microsoft.EntityFrameworkCore;
namespace BookshopDatabaseImplement.Implemets
{
internal class OrderStorage
public class OrderStorage : IOrderStorage
{
public List<OrderViewModel> GetFullList()
{
using var context = new BookshopDatabase();
return context.Orders
.Include(x => x.Author)
.Include(x => x.Genre)
.Select(x => x.GetViewModel)
.ToList();
}
public List<OrderViewModel> GetFilteredList(OrderSearchModel model)
{
if (string.IsNullOrEmpty(model.Title))
{
return new();
}
using var context = new BookshopDatabase();
return context.Orders
.Include(x => x.Author)
.Include(x => x.Genre)
.Where(x => x.Title.Contains(model.Title))
.Select(x => x.GetViewModel)
.ToList();
}
public OrderViewModel? GetElement(OrderSearchModel model)
{
if (string.IsNullOrEmpty(model.Title) && !model.Id.HasValue)
{
return null;
}
using var context = new BookshopDatabase();
return context.Orders
.Include(x => x.Author)
.Include(x => x.Genre)
.FirstOrDefault(x => x.Id.Equals(model.Id))
?.GetViewModel;
}
public OrderViewModel? Insert(OrderBindingModel model)
{
var newOrder = Order.Create(model);
if (newOrder == null)
{
return null;
}
using var context = new BookshopDatabase();
context.Orders.Add(newOrder);
context.SaveChanges();
return newOrder.GetViewModel;
}
public OrderViewModel? Update(OrderBindingModel model)
{
using var context = new BookshopDatabase();
var client = context.Orders.FirstOrDefault(x => x.Id == model.Id);
if (client == null)
{
return null;
}
client.Update(model);
context.SaveChanges();
return client.GetViewModel;
}
public OrderViewModel? Delete(OrderBindingModel model)
{
using var context = new BookshopDatabase();
var element = context.Orders.FirstOrDefault(x => x.Id == model.Id);
if (element != null)
{
context.Orders.Remove(element);
context.SaveChanges();
return element.GetViewModel;
}
return null;
}
}
}

View File

@ -1,68 +0,0 @@
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.ComponentModel.DataAnnotations.Schema;
using BookshopDataModels.Models;
using System.ComponentModel.DataAnnotations;
using BookshopContracts.BindingModels;
using BookshopContracts.ViewModels;
using System.Diagnostics.Metrics;
namespace BookshopDatabaseImplement.Models
{
public class Instance : IInstanceModel
{
public int Id { get; private set; }
[Required]
public bool Pictures { get; private set; }
[Required]
public int StockQuantity { get; private set; }
[Required]
public double Price { get; private set; }
[ForeignKey("BookId")]
public int BookId { get; private set; }
public virtual Book Book { get; private set; } = new();
[ForeignKey("PublisherId")]
public int PublisherId { get; private set; }
public virtual Publisher Publisher { get; private set; } = new();
public static Instance? Create(InstanceBindingModel model)
{
if (model == null)
{
return null;
}
return new()
{
Id = model.Id,
Pictures = model.Pictures,
StockQuantity = model.StockQuantity,
Price = model.Price,
BookId = model.BookId,
PublisherId = model.PublisherId
};
}
public void Update(InstanceBindingModel model)
{
if (model == null)
{
return;
}
Pictures = model.Pictures;
StockQuantity = model.StockQuantity;
Price = model.Price;
BookId = model.BookId;
PublisherId = model.PublisherId;
}
public InstanceViewModel GetViewModel => new()
{
Id = Id,
Pictures = Pictures,
StockQuantity = StockQuantity,
Price = Price,
BookId = BookId,
PublisherId=PublisherId
};
}
}

View File

@ -20,9 +20,9 @@ namespace BookshopDatabaseImplement.Models
public DateTime OrderDate { get; private set; }
[Required]
public int Quantity { get; private set; }
[ForeignKey("InstanceId")]
public int InstanceId { get; private set; }
public virtual Instance Instance { get; private set; } = new();
[ForeignKey("BookId")]
public int BookId { get; private set; }
public virtual Book Book { get; private set; } = new();
[ForeignKey("UserId")]
public int UserId { get; private set; }
public virtual User User { get; private set; } = new();
@ -37,7 +37,7 @@ namespace BookshopDatabaseImplement.Models
Id = model.Id,
OrderDate = model.OrderDate,
Quantity = model.Quantity,
InstanceId = model.InstanceId,
BookId = model.BookId,
UserId = model.UserId
};
}
@ -49,7 +49,7 @@ namespace BookshopDatabaseImplement.Models
}
OrderDate = model.OrderDate;
Quantity = model.Quantity;
InstanceId = model.InstanceId;
BookId = model.BookId;
UserId = model.UserId;
}
public OrderViewModel GetViewModel => new()
@ -57,7 +57,7 @@ namespace BookshopDatabaseImplement.Models
Id = Id,
OrderDate = OrderDate,
Quantity = Quantity,
InstanceId = InstanceId,
BookId = BookId,
UserId = UserId
};
}

View File

@ -19,9 +19,9 @@ namespace BookshopDatabaseImplement.Models
public string ReviewText { get; private set; } = string.Empty;
[Required]
public DateTime ReviewDate { get; private set; }
[ForeignKey("InstanceId")]
public int InstanceId { get; private set; }
public virtual Instance Instance { get; private set; } = new();
[ForeignKey("BookId")]
public int BookId { get; private set; }
public virtual Book Book { get; private set; } = new();
[ForeignKey("UserId")]
public int UserId { get; private set; }
public virtual User User { get; private set; } = new();
@ -36,7 +36,7 @@ namespace BookshopDatabaseImplement.Models
Id = model.Id,
ReviewText = model.ReviewText,
ReviewDate = model.ReviewDate,
InstanceId = model.InstanceId,
BookId = model.BookId,
UserId = model.UserId,
};
}
@ -48,7 +48,7 @@ namespace BookshopDatabaseImplement.Models
}
ReviewText = model.ReviewText;
ReviewDate = model.ReviewDate;
InstanceId = model.InstanceId;
BookId = model.BookId;
UserId = model.UserId;
}
public ReviewViewModel GetViewModel => new()
@ -56,7 +56,7 @@ namespace BookshopDatabaseImplement.Models
Id = Id,
ReviewText = ReviewText,
ReviewDate = ReviewDate,
InstanceId = InstanceId,
BookId = BookId,
UserId = UserId
};
}