Exemple #1
0
        public InProductTrainingModule(
            IInProductTrainingController inProductTrainingController,
            IMetadataRegistry metadataRegistry,
            IPolicyEvaluator policyEvaluator,
            ILoggerFactory loggerFactory)
            : base(InProductTrainingServiceBootstrapper.ServiceNameShort, metadataRegistry, policyEvaluator, loggerFactory)
        {
            _inProductTrainingController = inProductTrainingController;

            CreateRoute("CreateInProductTrainingView", HttpMethod.Post, $"{BaseInProductTrainingUrl}/viewed", CreateInProductTrainingViewAsync)
            .Description("Create a new InProductTraining resource")
            .StatusCodes(HttpStatusCode.Created, HttpStatusCode.Unauthorized, HttpStatusCode.Forbidden, HttpStatusCode.BadRequest, HttpStatusCode.InternalServerError)
            .RequestFormat(InProductTrainingViewRequest.Example())
            .ResponseFormat(InProductTrainingViewResponse.Example());

            CreateRoute("GetViewedInProductTraining", HttpMethod.Get, $"{BaseInProductTrainingUrl}/viewed/{{clientApplicationId:int}}", GetViewedInProductTrainingAsync)
            .Description("Get a InProductTrainingView resource by it's identifier.")
            .StatusCodes(HttpStatusCode.OK, HttpStatusCode.BadRequest, HttpStatusCode.Unauthorized, HttpStatusCode.Forbidden, HttpStatusCode.InternalServerError, HttpStatusCode.NotFound)
            .ResponseFormat(InProductTrainingViewResponse.Example());

            CreateRoute("CreateWizardView", HttpMethod.Post, $"{BaseWizardsUrl}/viewed", CreateWizardViewAsync)
            .Description("Create a new wizard view resource")
            .StatusCodes(HttpStatusCode.Created, HttpStatusCode.Unauthorized, HttpStatusCode.Forbidden, HttpStatusCode.BadRequest, HttpStatusCode.InternalServerError)
            .RequestFormat(ViewedWizard.Example())
            .ResponseFormat(WizardViewResponse.Example());

            CreateRoute("GetWizardViewsByUserId", HttpMethod.Get, $"{BaseWizardsUrl}/viewed/{{userId:guid}}", GetWizardViewsByUserIdAsync)
            .Description("Get a wizard view resource by it's identifier.")
            .StatusCodes(HttpStatusCode.OK, HttpStatusCode.BadRequest, HttpStatusCode.Unauthorized, HttpStatusCode.Forbidden, HttpStatusCode.InternalServerError, HttpStatusCode.NotFound)
            .ResponseFormat(WizardViewResponse.Example());
        }
        public async Task <ViewedWizard> CreateViewedWizardAsync(ViewedWizard wizardView)
        {
            using (var dc = new SynthesisDataContext())
            {
                dc.ViewedWizards.Add(wizardView);
                await dc.SaveChangesAsync();

                return(await dc.ViewedWizards.SingleAsync(x => x.UserId == wizardView.UserId && x.WizardType == wizardView.WizardType));
            }
        }
        public async Task <WizardViewResponse> CreateWizardViewAsync(ViewedWizard model)
        {
            var validationResult = _validatorLocator.Validate <ViewedWizardValidator>(model);

            if (!validationResult.IsValid)
            {
                _logger.Error("Validation failed while attempting to create a ViewedWizard resource.");
                throw new ValidationFailedException(validationResult.Errors);
            }

            try
            {
                var existingWizards = await RetrieveViewedWizardsAsync(model.UserId);

                var wizardsOfType = existingWizards.Where(w => w.WizardType == model.WizardType).ToList();

                if (wizardsOfType.Count > 0)
                {
                    return(new WizardViewResponse
                    {
                        WizardViews = wizardsOfType,
                        ResultMessage = $"Wizard(s) already viewed by user '{model.UserId}'.",
                        ResultCode = ResultCode.RecordAlreadyExists
                    });
                }

                var viewedWizard = await _dbService.CreateViewedWizardAsync(model);

                var key = KeyResolver.ViewedWizards(model.UserId);
                await _cache.KeyDeleteAsync(key, CacheCommandOptions.FireAndForget);

                return(new WizardViewResponse
                {
                    WizardViews = new List <ViewedWizard> {
                        viewedWizard
                    },
                    ResultMessage = $"Wizard marked as viewed successfully for user '{model.UserId}'.",
                    ResultCode = ResultCode.Success
                });
            }
            catch (Exception ex)
            {
                _logger.Error("A ViewedWizard resource has not been created due to an unknown error.", ex);
                return(new WizardViewResponse()
                {
                    ResultMessage = ex.ToString(),
                    ResultCode = ResultCode.Failed
                });
            }
        }