sozsoft-platform/api/src/Sozsoft.Platform.Application/DeveloperKit/CrudEndpointGenerateAppService.cs
2026-03-03 00:34:19 +03:00

207 lines
6.7 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.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;
using Microsoft.AspNetCore.Authorization;
using Microsoft.AspNetCore.Mvc;
using Sozsoft.Platform;
namespace Platform.Api.Application;
public class CrudEndpointGenerateAppService : CrudAppService<
CrudEndpoint,
CrudEndpointDto,
Guid,
PagedAndSortedResultRequestDto,
CreateUpdateCrudEndpointDto>, ICrudEndpointAppService
{
public CrudEndpointGenerateAppService(
IRepository<CrudEndpoint, Guid> repository)
: base(repository)
{
}
[HttpGet("api/app/crud-endpoint-generate/active-endpoints")]
[Authorize(PlatformConsts.AppCodes.DeveloperKits.CrudEndpoints)]
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)
{
var endpoints = await Repository.GetListAsync(x => x.EntityName == entityName);
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)
{
var endpoint = await Repository.GetAsync(id);
endpoint.IsActive = !endpoint.IsActive;
await Repository.UpdateAsync(endpoint, autoSave: true);
return ObjectMapper.Map<CrudEndpoint, CrudEndpointDto>(endpoint);
}
[HttpPost("api/app/crud-endpoint-generate/generate-crud-endpoints/{entityName}")]
[Authorize(PlatformConsts.AppCodes.DeveloperKits.CrudEndpoints)]
public virtual async Task<PagedResultDto<CrudEndpointDto>> GenerateCrudEndpointsAsync(string entityName)
{
// CRUD endpointleri oluştur
var endpoints = new List<CrudEndpoint>
{
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)
}
};
// Var olanları sil
var existingEndpoints = await Repository
.GetListAsync(x => x.EntityName == entityName);
await Repository.DeleteManyAsync(existingEndpoints);
// Yeni endpointleri ekle
await Repository.InsertManyAsync(endpoints, 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)
{
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)
{
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"");
}}
return Ok(entity);
}}";
}
private string GenerateCreateCode(string entityName)
{
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)
{
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"");
}}
throw;
}}
return NoContent();
}}";
}
private string GenerateDeleteCode(string entityName)
{
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"");
}}
_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);
}}";
}
}