using System.Collections.Generic;
using ChatworkBulkSender.Utils;
using ChatworkBulkSender.Dtos;
using System.Data;
using System;
namespace ChatworkBulkSender.Daos
{
class SenderMasterDao
{
private readonly DBAccess _dB = new DBAccess();
public List<SenderMasterDto> GetAll()
{
string sql = $@"
SELECT
TOP 1
*
FROM dbo.送信者情報マスタ";
DataTable dt = _dB.ExecQuery(sql);
var list = new List<SenderMasterDto>();
foreach (DataRow row in dt.Rows)
{
var dto = new SenderMasterDto
{
SenderInfoId = row.Field<int>("送信者情報ID"),
SenderApiToken = row.Field<string>("C_APIトークン"),
TestRoomId = row.Field<string>("C_テストルームID"),
SenderAccountName = row.Field<string>("送信用アカウント名"),
CreatedDate = row.Field<DateTime?>("登録日時"),
CreatedBy = row.Field<string>("登録ユーザ"),
UpdatedDate = row.Field<DateTime?>("更新日時"),
UpdatedBy = row.Field<string>("更新ユーザ")
,
};
list.Add(dto);
}
return list;
}
/// <summary>
/// 送信者情報を更新(楽観的ロック付き)
/// </summary>
/// <param name="dto">更新するデータ</param>
/// <param name="currentUpdatedDate">現在の更新日時(楽観的ロック用)</param>
/// <returns>更新件数(0の場合は楽観的ロックエラー)</returns>
public int Update(SenderMasterDto dto, DateTime currentUpdatedDate)
{
string sql = @"
UPDATE dbo.送信者情報マスタ
SET C_APIトークン = @ApiToken,
C_テストルームID = @TestRoomId,
送信用アカウント名 = @AccountName,
更新日時 = @UpdatedDate,
更新ユーザ = @UpdatedBy
WHERE 送信者情報ID = @SenderInfoId
AND 更新日時 = @CurrentUpdatedDate";
var param = new Dictionary<string, object>
{
{"@ApiToken", dto.SenderApiToken},
{"@TestRoomId", dto.TestRoomId},
{"@AccountName", dto.SenderAccountName},
{"@UpdatedDate", DateTime.Now},
{"@UpdatedBy", Environment.UserName},
{"@SenderInfoId", dto.SenderInfoId},
{"@CurrentUpdatedDate", currentUpdatedDate}
};
return _dB.ExecNonQuery(sql, param);
}
}
}