예제 #1
0
        public async Task <ServiceResult <bool> > Update(int id, UnitsCreateOrUpdateRequestViewModel viewModel)
        {
            if (id <= 0)
            {
                throw new ArgumentException("Argument should be greater than 0", nameof(viewModel));
            }

            var model = await _repository.GetSingleAsync(id);

            if (model == null)
            {
                return(ServiceResultFactory.Fail <bool>("Item not found"));
            }
            MapViewModelToModel(viewModel, model);

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

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

            _repository.Update(model);
            var changes = await _repository.SaveChangesAsync();

            return(ServiceResultFactory.Success(changes > 0));
        }
예제 #2
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));
        }
예제 #3
0
 private void MapViewModelToModel(UnitsCreateOrUpdateRequestViewModel viewModel, ProductUnit model)
 {
     throw new NotImplementedException();
 }
예제 #4
0
 public async Task <IActionResult> UpdateUnit([FromRoute] int id, [FromBody] UnitsCreateOrUpdateRequestViewModel viewModel)
 => await HandleResultAsync(() => _unitService.Update(id, viewModel));