示例#1
0
        public OutputDtoAddUnit Create(InputDtoAddUnit inputDtoAddUnit)
        {
            //DTO --> Domain
            var unitFromDto = _unitFactory.CreateFromType(inputDtoAddUnit.Type);
            //Repository demande un element du domain

            var unitsInDb = _unitRepository.Query();

            foreach (var unit in unitsInDb)
            {
                if (unit.Type == unitFromDto.Type)
                {
                    throw new Exception("Unit already in database");
                }
            }
            //On crée une unit dans la db
            var unitInDb = _unitRepository.Create(unitFromDto);

            //Domain -> DTO
            return(new OutputDtoAddUnit
            {
                Id = unitInDb.Id,
                Type = unitInDb.Type
            });
        }
        public void Create_InputDtoAddUnit_ThrowsAlreadyInDBException()
        {
            var input = new InputDtoAddUnit("cm");

            _unitFactory.CreateFromType(input.Type).Returns(new Unit(input.Type));
            _unitRepository.Query().Returns(getUnitList());

            var exception = Assert.Throws <Exception>(() => _unitService.Create(input));

            Assert.AreEqual("Unit already in database", exception.Message);
        }
        public void Create_InputDtoAddUnit_AreSame()
        {
            var input = new InputDtoAddUnit("km");

            _unitFactory.CreateFromType(input.Type).Returns(new Unit(input.Type));
            var iunit = _unitFactory.CreateFromType(input.Type);

            _unitRepository.Query().Returns(getUnitList());
            _unitRepository.Create(iunit).Returns(new Unit(input.Type));

            var res = _unitService.Create(input);

            var expected = new OutputDtoAddUnit("km");

            Assert.AreEqual(expected, res);
        }
 public ActionResult <OutputDtoAddUnit> Post([FromBody] InputDtoAddUnit inputDtoAddUnit)
 {
     return(Ok(_unitService.Create(inputDtoAddUnit)));
 }