UserControls/CustomerIndividualEditControl.cs

using ChatworkBulkSender.Dtos;
using ChatworkBulkSender.Utils;
using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows.Forms;
using ChatworkBulkSender.Services.Chatwork;

namespace ChatworkBulkSender.UserControls
{
    public partial class CustomerIndividualEditControl : UserControl
    {
        public CustomerIndividualEditControl(CustomerMasterDto customer = null)
        {
            InitializeComponent();
            if (customer != null)
            {
                LoadCustomerDataToControls(customer);              
                
            }
        }
        private void LoadCustomerDataToControls(CustomerMasterDto customer)
        {
            txtMgmtNumber.Text = customer.ManagementNumber.ToString();
            txtMgmtNumber.ReadOnly = true;
            txtCustomerName.Text = customer.CustomerName;
            txtRoomId.Text = customer.RoomId;
            txtDestinationAccountId.Text = customer.DestinationAccountId;
            txtSortOrder.Text = customer.SortOrder.ToString();
            chkUnusedData.Checked = customer.IsUnused;
        }

        public CustomerMasterDto GetCustomerData()
        {
            var newCustomer = new CustomerMasterDto
            {
                ManagementNumber = int.Parse(txtMgmtNumber.Text),
                CustomerName = txtCustomerName.Text,
                RoomId = txtRoomId.Text,
                DestinationAccountId = txtDestinationAccountId.Text,
                SortOrder = int.Parse(txtSortOrder.Text),
                CreatedAt = DateTime.Now,
                CreatedBy = Environment.MachineName
                ,
            };

            return newCustomer;
        }

        public bool ValidateInput_MgmtNumber(out string errorMessage)
        {
            errorMessage = string.Empty;

            var errors = new List<string>();

            // 管理番号
            if (string.IsNullOrWhiteSpace(txtMgmtNumber.Text))
            {
                errors.Add("管理番号を入力してください。");
            }
            // 数値変換可か、1以上か
            else if(!int.TryParse(txtMgmtNumber.Text, out int mgmtNumber) || mgmtNumber <= 0 )
            {
                errors.Add("管理番号は1以上の数値で入力してください。");
            }

            if (errors.Any())
            {
                errorMessage = string.Join("\n",errors);
                return false;
            }

            return true;
        }

        public bool ValidateInput_CustomerName(out string errorMessage)
        {
            errorMessage = string.Empty;

            var errors = new List<string>();

            // 顧客名
            if (string.IsNullOrWhiteSpace(txtCustomerName.Text))
            {
                errors.Add("顧客名を入力してください。");
            }
            // 文字数制限
            else if (txtCustomerName.TextLength > Constants.CUSTOMER_NAME_MAX_LENGTH)
            {
                errors.Add(ValidationHelperUtil.GetLengthErrorMessage("顧客名",txtCustomerName.TextLength,Constants.CUSTOMER_NAME_MAX_LENGTH));
            }

            if (errors.Any())
            {
                errorMessage = string.Join("\n", errors);
                return false;
            }

            return true;
        }

        public async Task<(bool isValid, string errorMessage)> ValidateInput_RoomIdAsync()
        {
            var errors = new List<string>();

            if (string.IsNullOrWhiteSpace(txtRoomId.Text))
            {
                errors.Add("ルームIDを入力してください。");
            }
            else if (txtRoomId.TextLength > Constants.ROOM_ID_MAX_LENGTH)
            {
                errors.Add(ValidationHelperUtil.GetLengthErrorMessage("ルームID", txtRoomId.TextLength, Constants.ROOM_ID_MAX_LENGTH));
            }
            else if (!ValidationHelperUtil.IsValidRoomId(txtRoomId.Text))
            {
                errors.Add(ValidationHelperUtil.GetRoomIdFormatErrorMessage("ルームID"));
            }
            else
            {
                bool roomExists = await new ChatworkService().IsRoomExistsAsync(txtRoomId.Text);

                if (!roomExists)
                {
                    errors.Add("指定されたルームIDが存在しません。");
                }
            }

            string errorMessage = string.Join("\n", errors);

            // 検証結果(成功フラグ、エラーメッセージ)を返す
            return (!errors.Any(), errorMessage);
        }

        public async Task<(bool isValid, string errorMessage)> ValidateInput_DestinationAccountId()
        {
            var errors = new List<string>();

            if (string.IsNullOrWhiteSpace(txtDestinationAccountId.Text))
            {
                errors.Add("宛先アカウントIDを入力してください。");
            }

            else
            {
                var resultRoomExists = await ValidateInput_RoomIdAsync();

                if (resultRoomExists.isValid)
                {
                    bool accountExists = await new ChatworkService().IsAccountInRoomAsync(txtRoomId.Text,txtDestinationAccountId.Text);

                    if (!accountExists)
                    {
                        errors.Add("指定されたアカウントはルームIDの参加者ではありません。");
                    }
                }

            }

            string errorMessage = string.Join("\n", errors);

            // 検証結果(成功フラグ、エラーメッセージ)を返す
            return (!errors.Any(), errorMessage);

        }

        public bool ValidateInput_SortOrder(out string errorMessage)
        {
            errorMessage = string.Empty;

            var errors = new List<string>();

            // 顧客名
            if (string.IsNullOrWhiteSpace(txtSortOrder.Text))
            {
                errors.Add("並び順を数字で入力してください。");
            }

            else if (!int.TryParse(txtSortOrder.Text, out int sortOrder) || sortOrder <= 0)
            {

            }

            if (errors.Any())
            {
                errorMessage = string.Join("\n", errors);
                return false;
            }

            return true;

        }
    }
}