Skip to content

Commit

Permalink
Inventory complete
Browse files Browse the repository at this point in the history
  • Loading branch information
gabo71096 committed Aug 30, 2023
1 parent a5ea87c commit 2330dea
Show file tree
Hide file tree
Showing 40 changed files with 2,039 additions and 484 deletions.
13 changes: 13 additions & 0 deletions .vscode/settings.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,13 @@
{
"sqltools.connections": [
{
"previewLimit": 50,
"server": "localhost",
"port": 5433,
"driver": "PostgreSQL",
"name": "pg",
"database": "store",
"username": "postgres"
}
]
}
2 changes: 2 additions & 0 deletions API/API.csproj
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,8 @@
</PropertyGroup>

<ItemGroup>
<PackageReference Include="AutoMapper.Extensions.Microsoft.DependencyInjection" Version="12.0.1" />
<PackageReference Include="CloudinaryDotNet" Version="1.22.0" />
<PackageReference Include="Microsoft.AspNetCore.Authentication.JwtBearer" Version="7.0.5" />
<PackageReference Include="Microsoft.AspNetCore.Identity.EntityFrameworkCore" Version="7.0.5" />
<PackageReference Include="Microsoft.AspNetCore.OpenApi" Version="7.0.5" />
Expand Down
86 changes: 84 additions & 2 deletions API/Controllers/ProductsController.cs
Original file line number Diff line number Diff line change
@@ -1,8 +1,13 @@
using System.Text.Json;
using API.Data;
using API.DTOs;
using API.Entities;
using API.Extensions;
using API.RequestHelpers;
using API.Services;
using AutoMapper;
using CloudinaryDotNet.Actions;
using Microsoft.AspNetCore.Authorization;
using Microsoft.AspNetCore.Mvc;
using Microsoft.EntityFrameworkCore;

Expand All @@ -11,8 +16,12 @@ namespace API.Controllers
public class ProductsController : BaseApiController
{
private readonly StoreContext _context;
public ProductsController(StoreContext context)
private readonly IMapper _mapper;
private readonly ImageService _imageService;
public ProductsController(StoreContext context, IMapper mapper, ImageService imageService)
{
_imageService = imageService;
_mapper = mapper;
_context = context;
}

Expand All @@ -32,7 +41,7 @@ public async Task<ActionResult<PagedList<Product>>> GetProducts([FromQuery] Prod
return products;
}

[HttpGet("{id}")]
[HttpGet("{id}", Name = "GetProduct")]
public async Task<ActionResult<Product>> GetProduct(int id)
{
var product = await _context.Products.FindAsync(id);
Expand All @@ -50,5 +59,78 @@ public async Task<IActionResult> GetFilters()

return Ok(new { brands, types });
}

[Authorize(Roles = "Admin")]
[HttpPost]
public async Task<ActionResult<Product>> CreateProduct([FromForm] CreateProductDto productDto)
{
var product = _mapper.Map<Product>(productDto);

if (productDto.File != null)
{
var imageResult = await _imageService.AddImageAsync(productDto.File);

if (imageResult.Error != null) return BadRequest(new ProblemDetails { Title = imageResult.Error.Message });

product.PictureUrl = imageResult.SecureUrl.ToString();
product.PublicId = imageResult.PublicId;
}

_context.Products.Add(product);

var result = await _context.SaveChangesAsync() > 0;

if (result) return CreatedAtRoute("GetProduct", new { Id = product.Id }, product);

return BadRequest(new ProblemDetails { Title = "Problem creating new product" });
}

[Authorize(Roles = "Admin")]
[HttpPut]
public async Task<ActionResult<Product>> UpdateProduct([FromForm] UpdateProductDto productDto)
{
var product = await _context.Products.FindAsync(productDto.Id);

if (product == null) return NotFound();

_mapper.Map(productDto, product);

if (productDto.File != null)
{
var imageResult = await _imageService.AddImageAsync(productDto.File);

if (imageResult.Error != null) return BadRequest(new ProblemDetails { Title = imageResult.Error.Message });

if (!string.IsNullOrEmpty(product.PublicId)) await _imageService.DeleteImageAsync(product.PublicId);

product.PictureUrl = imageResult.SecureUrl.ToString();
product.PublicId = imageResult.PublicId;
}

var result = await _context.SaveChangesAsync() > 0;

if (result) return Ok(product);

return BadRequest(new ProblemDetails { Title = "Problem updating product" });
}

[Authorize(Roles = "Admin")]
[HttpDelete("{id}")]
public async Task<ActionResult> DeleteProduct(int id)
{
var product = await _context.Products.FindAsync(id);

if (product == null) return NotFound();

if (!string.IsNullOrEmpty(product.PublicId)) await _imageService.DeleteImageAsync(product.PublicId);

_context.Products.Remove(product);

var result = await _context.SaveChangesAsync() > 0;

if (result) return Ok();

return BadRequest(new ProblemDetails { Title = "Problem deleting product" });
}
}
}
24 changes: 24 additions & 0 deletions API/DTOs/CreateProductDto.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,24 @@
using System.ComponentModel.DataAnnotations;

namespace API.DTOs
{
public class CreateProductDto
{
[Required]
public string Name { get; set; }
[Required]
public string Description { get; set; }
[Required]
[Range(100, Double.PositiveInfinity)]
public long Price { get; set; }
[Required]
public IFormFile File { get; set; }
[Required]
public string Type { get; set; }
[Required]
public string Brand { get; set; }
[Required]
[Range(0, 200)]
public int QuantityInStock { get; set; }
}
}
25 changes: 25 additions & 0 deletions API/DTOs/UpdateProductDto.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,25 @@
using System.ComponentModel.DataAnnotations;

namespace API.DTOs
{
public class UpdateProductDto
{
[Required]
public int Id { get; set; }
[Required]
public string Name { get; set; }
[Required]
public string Description { get; set; }
[Required]
[Range(100, Double.PositiveInfinity)]
public long Price { get; set; }
public IFormFile File { get; set; }
[Required]
public string Type { get; set; }
[Required]
public string Brand { get; set; }
[Required]
[Range(0, 200)]
public int QuantityInStock { get; set; }
}
}
2 changes: 1 addition & 1 deletion API/Data/DbInitializer.cs
Original file line number Diff line number Diff line change
Expand Up @@ -25,7 +25,7 @@ public static async Task Initialize(StoreContext context, UserManager<User> user
Email = "[email protected]"
};

await userManager.CreateAsync(admin, "P4$$W0RD!");
await userManager.CreateAsync(admin, "P4$$w0rd");
await userManager.AddToRolesAsync(admin, new[] { "Member", "Admin" });
}

Expand Down
Loading

0 comments on commit 2330dea

Please sign in to comment.