Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Feature/430 add images to project #445

Merged
merged 13 commits into from
Jun 2, 2021
2 changes: 1 addition & 1 deletion .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -338,4 +338,4 @@ IdentityServer/tempkey.rsa
!/API/Uploads/Images/.gitkeep

###
rabbitmq/data/
rabbitmq/data/
43 changes: 38 additions & 5 deletions API/Controllers/ProjectController.cs
Original file line number Diff line number Diff line change
Expand Up @@ -34,7 +34,6 @@
using System.Linq;
using System.Net;
using System.Threading.Tasks;
using File = Models.File;

namespace API.Controllers
{
Expand Down Expand Up @@ -349,7 +348,7 @@ await callToActionOptionService.GetCallToActionOptionFromValueAsync(
}

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

if(projectResource.FileId != 0 &&
file == null)
Expand All @@ -363,6 +362,23 @@ await callToActionOptionService.GetCallToActionOptionFromValueAsync(
return BadRequest(problem);
}

foreach(int projectResourceImageId in projectResource.ImageIds)
{
Models.File image = await fileService.FindAsync(projectResourceImageId);
if(image == null)
{
ProblemDetails problem = new ProblemDetails
{
Title = "Image was not found.",
Detail = "The specified image was not found while creating project.",
Instance = "B040FAAD-FD22-4C77-822E-C498DFA1A9CB"
};
return BadRequest(problem);
}

project.Images.Add(image);
}

project.ProjectIcon = file;
project.User = await HttpContext.GetContextUser(userService)
.ConfigureAwait(false);
Expand Down Expand Up @@ -511,15 +527,15 @@ await callToActionOptionService.GetCallToActionOptionFromValueAsync(
}

// Upload the new file if there is one
File file = null;
Models.File file = null;
if(projectResource.FileId != 0)
{
if(project.ProjectIconId != 0 &&
project.ProjectIconId != null)
{
if(project.ProjectIconId != projectResource.FileId)
{
File fileToDelete = await fileService.FindAsync(project.ProjectIconId.Value);
Models.File fileToDelete = await fileService.FindAsync(project.ProjectIconId.Value);

// Remove the file from the filesystem
fileUploader.DeleteFileFromDirectory(fileToDelete);
Expand Down Expand Up @@ -550,6 +566,23 @@ await callToActionOptionService.GetCallToActionOptionFromValueAsync(
}
}

foreach(int projectResourceImageId in projectResource.ImageIds)
{
Models.File image = await fileService.FindAsync(projectResourceImageId);
if(image == null)
{
ProblemDetails problem = new ProblemDetails
{
Title = "Image was not found.",
Detail = "The specified image was not found while creating project.",
Instance = "FC816E40-31A6-4187-BEBA-D22F06019F8F"
};
return BadRequest(problem);
}

project.Images.Add(image);
}

macfleury-2000 marked this conversation as resolved.
Show resolved Hide resolved
await projectCategoryService.ClearProjectCategories(project);
if(projectResource.Categories != null)
{
Expand Down Expand Up @@ -639,7 +672,7 @@ public async Task<IActionResult> DeleteProject(int projectId)
if(project.ProjectIconId.HasValue)
{
// We need to delete the old file.
File fileToDelete = await fileService.FindAsync(project.ProjectIconId.Value);
Models.File fileToDelete = await fileService.FindAsync(project.ProjectIconId.Value);
try
{
// Remove the file from the database
Expand Down
5 changes: 5 additions & 0 deletions API/Resources/ProjectResource.cs
Original file line number Diff line number Diff line change
Expand Up @@ -71,6 +71,11 @@ public class ProjectResource
/// </summary>
public ICollection<ProjectCategoryResource> Categories { get; set; }

/// <summary>
/// This gets or sets the image ID's
/// </summary>
public IEnumerable<int> ImageIds { get; set; } = new List<int>();

}

}
5 changes: 5 additions & 0 deletions API/Resources/ProjectResourceResult.cs
Original file line number Diff line number Diff line change
Expand Up @@ -97,6 +97,11 @@ public class ProjectResourceResult
/// </summary>
public List<ProjectLikesResourceResult> Likes { get; set; }

/// <summary>
/// This gets or sets the images in de project.
/// </summary>
public List<FileResourceResult> Images { get; set; }

/// <summary>
/// Sets or gets if project is visible to institute members only or not
/// </summary>
Expand Down
1 change: 1 addition & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,7 @@ and this project adheres to [Semantic Versioning](https://semver.org/spec/v2.0.0
### Added

- Added image to highlights - [#431](https://github.com/DigitalExcellence/dex-backend/issues/431)
- Added functionality to add multiples images to a project - [#430](https://github.com/DigitalExcellence/dex-backend/issues/430)

### Changed

Expand Down
Loading