some done

This commit is contained in:
Калышев Ян 2023-06-12 02:49:23 -07:00
parent 001b1d1619
commit 236b87031a
20 changed files with 341 additions and 115 deletions

View File

@ -61,5 +61,17 @@ namespace BlacksmithWorkshopListImplement.Implements
_source.MessageInfos.Add(newMessage);
return newMessage.GetViewModel;
}
}
public MessageInfoViewModel? Update(MessageInfoBindingModel model)
{
foreach (var message in _source.MessageInfos)
{
if (message.MessageId == model.MessageId)
{
message.Update(model);
return message.GetViewModel;
}
}
return null;
}
}
}

View File

@ -17,7 +17,9 @@ namespace BlacksmithWorkshopListImplement.Models
public DateTime DateDelivery { get; private set; } = DateTime.Now;
public string Subject { get; private set; } = string.Empty;
public string Body { get; private set; } = string.Empty;
public static MessageInfo? Create(MessageInfoBindingModel? model)
public bool IsRead { get; private set; } = false;
public string? ReplyText { get; private set; }
public static MessageInfo? Create(MessageInfoBindingModel? model)
{
if (model == null)
{
@ -33,14 +35,21 @@ namespace BlacksmithWorkshopListImplement.Models
Body = model.Body
};
}
public MessageInfoViewModel GetViewModel => new()
public void Update(MessageInfoBindingModel model)
{
IsRead = model.IsRead;
ReplyText = model.ReplyText;
}
public MessageInfoViewModel GetViewModel => new()
{
MessageId = MessageId,
ClientId = ClientId,
SenderName = SenderName,
DateDelivery = DateDelivery,
Subject = Subject,
Body = Body
};
Body = Body,
IsRead = IsRead,
ReplyText = ReplyText
};
}
}

View File

@ -14,33 +14,58 @@ namespace BlacksmithWorkshopBusinessLogic.BusinessLogics
{
public class MessageInfoLogic : IMessageInfoLogic
{
private readonly ILogger _logger;
private readonly IMessageInfoStorage _messageInfoStorage;
public MessageInfoLogic(ILogger<MessageInfoLogic> logger, IMessageInfoStorage messageInfoStorage)
{
_logger = logger;
_messageInfoStorage = messageInfoStorage;
}
public bool Create(MessageInfoBindingModel model)
{
if (_messageInfoStorage.Insert(model) == null)
{
_logger.LogWarning("Insert operation failed");
return false;
}
return true;
}
public List<MessageInfoViewModel>? ReadList(MessageInfoSearchModel? model)
{
_logger.LogInformation("ReadList. MessageId:{MessageId}", model?.MessageId);
var list = model == null ? _messageInfoStorage.GetFullList() : _messageInfoStorage.GetFilteredList(model);
if (list == null)
{
_logger.LogWarning("ReadList return null list");
return null;
}
_logger.LogInformation("ReadList. Count:{Count}", list.Count);
return list.OrderByDescending(x => x.DateDelivery).ToList();
}
}
private readonly ILogger _logger;
private readonly IMessageInfoStorage _messageInfoStorage;
public MessageInfoLogic(ILogger<MessageInfoLogic> logger, IMessageInfoStorage messageInfoStorage)
{
_logger = logger;
_messageInfoStorage = messageInfoStorage;
}
public bool Create(MessageInfoBindingModel model)
{
if (_messageInfoStorage.Insert(model) == null)
{
_logger.LogWarning("Insert operation failed");
return false;
}
return true;
}
public MessageInfoViewModel? ReadElement(MessageInfoSearchModel? model)
{
if (model == null)
{
throw new ArgumentNullException(nameof(model));
}
_logger.LogInformation("ReadElement. MessageId:{MessageId}", model.MessageId);
var element = _messageInfoStorage.GetElement(model);
if (element == null)
{
_logger.LogWarning("ReadElement element not found");
return null;
}
_logger.LogInformation("ReadElement find. MessageId:{Id}", element.MessageId);
return element;
}
public List<MessageInfoViewModel>? ReadList(MessageInfoSearchModel? model)
{
_logger.LogInformation("ReadList. MessageId:{MessageId}", model?.MessageId);
var list = model == null ? _messageInfoStorage.GetFullList() : _messageInfoStorage.GetFilteredList(model);
if (list == null)
{
_logger.LogWarning("ReadList return null list");
return null;
}
_logger.LogInformation("ReadList. Count:{Count}", list.Count);
return list.OrderByDescending(x => x.DateDelivery).ToList();
}
public bool Update(MessageInfoBindingModel model)
{
if (_messageInfoStorage.Update(model) == null)
{
_logger.LogWarning("Update operation failed");
return false;
}
return true;
}
}
}

