Esempio n. 1
0
        public async Task <ServiceResult <int> > Create(int companyId, CustomerCreateOrUpdateRequestViewModel viewModel)
        {
            var model = new Customer();

            MapViewModelToModel(viewModel, model);
            model.CompanyId = companyId;

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

            var model = await _repository.GetSingleByCompanyAsync(companyId, id);

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

            var validator        = new CustomerCreateOrUpdateRequestViewModelValidator();
            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));
        }
Esempio n. 3
0
 private void MapViewModelToModel(CustomerCreateOrUpdateRequestViewModel viewModel, Customer model)
 {
     model.Email     = viewModel.UsernameOrEmail;
     model.Name      = viewModel.Name;
     model.IsCompany = viewModel.CompanysMainUser;
     //model.IsSeller = viewModel.IsSeller; // TODO
     //model.IsActive = viewModel.IsActive; // TODO
 }
 public async Task <IActionResult> Update([FromRoute] int companyId, [FromRoute] int id, [FromBody] CustomerCreateOrUpdateRequestViewModel viewModel)
 => await HandleResultAsync(() => _service.Update(companyId, id, viewModel));