Skip to content

Commit

Permalink
add updatecustomization
Browse files Browse the repository at this point in the history
  • Loading branch information
Thomas-Valkenburg committed Jun 17, 2024
1 parent f4461af commit 7a7affe
Showing 1 changed file with 46 additions and 2 deletions.
48 changes: 46 additions & 2 deletions Talpa Api/Controllers/Api/CustomizationController.cs
Original file line number Diff line number Diff line change
Expand Up @@ -8,24 +8,68 @@ namespace Talpa_Api.Controllers.Api;
[ApiController]
public class CustomizationController(Context context) : ControllerBase
{
private readonly List<string> AllowedExtensions = [".jpg", ".jpeg", ".png", ".gif", ".bmp", ".webp"];

[HttpGet]
public IActionResult GetCustomization()
{
return Ok(context.Customization.FirstOrDefault());
}

[HttpPost]
public IActionResult UpdateCustomization([FromBody] Customization customization)
public IActionResult UpdateCustomization(bool? gradient, string? color1, string? color2, string? color3,
IFormFile? image)
{
var currentCustomization = context.Customization.FirstOrDefault();

if (image is not null)
{
if (!AllowedExtensions.Contains(Path.GetExtension(image.FileName).ToLowerInvariant()))
{
return BadRequest();
}

var path = Path.Combine("wwwroot", "images", "logo." + Path.GetExtension(image.FileName));
var stream = new FileStream(path, FileMode.Create);

image.CopyTo(stream);
}

if (currentCustomization is null)
{
if (!gradient.HasValue || color1 is null || image is null ||
(gradient is true && (color2 is null || color3 is null)))
{
return BadRequest();
}

var customization = new Customization(gradient!.Value, color1, color2, color3, image.FileName);

context.Customization.Add(customization);
return Created();
}

context.Customization.Update(customization);
if (gradient is not null)
{
currentCustomization.Gradient = gradient.Value;
}

if (color1 is not null)
{
currentCustomization.Color1 = color1;
}

if (color2 is not null)
{
currentCustomization.Color2 = color2;
}

if (color3 is not null)
{
currentCustomization.Color3 = color3;
}

context.Customization.Update(currentCustomization);
context.SaveChanges();

return NoContent();
Expand Down

0 comments on commit 7a7affe

Please sign in to comment.