View File

@ -133,13 +133,16 @@ namespace BlacksmithWorkshopClientApp.Controllers
return count * (prod?.Price ?? 1);
}
[HttpGet]
public IActionResult Mails()
public IActionResult Mails(int page = 0)
{
if (APIClient.Client == null)
{
return Redirect("~/Home/Enter");
}
return View(APIClient.GetRequest<List<MessageInfoViewModel>>($"api/client/getmessages?clientId={APIClient.Client.Id}"));
var messages = APIClient.GetRequest<List<MessageInfoViewModel>>($"api/client/getmessages?clientId={APIClient.Client.Id}&page={page}");
ViewBag.PageIsLast = messages!.Count == 0;
ViewBag.Page = page;
return View(messages);
}
}
}

View File

@ -10,6 +10,7 @@
<h1 class="display-4">Письма</h1>
</div>
<div class="text-center">
@{
if (Model == null)
@ -17,6 +18,7 @@
<h3 class="display-4">Авторизируйтесь</h3>
return;
}
<table class="table">
<thead>
<tr>
@ -49,4 +51,18 @@
</tbody>
</table>
}
</div>
@if (ViewBag.Page != 0)
{
<a href="@Url.Action("Mails", new { page = ViewBag.Page - 1 })"
class="btn btn-primary">
&lt; Назад
</a>
}
@if (!ViewBag.PageIsLast)
{
<a href="@Url.Action("Mails", new { page = ViewBag.Page + 1 })"
class="btn btn-primary">
Вперед &gt;
</a>
}
</div>

View File

@ -15,5 +15,7 @@ namespace BlacksmithWorkshopContracts.BindingModels
public string Subject { get; set; } = string.Empty;
public string Body { get; set; } = string.Empty;
public DateTime DateDelivery { get; set; }
}
public bool IsRead { get; set; } = false;
public string? ReplyText { get; set; } = string.Empty;
}
}

View File

@ -11,7 +11,9 @@ namespace BlacksmithWorkshopContracts.BusinessLogicsContracts
{
public interface IMessageInfoLogic
{
List<MessageInfoViewModel>? ReadList(MessageInfoSearchModel? model);
bool Create(MessageInfoBindingModel model);
}
List<MessageInfoViewModel>? ReadList(MessageInfoSearchModel? model);
bool Create(MessageInfoBindingModel model);
MessageInfoViewModel? ReadElement(MessageInfoSearchModel? model);
bool Update(MessageInfoBindingModel model);
}
}

View File

@ -10,5 +10,7 @@ namespace BlacksmithWorkshopContracts.SearchModels
{
public int? ClientId { get; set; }
public string? MessageId { get; set; }
}
public int? Page { get; set; }
public int? PageSize { get; set; }
}
}

View File

@ -15,5 +15,6 @@ namespace BlacksmithWorkshopContracts.StorageContracts
List<MessageInfoViewModel> GetFilteredList(MessageInfoSearchModel model);
MessageInfoViewModel? GetElement(MessageInfoSearchModel model);
MessageInfoViewModel? Insert(MessageInfoBindingModel model);
}
MessageInfoViewModel? Update(MessageInfoBindingModel model);
}
}

View File

@ -20,5 +20,9 @@ namespace BlacksmithWorkshopContracts.ViewModels
public string Subject { get; set; } = string.Empty;
[DisplayName("Текст")]
public string Body { get; set; } = string.Empty;
}
[DisplayName("Прочитано")]
public bool IsRead { get; set; } = false;
[DisplayName("Ответ")]
public string? ReplyText { get; set; }
}
}

View File

@ -14,5 +14,7 @@ namespace BlacksmithWorkshopDataModels.Models
DateTime DateDelivery { get; }
string Subject { get; }
string Body { get; }
}
bool IsRead { get; }
string? ReplyText { get; }
}
}

View File

