Esempio n. 1
0
        public async Task <IActionResult> Post([FromBody] AddUnitViewModel model)
        {
            if (model == null)
            {
                return(BadRequest());
            }

            if (await _unitRepo.IsExistNameAsync(model.Name))
            {
                ModelState.AddModelError("Name", Resources.Global.Common.ThisNameExist);
                return(BadRequest(ModelState.GetWithErrorsKey()));
            }

            var unit = new Unit(model.Name);

            var affectedRows = await _unitRepo.AddAsync(unit);

            if (affectedRows > 0)
            {
                var viewModel = AutoMapper.Mapper.Map <UnitViewModel>(unit);

                return(CreatedAtRoute("GetUnit", new { id = unit.Number }, viewModel));
            }
            return(BadRequest());
        }
        public async Task AddUnitAsync(UnitDTO unitDTO)
        {
            if (unitDTO == null)
            {
                throw new TestExamplesException("Eklenecek birim boş olamaz.");
            }

            var unit = new Unit
            {
                Description = unitDTO.Description,
                Name        = unitDTO.Name
            };

            await _unitRepository.AddAsync(unit).ConfigureAwait(false);
        }
Esempio n. 3
0
        public async Task <ServiceResult <int> > Create(UnitsCreateOrUpdateRequestViewModel viewModel)
        {
            var model = new ProductUnit();

            MapViewModelToModel(viewModel, model);

            var validator        = new ProductUnitCreateOrUpdateRequestViewModelValidator();
            var validationResult = await validator.ValidateAsync(model);

            if (!validationResult.IsValid)
            {
                return(ServiceResultFactory.Fail <int>(validationResult));
            }

            await _repository.AddAsync(model);

            var changes = await _repository.SaveChangesAsync();

            if (changes == 0)
            {
                return(ServiceResultFactory.Fail <int>("Insert fails"));
            }
            return(ServiceResultFactory.Success(model.Id));
        }