88 lines
3.2 KiB
C#
88 lines
3.2 KiB
C#
|
|
using System.Collections.Generic;
|
|||
|
|
using System.Linq;
|
|||
|
|
using System.Text.Json;
|
|||
|
|
using System.Threading.Tasks;
|
|||
|
|
using Microsoft.AspNetCore.Authorization;
|
|||
|
|
using Sozsoft.Mcp.Dtos;
|
|||
|
|
using Sozsoft.Mcp.Services;
|
|||
|
|
using Sozsoft.Platform;
|
|||
|
|
using Sozsoft.Platform.ListForms.Select;
|
|||
|
|
using Volo.Abp;
|
|||
|
|
|
|||
|
|
namespace Sozsoft.Mcp;
|
|||
|
|
|
|||
|
|
/// <summary>
|
|||
|
|
/// Veri yazma yuzeyi. Yazma her zaman bir ListForm ekraninin insert/update/delete
|
|||
|
|
/// sozlesmesinden gecer; ekran yetkisi, tenant filtresi ve seed senkronizasyonu oradaki
|
|||
|
|
/// yoldan aynen uygulanir.
|
|||
|
|
/// </summary>
|
|||
|
|
[Authorize(PlatformConsts.AppCodes.DeveloperKits.McpServers.Write)]
|
|||
|
|
public class McpDataAppService(
|
|||
|
|
IMcpServerConfigProvider serverConfigProvider,
|
|||
|
|
IListFormDataAppService listFormDataAppService)
|
|||
|
|
: McpAppService(serverConfigProvider), IMcpDataAppService
|
|||
|
|
{
|
|||
|
|
public async Task<McpWriteResultDto> InsertRowAsync(McpRowRequestDto input)
|
|||
|
|
{
|
|||
|
|
var request = await BuildRequestAsync(input, McpToolNames.InsertRow, requireKeys: false, requireValues: true);
|
|||
|
|
var key = await listFormDataAppService.PostInsertAsync(request);
|
|||
|
|
|
|||
|
|
return new McpWriteResultDto { Key = ((object?)key)?.ToString() };
|
|||
|
|
}
|
|||
|
|
|
|||
|
|
public async Task<McpWriteResultDto> UpdateRowAsync(McpRowRequestDto input)
|
|||
|
|
{
|
|||
|
|
var request = await BuildRequestAsync(input, McpToolNames.UpdateRow, requireKeys: true, requireValues: true);
|
|||
|
|
var affected = await listFormDataAppService.PostUpdateAsync(request);
|
|||
|
|
|
|||
|
|
return new McpWriteResultDto { AffectedRows = affected };
|
|||
|
|
}
|
|||
|
|
|
|||
|
|
public async Task<McpWriteResultDto> DeleteRowAsync(McpRowRequestDto input)
|
|||
|
|
{
|
|||
|
|
var request = await BuildRequestAsync(input, McpToolNames.DeleteRow, requireKeys: true, requireValues: false);
|
|||
|
|
var affected = await listFormDataAppService.PostDeleteAsync(request);
|
|||
|
|
|
|||
|
|
return new McpWriteResultDto { AffectedRows = affected };
|
|||
|
|
}
|
|||
|
|
|
|||
|
|
/// <summary>
|
|||
|
|
/// Istegi ekranin bekledigi bicime cevirir. <c>Data</c> JSON metni olarak verilir; ayni
|
|||
|
|
/// bicim ekranin kopyalama yolunda da kullanilir.
|
|||
|
|
/// </summary>
|
|||
|
|
private async Task<DataRequestDto> BuildRequestAsync(
|
|||
|
|
McpRowRequestDto input,
|
|||
|
|
string toolName,
|
|||
|
|
bool requireKeys,
|
|||
|
|
bool requireValues)
|
|||
|
|
{
|
|||
|
|
Check.NotNull(input, nameof(input));
|
|||
|
|
|
|||
|
|
// AllowWrite kapaliysa yazma araclari McpToolPolicy tarafindan zaten reddedilir;
|
|||
|
|
// burada ayri bir kontrol yok.
|
|||
|
|
await EnsureToolAllowedAsync(toolName);
|
|||
|
|
|
|||
|
|
if (string.IsNullOrWhiteSpace(input.ListFormCode))
|
|||
|
|
{
|
|||
|
|
throw new UserFriendlyException(L[McpErrorCodes.ListFormCodeRequired]);
|
|||
|
|
}
|
|||
|
|
|
|||
|
|
if (requireValues && (input.Values is null || input.Values.Count == 0))
|
|||
|
|
{
|
|||
|
|
throw new UserFriendlyException(L[McpErrorCodes.ValuesRequired]);
|
|||
|
|
}
|
|||
|
|
|
|||
|
|
if (requireKeys && (input.Keys is null || input.Keys.Count == 0))
|
|||
|
|
{
|
|||
|
|
throw new UserFriendlyException(L[McpErrorCodes.KeysRequired]);
|
|||
|
|
}
|
|||
|
|
|
|||
|
|
return new DataRequestDto
|
|||
|
|
{
|
|||
|
|
ListFormCode = input.ListFormCode,
|
|||
|
|
Data = JsonSerializer.Serialize(input.Values ?? new Dictionary<string, object?>()),
|
|||
|
|
Keys = [.. (input.Keys ?? []).Select(key => key!)]
|
|||
|
|
};
|
|||
|
|
}
|
|||
|
|
}
|