@ -13,39 +13,65 @@ namespace BlacksmithWorkshopDatabaseImplement.Implements
{
public class MessageInfoStorage : IMessageInfoStorage
{
public List<MessageInfoViewModel> GetFullList()
{
using var context = new BlacksmithWorkshopDatabase();
return context.MessageInfos
.Select(x => x.GetViewModel)
.ToList();
}
public List<MessageInfoViewModel> GetFilteredList(MessageInfoSearchModel model)
{
using var context = new BlacksmithWorkshopDatabase();
return context.MessageInfos
.Where(x => x.ClientId.HasValue && x.ClientId == model.ClientId)
.Select(x => x.GetViewModel)
.ToList();
}
public MessageInfoViewModel? GetElement(MessageInfoSearchModel model)
{
using var context = new BlacksmithWorkshopDatabase();
return context.MessageInfos
.FirstOrDefault(x => !string.IsNullOrEmpty(x.MessageId) && x.MessageId == model.MessageId)
?.GetViewModel;
}
public MessageInfoViewModel? Insert(MessageInfoBindingModel model)
{
using var context = new BlacksmithWorkshopDatabase();
var newMessage = MessageInfo.Create(model);
if (newMessage == null)
{
return null;
}
context.MessageInfos.Add(newMessage);
context.SaveChanges();
return newMessage.GetViewModel;
}
}
public List<MessageInfoViewModel> GetFullList()
{
using var context = new BlacksmithWorkshopDatabase();
return context.MessageInfos
.Select(x => x.GetViewModel)
.ToList();
}
public List<MessageInfoViewModel> GetFilteredList(MessageInfoSearchModel model)
{
using var context = new BlacksmithWorkshopDatabase();
if (model.ClientId.HasValue && model.Page.HasValue && model.PageSize.HasValue)
{
return context.MessageInfos
.Where(x => x.ClientId == model.ClientId)
.Skip(model.PageSize.Value * model.Page.Value)
.Take(model.PageSize.Value)
.Select(x => x.GetViewModel)
.ToList();
}
else if (model.Page.HasValue && model.PageSize.HasValue)
{
return context.MessageInfos
.Skip(model.PageSize.Value * model.Page.Value)
.Take(model.PageSize.Value)
.Select(x => x.GetViewModel)
.ToList();
}
return new();
}
public MessageInfoViewModel? GetElement(MessageInfoSearchModel model)
{
using var context = new BlacksmithWorkshopDatabase();
return context.MessageInfos
.FirstOrDefault(x => !string.IsNullOrEmpty(x.MessageId) && x.MessageId == model.MessageId)
?.GetViewModel;
}
public MessageInfoViewModel? Insert(MessageInfoBindingModel model)
{
using var context = new BlacksmithWorkshopDatabase();
var newMessage = MessageInfo.Create(model);
if (newMessage == null)
{
return null;
}
context.MessageInfos.Add(newMessage);
context.SaveChanges();
return newMessage.GetViewModel;
}
public MessageInfoViewModel? Update(MessageInfoBindingModel model)
{
using var context = new BlacksmithWorkshopDatabase();
var message = context.MessageInfos.FirstOrDefault(x => x.MessageId == model.MessageId);
if (message == null)
{
return null;
}
message.Update(model);
context.SaveChanges();
return message.GetViewModel;
}
}
}

View File

@ -23,7 +23,10 @@ namespace BlacksmithWorkshopDatabaseImplement.Models
public string Subject { get; set; } = string.Empty;
[Required]
public string Body { get; set; } = string.Empty;
public virtual Client? Client { get; set; }
[Required]
public bool IsRead { get; private set; } = false;
public string? ReplyText { get; private set; }
public virtual Client? Client { get; set; }
public static MessageInfo? Create(MessageInfoBindingModel? model)
{
if (model == null)
@ -40,14 +43,21 @@ namespace BlacksmithWorkshopDatabaseImplement.Models
Body = model.Body
};
}
public MessageInfoViewModel GetViewModel => new()
{
MessageId = MessageId,
ClientId = ClientId,
SenderName = SenderName,
DateDelivery = DateDelivery,
Subject = Subject,
Body = Body
};
}
public void Update(MessageInfoBindingModel model)
{
IsRead = model.IsRead;
ReplyText = model.ReplyText;
}
public MessageInfoViewModel GetViewModel => new()
{
MessageId = MessageId,
ClientId = ClientId,
SenderName = SenderName,
DateDelivery = DateDelivery,
Subject = Subject,
Body = Body,
IsRead = IsRead,
ReplyText = ReplyText
};
}
}

View File

