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

208 lines
6.7 KiB
C#
Raw Normal View History

2026-02-24 20:44:16 +00:00
using System;
using System.Collections.Generic;
using System.Threading.Tasks;
using Sozsoft.Platform.DeveloperKit;
using Sozsoft.Platform.Entities;
using Volo.Abp.Application.Dtos;
using Volo.Abp.Application.Services;
using Volo.Abp.Domain.Repositories;
2026-03-02 07:36:38 +00:00
using Microsoft.AspNetCore.Authorization;
using Microsoft.AspNetCore.Mvc;
using Sozsoft.Platform;
2026-02-24 20:44:16 +00:00
namespace Platform.Api.Application;
public class CrudEndpointGenerateAppService : CrudAppService<
CrudEndpoint,
CrudEndpointDto,
Guid,
PagedAndSortedResultRequestDto,
CreateUpdateCrudEndpointDto>, ICrudEndpointAppService
{
public CrudEndpointGenerateAppService(
IRepository<CrudEndpoint, Guid> repository)
2026-02-24 20:44:16 +00:00
: base(repository)
{
}
[HttpGet("api/app/crud-endpoint-generate/active-endpoints")]
[Authorize(PlatformConsts.AppCodes.DeveloperKits.CrudEndpoints)]
2026-02-24 20:44:16 +00:00
public virtual async Task<List<CrudEndpointDto>> GetActiveEndpointsAsync()
{
var endpoints = await Repository.GetListAsync(x => x.IsActive);
return await MapToGetListOutputDtosAsync(endpoints);
}
[HttpGet("api/app/crud-endpoint-generate/endpoints-by-entity/{entityName}")]
[Authorize(PlatformConsts.AppCodes.DeveloperKits.CrudEndpoints)]
public virtual async Task<List<CrudEndpointDto>> GetEndpointsByEntityAsync(string entityName)
2026-02-24 20:44:16 +00:00
{
var endpoints = await Repository.GetListAsync(x => x.EntityName == entityName);
2026-02-24 20:44:16 +00:00
return ObjectMapper.Map<List<CrudEndpoint>, List<CrudEndpointDto>>(endpoints);
}
[HttpPost("api/app/crud-endpoint-generate/{id}/toggle")]
[Authorize(PlatformConsts.AppCodes.DeveloperKits.CrudEndpoints)]
public virtual async Task<CrudEndpointDto> ToggleAsync(Guid id)
2026-02-24 20:44:16 +00:00
{
var endpoint = await Repository.GetAsync(id);
endpoint.IsActive = !endpoint.IsActive;
await Repository.UpdateAsync(endpoint, autoSave: true);
return ObjectMapper.Map<CrudEndpoint, CrudEndpointDto>(endpoint);
}
2026-02-24 20:44:16 +00:00
[HttpPost("api/app/crud-endpoint-generate/generate-crud-endpoints/{entityName}")]
[Authorize(PlatformConsts.AppCodes.DeveloperKits.CrudEndpoints)]
public virtual async Task<PagedResultDto<CrudEndpointDto>> GenerateCrudEndpointsAsync(string entityName)
{
2026-02-24 20:44:16 +00:00
// CRUD endpointleri oluştur
var endpoints = new List<CrudEndpoint>
2026-02-24 20:44:16 +00:00
{
new() {
EntityName = entityName,
Method = "GET",
Path = $"/api/app/crudendpoint/{entityName}",
OperationType = "GetList",
IsActive = true,
CsharpCode = GenerateGetAllCode(entityName)
},
new() {
EntityName = entityName,
Method = "GET",
Path = $"/api/app/crudendpoint/{entityName}/{{id}}",
OperationType = "GetById",
IsActive = true,
CsharpCode = GenerateGetByIdCode(entityName)
},
new() {
EntityName = entityName,
Method = "POST",
Path = $"/api/app/crudendpoint/{entityName}",
OperationType = "Create",
IsActive = true,
CsharpCode = GenerateCreateCode(entityName)
},
new() {
EntityName = entityName,
Method = "PUT",
Path = $"/api/app/crudendpoint/{entityName}/{{id}}",
OperationType = "Update",
IsActive = true,
CsharpCode = GenerateUpdateCode(entityName)
},
new() {
EntityName = entityName,
Method = "DELETE",
Path = $"/api/app/crudendpoint/{entityName}/{{id}}",
OperationType = "Delete",
IsActive = true,
CsharpCode = GenerateDeleteCode(entityName)
}
};
2026-02-24 20:44:16 +00:00
// Var olanları sil
var existingEndpoints = await Repository
.GetListAsync(x => x.EntityName == entityName);
2026-02-24 20:44:16 +00:00
await Repository.DeleteManyAsync(existingEndpoints);
2026-02-24 20:44:16 +00:00
// Yeni endpointleri ekle
await Repository.InsertManyAsync(endpoints, autoSave: true);
2026-02-24 20:44:16 +00:00
var result = ObjectMapper.Map<List<CrudEndpoint>, List<CrudEndpointDto>>(endpoints);
return new PagedResultDto<CrudEndpointDto>
{
Items = result,
TotalCount = result.Count
};
}
private string GenerateGetAllCode(string entityName)
2026-02-24 20:44:16 +00:00
{
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)
2026-02-24 20:44:16 +00:00
{
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($""{entityName} with ID {{id}} not found"");
2026-02-24 20:44:16 +00:00
}}
return Ok(entity);
}}";
}
private string GenerateCreateCode(string entityName)
2026-02-24 20:44:16 +00:00
{
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)
2026-02-24 20:44:16 +00:00
{
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($""{entityName} with ID {{id}} not found"");
2026-02-24 20:44:16 +00:00
}}
throw;
}}
return NoContent();
}}";
}
private string GenerateDeleteCode(string entityName)
2026-02-24 20:44:16 +00:00
{
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($""{entityName} with ID {{id}} not found"");
2026-02-24 20:44:16 +00:00
}}
_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);
}}";
}
}