public IActionResult Post([FromBody] TypeReqModelIn typeReqIn) { if (ModelState.IsValid) { try { var type = mapper.Map <TypeReqModelIn, TypeReqEntity>(typeReqIn); var id = typeReqLogic.Add(type); var addedType = typeReqLogic.GetByName(type.Name); var addedTypeOut = mapper.Map <TypeReqEntity, TypeReqModelOut>(addedType); return(Created("Posted succesfully", addedTypeOut)); } catch (ArgumentException ex) { return(BadRequest(ex.Message)); } catch (Exception ex) { return(BadRequest(ex.Message)); } } else { return(BadRequest(ModelState)); } }
private void importTypes(ICollection <Parameter> parameters, ref Tuple <int, int>[] result, IGenericImporter genericInterface) { try { ICollection <TypeReqEntity> importedTypesCollection = genericInterface.GetTypeReqs(parameters); List <TypeReqEntity> importedTypes = importedTypesCollection.ToList(); int totalTypes = importedTypes.Count; int succesfulTypes = 0; foreach (TypeReqEntity importedType in importedTypes) { try { typeLogic.Add(importedType); succesfulTypes++; } catch (ArgumentException) { } } result[2] = new Tuple <int, int>(totalTypes, succesfulTypes); } catch (ArgumentException e) { throw new ArgumentException(e.Message); } }
public void AddNewTypeReqTest() { typeReqRepository.SetupSequence(r => r.Exists(It.IsAny <Expression <Func <TypeReqEntity, bool> > >())) .Returns(false).Returns(false).Returns(true); topicRepository.Setup(u => u.Exists(It.IsAny <Expression <Func <TopicEntity, bool> > >())).Returns(true);; typeReqRepository.Setup(u => u.Add(It.IsAny <TypeReqEntity>())).Verifiable(); typeReqRepository.Setup(r => r.FirstOrDefault(It.IsAny <Expression <Func <TypeReqEntity, bool> > >())) .Returns(testTypeReqEntity); unitOfWork.Setup(r => r.Save()); typeReqRepository.Setup(r => r.FirstOrDefault(It.IsAny <Expression <Func <TypeReqEntity, bool> > >())) .Returns(testTypeReqEntity); typeReqLogic.Add(testTypeReqEntity); var result = unitOfWork.Object.TypeReqRepository.FirstOrDefault(t => t.Id == testTypeReqEntity.Id); typeReqRepository.VerifyAll(); Assert.IsNotNull(result); Assert.AreEqual(testTypeReqEntity.Id, result.Id); Assert.AreEqual(testTypeReqEntity.Name, result.Name); }