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 _entityRepository; private readonly IRepository _migrationRepository; private readonly IRepository _endpointRepository; public CrudEndpointGenerateAppService( IRepository repository, IRepository entityRepository, IRepository migrationRepository, IRepository endpointRepository) : base(repository) { _entityRepository = entityRepository; _migrationRepository = migrationRepository; _endpointRepository = endpointRepository; } public virtual async Task> GetActiveEndpointsAsync() { var endpoints = await Repository.GetListAsync(x => x.IsActive); return await MapToGetListOutputDtosAsync(endpoints); } public virtual async Task> GetEndpointsByEntityAsync(Guid entityId) { var endpoints = await _endpointRepository.GetListAsync(x => x.EntityId == entityId); return ObjectMapper.Map, List>(endpoints); } public virtual async Task> 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(); 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>(endpoints); return new PagedResultDto { Items = result, TotalCount = result.Count }; } private string GenerateGetAllCode(string entityName, string displayName) { return $@"[HttpGet] public async Task>> 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> 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> 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 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 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 {entityName}ExistsAsync(Guid id) {{ return await _context.{entityName}s.AnyAsync(e => e.Id == id); }}"; } }