Add bulk line creation quantity

This commit is contained in:
Eduardo Lopes 2026-01-30 10:57:21 -03:00
parent 40a94f0e4e
commit bb6f1fc044
2 changed files with 64 additions and 47 deletions

View File

@ -350,31 +350,39 @@ namespace line_gestao_api.Controllers
if (string.IsNullOrWhiteSpace(req.Cliente)) if (string.IsNullOrWhiteSpace(req.Cliente))
return BadRequest(new { message = "O nome do Cliente é obrigatório." }); return BadRequest(new { message = "O nome do Cliente é obrigatório." });
if (string.IsNullOrWhiteSpace(req.Linha)) var quantidade = req.QtdLinhas.GetValueOrDefault(1);
return BadRequest(new { message = "O número da Linha é obrigatório." }); if (quantidade < 1)
return BadRequest(new { message = "A quantidade de linhas deve ser maior que zero." });
var linhaLimpa = OnlyDigits(req.Linha); var linhaLimpa = OnlyDigits(req.Linha);
var chipLimpo = OnlyDigits(req.Chip); var chipLimpo = OnlyDigits(req.Chip);
if (string.IsNullOrWhiteSpace(linhaLimpa)) if (quantidade == 1 && string.IsNullOrWhiteSpace(req.Linha))
return BadRequest(new { message = "O número da Linha é obrigatório." });
if (!string.IsNullOrWhiteSpace(req.Linha) && string.IsNullOrWhiteSpace(linhaLimpa))
return BadRequest(new { message = "Número de linha inválido." }); return BadRequest(new { message = "Número de linha inválido." });
if (!string.IsNullOrWhiteSpace(linhaLimpa))
{
var exists = await _db.MobileLines.AsNoTracking().AnyAsync(x => x.Linha == linhaLimpa); var exists = await _db.MobileLines.AsNoTracking().AnyAsync(x => x.Linha == linhaLimpa);
if (exists) if (exists)
return Conflict(new { message = $"A linha {req.Linha} já está cadastrada no sistema." }); return Conflict(new { message = $"A linha {req.Linha} já está cadastrada no sistema." });
}
var maxItem = await _db.MobileLines.MaxAsync(x => (int?)x.Item) ?? 0; var maxItem = await _db.MobileLines.MaxAsync(x => (int?)x.Item) ?? 0;
var nextItem = maxItem + 1;
var now = DateTime.UtcNow; var now = DateTime.UtcNow;
var created = new List<MobileLine>(quantidade);
for (var i = 0; i < quantidade; i++)
{
var newLine = new MobileLine var newLine = new MobileLine
{ {
Id = Guid.NewGuid(), Id = Guid.NewGuid(),
Item = nextItem, Item = maxItem + 1 + i,
Cliente = req.Cliente.Trim().ToUpper(), Cliente = req.Cliente.Trim().ToUpper(),
Linha = linhaLimpa, Linha = i == 0 ? linhaLimpa : null,
Chip = string.IsNullOrWhiteSpace(chipLimpo) ? null : chipLimpo, Chip = i == 0 && !string.IsNullOrWhiteSpace(chipLimpo) ? chipLimpo : null,
Usuario = req.Usuario?.Trim(), Usuario = req.Usuario?.Trim(),
Status = req.Status?.Trim(), Status = req.Status?.Trim(),
Skil = req.Skil?.Trim(), Skil = req.Skil?.Trim(),
@ -410,8 +418,10 @@ namespace line_gestao_api.Controllers
}; };
ApplyReservaRule(newLine); ApplyReservaRule(newLine);
created.Add(newLine);
}
_db.MobileLines.Add(newLine); _db.MobileLines.AddRange(created);
try try
{ {
@ -422,9 +432,15 @@ namespace line_gestao_api.Controllers
return StatusCode(500, new { message = "Erro ao salvar no banco de dados." }); return StatusCode(500, new { message = "Erro ao salvar no banco de dados." });
} }
if (created.Count == 1)
{
var newLine = created[0];
return CreatedAtAction(nameof(GetById), new { id = newLine.Id }, ToDetailDto(newLine)); return CreatedAtAction(nameof(GetById), new { id = newLine.Id }, ToDetailDto(newLine));
} }
return StatusCode(StatusCodes.Status201Created, created.Select(ToDetailDto).ToList());
}
// ========================================================== // ==========================================================
// ✅ 6. UPDATE // ✅ 6. UPDATE
// ========================================================== // ==========================================================

View File

@ -12,6 +12,7 @@ namespace line_gestao_api.Dtos
public string? Chip { get; set; } // ICCID public string? Chip { get; set; } // ICCID
public string? Cliente { get; set; } // Obrigatório na validação do Controller public string? Cliente { get; set; } // Obrigatório na validação do Controller
public string? Usuario { get; set; } public string? Usuario { get; set; }
public int? QtdLinhas { get; set; } // Quantidade de linhas a serem criadas
// ========================== // ==========================
// Classificação e Status // Classificação e Status