2025-08-11 06:34:44 +00:00
|
|
|
|
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;
|
|
|
|
|
|
|
2025-11-05 09:02:16 +00:00
|
|
|
|
public class CrudEndpointGenerateAppService : CrudAppService<
|
|
|
|
|
|
CrudEndpoint,
|
|
|
|
|
|
CrudEndpointDto,
|
2025-08-11 06:34:44 +00:00
|
|
|
|
Guid,
|
|
|
|
|
|
PagedAndSortedResultRequestDto,
|
2025-11-05 09:02:16 +00:00
|
|
|
|
CreateUpdateCrudEndpointDto>, ICrudEndpointAppService
|
2025-08-11 06:34:44 +00:00
|
|
|
|
{
|
|
|
|
|
|
private readonly IRepository<CustomEntity, Guid> _entityRepository;
|
2025-11-05 09:02:16 +00:00
|
|
|
|
private readonly IRepository<CrudMigration, Guid> _migrationRepository;
|
|
|
|
|
|
private readonly IRepository<CrudEndpoint, Guid> _endpointRepository;
|
2025-08-11 06:34:44 +00:00
|
|
|
|
|
2025-11-05 09:02:16 +00:00
|
|
|
|
public CrudEndpointGenerateAppService(
|
|
|
|
|
|
IRepository<CrudEndpoint, Guid> repository,
|
2025-08-11 06:34:44 +00:00
|
|
|
|
IRepository<CustomEntity, Guid> entityRepository,
|
2025-11-05 09:02:16 +00:00
|
|
|
|
IRepository<CrudMigration, Guid> migrationRepository,
|
|
|
|
|
|
IRepository<CrudEndpoint, Guid> endpointRepository)
|
2025-08-11 06:34:44 +00:00
|
|
|
|
: base(repository)
|
|
|
|
|
|
{
|
|
|
|
|
|
_entityRepository = entityRepository;
|
|
|
|
|
|
_migrationRepository = migrationRepository;
|
|
|
|
|
|
_endpointRepository = endpointRepository;
|
|
|
|
|
|
}
|
|
|
|
|
|
|
2025-11-05 09:02:16 +00:00
|
|
|
|
public virtual async Task<List<CrudEndpointDto>> GetActiveEndpointsAsync()
|
2025-08-11 06:34:44 +00:00
|
|
|
|
{
|
|
|
|
|
|
var endpoints = await Repository.GetListAsync(x => x.IsActive);
|
|
|
|
|
|
return await MapToGetListOutputDtosAsync(endpoints);
|
|
|
|
|
|
}
|
|
|
|
|
|
|
2025-11-05 09:02:16 +00:00
|
|
|
|
public virtual async Task<List<CrudEndpointDto>> GetEndpointsByEntityAsync(Guid entityId)
|
2025-08-11 06:34:44 +00:00
|
|
|
|
{
|
|
|
|
|
|
var endpoints = await _endpointRepository.GetListAsync(x => x.EntityId == entityId);
|
2025-11-05 09:02:16 +00:00
|
|
|
|
return ObjectMapper.Map<List<CrudEndpoint>, List<CrudEndpointDto>>(endpoints);
|
2025-08-11 06:34:44 +00:00
|
|
|
|
}
|
|
|
|
|
|
|
2025-11-05 09:02:16 +00:00
|
|
|
|
public virtual async Task<PagedResultDto<CrudEndpointDto>> GenerateCrudEndpointsAsync(Guid entityId)
|
2025-08-11 06:34:44 +00:00
|
|
|
|
{
|
|
|
|
|
|
// 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
|
2025-11-05 09:02:16 +00:00
|
|
|
|
var endpoints = new List<CrudEndpoint>();
|
2025-08-11 06:34:44 +00:00
|
|
|
|
var entityName = entity.Name;
|
|
|
|
|
|
var entityDisplayName = entity.DisplayName;
|
|
|
|
|
|
|
2025-11-05 09:02:16 +00:00
|
|
|
|
endpoints.Add(new CrudEndpoint
|
2025-08-11 06:34:44 +00:00
|
|
|
|
{
|
|
|
|
|
|
EntityId = entityId,
|
|
|
|
|
|
EntityName = entityName,
|
|
|
|
|
|
Method = "GET",
|
2025-11-05 09:02:16 +00:00
|
|
|
|
Path = $"/api/app/crudendpoint/{entityName.ToLower()}",
|
2025-08-11 06:34:44 +00:00
|
|
|
|
OperationType = "GetList",
|
|
|
|
|
|
IsActive = true,
|
|
|
|
|
|
CsharpCode = GenerateGetAllCode(entityName, entityDisplayName)
|
|
|
|
|
|
});
|
|
|
|
|
|
|
2025-11-05 09:02:16 +00:00
|
|
|
|
endpoints.Add(new CrudEndpoint
|
2025-08-11 06:34:44 +00:00
|
|
|
|
{
|
|
|
|
|
|
EntityId = entityId,
|
|
|
|
|
|
EntityName = entityName,
|
|
|
|
|
|
Method = "GET",
|
2025-11-05 09:02:16 +00:00
|
|
|
|
Path = $"/api/app/crudendpoint/{entityName.ToLower()}/{{id}}",
|
2025-08-11 06:34:44 +00:00
|
|
|
|
OperationType = "GetById",
|
|
|
|
|
|
IsActive = true,
|
|
|
|
|
|
CsharpCode = GenerateGetByIdCode(entityName, entityDisplayName)
|
|
|
|
|
|
});
|
|
|
|
|
|
|
2025-11-05 09:02:16 +00:00
|
|
|
|
endpoints.Add(new CrudEndpoint
|
2025-08-11 06:34:44 +00:00
|
|
|
|
{
|
|
|
|
|
|
EntityId = entityId,
|
|
|
|
|
|
EntityName = entityName,
|
|
|
|
|
|
Method = "POST",
|
2025-11-05 09:02:16 +00:00
|
|
|
|
Path = $"/api/app/crudendpoint/{entityName.ToLower()}",
|
2025-08-11 06:34:44 +00:00
|
|
|
|
OperationType = "Create",
|
|
|
|
|
|
IsActive = true,
|
|
|
|
|
|
CsharpCode = GenerateCreateCode(entityName, entityDisplayName)
|
|
|
|
|
|
});
|
|
|
|
|
|
|
2025-11-05 09:02:16 +00:00
|
|
|
|
endpoints.Add(new CrudEndpoint
|
2025-08-11 06:34:44 +00:00
|
|
|
|
{
|
|
|
|
|
|
EntityId = entityId,
|
|
|
|
|
|
EntityName = entityName,
|
|
|
|
|
|
Method = "PUT",
|
2025-11-05 09:02:16 +00:00
|
|
|
|
Path = $"/api/app/crudendpoint/{entityName.ToLower()}/{{id}}",
|
2025-08-11 06:34:44 +00:00
|
|
|
|
OperationType = "Update",
|
|
|
|
|
|
IsActive = true,
|
|
|
|
|
|
CsharpCode = GenerateUpdateCode(entityName, entityDisplayName)
|
|
|
|
|
|
});
|
|
|
|
|
|
|
2025-11-05 09:02:16 +00:00
|
|
|
|
endpoints.Add(new CrudEndpoint
|
2025-08-11 06:34:44 +00:00
|
|
|
|
{
|
|
|
|
|
|
EntityId = entityId,
|
|
|
|
|
|
EntityName = entityName,
|
|
|
|
|
|
Method = "DELETE",
|
2025-11-05 09:02:16 +00:00
|
|
|
|
Path = $"/api/app/crudendpoint/{entityName.ToLower()}/{{id}}",
|
2025-08-11 06:34:44 +00:00
|
|
|
|
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);
|
|
|
|
|
|
|
2025-11-05 09:02:16 +00:00
|
|
|
|
var result = ObjectMapper.Map<List<CrudEndpoint>, List<CrudEndpointDto>>(endpoints);
|
2025-08-11 06:34:44 +00:00
|
|
|
|
|
2025-11-05 09:02:16 +00:00
|
|
|
|
return new PagedResultDto<CrudEndpointDto>
|
2025-08-11 06:34:44 +00:00
|
|
|
|
{
|
|
|
|
|
|
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);
|
|
|
|
|
|
}}";
|
|
|
|
|
|
}
|
|
|
|
|
|
}
|
|
|
|
|
|
|