This commit is contained in:
antoc0der 2024-04-22 20:37:14 +04:00
parent 805e5e9dab
commit 381b8c6ec4
6 changed files with 220 additions and 21 deletions

View File

@ -23,6 +23,7 @@ namespace FlowerShopDatabaseImplement
public virtual DbSet<FlowerComponent> FlowerComponents { set; get; } public virtual DbSet<FlowerComponent> FlowerComponents { set; get; }
public virtual DbSet<Order> Orders { set; get; } public virtual DbSet<Order> Orders { set; get; }
public virtual DbSet<Client> Clients { set; get; } public virtual DbSet<Client> Clients { set; get; }
public virtual DbSet<Implementer> Implementers { set; get; }
} }
} }

View File

@ -1,11 +1,13 @@
using System; using System;
using System.Collections.Generic; using System.Collections.Generic;
using System.ComponentModel.DataAnnotations.Schema;
using System.ComponentModel.DataAnnotations;
using System.Linq; using System.Linq;
using System.Text; using System.Text;
using System.Threading.Tasks; using System.Threading.Tasks;
using FlowerShopContracts.BindingModels; using FlowerShopContracts.BindingModels;
using FlowerShopContracts.ViewModels; using FlowerShopContracts.ViewModels;
using FlowerShopDataModels; using FlowerShopDataModels.Models;
namespace FlowerShopDatabaseImplement.Models namespace FlowerShopDatabaseImplement.Models
{ {

View File

@ -0,0 +1,87 @@
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using FlowerShopContracts.BindingModels;
using FlowerShopContracts.SearchModels;
using FlowerShopContracts.StoragesContracts;
using FlowerShopContracts.ViewModels;
using FlowerShopDatabaseImplement.Models;
namespace FlowerShopDatabaseImplement.Implements
{
public class ImplementerStorage : IImplementerStorage
{
public ImplementerViewModel? Delete(ImplementerBindingModel model)
{
using var context = new FlowerShopDataBase();
var res = context.Implementers.FirstOrDefault(x => x.Id == model.Id);
if (res != null)
{
context.Implementers.Remove(res);
context.SaveChanges();
}
return res?.GetViewModel;
}
public ImplementerViewModel? GetElement(ImplementerSearchModel model)
{
if (string.IsNullOrEmpty(model.ImplementerFIO) && string.IsNullOrEmpty(model.Password) &&
!model.Id.HasValue)
{
return null;
}
using var context = new FlowerShopDataBase();
return context.Implementers
.FirstOrDefault(x => (string.IsNullOrEmpty(model.ImplementerFIO) || x.ImplementerFIO == model.ImplementerFIO) &&
(!model.Id.HasValue || x.Id == model.Id) &&
(string.IsNullOrEmpty(model.Password) || x.Password == model.Password))
?.GetViewModel;
}
public List<ImplementerViewModel> GetFilteredList(ImplementerSearchModel model)
{
if (model == null)
{
return new();
}
if (model.Id.HasValue)
{
var res = GetElement(model);
return res != null ? new() { res } : new();
}
return new();
}
public List<ImplementerViewModel> GetFullList()
{
using var context = new FlowerShopDataBase();
return context.Implementers.Select(x => x.GetViewModel).ToList();
}
public ImplementerViewModel? Insert(ImplementerBindingModel model)
{
using var context = new FlowerShopDataBase();
var res = Implementer.Create(model);
if (res != null)
{
context.Implementers.Add(res);
context.SaveChanges();
}
return res?.GetViewModel;
}
public ImplementerViewModel? Update(ImplementerBindingModel model)
{
using var context = new FlowerShopDataBase();
var res = context.Implementers.FirstOrDefault(x => x.Id == model.Id);
if (res != null)
{
res.Update(model);
context.SaveChanges();
}
return res?.GetViewModel;
}
}
}

View File

@ -31,7 +31,9 @@ namespace FlowerShopDatabaseImplement.Models
public int ClientId { get; private set; } public int ClientId { get; private set; }
public virtual Client? Client { get; private set; } public virtual Client? Client { get; private set; }
public virtual Flower? Flower { get; set; } public virtual Flower? Flower { get; set; }
public static Order? Create(OrderBindingModel model) public int? ImplementerId { get; private set; } = null;
public virtual Implementer? Implementer { get; private set; }
public static Order? Create(OrderBindingModel model)
{ {
if (model == null) if (model == null)
{ {
@ -47,7 +49,10 @@ namespace FlowerShopDatabaseImplement.Models
DateImplement = model.DateImplement, DateImplement = model.DateImplement,
FlowerId = model.FlowerId, FlowerId = model.FlowerId,
ClientId = model.ClientId, ClientId = model.ClientId,
}; ImplementerId = model.ImplementerId,
Implementer = (model.ImplementerId.HasValue ? context.Implementers.FirstOrDefault(x => x.Id == model.ImplementerId)
: null),
};
} }
public void Update(OrderBindingModel? model) public void Update(OrderBindingModel? model)
@ -58,7 +63,8 @@ namespace FlowerShopDatabaseImplement.Models
} }
Status = model.Status; Status = model.Status;
DateImplement = model.DateImplement; DateImplement = model.DateImplement;
} ImplementerId = model.ImplementerId;
}
public OrderViewModel GetViewModel => new() public OrderViewModel GetViewModel => new()
{ {
@ -67,12 +73,14 @@ namespace FlowerShopDatabaseImplement.Models
Sum = Sum, Sum = Sum,
Status = Status, Status = Status,
ClientId = ClientId, ClientId = ClientId,
DateCreate = DateCreate, ImplementerId = ImplementerId,
DateCreate = DateCreate,
DateImplement = DateImplement, DateImplement = DateImplement,
FlowerName = Flower?.FlowerName ?? String.Empty, FlowerName = Flower?.FlowerName ?? String.Empty,
Id = Id, Id = Id,
ClientFIO = Client?.ClientFIO ?? String.Empty, ClientFIO = Client?.ClientFIO ?? String.Empty,
}; ImplementerFIO = (Implementer != null ? Implementer.ImplementerFIO : string.Empty)
};
} }
} }

