private void MapViewModelToModel(ProductCreateOrUpdateRequestViewModel viewModel, Product model)
 {
     model.Name              = viewModel.ProductName;
     model.Code              = viewModel.Code;
     model.EanCode           = viewModel.EanCode;
     model.ProductCategoryId = viewModel.ProductGroupId;
     model.Description       = viewModel.Description;
     model.Price             = viewModel.SellingPriceBruto;
     model.Vat           = viewModel.VatPercent;
     model.ProductType   = viewModel.Type;
     model.Unit          = viewModel.Unit;
     model.UseProductVat = viewModel.UseProductVat;
 }
        public async Task <ServiceResult <int> > Create(ProductCreateOrUpdateRequestViewModel viewModel)
        {
            var model = new Product();

            MapViewModelToModel(viewModel, model);

            var validator        = new ProductCreateOrUpdateRequestViewModelValidator();
            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));
        }
        public async Task <ServiceResult <bool> > Update(int id, ProductCreateOrUpdateRequestViewModel viewModel)
        {
            if (id <= 0)
            {
                throw new ArgumentException("Argument should be greater than 0", nameof(id));
            }

            var model = await _repository.GetSingleAsync(id);

            MapViewModelToModel(viewModel, model);

            var validator        = new ProductCreateOrUpdateRequestViewModelValidator();
            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));
        }
 public async Task <IActionResult> Update([FromRoute] int id, [FromBody] ProductCreateOrUpdateRequestViewModel viewModel)
 => await HandleResultAsync(() => _service.Update(id, viewModel));
 public async Task <IActionResult> Create([FromBody] ProductCreateOrUpdateRequestViewModel viewModel)
 => await HandleResultAsync(() => _service.Create(viewModel));