public Moc AddMocPartToMoc(int mocId, List<MocPart> mocParts) { var dbMoc = mocRepository.GetById(mocId); if (dbMoc == null) { throw new DataException("Moc not found!"); } else { var newMocPart = new List<MocPart>(); foreach (var p in mocParts) { var part = partRepository.GetById(p.Part.Id); if (part != null) { var boja = colorRepository.GetById(p.Color.Id); if (boja != null) { var existingMocDijelovi = mocPartRepository.Query() .FirstOrDefault(m => m.Color.Id == boja.Id && m.Part.Id == part.Id && m.Moc.Id == dbMoc.Id); if (existingMocDijelovi == null) { var newMocDio = new MocPart() { Color = boja, Part = part, Number = p.Number, Moc = dbMoc }; mocPartRepository.Save(newMocDio); newMocPart.Add(newMocDio); } else { existingMocDijelovi.Number = p.Number; mocPartRepository.Save(existingMocDijelovi); } } } } dbMoc.MocParts.ToList().AddRange(newMocPart); return dbMoc; } }
public Moc AddMoc(Moc newMoc) { var parts = new List<MocPart>(); if (newMoc.MocParts != null && newMoc.MocParts.Count() > 0) { parts.AddRange(newMoc.MocParts); newMoc.MocParts = null; } var user = userRepository.GetById(newMoc.User.Id); if (user != null) { var dbMoc = mocRepository.GetById(newMoc.Id); if (dbMoc == null) { mocRepository.Save(newMoc); if (parts.Count > 0) { var newMocPart = new List<MocPart>(); foreach (var p in parts) { var part = partRepository.GetById(p.Part.Id); if (part != null) { var color = colorRepository.GetById(p.Color.Id); if (color != null) { var newMocDio = new MocPart() { Color = color, Part = part, Number = p.Number, Moc = newMoc }; mocPartRepository.Save(newMocDio); newMocPart.Add(newMocDio); } } } newMoc.MocParts = newMocPart; } return newMoc; } else { dbMoc.Name = newMoc.Name; dbMoc.Description = newMoc.Description; dbMoc.Theme1 = newMoc.Theme1; dbMoc.Theme2 = newMoc.Theme2; dbMoc.Theme3 = newMoc.Theme3; dbMoc.NumberOfParts = newMoc.NumberOfParts; mocRepository.Save(dbMoc); return dbMoc; } } throw new UserException(UserException.UserExceptionsText(UserExceptionEnum.NotFound)); }