41 lines
		
	
	
		
			1.7 KiB
		
	
	
	
		
			C#
		
	
	
	
	
	
		
		
			
		
	
	
			41 lines
		
	
	
		
			1.7 KiB
		
	
	
	
		
			C#
		
	
	
	
	
	
|  | using System; | ||
|  | using System.Collections.Generic; | ||
|  | using System.ComponentModel.DataAnnotations; | ||
|  | 
 | ||
|  | namespace GestionIntegral.Api.Dtos.Distribucion | ||
|  | { | ||
|  |     public class CreateBulkEntradaSalidaCanillaDto | ||
|  |     { | ||
|  |         [Required(ErrorMessage = "El ID del canillita es obligatorio.")] | ||
|  |         public int IdCanilla { get; set; } | ||
|  | 
 | ||
|  |         [Required(ErrorMessage = "La fecha del movimiento es obligatoria.")] | ||
|  |         public DateTime Fecha { get; set; } // Fecha común para todos los ítems | ||
|  | 
 | ||
|  |         [Required(ErrorMessage = "Debe haber al menos un ítem de movimiento.")] | ||
|  |         [MinLength(1, ErrorMessage = "Debe agregar al menos una publicación.")] | ||
|  |         public List<EntradaSalidaCanillaItemDto> Items { get; set; } = new List<EntradaSalidaCanillaItemDto>(); | ||
|  |          | ||
|  |         // Validar que no haya publicaciones duplicadas en la lista de items | ||
|  |         [CustomValidation(typeof(CreateBulkEntradaSalidaCanillaDto), nameof(ValidateNoDuplicatePublications))] | ||
|  |         public string? DuplicateError { get; set; } | ||
|  | 
 | ||
|  |         public static ValidationResult? ValidateNoDuplicatePublications(CreateBulkEntradaSalidaCanillaDto dto, ValidationContext context) | ||
|  |         { | ||
|  |             if (dto.Items != null) | ||
|  |             { | ||
|  |                 var duplicatePublications = dto.Items | ||
|  |                     .GroupBy(item => item.IdPublicacion) | ||
|  |                     .Where(group => group.Count() > 1) | ||
|  |                     .Select(group => group.Key) | ||
|  |                     .ToList(); | ||
|  | 
 | ||
|  |                 if (duplicatePublications.Any()) | ||
|  |                 { | ||
|  |                     return new ValidationResult($"No puede agregar la misma publicación varias veces. Publicaciones duplicadas: {string.Join(", ", duplicatePublications)}"); | ||
|  |                 } | ||
|  |             } | ||
|  |             return ValidationResult.Success; | ||
|  |         } | ||
|  |     } | ||
|  | } |