Required selling and check methods were moved + some additons.

This commit is contained in:
Yuee Shiness 2023-05-30 22:26:47 +04:00
parent 6e08782795
commit d1891a4b1b
8 changed files with 160 additions and 103 deletions

View File

@ -147,45 +147,13 @@ namespace DressAtelierBusinessLogic.BusinessLogic
atelierModel.Address = atelier.Address;
atelierModel.DressesList = atelier.DressesList;
atelierModel.OpeningDate = atelier.OpeningDate;
if(_atelierStorage.Update(atelierModel) == null)
if(_atelierStorage.Update(atelierModel) == null || atelier.DressesList.Count > atelier.MaxTotalOfDresses)
{
_logger.LogWarning("Restocking operation failed");
return false;
}
return true;
}
public bool CheckGoods(DressBindingModel model, int quantity)
{
var ateliers = _atelierStorage.GetFilteredList(new AtelierSearchModel { DressID = model.ID});
int requiredQuantity = quantity;
using (TransactionScope scope = new TransactionScope())
{
try
{
foreach(var atelier in ateliers)
{
if (atelier.DressesList[model.ID].Item2 == requiredQuantity)
{
atelier.DressesList.Remove(model.ID);
scope.Complete();
return true;
}
requiredQuantity -= atelier.DressesList[model.ID].Item2;
atelier.DressesList.Remove(model.ID);
}
if(requiredQuantity > 0)
{
throw new Exception("Not enough dresses in ateliers");
}
}
catch(Exception ex)
{
return false;
}
}
return false;
}
}
}

View File

@ -17,6 +17,5 @@ namespace DressAtelierContracts.BusinessLogicContracts
bool Create(AtelierBindingModel model);
bool Update(AtelierBindingModel model);
bool Delete(AtelierBindingModel model);
bool CheckGoods(DressBindingModel model, int quantity);
}
}

View File

@ -17,5 +17,7 @@ namespace DressAtelierContracts.StorageContracts
AtelierViewModel? Insert(AtelierBindingModel model);
AtelierViewModel? Update(AtelierBindingModel model);
AtelierViewModel? Delete(AtelierBindingModel model);
AtelierViewModel? CheckDressesQuantity(DressBindingModel model, int quantity);
bool SellDresses(DressBindingModel model, int quantity);
}
}

View File

@ -8,6 +8,7 @@ using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Transactions;
namespace DressAtelierFileImplement.Implements
{
@ -96,6 +97,60 @@ namespace DressAtelierFileImplement.Implements
return _source.Ateliers.Select(x => x.GetViewModel).ToList();
}
public AtelierViewModel? CheckDressesQuantity(DressBindingModel model, int quantity)
{
var ateliers = GetFilteredList(new AtelierSearchModel { DressID = model.ID });
foreach(var atelier in ateliers)
{
if (atelier.DressesList[model.ID].Item2 >= quantity)
{
return atelier;
}
}
return null;
}
public bool SellDresses(DressBindingModel model, int quantity)
{
var specatelier = CheckDressesQuantity(model, quantity);
if (specatelier == null)
{
var ateliers = GetFilteredList(new AtelierSearchModel { DressID = model.ID });
int requiredQuantity = quantity;
using TransactionScope scope = new TransactionScope();
try
{
foreach (var atelier in ateliers)
{
if (atelier.DressesList[model.ID].Item2 >= requiredQuantity)
{
atelier.DressesList.Remove(model.ID);
_source.SaveAteliers();
scope.Complete();
return true;
}
requiredQuantity -= atelier.DressesList[model.ID].Item2;
atelier.DressesList.Remove(model.ID);
}
if (requiredQuantity > 0)
{
throw new Exception("Not enough dresses in ateliers");
}
_source.SaveAteliers();
return true;
}
catch (Exception ex)
{
return false;
}
}
specatelier.DressesList.Remove(model.ID);
_source.SaveAteliers();
return true;
}
}
}

