public async Task <ArrivalsDB> Add(ArrivalsDB arrival, List <SerialsDB> serials) { EntityEntry <ArrivalsDB> result; try { result = await _context.Arrivals.AddAsync(arrival); } catch { throw new Exception("Error al registrar la recepción"); } int added; try { added = AddSerials(serials, arrival.Id).Result; } catch { throw new Exception("Error insertando los seriales"); } if (added == serials.Count) { try { await _context.SaveChangesAsync(); return(result.Entity); } catch (Exception ex) { throw ex; } } else { throw new Exception("Algunos seriales no se han insertado, se ha cancelado la operacion"); } }
public async Task <string> Handle(SubmitArrivalCommand request, CancellationToken cancellationToken) { ArrivalRequestDTO dto = request.Request; try { dto.ValidateObject("La request no puede ser null"); LocalValidations(dto); } catch (Exception ex) { throw new Exception(ex.Message); } if (dto.Serials.Count == 0) { throw new Exception("No hay seriales que registrar. Se ha cancelado la operacion."); } var arrivalF = await _facilityService.GetFacilityById(dto.FID); if (arrivalF == null) { throw new Exception("No existe el FID de recepción"); } if (arrivalF.Id != null) { List <SerialsDB> serials = new List <SerialsDB>(); foreach (var s in dto.Serials) { if (s != "" && s != null && s != "[]") { serials.Add(new SerialsDB { Serial = s }); } } var newId = _arrivalService.GetLastIdArrival().Result; var arrival = new ArrivalsDB { Id = newId + 1, FID = arrivalF.Id, Comments = dto.Comments, ArrivalDate = dto.ArrivalDate }; try { var added = _arrivalsRepository.Add(arrival, serials).Result; return(added.Id.ToString()); } catch (Exception ex) { throw new Exception(ex.Message); } } else { throw new Exception("Error al registrar la recepción"); } }