Ejemplo n.º 1
0
        private async Task UpsertMeasures(Product product, IReadOnlyCollection <UpdateProductMeasureDto> requestedMeasures)
        {
            var measures = await _measuresRepository.GetMeasures(requestedMeasures.Select(x => x.MeasureGuid));

            foreach (var measureToDeactivate in product.ProductMeasures
                     .Where(x => x.IsActive && x.ProductMeasureId != 0 && !requestedMeasures.Any(rm => rm.ProductMeasureGuid.HasValue && rm.ProductMeasureGuid == x.ProductMeasureGuid)).ToList())
            {
                measureToDeactivate.Deactivate();
            }

            foreach (var measureData in requestedMeasures)
            {
                if (measureData.ProductMeasureGuid.HasValue)
                {
                    var toUpdate = product.ProductMeasures.First(x => x.ProductMeasureGuid == measureData.ProductMeasureGuid.Value);
                    toUpdate.SetValueInGrams(measureData.ValueInGrams);
                    if (measureData.IsDefault)
                    {
                        product.SetDefaultMeasure(toUpdate.Measure);
                    }
                }
                else
                {
                    var measure = measures.FirstOrDefault(x => x.MeasureGuid == measureData.MeasureGuid);
                    if (measure == null)
                    {
                        throw new InternalException(ErrorCode.MeasureNotExists, measureData.MeasureGuid.ToString());
                    }

                    var valueInGrams   = measure.IsWeight ? measure.ValueInGrams ?? measureData.ValueInGrams : measureData.ValueInGrams;
                    var productMeasure = new ProductMeasure(_guid.GetGuid(), measure, valueInGrams);
                    if (measureData.IsDefault)
                    {
                        product.SetDefaultMeasure(measure);
                    }

                    product.AddProductMeasure(productMeasure);
                }
            }

            if (!requestedMeasures.Any(x => x.IsDefault))
            {
                if (!product.ProductMeasures.Any(x => x.IsActive && x.Measure.IsBaseline))
                {
                    var baselineMeasure = await _measuresRepository.GetBaselineMeasure();

                    var productMeasure = new ProductMeasure(_guid.GetGuid(), baselineMeasure, baselineMeasure.ValueInGrams.Value);

                    product
                    .AddProductMeasure(productMeasure)
                    .SetDefaultMeasure(baselineMeasure);
                }
                else
                {
                    var defaultMeasure = product.ProductMeasures.First(x => x.IsActive && x.Measure.IsBaseline);
                    product.SetDefaultMeasure(defaultMeasure.Measure);
                }
            }
        }
        public override async Task <RequestResult <RecipeDto> > Handle(CreateRecipeCommand request, CancellationToken cancellationToken)
        {
            var recipe = new Recipe(_guid.GetGuid(), request.Context.User.UserId, request.Name, request.Description);

            await CreateIngredients(recipe, request.Ingredients);

            await _recipesRepository.Save(recipe);

            return(new RequestResult <RecipeDto>(_mapper.Map <RecipeDto>(recipe)));
        }
        public override async Task <RequestResult <ProductDto> > Handle(CreateProductCommand request, CancellationToken cancellationToken)
        {
            await ValidateProduct(request);

            var productCategoryId = await GetCategoryId(request.ProductCategoryGuid);

            var defaultMeasureId = await GetDefaultMeasure(request.ProductMeasures);

            var nutrients = CreateProductNutrients(request);

            var product = new Product(_guid.GetGuid(), request.Name, request.ShortName, request.Description, productCategoryId, defaultMeasureId)
                          .SetProductNutrients(nutrients);

            await CreateProductMeasures(product, request.ProductMeasures);

            await _productsRepository.Save(product);

            return(request.Success(_mapper.Map <ProductDto>(product)));
        }
Ejemplo n.º 4
0
        public override async Task <RequestResult <ProductCategoryDto> > Handle(
            CreateProductCategoryCommand request, CancellationToken cancellationToken)
        {
            if (await _productCategoriesRepository.IsNameTaken(request.Name))
            {
                throw new DomainException(ErrorCode.ProductCategoryNameTaken, new { request.Name });
            }

            var productCategory = new ProductCategory(_guid.GetGuid(), request.Name);
            await _productCategoriesRepository.Save(productCategory);

            return(request.Success(_mapper.Map <ProductCategoryDto>(productCategory)));
        }
        public override async Task <RequestResult <MeasureDto> > Handle(CreateMeasureCommand request, CancellationToken cancellationToken)
        {
            if (await _measuresRepository.IsNameTaken(request.Name))
            {
                throw new DomainException(ErrorCode.MeasureNameTaken, new { request.Name });
            }
            if (await _measuresRepository.IsSymbolTaken(request.Symbol))
            {
                throw new DomainException(ErrorCode.MeasureSymbolTaken, new { request.Symbol });
            }

            var measure = new Measure(_guid.GetGuid(), request.Name, request.Symbol, request.IsWeight, request.ValueInGrams);
            await _measuresRepository.Save(measure);

            return(request.Success(_mapper.Map <MeasureDto>(measure)));
        }
Ejemplo n.º 6
0
        private async Task <RequestResult <AuthorizationData> > CreateNewFacebookUser(TokenInfo tokenInfo,
                                                                                      TenantContext requestTenant)
        {
            var userGuid = _guid.GetGuid();

            var userData = _mapper.Map <CreateUserData>(tokenInfo);

            userData.UserGuid   = userGuid;
            userData.TenantId   = requestTenant.TenantId;
            userData.AvatarType = AvatarType.Facebook;

            var user = await _authRepository.CreateFacebookUser(userData);

            await _avatarRepository
            .StoreAvatar(user.Id, AvatarType.Facebook, tokenInfo.ImageUrl);

            return(await GetAuthorizationDataResult(requestTenant, user));
        }
        public override async Task <RequestResult <TenantApplicationDto> > Handle(ApplyForTenantCommand request,
                                                                                  CancellationToken cancellationToken)
        {
            if (await _tenantsRepository.TenantCodeExists(request.TenantCode))
            {
                return(RequestResult <TenantApplicationDto> .Failure(ErrorCode.TenantAlreadyExistsForCode,
                                                                     new { request.TenantCode }));
            }

            var createApplicationData = _mapper.Map <SaveTenantApplicationData>(request);

            createApplicationData.TenantApplicationGuid = _guid.GetGuid();

            var tenantApplication = await _tenantApplicationsRepository
                                    .CreateTenantApplication(createApplicationData);

            return(RequestResult <TenantApplicationDto>
                   .Success(_mapper.Map <TenantApplicationDto>(tenantApplication)));
        }
Ejemplo n.º 8
0
        private async Task <RequestResult <UserDto> > CreateUser(
            IAdminTenantConfiguration config, Tenant tenant, Guid correlationId)
        {
            var userContext = new RequestContext()
                              .WithTenantContext(new TenantContext(tenant.Id, tenant.Code, true));
            var createUserCommand = new RegisterUserWithCredentialsCommand(
                _guid.GetGuid(),
                config.Email,
                config.UserDisplayName,
                config.Password)
                                    .WithCorrelationId(correlationId)
                                    .WithRequestContext(userContext);

            var createUserResult = await _mediator.Send(createUserCommand);

            if (!createUserResult.IsSuccess)
            {
                throw new DomainException(createUserResult);
            }
            return(createUserResult);
        }