View File

@ -87,7 +87,7 @@ namespace DressAtelierFileImplement.Models
Name = atelier.Name;
Address = atelier.Address;
CurrentDresses = atelier.DressesList.ToDictionary(x => x.Key, x => x.Value.Item2);
DressesList = null;
DressesList.Clear();
OpeningDate = atelier.OpeningDate;
MaxTotalOfDresses = atelier.MaxTotalOfDresses;
}

View File

@ -98,5 +98,15 @@ namespace DressAtelierListImplement.Implements
}
return null;
}
public AtelierViewModel? CheckDressesQuantity(DressBindingModel model, int quantity)
{
throw new NotImplementedException();
}
public bool SellDresses(DressBindingModel model, int quantity)
{
throw new NotImplementedException();
}
}
}

View File

@ -28,85 +28,105 @@
/// </summary>
private void InitializeComponent()
{
this.cancelButton = new System.Windows.Forms.Button();
this.saveAtelierButton = new System.Windows.Forms.Button();
this.atelierAddressLabel = new System.Windows.Forms.Label();
this.atelierNameLabel = new System.Windows.Forms.Label();
this.atelierAddressTextBox = new System.Windows.Forms.TextBox();
this.atelierNameTextBox = new System.Windows.Forms.TextBox();
this.SuspendLayout();
cancelButton = new Button();
saveAtelierButton = new Button();
atelierAddressLabel = new Label();
atelierNameLabel = new Label();
atelierAddressTextBox = new TextBox();
atelierNameTextBox = new TextBox();
labelMaxQuantity = new Label();
atelierTextBoxCapacity = new TextBox();
SuspendLayout();
//
// cancelButton
//
this.cancelButton.Location = new System.Drawing.Point(310, 95);
this.cancelButton.Name = "cancelButton";
this.cancelButton.Size = new System.Drawing.Size(80, 25);
this.cancelButton.TabIndex = 11;
this.cancelButton.Text = "Cancel";
this.cancelButton.UseVisualStyleBackColor = true;
this.cancelButton.Click += new System.EventHandler(this.cancelButton_Click);
cancelButton.Location = new Point(310, 129);
cancelButton.Name = "cancelButton";
cancelButton.Size = new Size(80, 25);
cancelButton.TabIndex = 11;
cancelButton.Text = "Cancel";
cancelButton.UseVisualStyleBackColor = true;
cancelButton.Click += cancelButton_Click;
//
// saveAtelierButton
//
this.saveAtelierButton.Location = new System.Drawing.Point(224, 95);
this.saveAtelierButton.Name = "saveAtelierButton";
this.saveAtelierButton.Size = new System.Drawing.Size(80, 25);
this.saveAtelierButton.TabIndex = 10;
this.saveAtelierButton.Text = "Save";
this.saveAtelierButton.UseVisualStyleBackColor = true;
this.saveAtelierButton.Click += new System.EventHandler(this.saveAtelierButton_Click);
saveAtelierButton.Location = new Point(224, 129);
saveAtelierButton.Name = "saveAtelierButton";
saveAtelierButton.Size = new Size(80, 25);
saveAtelierButton.TabIndex = 10;
saveAtelierButton.Text = "Save";
saveAtelierButton.UseVisualStyleBackColor = true;
saveAtelierButton.Click += saveAtelierButton_Click;
//
// atelierAddressLabel
//
this.atelierAddressLabel.AutoSize = true;
this.atelierAddressLabel.Font = new System.Drawing.Font("Segoe UI", 14.25F, System.Drawing.FontStyle.Regular, System.Drawing.GraphicsUnit.Point);
this.atelierAddressLabel.Location = new System.Drawing.Point(1, 46);
this.atelierAddressLabel.Name = "atelierAddressLabel";
this.atelierAddressLabel.Size = new System.Drawing.Size(83, 25);
this.atelierAddressLabel.TabIndex = 9;
this.atelierAddressLabel.Text = "Address:";
atelierAddressLabel.AutoSize = true;
atelierAddressLabel.Font = new Font("Segoe UI", 14.25F, FontStyle.Regular, GraphicsUnit.Point);
atelierAddressLabel.Location = new Point(1, 46);
atelierAddressLabel.Name = "atelierAddressLabel";
atelierAddressLabel.Size = new Size(83, 25);
atelierAddressLabel.TabIndex = 9;
atelierAddressLabel.Text = "Address:";
//
// atelierNameLabel
//
this.atelierNameLabel.AutoSize = true;
this.atelierNameLabel.Font = new System.Drawing.Font("Segoe UI", 14.25F, System.Drawing.FontStyle.Regular, System.Drawing.GraphicsUnit.Point);
this.atelierNameLabel.Location = new System.Drawing.Point(1, 10);
this.atelierNameLabel.Name = "atelierNameLabel";
this.atelierNameLabel.Size = new System.Drawing.Size(66, 25);
this.atelierNameLabel.TabIndex = 8;
this.atelierNameLabel.Text = "Name:";
atelierNameLabel.AutoSize = true;
atelierNameLabel.Font = new Font("Segoe UI", 14.25F, FontStyle.Regular, GraphicsUnit.Point);
atelierNameLabel.Location = new Point(1, 10);
atelierNameLabel.Name = "atelierNameLabel";
atelierNameLabel.Size = new Size(66, 25);
atelierNameLabel.TabIndex = 8;
atelierNameLabel.Text = "Name:";
//
// atelierAddressTextBox
//
this.atelierAddressTextBox.Location = new System.Drawing.Point(90, 48);
this.atelierAddressTextBox.Name = "atelierAddressTextBox";
this.atelierAddressTextBox.Size = new System.Drawing.Size(300, 23);
this.atelierAddressTextBox.TabIndex = 7;
atelierAddressTextBox.Location = new Point(90, 48);
atelierAddressTextBox.Name = "atelierAddressTextBox";
atelierAddressTextBox.Size = new Size(300, 23);
atelierAddressTextBox.TabIndex = 7;
//
// atelierNameTextBox
//
this.atelierNameTextBox.Location = new System.Drawing.Point(90, 12);
this.atelierNameTextBox.Name = "atelierNameTextBox";
this.atelierNameTextBox.Size = new System.Drawing.Size(300, 23);
this.atelierNameTextBox.TabIndex = 6;
atelierNameTextBox.Location = new Point(90, 12);
atelierNameTextBox.Name = "atelierNameTextBox";
atelierNameTextBox.Size = new Size(300, 23);
atelierNameTextBox.TabIndex = 6;
//
// labelMaxQuantity
//
labelMaxQuantity.AutoSize = true;
labelMaxQuantity.Font = new Font("Segoe UI", 14.25F, FontStyle.Regular, GraphicsUnit.Point);
labelMaxQuantity.Location = new Point(1, 87);
labelMaxQuantity.Name = "labelMaxQuantity";
labelMaxQuantity.Size = new Size(88, 25);
labelMaxQuantity.TabIndex = 13;
labelMaxQuantity.Text = "Capacity:";
//
// atelierTextBoxCapacity
//
atelierTextBoxCapacity.Location = new Point(90, 87);
atelierTextBoxCapacity.Name = "atelierTextBoxCapacity";
atelierTextBoxCapacity.Size = new Size(41, 23);
atelierTextBoxCapacity.TabIndex = 12;
//
// FormAtelier
//
this.AutoScaleDimensions = new System.Drawing.SizeF(7F, 15F);
this.AutoScaleMode = System.Windows.Forms.AutoScaleMode.Font;
this.ClientSize = new System.Drawing.Size(396, 129);
this.Controls.Add(this.cancelButton);
this.Controls.Add(this.saveAtelierButton);
this.Controls.Add(this.atelierAddressLabel);
this.Controls.Add(this.atelierNameLabel);
this.Controls.Add(this.atelierAddressTextBox);
this.Controls.Add(this.atelierNameTextBox);
this.Name = "FormAtelier";
this.Text = "FormAtelier";
this.Load += new System.EventHandler(this.FormAtelier_Load);
this.ResumeLayout(false);
this.PerformLayout();
AutoScaleDimensions = new SizeF(7F, 15F);
AutoScaleMode = AutoScaleMode.Font;
ClientSize = new Size(396, 166);
Controls.Add(labelMaxQuantity);
Controls.Add(atelierTextBoxCapacity);
Controls.Add(cancelButton);
Controls.Add(saveAtelierButton);
Controls.Add(atelierAddressLabel);
Controls.Add(atelierNameLabel);
Controls.Add(atelierAddressTextBox);
Controls.Add(atelierNameTextBox);
Name = "FormAtelier";
Text = "FormAtelier";
Load += FormAtelier_Load;
ResumeLayout(false);
PerformLayout();
}
#endregion
@ -117,5 +137,7 @@
private Label atelierNameLabel;
private TextBox atelierAddressTextBox;
private TextBox atelierNameTextBox;
private Label labelMaxQuantity;
private TextBox atelierTextBoxCapacity;
}
}

