예제 #1
0
        public IActionResult CreateHighlight(HighlightResource highlightResource)
        {
            if (highlightResource == null)
            {
                ProblemDetails problem = new ProblemDetails
                {
                    Title    = "Failed creating highlight.",
                    Detail   = "The highlight resource is null.",
                    Instance = "2206D5EC-9E20-44A7-A729-0205BEA994E5"
                };
                return(BadRequest(problem));
            }

            Highlight highlight = mapper.Map <HighlightResource, Highlight>(highlightResource);

            try
            {
                highlightService.Add(highlight);
                highlightService.Save();
                return(Created(nameof(CreateHighlight), mapper.Map <Highlight, HighlightResourceResult>(highlight)));
            } catch (DbUpdateException e)
            {
                Log.Logger.Error(e, "Database exception");

                ProblemDetails problem = new ProblemDetails
                {
                    Title    = "Failed Saving highlight.",
                    Detail   = "Failed saving the highlight to the database.",
                    Instance = "764E41C3-06A5-47D6-9642-C9E8A0B7CFC7"
                };
                return(BadRequest(problem));
            }
        }
        public async Task <IActionResult> CreateHighlight(HighlightResource highlightResource)
        {
            if (highlightResource == null)
            {
                ProblemDetails problem = new ProblemDetails
                {
                    Title    = "Failed creating highlight.",
                    Detail   = "The highlight resource is null.",
                    Instance = "2206D5EC-9E20-44A7-A729-0205BEA994E5"
                };
                return(BadRequest(problem));
            }

            Highlight highlight = mapper.Map <HighlightResource, Highlight>(highlightResource);
            File      file      = null;

            if (highlightResource.ImageId != null)
            {
                file = await fileService.FindAsync(highlightResource.ImageId.Value);

                if (file == null)
                {
                    ProblemDetails problem = new ProblemDetails
                    {
                        Title    = "File was not found.",
                        Detail   = "The specified file was not found while creating highlight.",
                        Instance = "F4AF3D06-DD74-40E0-99BB-8E1183A6A734"
                    };
                    return(NotFound(problem));
                }
            }

            Project project = await projectService.FindWithUserCollaboratorsAndInstitutionsAsync(highlightResource.ProjectId);

            if (project == null)
            {
                ProblemDetails problem = new ProblemDetails
                {
                    Title    = "Project was not found.",
                    Detail   = "The specified project was not found while creating highlight.",
                    Instance = "A6DBEE27-0363-479B-B099-A467D4B2CF00"
                };
                return(NotFound(problem));
            }

            highlight.Image   = file;
            highlight.Project = project;

            try
            {
                highlightService.Add(highlight);
                highlightService.Save();
                return(Created(nameof(CreateHighlight), mapper.Map <Highlight, HighlightResourceResult>(highlight)));
            } catch (DbUpdateException e)
            {
                Log.Logger.Error(e, "Database exception");

                ProblemDetails problem = new ProblemDetails
                {
                    Title    = "Failed Saving highlight.",
                    Detail   = "Failed saving the highlight to the database.",
                    Instance = "764E41C3-06A5-47D6-9642-C9E8A0B7CFC7"
                };
                return(BadRequest(problem));
            }
        }