108 lines
3.3 KiB
C#
108 lines
3.3 KiB
C#
using Microsoft.VisualBasic.FileIO;
|
|
using Publication.Entites;
|
|
using Publication.Entites.Enums;
|
|
using Publication.Repositories;
|
|
using System;
|
|
using System.Collections.Generic;
|
|
using System.ComponentModel;
|
|
using System.Data;
|
|
using System.Drawing;
|
|
using System.Linq;
|
|
using System.Reflection.Metadata.Ecma335;
|
|
using System.Text;
|
|
using System.Threading.Tasks;
|
|
using System.Windows.Forms;
|
|
|
|
namespace Publication.Forms;
|
|
|
|
public partial class FormMaterial : Form
|
|
{
|
|
private readonly IMaterialRepository materialRepository;
|
|
|
|
private int? materialId;
|
|
|
|
public int Id
|
|
{
|
|
set
|
|
{
|
|
try
|
|
{
|
|
var material = materialRepository.ReadMaterialById(value);
|
|
if (material == null)
|
|
{
|
|
throw new InvalidDataException(nameof(material));
|
|
}
|
|
foreach (TypeMaterials elem in Enum.GetValues(typeof(TypeMaterials)))
|
|
{
|
|
if ((elem & material.Material) != 0)
|
|
{
|
|
|
|
checkedListBox.SetItemChecked(checkedListBox.Items.IndexOf(elem), true);
|
|
}
|
|
}
|
|
MessageBox.Show(material.DateMaterials.ToString());
|
|
numericUpDownCount.Value=material.Count;
|
|
dateTimePickerDateMaterial.Value = material.DateMaterials;
|
|
materialId = value;
|
|
}
|
|
catch (Exception ex)
|
|
{
|
|
MessageBox.Show(ex.Message, "Ошибка при сохранении",
|
|
MessageBoxButtons.OK, MessageBoxIcon.Error);
|
|
}
|
|
}
|
|
}
|
|
public FormMaterial(IMaterialRepository _materialRepository)
|
|
{
|
|
materialRepository = _materialRepository ??
|
|
throw new ArgumentNullException(nameof(_materialRepository));
|
|
InitializeComponent();
|
|
foreach (var item in Enum.GetValues(typeof(TypeMaterials)))
|
|
{
|
|
checkedListBox.Items.Add(item);
|
|
}
|
|
}
|
|
|
|
|
|
|
|
private void ButtonSave_Click(object sender, EventArgs e)
|
|
{
|
|
try
|
|
{
|
|
if (int.Parse(numericUpDownCount.Value.ToString()) <= 0 || dateTimePickerDateMaterial.Value.Date <= DateTime.Now || checkedListBox1.SelectedItems.Count == 0)
|
|
{
|
|
throw new Exception("Имеются незаполненные поля");
|
|
}
|
|
if (materialId.HasValue)
|
|
{
|
|
|
|
materialRepository.UpdateMaterial(CreateMaterials(materialId.Value));
|
|
}
|
|
|
|
|
|
else
|
|
{
|
|
materialRepository.CreateMaterial(CreateMaterials(1));
|
|
}
|
|
|
|
Close();
|
|
}
|
|
catch (Exception ex)
|
|
{
|
|
MessageBox.Show(ex.Message, "Ошибка при сохранении", MessageBoxButtons.OK, MessageBoxIcon.Error);
|
|
}
|
|
}
|
|
|
|
private void ButtonBreak_Click(object sender, EventArgs e) => Close();
|
|
private Materials CreateMaterials(int id)
|
|
{
|
|
TypeMaterials typeMaterials = TypeMaterials.None;
|
|
foreach (var elem in checkedListBox.CheckedItems)
|
|
{
|
|
typeMaterials |= (TypeMaterials)elem;
|
|
}
|
|
MessageBox.Show(dateTimePickerDateMaterial.Value.ToString());
|
|
return Materials.CreateEntity(id, dateTimePickerDateMaterial.Value, (int)numericUpDownCount.Value, typeMaterials);
|
|
}
|
|
}
|