Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
484 changes: 484 additions & 0 deletions Ecommerce.Api.Lepros311/.gitignore

Large diffs are not rendered by default.

765 changes: 765 additions & 0 deletions Ecommerce.Api.Lepros311/Ecommerce API.postman_collection.json

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

🟢 Postman Collection

Large diffs are not rendered by default.

25 changes: 25 additions & 0 deletions Ecommerce.Api.Lepros311/Ecommerce.Api.sln
Original file line number Diff line number Diff line change
@@ -0,0 +1,25 @@

Microsoft Visual Studio Solution File, Format Version 12.00
# Visual Studio Version 17
VisualStudioVersion = 17.14.36327.8 d17.14
MinimumVisualStudioVersion = 10.0.40219.1
Project("{FAE04EC0-301F-11D3-BF4B-00C04F79EFBC}") = "Ecommerce.Api", "Ecommerce.Api\Ecommerce.Api.csproj", "{D8CA0565-FF7F-4CE6-A92D-7B29C2CB14B0}"
EndProject
Global
GlobalSection(SolutionConfigurationPlatforms) = preSolution
Debug|Any CPU = Debug|Any CPU
Release|Any CPU = Release|Any CPU
EndGlobalSection
GlobalSection(ProjectConfigurationPlatforms) = postSolution
{D8CA0565-FF7F-4CE6-A92D-7B29C2CB14B0}.Debug|Any CPU.ActiveCfg = Debug|Any CPU
{D8CA0565-FF7F-4CE6-A92D-7B29C2CB14B0}.Debug|Any CPU.Build.0 = Debug|Any CPU
{D8CA0565-FF7F-4CE6-A92D-7B29C2CB14B0}.Release|Any CPU.ActiveCfg = Release|Any CPU
{D8CA0565-FF7F-4CE6-A92D-7B29C2CB14B0}.Release|Any CPU.Build.0 = Release|Any CPU
EndGlobalSection
GlobalSection(SolutionProperties) = preSolution
HideSolutionNode = FALSE
EndGlobalSection
GlobalSection(ExtensibilityGlobals) = postSolution
SolutionGuid = {F8125966-4238-49C5-BE43-7136A2350AA5}
EndGlobalSection
EndGlobal
Original file line number Diff line number Diff line change
@@ -0,0 +1,119 @@
using Ecommerce.Api.Models;
using Ecommerce.Api.Responses;
using Ecommerce.Api.Services;
using Microsoft.AspNetCore.Mvc;

namespace Ecommerce.Api.Controllers
{
[Route("api/categories")]
[ApiController]
public class CategoryController : ControllerBase
{
private readonly ICategoryService _categoryService;

public CategoryController(ICategoryService categoryService)
{
_categoryService = categoryService;
}

[HttpGet]
public async Task<ActionResult<List<CategoryDto>>> GetPagedCategories([FromQuery] PaginationParams paginationParams)
{
var responseWithDtos = await _categoryService.GetPagedCategories(paginationParams);

if (responseWithDtos.Status == ResponseStatus.Fail)
{
return BadRequest(responseWithDtos.Message);
}

return Ok(responseWithDtos.Data);
}

[HttpGet("{id}")]
public async Task<ActionResult<CategoryDto>> GetCategoryById(int id)
{
var response = await _categoryService.GetCategoryById(id);

if (response.Status == ResponseStatus.Fail)
{
return NotFound(response.Message);
}

var returnedCategory = response.Data;

var categoryDto = new CategoryDto
{
CategoryId = returnedCategory.CategoryId,
CategoryName = returnedCategory.CategoryName,
Products = returnedCategory.Products.Select(p => new ProductDto
{
ProductId = p.ProductId,
ProductName = p.ProductName,
Price = p.Price,
Category = p.Category.CategoryName
}).ToList()
};

return Ok(categoryDto);
}

[HttpPost]
public async Task<ActionResult<CategoryDto>> CreateCategory([FromBody] WriteCategoryDto writeCategoryDto)
{
var responseWithDataDto = await _categoryService.CreateCategory(writeCategoryDto);

if (responseWithDataDto.Message == "Category not found.")
{
return NotFound(responseWithDataDto.Message);
}

if (responseWithDataDto.Status == ResponseStatus.Fail)
{
return BadRequest(responseWithDataDto.Message);
}

return CreatedAtAction(nameof(GetCategoryById),
new { id = responseWithDataDto.Data.CategoryId }, responseWithDataDto.Data);
}

[HttpPut("{id}")]
public async Task<ActionResult<CategoryDto>> UpdateCategory(int id, [FromBody] WriteCategoryDto writeCategoryDto)
{
var response = await _categoryService.UpdateCategory(id, writeCategoryDto);

if (response.Message == "Category not found.")
{
return NotFound(response.Message);
}

if (response.Status == ResponseStatus.Fail)
{
return BadRequest(response.Message);
}

return NoContent();
}

[HttpDelete("{id}")]
public async Task<IActionResult> DeleteCategory(int id)
{
var response = await _categoryService.DeleteCategory(id);

if (response.Message == "Category not found.")
{
return NotFound(response.Message);
}
else if (response.Message == "Cannot delete categories that contain products.")
{
return Conflict(response.Message);
}
else if (response.Status == ResponseStatus.Fail)
{
return BadRequest(response.Message);
}

return NoContent();
}
}
}

Original file line number Diff line number Diff line change
@@ -0,0 +1,112 @@
using Ecommerce.Api.Models;
using Ecommerce.Api.Responses;
using Ecommerce.Api.Services;
using Microsoft.AspNetCore.Mvc;

