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> UpdateHighlight(int highlightId, [FromBody] HighlightResource highlightResource) { Highlight highlight = await highlightService.FindAsync(highlightId); if (highlight == null) { ProblemDetails problem = new ProblemDetails { Title = "Failed getting the highlight.", Detail = "The database does not contain a highlight with that id.", Instance = "795CDB7E-DB46-4A24-8F12-7557BDD79D15" }; return(NotFound(problem)); } mapper.Map(highlightResource, highlight); highlightService.Update(highlight); highlightService.Save(); return(Ok(mapper.Map <Highlight, HighlightResourceResult>(highlight))); }
public async Task <IActionResult> UpdateHighlight(int highlightId, [FromBody] HighlightResource highlightResource) { Highlight highlight = await highlightService.FindAsync(highlightId); if (highlight == null) { ProblemDetails problem = new ProblemDetails { Title = "Failed getting the highlight.", Detail = "The database does not contain a highlight with that id.", Instance = "795CDB7E-DB46-4A24-8F12-7557BDD79D15" }; return(NotFound(problem)); } mapper.Map(highlightResource, highlight); 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 = "412CC9A8-CB2A-4389-9F9C-F8494AB65AE0" }; 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 = "B923484E-D562-4F03-A57E-88D02819EBD6" }; return(NotFound(problem)); } highlight.Image = file; highlight.Project = project; try { highlightService.Update(highlight); highlightService.Save(); return(Ok(mapper.Map <Highlight, HighlightResourceResult>(highlight))); } catch (DbUpdateException e) { Log.Logger.Error(e, "Database exception"); ProblemDetails problem = new ProblemDetails { Title = "Failed updating highlight.", Detail = "Failed updating the highlight to the database.", Instance = "D9933508-F7B6-44B1-9266-5B040D2EA07D" }; 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)); } }