public async Task <IActionResult> CreateCallToActionOption(CallToActionOptionResource callToActionOptionResource)
        {
            if (callToActionOptionResource == null)
            {
                ProblemDetails problem = new ProblemDetails
                {
                    Title    = "Failed creating the call to action option.",
                    Detail   = "The institution resource is null.",
                    Instance = "E2FD8F7B-96B1-4406-9E90-138AA36B570B"
                };
                return(BadRequest(problem));
            }

            CallToActionOption option =
                mapper.Map <CallToActionOptionResource, CallToActionOption>(callToActionOptionResource);

            if ((await callToActionOptionService.GetCallToActionOptionsFromTypeAsync(option.Type)).Any() &&
                (await callToActionOptionService.GetCallToActionOptionFromValueAsync(option.Value)).Any())
            {
                ProblemDetails problem = new ProblemDetails
                {
                    Title    = "Failed creating the call to action option.",
                    Detail   = "Identical call to action option already exists.",
                    Instance = "1DA7B168-FAD1-41B6-A90F-3AAEB26147CE"
                };
                return(BadRequest(problem));
            }

            try
            {
                callToActionOptionService.Add(option);
                callToActionOptionService.Save();
                CallToActionOptionResourceResult model =
                    mapper.Map <CallToActionOption, CallToActionOptionResourceResult>(option);
                return(Created(nameof(CreateCallToActionOption), model));
            } catch (DbUpdateException)
            {
                Log.Logger.Error("Database exception");

                ProblemDetails problem = new ProblemDetails
                {
                    Title    = "Failed Saving the call to action option.",
                    Detail   = "Failed saving the call to action option to the database.",
                    Instance = "5A1D2B14-E320-4FAE-84DF-BC02B996588B"
                };
                return(BadRequest(problem));
            }
        }
Esempio n. 2
0
        public async Task <IActionResult> CreateProjectAsync([FromBody] ProjectResource projectResource)
        {
            if (projectResource == null)
            {
                ProblemDetails problem = new ProblemDetails
                {
                    Title    = "Failed to create a new project.",
                    Detail   = "The specified project resource was null.",
                    Instance = "8D3D9119-0D12-4631-B2DC-56494639A849"
                };
                return(BadRequest(problem));
            }

            if (projectResource.CallToAction != null)
            {
                IEnumerable <CallToActionOption> callToActionOptions =
                    await callToActionOptionService.GetCallToActionOptionFromValueAsync(
                        projectResource.CallToAction.OptionValue);

                if (!callToActionOptions.Any())
                {
                    ProblemDetails problem = new ProblemDetails
                    {
                        Title  = "Call to action value was not found.",
                        Detail =
                            "The specified call to action value was not found while creating the project.",
                        Instance = "40EE82EB-930F-40C8-AE94-0041F7573FE9"
                    };
                    return(BadRequest(problem));
                }
            }

            Project project = mapper.Map <ProjectResource, Project>(projectResource);
            File    file    = await fileService.FindAsync(projectResource.FileId);

            if (projectResource.FileId != 0 &&
                file == null)
            {
                ProblemDetails problem = new ProblemDetails
                {
                    Title    = "File was not found.",
                    Detail   = "The specified file was not found while creating project.",
                    Instance = "8CABE64D-6B73-4C88-BBD8-B32FA9FE6EC7"
                };
                return(BadRequest(problem));
            }

            project.ProjectIcon = file;
            project.User        = await HttpContext.GetContextUser(userService)
                                  .ConfigureAwait(false);

            try
            {
                projectService.Add(project);
                projectService.Save();
                return(Created(nameof(CreateProjectAsync), mapper.Map <Project, ProjectResourceResult>(project)));
            } catch (DbUpdateException e)
            {
                Log.Logger.Error(e, "Database exception");


                ProblemDetails problem = new ProblemDetails
                {
                    Title    = "Failed to save new project.",
                    Detail   = "There was a problem while saving the project to the database.",
                    Instance = "9FEEF001-F91F-44E9-8090-6106703AB033"
                };
                return(BadRequest(problem));
            }
        }