@ -54,5 +54,16 @@ namespace BlacksmithWorkshopFileImplement.Implements
_source.SaveClients();
return newMessage.GetViewModel;
}
}
public MessageInfoViewModel? Update(MessageInfoBindingModel model)
{
var message = _source.MessageInfos.FirstOrDefault(x => x.MessageId == model.MessageId);
if (message == null)
{
return null;
}
message.Update(model);
_source.SaveMessageInfos();
return message.GetViewModel;
}
}
}

View File

@ -18,7 +18,9 @@ namespace BlacksmithWorkshopFileImplement.Models
public DateTime DateDelivery { get; set; } = DateTime.Now;
public string Subject { get; set; } = string.Empty;
public string Body { get; set; } = string.Empty;
public static MessageInfo? Create(MessageInfoBindingModel model)
public bool IsRead { get; private set; } = false;
public string? ReplyText { get; private set; }
public static MessageInfo? Create(MessageInfoBindingModel model)
{
if (model == null)
{
@ -50,21 +52,30 @@ namespace BlacksmithWorkshopFileImplement.Models
Body = element.Element("MessageId")!.Value
};
}
public MessageInfoViewModel GetViewModel => new()
{
MessageId = MessageId,
ClientId = ClientId,
SenderName = SenderName,
DateDelivery = DateDelivery,
Subject = Subject,
Body = Body
};
public XElement GetXElement => new("MessageInfo",
new XAttribute("MessageId", MessageId),
new XElement("ClientId", ClientId),
new XElement("SenderName", SenderName),
new XElement("DateDelivery", DateDelivery),
new XElement("Subject", Subject),
new XElement("Body", Body));
}
public void Update(MessageInfoBindingModel model)
{
IsRead = model.IsRead;
ReplyText = model.ReplyText;
}
public MessageInfoViewModel GetViewModel => new()
{
MessageId = MessageId,
ClientId = ClientId,
SenderName = SenderName,
DateDelivery = DateDelivery,
Subject = Subject,
Body = Body,
IsRead = IsRead,
ReplyText = ReplyText
};
public XElement GetXElement => new("MessageInfo",
new XAttribute("MessageId", MessageId),
new XElement("ClientId", ClientId),
new XElement("SenderName", SenderName),
new XElement("DateDelivery", DateDelivery),
new XElement("Subject", Subject),
new XElement("Body", Body),
new XElement("IsRead", IsRead),
new XElement("ReplyText", ReplyText));
}
}

View File

@ -49,6 +49,7 @@
buttonRef = new Button();
buttonAddManufactureInShop = new Button();
buttonSellManufacture = new Button();
messagesToolStripMenuItem = new ToolStripMenuItem();
menuStrip1.SuspendLayout();
((System.ComponentModel.ISupportInitialize)dataGridView).BeginInit();
SuspendLayout();
@ -226,6 +227,7 @@
buttonSellManufacture.Text = "Продать изделие";
buttonSellManufacture.UseVisualStyleBackColor = true;
buttonSellManufacture.Click += ButtonSellManufacture_Click;
//
// messagesToolStripMenuItem
//
messagesToolStripMenuItem.Name = "messagesToolStripMenuItem";

View File

