Skip to content

Commit

Permalink
Merge branch 'main' into 51413
Browse files Browse the repository at this point in the history
  • Loading branch information
tsakiro committed Jul 10, 2024
2 parents e57559a + 6290774 commit 50d9cba
Show file tree
Hide file tree
Showing 23 changed files with 3,923 additions and 316 deletions.
72 changes: 62 additions & 10 deletions backend/api/Controllers/BlobStorageController.cs
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@

[Authorize]
[ApiController]
[Route("projects/{projectId}/cases/{caseId}/images")]
[Route("projects/{projectId}")]
public class BlobStorageController : ControllerBase
{
private readonly IBlobStorageService _blobStorageService;
Expand All @@ -15,8 +15,7 @@ public BlobStorageController(IBlobStorageService blobStorageService)
_blobStorageService = blobStorageService;
}

[HttpPost]
public async Task<ActionResult<ImageDto>> UploadImage(Guid projectId, [FromForm] string projectName, Guid caseId, [FromForm] IFormFile image)
private async Task<ActionResult<ImageDto>> UploadImage(Guid projectId, string projectName, Guid? caseId, IFormFile image)
{
const int maxFileSize = 5 * 1024 * 1024; // 5MB
string[] permittedExtensions = { ".jpg", ".jpeg", ".png", ".gif" };
Expand All @@ -37,12 +36,32 @@ public async Task<ActionResult<ImageDto>> UploadImage(Guid projectId, [FromForm]
return BadRequest($"File {image.FileName} has an invalid extension. Only image files are allowed.");
}

// Process the image upload
var imageDto = await _blobStorageService.SaveImage(projectId, projectName, image, caseId);
return Ok(imageDto);
try
{
if (caseId.HasValue)
{
var imageDto = await _blobStorageService.SaveImage(projectId, projectName, image, caseId.Value);
return Ok(imageDto);
}
else
{
var imageDto = await _blobStorageService.SaveImage(projectId, projectName, image);
return Ok(imageDto);
}
}
catch (Exception)
{
return StatusCode(StatusCodes.Status500InternalServerError, "An error occurred while uploading the image.");
}
}

[HttpPost("cases/{caseId}/images")]
public Task<ActionResult<ImageDto>> UploadCaseImage(Guid projectId, [FromForm] string projectName, Guid caseId, [FromForm] IFormFile image)
{
return UploadImage(projectId, projectName, caseId, image);
}

[HttpGet]
[HttpGet("cases/{caseId}/images")]
public async Task<ActionResult<List<ImageDto>>> GetImages(Guid projectId, Guid caseId)
{
try
Expand All @@ -56,12 +75,12 @@ public async Task<ActionResult<List<ImageDto>>> GetImages(Guid projectId, Guid c
}
}

[HttpDelete("{imageId}")]
public async Task<ActionResult> DeleteImage(Guid projectId, Guid caseId, Guid imageId)
[HttpDelete("cases/{caseId}/images/{imageId}")]
public async Task<ActionResult> DeleteCaseImage(Guid imageId)
{
try
{
await _blobStorageService.DeleteImage(caseId, imageId);
await _blobStorageService.DeleteImage(imageId);
return NoContent();
}
catch (Exception)
Expand All @@ -70,4 +89,37 @@ public async Task<ActionResult> DeleteImage(Guid projectId, Guid caseId, Guid im
}
}

[HttpPost("images")]
public Task<ActionResult<ImageDto>> UploadProjectImage(Guid projectId, [FromForm] string projectName, [FromForm] IFormFile image)
{
return UploadImage(projectId, projectName, null, image);
}

[HttpGet("images")]
public async Task<ActionResult<List<ImageDto>>> GetProjectImages(Guid projectId)
{
try
{
var imageDtos = await _blobStorageService.GetProjectImages(projectId);
return Ok(imageDtos);
}
catch (Exception)
{
return StatusCode(StatusCodes.Status500InternalServerError, "An error occurred while retrieving images.");
}
}

[HttpDelete("images/{imageId}")]
public async Task<ActionResult> DeleteProjectImage(Guid imageId)
{
try
{
await _blobStorageService.DeleteImage(imageId);
return NoContent();
}
catch (Exception)
{
return StatusCode(StatusCodes.Status500InternalServerError, "An error occurred while deleting the image.");
}
}
}
2 changes: 1 addition & 1 deletion backend/api/Dtos/Image/imageDto.cs
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,7 @@ public class ImageDto
public string? Description { get; set; }

[Required]
public Guid CaseId { get; set; }
public Guid? CaseId { get; set; }

[Required]
public string ProjectName { get; set; } = null!;
Expand Down
Loading

0 comments on commit 50d9cba

Please sign in to comment.