namespace Ecommerce.Api.Controllers
{
[Route("api/lineitems")]
[ApiController]
public class LineItemController : ControllerBase
{
private readonly ILineItemService _lineItemService;

public LineItemController(ILineItemService lineItemService)
{
_lineItemService = lineItemService;
}

[HttpGet]
public async Task<ActionResult<List<LineItemDto>>> GetPagedLineItems([FromQuery] PaginationParams paginationParams)
{
var responseWithDtos = await _lineItemService.GetPagedLineItems(paginationParams);

if (responseWithDtos.Status == ResponseStatus.Fail)
{
return BadRequest(responseWithDtos.Message);
}

return Ok(responseWithDtos.Data);
}

[HttpGet("{id}")]
public async Task<ActionResult<LineItemDto>> GetLineItemById(int id)
{
var response = await _lineItemService.GetLineItemById(id);

if (response.Status == ResponseStatus.Fail)
{
return NotFound(response.Message);
}

var returnedLineItem = response.Data;

var lineItemDto = new LineItemDto
{
LineItemId = returnedLineItem.LineItemId,
ProductId = returnedLineItem.ProductId,
ProductName = returnedLineItem.Product.ProductName,
Category = returnedLineItem.Product.Category.CategoryName,
Quantity = returnedLineItem.Quantity,
UnitPrice = returnedLineItem.UnitPrice,
SaleId = returnedLineItem.SaleId,
};

return Ok(lineItemDto);
}

[HttpPost]
public async Task<ActionResult<LineItemDto>> CreateLineItem([FromBody] WriteLineItemDto writeLineItemDto)
{
var responseWithDataDto = await _lineItemService.CreateLineItem(writeLineItemDto);

if (responseWithDataDto.Message == "Sale not found.")
{
return NotFound(responseWithDataDto.Message);
}

if (responseWithDataDto.Status == ResponseStatus.Fail)
{
return BadRequest(responseWithDataDto.Message);
}

return CreatedAtAction(nameof(GetLineItemById),
new { id = responseWithDataDto.Data.LineItemId }, responseWithDataDto.Data);
}

[HttpPut("{id}")]
public async Task<ActionResult<LineItemDto>> UpdateLineItem(int id, [FromBody] WriteLineItemDto writeLineItemDto)
{
var response = await _lineItemService.UpdateLineItem(id, writeLineItemDto);

if (response.Message == "Line Item not found." || response.Message == "Sale not found." || response.Message == "Product not found.")
{
return NotFound(response.Message);
}

if (response.Status == ResponseStatus.Fail)
{
return BadRequest(response.Message);
}

return NoContent();
}

[HttpDelete("{id}")]
public async Task<IActionResult> DeleteLineItem(int id)
{
var response = await _lineItemService.DeleteLineItem(id);

if (response.Message == "Line Item not found.")
{
return NotFound(response.Message);
}
else if (response.Status == ResponseStatus.Fail)
{
return BadRequest(response.Message);
}

return NoContent();
}
}
}
109 changes: 109 additions & 0 deletions Ecommerce.Api.Lepros311/Ecommerce.Api/Controllers/ProductController.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,109 @@
using Ecommerce.Api.Models;
using Ecommerce.Api.Responses;
using Ecommerce.Api.Services;
using Microsoft.AspNetCore.Mvc;

namespace Ecommerce.Api.Controllers
{
[Route("api/products")]
[ApiController]
public class ProductController : ControllerBase
{
private readonly IProductService _productService;

public ProductController(IProductService productService)
{
_productService = productService;
}

[HttpGet]
public async Task<ActionResult<List<ProductDto>>> GetPagedProducts([FromQuery] PaginationParams paginationParams)

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

🟢 GetProducts endpoints need to have pagination capabilities

{
var responseWithDtos = await _productService.GetPagedProducts(paginationParams);

if (responseWithDtos.Status == ResponseStatus.Fail)
{
return BadRequest(responseWithDtos.Message);
}

return Ok(responseWithDtos.Data);
}

[HttpGet("{id}")]
public async Task<ActionResult<ProductDto>> GetProductById(int id)
{
var response = await _productService.GetProductById(id);

if (response.Status == ResponseStatus.Fail)
{
return NotFound(response.Message);
}

var returnedProduct = response.Data;

var productDto = new ProductDto
{
ProductId = returnedProduct.ProductId,
ProductName = returnedProduct.ProductName,
Price = returnedProduct.Price,
Category = returnedProduct.Category.CategoryName
};

return Ok(productDto);
}

[HttpPost]
public async Task<ActionResult<ProductDto>> CreateProduct([FromBody] WriteProductDto writeProductDto)
{
var responseWithDataDto = await _productService.CreateProduct(writeProductDto);

if (responseWithDataDto.Message == "Category not found.")
{
return NotFound(responseWithDataDto.Message);
}

if (responseWithDataDto.Status == ResponseStatus.Fail)
{
return BadRequest(responseWithDataDto.Message);
}

return CreatedAtAction(nameof(GetProductById),
new { id = responseWithDataDto.Data.ProductId }, responseWithDataDto.Data);
}

[HttpPut("{id}")]
public async Task<ActionResult<ProductDto>> UpdateProduct(int id, [FromBody] WriteProductDto writeProductDto)
{
var response = await _productService.UpdateProduct(id, writeProductDto);

if (response.Message == "Product not found." || response.Message == "Category not found.")
{
return NotFound(response.Message);
}

if (response.Status == ResponseStatus.Fail)
{
return BadRequest(response.Message);
}

return NoContent();
}

[HttpDelete("{id}")]
public async Task<IActionResult> DeleteProduct(int id)
{
var response = await _productService.DeleteProduct(id);

if (response.Message == "Product not found.")
{
return NotFound(response.Message);
}
else if (response.Status == ResponseStatus.Fail)
{
return BadRequest(response.Message);
}

return NoContent();
}
}
}
Loading