View File

@ -18,14 +18,12 @@ namespace FlowerShopDatabaseImplement.Implements
public List<OrderViewModel> GetFullList() public List<OrderViewModel> GetFullList()
{ {
using var context = new FlowerShopDataBase(); using var context = new FlowerShopDataBase();
return context.Orders.Include(x => x.Flower).Include(x => x.Client) return context.Orders.Include(x => x.Flower).
.Select(x => x.GetViewModel) Include(x => x.Client).Include(x => x.Implementer).Select(x => x.GetViewModel)
.ToList(); .ToList();
} }
public List<OrderViewModel> GetFilteredList(OrderSearchModel model) public List<OrderViewModel> GetFilteredList(OrderSearchModel model)
{ {
using var context = new FlowerShopDataBase(); using var context = new FlowerShopDataBase();
return context.Orders.Include(x => x.Flower) return context.Orders.Include(x => x.Flower)
.Where(x => ((!model.Id.HasValue || x.Id == model.Id) && .Where(x => ((!model.Id.HasValue || x.Id == model.Id) &&
@ -37,13 +35,13 @@ namespace FlowerShopDatabaseImplement.Implements
} }
public OrderViewModel? GetElement(OrderSearchModel model) public OrderViewModel? GetElement(OrderSearchModel model)
{ {
if (!model.Id.HasValue)
{
return null;
}
using var context = new FlowerShopDataBase(); using var context = new FlowerShopDataBase();
return context.Orders.Include(x => x.Flower).Include(x => x.Client).FirstOrDefault(x => x.Id == model.Id)?.GetViewModel; return context.Orders.Include(x => x.Flower).Include(x => x.Client).Include(x => x.Implementer).FirstOrDefault(
} x => ((model.Id.HasValue && x.Id == model.Id) ||
(model.ImplementerId.HasValue && model.Status.HasValue &&
x.ImplementerId == model.ImplementerId && x.Status == model.Status)))?.GetViewModel;
}
public OrderViewModel? Insert(OrderBindingModel model) public OrderViewModel? Insert(OrderBindingModel model)
{ {
var newOrder = Order.Create(model); var newOrder = Order.Create(model);
@ -59,9 +57,9 @@ namespace FlowerShopDatabaseImplement.Implements
public OrderViewModel? Update(OrderBindingModel model) public OrderViewModel? Update(OrderBindingModel model)
{ {
using var context = new FlowerShopDataBase(); using var context = new FlowerShopDataBase();
var order = context.Orders.Include(x => x.Flower).Include(x => x.Client).FirstOrDefault(x => x.Id == var order = context.Orders.Include(x => x.Flower).Include(x => x.Client).Include(x => x.Implementer).FirstOrDefault(x => x.Id == model.Id);
model.Id);
if (order == null) if (order == null)
{ {
return null; return null;
} }

View File

@ -0,0 +1,103 @@
using FlowerShopContracts.BindingModels;
using FlowerShopContracts.BusinessLogicsContracts;
using FlowerShopContracts.SearchModels;
using FlowerShopContracts.ViewModels;
using FlowerShopDataModels.Enums;
using Microsoft.AspNetCore.Mvc;
namespace FlowerShopRestApi.Controllers
{
[Route("api/[controller]/[action]")]
[ApiController]
public class ImplementerController : ControllerBase
{
private readonly ILogger _logger;
private readonly IOrderLogic _order;
private readonly IImplementerLogic _logic;
public ImplementerController(IOrderLogic order, IImplementerLogic logic,
ILogger<ImplementerController> logger)
{
_logger = logger;
_order = order;
_logic = logic;
}
[HttpGet]
public ImplementerViewModel? Login(string login, string password)
{
try
{
return _logic.ReadElement(new ImplementerSearchModel
{
ImplementerFIO = login,
Password = password
});
}
catch (Exception ex)
{
_logger.LogError(ex, "Ошибка авторизации сотрудника");
throw;
}
}
[HttpGet]
public List<OrderViewModel>? GetNewOrders()
{
try
{
return _order.ReadList(new OrderSearchModel
{
Status = OrderStatus.Принят
});
}
catch (Exception ex)
{
_logger.LogError(ex, "Ошибка получения новых заказов");
throw;
}
}
[HttpGet]
public OrderViewModel? GetImplementerOrder(int implementerId)
{
try
{
return _order.ReadElement(new OrderSearchModel
{
ImplementerId = implementerId
});
}
catch (Exception ex)
{
_logger.LogError(ex, "Ошибка получения текущего заказа исполнителя");
throw;
}
}
[HttpPost]
public void TakeOrderInWork(OrderBindingModel model)
{
try
{
_order.TakeOrderInWork(model);
}
catch (Exception ex)
{
_logger.LogError(ex, "Ошибка перевода заказа с №{Id} в работу",
model.Id);
throw;
}
}
[HttpPost]
public void FinishOrder(OrderBindingModel model)
{
try
{
_order.FinishOrder(model);
}
catch (Exception ex)
{
_logger.LogError(ex, "Ошибка отметки о готовности заказа с №{ Id}", model.Id);
throw;
}
}
}
}