erp-platform/api/src/Kurs.Platform.Application/DeveloperKit/CrudEndpointGenerateAppService.cs
2025-11-05 12:02:16 +03:00

242 lines
7.8 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 System;
using System.Collections.Generic;
using System.Linq;
using System.Threading.Tasks;
using Kurs.Platform.DeveloperKit;
using Kurs.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
{
private readonly IRepository<CustomEntity, Guid> _entityRepository;
private readonly IRepository<CrudMigration, Guid> _migrationRepository;
private readonly IRepository<CrudEndpoint, Guid> _endpointRepository;
public CrudEndpointGenerateAppService(
IRepository<CrudEndpoint, Guid> repository,
IRepository<CustomEntity, Guid> entityRepository,
IRepository<CrudMigration, Guid> migrationRepository,
IRepository<CrudEndpoint, Guid> endpointRepository)
: base(repository)
{
_entityRepository = entityRepository;
_migrationRepository = migrationRepository;
_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");
}
// Migration kontrolü
var migrationQueryable = await _migrationRepository.GetQueryableAsync();
var migration = await migrationQueryable
.Where(x => x.EntityId == entityId && x.Status == "applied")
.FirstOrDefaultAsync();
if (migration == null)
{
throw new Exception($"No applied migration found for entity {entity.Name}. Please apply migration first.");
}
// 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 = "applied";
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);
}}";
}
}