SUBD/CarRent/FormAddReview.cs

100 lines
2.7 KiB
C#
Raw Blame History

This file contains ambiguous Unicode characters

This file contains Unicode characters that might be confused with other characters. If you think that this is intentional, you can safely ignore this warning. Use the Escape button to reveal them.

using CarRentBusinessLogic.BusinessLogics;
using CarRentContracts.BindingModels;
using CarRentContracts.BusinessLogicContracts;
using CarRentContracts.SearchModels;
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 CarRent
{
public partial class FormAddReview : Form
{
private readonly IRentalLogic _rentalLogic;
private int? _id;
public int Id { set { _id = value; } }
public FormAddReview(IRentalLogic rentalLogic)
{
InitializeComponent();
_rentalLogic = rentalLogic;
numericUpDown.Value = 5;
numericUpDown.Maximum = 5;
numericUpDown.Minimum = 1;
}
private void FormAddReview_Load(object sender, EventArgs e)
{
if (_id.HasValue)
{
try
{
var view = _rentalLogic.ReadElement(new RentalSearchModel { Id = _id.Value });
if (view != null && (view.ReviewRating >= 1 || view.ReviewRating <= 5))
{
textBoxReviewText.Text = view.ReviewText;
numericUpDown.Value = Convert.ToDecimal(view.ReviewRating);
}
}
catch (Exception ex)
{
MessageBox.Show(ex.Message, "Ошибка", MessageBoxButtons.OK, MessageBoxIcon.Error);
}
}
}
private void buttonCancel_Click(object sender, EventArgs e)
{
DialogResult = DialogResult.Cancel;
Close();
}
private void buttonAddReview_Click(object sender, EventArgs e)
{
if (string.IsNullOrEmpty(textBoxReviewText.Text))
{
MessageBox.Show("Введите текст отзыва", "Ошибка", MessageBoxButtons.OK, MessageBoxIcon.Warning);
return;
}
if (numericUpDown.Value < 1 || numericUpDown.Value > 5)
{
MessageBox.Show("Укажите корректную оценку", "Ошибка", MessageBoxButtons.OK, MessageBoxIcon.Warning);
return;
}
try
{
var model = new RentalBindingModel
{
Id = _id ?? 0,
ReviewRating = Convert.ToInt32(numericUpDown.Value),
ReviewText = textBoxReviewText.Text,
};
var operationResult = _id.HasValue && _id != 0 ? _rentalLogic.Update(model) : throw new Exception("Проблема с отзывом к аренде");
if (!operationResult)
{
throw new Exception("Ошибка при сохранеии.");
}
MessageBox.Show("Сохранение прошло успешно", "Сообщение", MessageBoxButtons.OK, MessageBoxIcon.Information);
DialogResult = DialogResult.OK;
Close();
}
catch (Exception ex)
{
MessageBox.Show(ex.Message, "Ошибка", MessageBoxButtons.OK, MessageBoxIcon.Error);
}
}
}
}