Example #1
0
        public ShipDTO Add(ShipDTO shipDTO)
        {
            var ship = shipDTO.ToModel();

            ship.Id      = IdentityGenerator.NewSequentialGuid();
            ship.Created = DateTime.UtcNow;

            if (ship.Name.IsNullOrBlank())
            {
                throw new DefinedException(CommonMessageResources.Name_Empty);
            }

            if (_Repository.Exists(ship))
            {
                throw new DataExistsException(string.Format(BaseMessagesResources.Ship_Exists_WithValue, ship.Name));
            }

            var ruleName = CommonMessageResources.SerialNumberRule_Ship;
            var snRule   = SerialNumberRuleQuerier.FindBy(ruleName);

            if (snRule == null)
            {
                throw new DataNotFoundException(string.Format(BaseMessagesResources.SerialNumberRule_NotExists_WithValue, ruleName));
            }
            ship.Sn = SerialNumberGenerator.GetSerialNumber(snRule.Prefix, snRule.UseDateNumber, snRule.NumberLength);

            _Repository.Add(ship);

            #region 操作日志

            var shipDto = ship.ToDto();

            OperateRecorder.RecordOperation(shipDto.Sn,
                                            BaseMessagesResources.Add_Ship,
                                            shipDto.GetOperationLog());

            #endregion

            //commit the unit of work
            _Repository.UnitOfWork.Commit();

            return(ship.ToDto());
        }
Example #2
0
        public ActionResult <ShipDto> AddShip(ApiVersion version, [FromBody] ShipCreateDto shipCreateDto)
        {
            if (shipCreateDto == null)
            {
                return(BadRequest());
            }

            ShipEntity toAdd = _mapper.Map <ShipEntity>(shipCreateDto);

            _shipRepository.Add(toAdd);

            if (!_shipRepository.Save())
            {
                throw new Exception("Creating a ship failed on save.");
            }

            ShipEntity newShipItem = _shipRepository.GetSingle(toAdd.ShipId);

            return(CreatedAtRoute(nameof(GetSingleShip), new { version = version.ToString(), id = newShipItem.ShipId },
                                  _mapper.Map <ShipDto>(newShipItem)));
        }