Ejemplo n.º 1
0
        public async Task <ServiceResult <int> > Create(int companyId, CompanyLicenceCreateOrUpdateRequestViewModel viewModel)
        {
            var model = new CompanyLicence();

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

            var validator        = new CompanyLicenceCreateOrUpdateRequestViewModelValidator();
            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));
        }
Ejemplo n.º 2
0
        public async Task <ServiceResult <bool> > Update(int companyId, int id, CompanyLicenceCreateOrUpdateRequestViewModel 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 CompanyLicenceCreateOrUpdateRequestViewModelValidator();
            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 <ServiceResult <int> > Create(InfoChannelMessageCreateOrUpdateRequestViewModel viewModel)
        {
            var model = new InfoChannelMessage();

            MapViewModelToModel(viewModel, model);
            model.Status = "1"; // TODO: What is status for?

            var validator        = new InfoChannelMessageCreateOrUpdateRequestViewModelValidator();
            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));
        }
Ejemplo n.º 4
0
        public ServiceResult <PersonDto> GetSingle(int personId)
        {
            personId.ArgumentNotLessThan(0, nameof(personId), "Person id was invalid. Please supply positive id.");

            var model = _personRepository.Get(personId);

            if (model == null)
            {
                return(ServiceResultFactory.Fail <PersonDto>("Person with Id {personId} was not found."));
            }

            var dto = Mapper.Map <PersonDto>(model);

            return(ServiceResultFactory.Success(dto));
        }
Ejemplo n.º 5
0
        public async Task <IActionResult> Register([FromBody] RegisterViewModel model)
        {
            return(await HandleResultAsync(async() =>
            {
                var user = await _userManager.FindByNameAsync(model.Email);
                if (user != null)
                {
                    return ServiceResultFactory.Fail <IdentityResult>($"Error {StatusCodes.Status409Conflict}");
                }

                user = new ApplicationUser {
                    UserName = model.Email, Email = model.Email
                };
                var result = await _userManager.CreateAsync(user, model.Password);

                return ServiceResultFactory.Success(result);
            }));
        }
Ejemplo n.º 6
0
        public async Task <ServiceResult <bool> > Delete(int companyId, int id)
        {
            if (id <= 0)
            {
                throw new ArgumentException("Argument should be greater than 0", nameof(id));
            }

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

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

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

            return(ServiceResultFactory.Success(changes > 0));
        }
Ejemplo n.º 7
0
        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));
        }