erp-platform/api/src/Kurs.Platform.Application/DeveloperKit/CustomEntityAppService.cs
Sedat Öztürk 14afb6181f Düzenleme
2025-08-11 00:39:45 +03:00

236 lines
8.3 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 Microsoft.EntityFrameworkCore;
using Volo.Abp.Application.Dtos;
using Volo.Abp.Application.Services;
using Volo.Abp.Domain.Repositories;
using Volo.Abp.Domain.Entities;
using System.Text;
using Kurs.Platform.Entities;
using Kurs.Platform.DeveloperKit;
using System;
using System.Threading.Tasks;
using System.Collections.Generic;
using System.Linq;
using Microsoft.Extensions.Logging;
namespace Platform.Api.Application;
public class CustomEntityAppService : CrudAppService<
CustomEntity,
CustomEntityDto,
Guid,
PagedAndSortedResultRequestDto,
CreateUpdateCustomEntityDto>, ICustomEntityAppService
{
private readonly IRepository<CustomEntity, Guid> _repository;
private readonly IRepository<ApiMigration, Guid> _migrationRepository;
private readonly IRepository<ApiEndpoint, Guid> _endpointRepository;
private readonly IRepository<CustomEntityField, Guid> _fieldRepository;
public CustomEntityAppService(
IRepository<CustomEntity, Guid> repository,
IRepository<ApiMigration, Guid> migrationRepository,
IRepository<ApiEndpoint, Guid> endpointRepository,
IRepository<CustomEntityField, Guid> fieldRepository) : base(repository)
{
_repository = repository;
_migrationRepository = migrationRepository;
_endpointRepository = endpointRepository;
}
public override async Task<PagedResultDto<CustomEntityDto>> GetListAsync(PagedAndSortedResultRequestDto input)
{
var query = await _repository.GetQueryableAsync();
var fullQuery = query.Include(x => x.Fields);
var totalCount = await fullQuery.CountAsync();
var entities = await fullQuery
.OrderBy(x => x.CreationTime)
.Skip(input.SkipCount)
.Take(input.MaxResultCount)
.ToListAsync();
var dtos = ObjectMapper.Map<List<CustomEntity>, List<CustomEntityDto>>(entities);
return new PagedResultDto<CustomEntityDto>(totalCount, dtos);
}
public override async Task<CustomEntityDto> GetAsync(Guid id)
{
var query = await _repository.GetQueryableAsync();
var entity = await query
.Include(x => x.Fields)
.FirstOrDefaultAsync(x => x.Id == id);
if (entity == null)
throw new EntityNotFoundException($"CustomEntity with id {id} not found");
return ObjectMapper.Map<CustomEntity, CustomEntityDto>(entity);
}
public async Task<List<CustomEntityDto>> GetActiveEntitiesAsync()
{
var query = await _repository.GetQueryableAsync();
var entities = await query
.Include(x => x.Fields)
.Where(x => x.IsActive)
.ToListAsync();
return ObjectMapper.Map<List<CustomEntity>, List<CustomEntityDto>>(entities);
}
public async Task<CustomEntityDto> ToggleActiveStatusAsync(Guid id)
{
var query = await _repository.GetQueryableAsync();
var entity = await query
.Include(x => x.Fields)
.FirstOrDefaultAsync(x => x.Id == id);
if (entity == null)
throw new EntityNotFoundException($"CustomEntity with id {id} not found");
entity.IsActive = !entity.IsActive;
await _repository.UpdateAsync(entity, autoSave: true);
return ObjectMapper.Map<CustomEntity, CustomEntityDto>(entity);
}
public override async Task<CustomEntityDto> UpdateAsync(Guid id, CreateUpdateCustomEntityDto input)
{
var query = await _repository.GetQueryableAsync();
var entity = await query
.Include(x => x.Fields)
.FirstOrDefaultAsync(e => e.Id == id);
if (entity == null)
throw new EntityNotFoundException(typeof(CustomEntity), id);
entity.Name = input.Name;
entity.DisplayName = input.DisplayName;
entity.TableName = input.TableName;
entity.Description = input.Description;
entity.IsActive = input.IsActive;
entity.HasAuditFields = input.HasAuditFields;
entity.HasSoftDelete = input.HasSoftDelete;
var updatedFields = new List<CustomEntityField>();
foreach (var dtoField in input.Fields)
{
CustomEntityField? existingField = null;
if (dtoField.Id.HasValue)
{
existingField = entity.Fields.FirstOrDefault(f => f.Id == dtoField.Id.Value);
}
if (existingField != null)
{
existingField.Name = dtoField.Name;
existingField.Type = dtoField.Type;
existingField.IsRequired = dtoField.IsRequired;
existingField.MaxLength = dtoField.MaxLength;
existingField.IsUnique = dtoField.IsUnique;
existingField.DefaultValue = dtoField.DefaultValue;
existingField.Description = dtoField.Description;
updatedFields.Add(existingField);
}
else
{
var newField = new CustomEntityField
{
EntityId = entity.Id,
Name = dtoField.Name,
Type = dtoField.Type,
IsRequired = dtoField.IsRequired,
MaxLength = dtoField.MaxLength,
IsUnique = dtoField.IsUnique,
DefaultValue = dtoField.DefaultValue,
Description = dtoField.Description
};
await _fieldRepository.InsertAsync(newField);
updatedFields.Add(newField);
}
}
// Silinecek alanlar
var toRemove = entity.Fields
.Where(existing => updatedFields.All(f => f.Id != existing.Id))
.ToList();
if (toRemove.Any())
{
await _fieldRepository.DeleteManyAsync(toRemove);
}
await _repository.UpdateAsync(entity, autoSave: true);
return ObjectMapper.Map<CustomEntity, CustomEntityDto>(entity);
}
public override async Task<CustomEntityDto> CreateAsync(CreateUpdateCustomEntityDto input)
{
// Entity oluştur
var entity = new CustomEntity
{
Name = input.Name,
DisplayName = input.DisplayName,
TableName = input.TableName,
Description = input.Description,
IsActive = input.IsActive,
HasAuditFields = input.HasAuditFields,
HasSoftDelete = input.HasSoftDelete,
MigrationStatus = "pending"
};
// Fields ekle
foreach (var fieldDto in input.Fields)
{
var field = new CustomEntityField
{
EntityId = entity.Id,
Name = fieldDto.Name,
Type = fieldDto.Type,
IsRequired = fieldDto.IsRequired,
MaxLength = fieldDto.MaxLength,
IsUnique = fieldDto.IsUnique,
DefaultValue = fieldDto.DefaultValue,
Description = fieldDto.Description
};
entity.Fields.Add(field);
}
entity = await _repository.InsertAsync(entity, autoSave: true);
return ObjectMapper.Map<CustomEntity, CustomEntityDto>(entity);
}
public override async Task DeleteAsync(Guid id)
{
// İlgili entity'nin var olup olmadığını kontrol et
var entity = await _repository.GetAsync(id);
// İlgili ApiMigration kayıtlarını sil
var relatedMigrations = await _migrationRepository.GetListAsync(m => m.EntityId == id);
if (relatedMigrations.Any())
{
Logger.LogInformation($"Deleting {relatedMigrations.Count} related migrations for entity {entity.Name} (ID: {id})");
await _migrationRepository.DeleteManyAsync(relatedMigrations);
}
// İlgili ApiEndpoint kayıtlarını sil
var relatedEndpoints = await _endpointRepository.GetListAsync(e => e.EntityId == id);
if (relatedEndpoints.Any())
{
Logger.LogInformation($"Deleting {relatedEndpoints.Count} related API endpoints for entity {entity.Name} (ID: {id})");
await _endpointRepository.DeleteManyAsync(relatedEndpoints);
}
Logger.LogInformation($"Deleting CustomEntity {entity.Name} (ID: {id}) and its fields");
// Ana entity'yi sil (CustomEntityField'lar otomatik silinecek - cascade delete)
await base.DeleteAsync(id);
}
}