sozsoft-platform/api/src/Sozsoft.Platform.Application/DeveloperKit/CrudEndpointGenerateAppService.cs

230 lines
7.2 KiB
C#
Raw Normal View History

2026-02-24 20:44:16 +00:00
using System;
using System.Collections.Generic;
using System.Linq;
using System.Threading.Tasks;
using Sozsoft.Platform.DeveloperKit;
using Sozsoft.Platform.Entities;
using Microsoft.EntityFrameworkCore;
using Volo.Abp.Application.Dtos;
using Volo.Abp.Application.Services;
using Volo.Abp.Domain.Repositories;
namespace Platform.Api.Application;
public class CrudEndpointGenerateAppService : CrudAppService<
CrudEndpoint,
CrudEndpointDto,
Guid,
PagedAndSortedResultRequestDto,
CreateUpdateCrudEndpointDto>, ICrudEndpointAppService
{
2026-03-01 20:43:25 +00:00
private readonly IRepository<SqlTable, Guid> _entityRepository;
2026-02-24 20:44:16 +00:00
private readonly IRepository<CrudEndpoint, Guid> _endpointRepository;
public CrudEndpointGenerateAppService(
IRepository<CrudEndpoint, Guid> repository,
2026-03-01 20:43:25 +00:00
IRepository<SqlTable, Guid> entityRepository,
2026-02-24 20:44:16 +00:00
IRepository<CrudEndpoint, Guid> endpointRepository)
: base(repository)
{
_entityRepository = entityRepository;
_endpointRepository = endpointRepository;
}
public virtual async Task<List<CrudEndpointDto>> GetActiveEndpointsAsync()
{
var endpoints = await Repository.GetListAsync(x => x.IsActive);
return await MapToGetListOutputDtosAsync(endpoints);
}
public virtual async Task<List<CrudEndpointDto>> GetEndpointsByEntityAsync(Guid entityId)
{
var endpoints = await _endpointRepository.GetListAsync(x => x.EntityId == entityId);
return ObjectMapper.Map<List<CrudEndpoint>, List<CrudEndpointDto>>(endpoints);
}
public virtual async Task<PagedResultDto<CrudEndpointDto>> GenerateCrudEndpointsAsync(Guid entityId)
{
// Entity + Fields
var entityQueryable = await _entityRepository.GetQueryableAsync();
var entity = await entityQueryable
.Include(x => x.Fields)
.FirstOrDefaultAsync(x => x.Id == entityId);
if (entity == null)
{
throw new Exception($"Entity with ID {entityId} not found");
}
// CRUD endpointleri oluştur
var endpoints = new List<CrudEndpoint>();
var entityName = entity.Name;
var entityDisplayName = entity.DisplayName;
endpoints.Add(new CrudEndpoint
{
EntityId = entityId,
EntityName = entityName,
Method = "GET",
Path = $"/api/app/crudendpoint/{entityName.ToLower()}",
OperationType = "GetList",
IsActive = true,
CsharpCode = GenerateGetAllCode(entityName, entityDisplayName)
});
endpoints.Add(new CrudEndpoint
{
EntityId = entityId,
EntityName = entityName,
Method = "GET",
Path = $"/api/app/crudendpoint/{entityName.ToLower()}/{{id}}",
OperationType = "GetById",
IsActive = true,
CsharpCode = GenerateGetByIdCode(entityName, entityDisplayName)
});
endpoints.Add(new CrudEndpoint
{
EntityId = entityId,
EntityName = entityName,
Method = "POST",
Path = $"/api/app/crudendpoint/{entityName.ToLower()}",
OperationType = "Create",
IsActive = true,
CsharpCode = GenerateCreateCode(entityName, entityDisplayName)
});
endpoints.Add(new CrudEndpoint
{
EntityId = entityId,
EntityName = entityName,
Method = "PUT",
Path = $"/api/app/crudendpoint/{entityName.ToLower()}/{{id}}",
OperationType = "Update",
IsActive = true,
CsharpCode = GenerateUpdateCode(entityName, entityDisplayName)
});
endpoints.Add(new CrudEndpoint
{
EntityId = entityId,
EntityName = entityName,
Method = "DELETE",
Path = $"/api/app/crudendpoint/{entityName.ToLower()}/{{id}}",
OperationType = "Delete",
IsActive = true,
CsharpCode = GenerateDeleteCode(entityName, entityDisplayName)
});
// Var olanları sil
var existingEndpoints = await _endpointRepository
.GetListAsync(x => x.EntityId == entityId);
await _endpointRepository.DeleteManyAsync(existingEndpoints);
// Yeni endpointleri ekle
await _endpointRepository.InsertManyAsync(endpoints, autoSave: true);
// Entity endpoint durumu güncelle
entity.EndpointStatus = "Uygulandı";
await _entityRepository.UpdateAsync(entity, autoSave: true);
var result = ObjectMapper.Map<List<CrudEndpoint>, List<CrudEndpointDto>>(endpoints);
return new PagedResultDto<CrudEndpointDto>
{
Items = result,
TotalCount = result.Count
};
}
private string GenerateGetAllCode(string entityName, string displayName)
{
return $@"[HttpGet]
public async Task<ActionResult<List<{entityName}>>> GetAll{entityName}sAsync()
{{
var entities = await _context.{entityName}s.ToListAsync();
return Ok(entities);
}}";
}
private string GenerateGetByIdCode(string entityName, string displayName)
{
return $@"[HttpGet(""{{id}}"")]
public async Task<ActionResult<{entityName}>> Get{entityName}Async(Guid id)
{{
var entity = await _context.{entityName}s.FindAsync(id);
if (entity == null)
{{
return NotFound($""{displayName} with ID {{id}} not found"");
}}
return Ok(entity);
}}";
}
private string GenerateCreateCode(string entityName, string displayName)
{
return $@"[HttpPost]
public async Task<ActionResult<{entityName}>> Create{entityName}Async({entityName} {entityName.ToLower()})
{{
_context.{entityName}s.Add({entityName.ToLower()});
await _context.SaveChangesAsync();
return CreatedAtAction(nameof(Get{entityName}Async), new {{ id = {entityName.ToLower()}.Id }}, {entityName.ToLower()});
}}";
}
private string GenerateUpdateCode(string entityName, string displayName)
{
return $@"[HttpPut(""{{id}}"")]
public async Task<IActionResult> Update{entityName}Async(Guid id, {entityName} {entityName.ToLower()})
{{
if (id != {entityName.ToLower()}.Id)
{{
return BadRequest(""ID mismatch"");
}}
_context.Entry({entityName.ToLower()}).State = EntityState.Modified;
try
{{
await _context.SaveChangesAsync();
}}
catch (DbUpdateConcurrencyException)
{{
if (!await {entityName}ExistsAsync(id))
{{
return NotFound($""{displayName} with ID {{id}} not found"");
}}
throw;
}}
return NoContent();
}}";
}
private string GenerateDeleteCode(string entityName, string displayName)
{
return $@"[HttpDelete(""{{id}}"")]
public async Task<IActionResult> Delete{entityName}Async(Guid id)
{{
var {entityName.ToLower()} = await _context.{entityName}s.FindAsync(id);
if ({entityName.ToLower()} == null)
{{
return NotFound($""{displayName} with ID {{id}} not found"");
}}
_context.{entityName}s.Remove({entityName.ToLower()});
await _context.SaveChangesAsync();
return NoContent();
}}
private async Task<bool> {entityName}ExistsAsync(Guid id)
{{
return await _context.{entityName}s.AnyAsync(e => e.Id == id);
}}";
}
}