View File

@ -21,7 +21,7 @@ namespace SewingDresses
private readonly IAtelierLogic _logic;
private int? _id;
public int ID { set { _id = value; } }
public FormAtelier(ILogger<FormAtelier> logger,IAtelierLogic logic)
public FormAtelier(ILogger<FormAtelier> logger, IAtelierLogic logic)
{
InitializeComponent();
_logger = logger;
@ -30,7 +30,7 @@ namespace SewingDresses
private void FormAtelier_Load(object sender, EventArgs e)
{
if(_id.HasValue)
if (_id.HasValue)
{
try
{
@ -43,20 +43,20 @@ namespace SewingDresses
{
atelierNameTextBox.Text = view.Name;
atelierAddressTextBox.Text = view.Address;
}
}
catch(Exception ex)
catch (Exception ex)
{
_logger.LogError(ex, "Error receiving atelier");
MessageBox.Show(ex.Message, "Error", MessageBoxButtons.OK,MessageBoxIcon.Error);
MessageBox.Show(ex.Message, "Error", MessageBoxButtons.OK, MessageBoxIcon.Error);
}
}
}
private void saveAtelierButton_Click(object sender, EventArgs e)
{
if(string.IsNullOrEmpty(atelierNameTextBox.Text) || string.IsNullOrEmpty(atelierAddressTextBox.Text))
if (string.IsNullOrEmpty(atelierNameTextBox.Text) || string.IsNullOrEmpty(atelierAddressTextBox.Text))
{
MessageBox.Show("Fill all fields", "Error", MessageBoxButtons.OK, MessageBoxIcon.Error);
return;
@ -69,10 +69,11 @@ namespace SewingDresses
ID = _id ?? 0,
Name = atelierNameTextBox.Text,
Address = atelierAddressTextBox.Text,
MaxTotalOfDresses = Convert.ToInt32(atelierTextBoxCapacity.Text),
DressesList = new Dictionary<int, (IDressModel, int)>()
};
var operationResult = _id.HasValue ? _logic.Update(model) : _logic.Create(model);
if(!operationResult)
if (!operationResult)
{
throw new Exception("Error during saving. Extra info in logs.");
}
@ -80,7 +81,7 @@ namespace SewingDresses
DialogResult = DialogResult.OK;
Close();
}
catch(Exception ex)
catch (Exception ex)
{
_logger.LogError(ex, "Error saving atelier");
MessageBox.Show(ex.Message, "Error", MessageBoxButtons.OK, MessageBoxIcon.Error);