@ -202,6 +202,10 @@ namespace BlacksmithWorkshopView
{
var service = Program.ServiceProvider?.GetService(typeof(FormReportOrdersByDate));
if (service is FormReportOrdersByDate form)
{
form.ShowDialog();
}
}
private void messagesToolStripMenuItem_Click(object sender, EventArgs e)
{
var service = Program.ServiceProvider?.GetService(typeof(FormMessages));

View File

@ -29,7 +29,12 @@
private void InitializeComponent()
{
dataGridView = new DataGridView();
panel = new Panel();
buttonForward = new Button();
buttonBack = new Button();
buttonOpen = new Button();
((System.ComponentModel.ISupportInitialize)dataGridView).BeginInit();
panel.SuspendLayout();
SuspendLayout();
//
// dataGridView
@ -41,25 +46,74 @@
dataGridView.Name = "dataGridView";
dataGridView.RowHeadersWidth = 62;
dataGridView.RowTemplate.Height = 25;
dataGridView.Size = new Size(1143, 750);
dataGridView.Size = new Size(1279, 697);
dataGridView.TabIndex = 0;
//
// panel
//
panel.Controls.Add(buttonForward);
panel.Controls.Add(buttonBack);
panel.Controls.Add(buttonOpen);
panel.Dock = DockStyle.Right;
panel.Location = new Point(1096, 0);
panel.Name = "panel";
panel.Size = new Size(183, 697);
panel.TabIndex = 1;
//
// buttonForward
//
buttonForward.Anchor = AnchorStyles.Bottom | AnchorStyles.Right;
buttonForward.Location = new Point(106, 651);
buttonForward.Name = "buttonForward";
buttonForward.Size = new Size(65, 34);
buttonForward.TabIndex = 2;
buttonForward.Text = ">";
buttonForward.UseVisualStyleBackColor = true;
buttonForward.Click += ButtonForward_Click;
//
// buttonBack
//
buttonBack.Anchor = AnchorStyles.Bottom | AnchorStyles.Right;
buttonBack.Location = new Point(14, 651);
buttonBack.Name = "buttonBack";
buttonBack.Size = new Size(65, 34);
buttonBack.TabIndex = 1;
buttonBack.Text = "<";
buttonBack.UseVisualStyleBackColor = true;
buttonBack.Click += ButtonBack_Click;
//
// buttonOpen
//
buttonOpen.Location = new Point(14, 12);
buttonOpen.Name = "buttonOpen";
buttonOpen.Size = new Size(157, 34);
buttonOpen.TabIndex = 0;
buttonOpen.Text = "Открыть";
buttonOpen.UseVisualStyleBackColor = true;
buttonOpen.Click += ButtonOpen_Click;
//
// FormMessages
//
AutoScaleDimensions = new SizeF(10F, 25F);
AutoScaleMode = AutoScaleMode.Font;
ClientSize = new Size(1143, 750);
ClientSize = new Size(1279, 697);
Controls.Add(panel);
Controls.Add(dataGridView);
Margin = new Padding(4, 5, 4, 5);
Name = "FormMessages";
Text = "Сообщения";
Load += FormMessages_Load;
((System.ComponentModel.ISupportInitialize)dataGridView).EndInit();
panel.ResumeLayout(false);
ResumeLayout(false);
}
#endregion
private DataGridView dataGridView;
private Panel panel;
private Button buttonOpen;
private Button buttonForward;
private Button buttonBack;
}
}

View File

@ -9,6 +9,7 @@ using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows.Forms;
using static BlacksmithWorkshopView.FormMessages;
namespace BlacksmithWorkshopView
{
@ -16,6 +17,8 @@ namespace BlacksmithWorkshopView
{
private readonly ILogger _logger;
private readonly IMessageInfoLogic _logic;
private readonly int pageSize = 5;
private int pageNumber = 0;
public FormMessages(ILogger<FormMessages> logger, IMessageInfoLogic logic)
{
InitializeComponent();
@ -24,13 +27,13 @@ namespace BlacksmithWorkshopView
}
private void FormMessages_Load(object sender, EventArgs e)
{
LoadData();
LoadData(0);
}
private void LoadData()
private void LoadData(int page)
{
try
{
var list = _logic.ReadList(null);
var list = _logic.ReadList(new() { Page = page, PageSize = pageSize });
if (list != null)
{
dataGridView.DataSource = list;
@ -46,5 +49,31 @@ namespace BlacksmithWorkshopView
MessageBox.Show(ex.Message, "Ошибка", MessageBoxButtons.OK, MessageBoxIcon.Error);
}
}
private void ButtonBack_Click(object sender, EventArgs e)
{
if (pageNumber != 0)
{
pageNumber--;
}
LoadData(pageNumber);
}
private void ButtonForward_Click(object sender, EventArgs e)
{
pageNumber++;
LoadData(pageNumber);
}
private void ButtonOpen_Click(object sender, EventArgs e)
{
if (dataGridView.SelectedRows.Count == 1)
{
var service = Program.ServiceProvider?.GetService(typeof(FormMessage));
if (service is FormMessage form)
{
form.Id = dataGridView.SelectedRows[0].Cells["MessageId"].Value.ToString();
form.ShowDialog();
LoadData(0);
}
}
}
}
}

View File

@ -103,6 +103,7 @@ namespace BlacksmithWorkshopView
services.AddTransient<FormImplementers>();
services.AddTransient<FormImplementer>();
services.AddTransient<FormMessages>();
services.AddTransient<FormMessage>();
}
private static void MailCheck(object obj) => ServiceProvider?.GetService<AbstractMailWorker>()?.MailCheck();
}