Skip to content

Commit

Permalink
Merge branch 'origin/develop'
Browse files Browse the repository at this point in the history
  • Loading branch information
vanyachernov committed Jul 28, 2024
2 parents d235385 + 3f1e915 commit 31ce431
Show file tree
Hide file tree
Showing 139 changed files with 9,433 additions and 213 deletions.
40 changes: 40 additions & 0 deletions Topers.Api/Controllers/AccountController.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,40 @@
using Microsoft.AspNetCore.Mvc;
using Microsoft.Extensions.Options;
using Topers.Core.Abstractions;
using Topers.Core.Dtos;
using Topers.Infrastructure.Features;


namespace Topers.Api.Controllers;

[ApiController]
[Route("api/account")]
public class AccountController(
IUsersService userService,
IOptions<CookiesOptions> options) : ControllerBase
{
private readonly IUsersService _userService = userService;
private readonly CookiesOptions _cookieOptions = options.Value;

[HttpPost("register")]
public async Task<IResult> Register(
[FromBody] RegisterUserRequestDto request,
CancellationToken cancellationToken)
{
await _userService.Register(request.Username, request.Email, request.Password, cancellationToken);

return Results.Ok();
}

[HttpPost("login")]
public async Task<IResult> Login(
[FromBody] LoginUserRequestDto request,
CancellationToken cancellationToken)
{
var token = await _userService.Login(request.Username, request.Password, cancellationToken);

HttpContext.Response.Cookies.Append(_cookieOptions.Name, token);

return Results.Ok(token);
}
}
48 changes: 48 additions & 0 deletions Topers.Api/Controllers/AddressesController.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,48 @@
using Microsoft.AspNetCore.Mvc;
using Swashbuckle.AspNetCore.Annotations;
using Topers.Core.Abstractions;
using Topers.Core.Dtos;
using Topers.Core.Models;
using Topers.Core.Validators;

namespace Topers.Api.Controllers;

[ApiController]
[Route("api/addresses")]
public class AddressesController(IAddressesService addressesService) : ControllerBase
{
private readonly IAddressesService _addressService = addressesService;

[HttpPost("{customerId:guid}")]
[SwaggerResponse(200, Description = "Returns the new address data of the customer.", Type = typeof(AddressResponseDto))]
[SwaggerResponse(400, Description = "There are some errors in the model.")]
public async Task<ActionResult<AddressResponseDto>> AddAddressToCustomer(
[FromRoute] Guid customerId,
[FromBody] AddressRequestDto address,
CancellationToken cancellationToken)
{
var newAddressValidator = new AddressDtoValidator();

var newAddressValidatorResult = newAddressValidator.Validate(address);

if (!newAddressValidatorResult.IsValid)
{
return BadRequest(newAddressValidatorResult.Errors);
}

var newAddress = new Address
(
Guid.Empty,
customerId,
address.Street,
address.City,
address.State,
address.PostalCode,
address.Country
);

var addressEntity = await _addressService.AddAddressToCustomerAsync(newAddress, cancellationToken);

return Ok(addressEntity);
}
}
112 changes: 112 additions & 0 deletions Topers.Api/Controllers/CartsController.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,112 @@
using Microsoft.AspNetCore.Mvc;
using Swashbuckle.AspNetCore.Annotations;
using Topers.Core.Abstractions;
using Topers.Core.Dtos;
using Topers.Core.Models;

namespace Topers.Api.Controllers
{
[ApiController]
[Route("api/cart")]
public class CartsController(ICartsService cartService) : ControllerBase
{
private readonly ICartsService _cartService = cartService;

[HttpGet("{cartId:guid}")]
[SwaggerResponse(
200,
Description = "Returns a cart by identifier.",
Type = typeof(CartResponseDto)
)]
[SwaggerResponse(400, Description = "Cart not found.")]
public async Task<ActionResult<OrderResponseDto>> GetCartById(
[FromRoute] Guid cartId,
CancellationToken cancellationToken
)
{
var cart = await _cartService.GetCartById(cartId, cancellationToken);

if (cart == null)
{
return NotFound();
}

return Ok(cart);
}

[HttpGet("{customerId:guid}")]
[SwaggerResponse(
200,
Description = "Returns a cart by customer identifier.",
Type = typeof(CartResponseDto)
)]
[SwaggerResponse(400, Description = "Cart not found.")]
public async Task<ActionResult<OrderResponseDto>> GetCartByCustomerId(
[FromRoute] Guid customerId,
CancellationToken cancellationToken
)
{
var cart = await _cartService.GetCartByCustomerId(customerId, cancellationToken);

if (cart == null)
{
return NotFound();
}

return Ok(cart);
}

[HttpPost("create")]
[SwaggerResponse(200, Description = "Create a new cart.")]
[SwaggerResponse(400, Description = "There are some errors in the model.")]
public async Task<ActionResult<CartResponseDto>> CreateCart(
[FromBody] CartRequestDto cart,
CancellationToken cancellationToken
)
{
var newCart = new Cart
(
Guid.Empty,
cart.CustomerId,
DateTime.UtcNow,
DateTime.UtcNow
);

var newCartEntity = await _cartService.CreateCartAsync(newCart, cancellationToken);

return Ok(newCartEntity);
}

[HttpPost("{cartId:guid}/addGood")]
[SwaggerResponse(200, Description = "Add good to a customer cart.")]
[SwaggerResponse(400, Description = "There are some errors in the model.")]
public async Task<ActionResult<CartResponseDto>> AddProductToCart(
[FromRoute] Guid cartId,
[FromBody] AddProductRequestDto cartDetail,
CancellationToken cancellationToken
)
{
var newGoodDetail = new CartItems(
Guid.Empty,
cartId,
cartDetail.GoodScopeId,
cartDetail.GoodQuantity,
default
);

var newGoodScope = new GoodScope(
Guid.Empty,
cartDetail.GoodScopeId,
cartDetail.GoodLitre
);

var newGoodDetailIdentifier = await _cartService.AddGoodToCartAsync(
newGoodDetail,
newGoodScope,
cancellationToken
);

return Ok(newGoodDetailIdentifier);
}
}
}
141 changes: 141 additions & 0 deletions Topers.Api/Controllers/CategoriesController.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,141 @@
namespace Topers.Api.Controllers;

using Microsoft.AspNetCore.Authentication.JwtBearer;
using Microsoft.AspNetCore.Authorization;
using Microsoft.AspNetCore.Mvc;
using Swashbuckle.AspNetCore.Annotations;
using Topers.Core.Abstractions;
using Topers.Core.Dtos;
using Topers.Core.Models;
using Topers.Core.Validators;

[ApiController]
[Route("api/categories")]
public class CategoriesController : ControllerBase
{
private readonly ICategoriesService _categoryService;

public CategoriesController(ICategoriesService categoryService)
{
_categoryService = categoryService;
}

[HttpGet]
[SwaggerResponse(200, Description = "Returns a category list.", Type = typeof(IEnumerable<CategoryResponseDto>))]
[SwaggerResponse(400, Description = "Categories not found.")]
public async Task<ActionResult<List<CategoryResponseDto>>> GetCategories(CancellationToken cancellationToken)
{
var categories = await _categoryService.GetAllCategoriesAsync(cancellationToken);

if (categories == null)
{
return BadRequest();
}

return Ok(categories);
}

[Authorize(AuthenticationSchemes = JwtBearerDefaults.AuthenticationScheme)]
[HttpGet("{categoryId:guid}")]
[SwaggerResponse(200, Description = "Returns a category.", Type = typeof(CategoryResponseDto))]
[SwaggerResponse(400, Description = "Category not found.")]
public async Task<ActionResult<CategoryResponseDto>> GetCategory(
[FromRoute] Guid categoryId,
CancellationToken cancellationToken)
{
var category = await _categoryService.GetCategoryByIdAsync(categoryId, cancellationToken);

if (category == null)
{
return BadRequest();
}

return Ok(category);
}

[Authorize(AuthenticationSchemes = JwtBearerDefaults.AuthenticationScheme)]
[HttpGet("{categoryId:guid}/goods")]
[SwaggerResponse(200, Description = "Returns a category goods.", Type = typeof(IEnumerable<GoodResponseDto>))]
[SwaggerResponse(400, Description = "Goods not found.")]
public async Task<ActionResult<List<GoodResponseDto>>> GetCategoryGoods(
[FromRoute] Guid categoryId,
CancellationToken cancellationToken)
{
var goods = await _categoryService.GetGoodsByCategoryIdAsync(categoryId, cancellationToken);

if (goods == null)
{
return BadRequest();
}

return Ok(goods);
}

[HttpPost("create")]
[SwaggerResponse(200, Description = "Create a new category.")]
[SwaggerResponse(400, Description = "There are some errors in the model.")]
public async Task<ActionResult<CategoryResponseDto>> CreateCategory(
[FromBody] CategoryRequestDto category,
CancellationToken cancellationToken)
{
var categoryValidator = new CategoryDtoValidator();

var categoryValidatorResult = categoryValidator.Validate(category);

if (!categoryValidatorResult.IsValid)
{
return BadRequest(categoryValidatorResult.Errors);
}

var newCategory = new Category
(
Guid.Empty,
category.Name,
category.Description
);

var newCategoryEntity = await _categoryService.CreateCategoryAsync(newCategory, cancellationToken);

return Ok(newCategoryEntity);
}

[HttpPut("{categoryId:guid}")]
[SwaggerResponse(200, Description = "Update an existing category.")]
[SwaggerResponse(400, Description = "There are some errors in the model.")]
public async Task<ActionResult<CategoryResponseDto>> UpdateCategory(
[FromRoute] Guid categoryId,
[FromBody] CategoryRequestDto category,
CancellationToken cancellationToken)
{
var categoryValidator = new CategoryDtoValidator();

var categoryValidatorResult = categoryValidator.Validate(category);

if (!categoryValidatorResult.IsValid)
{
return BadRequest(categoryValidatorResult.Errors);
}

var existCategory = new Category
(
categoryId,
category.Name,
category.Description
);

var updatedCategory = await _categoryService.UpdateCategoryAsync(existCategory, cancellationToken);

return Ok(updatedCategory);
}

[HttpDelete("{categoryId:guid}")]
[SwaggerResponse(200, Description = "Delete category.")]
public async Task<ActionResult<Guid>> DeleteCategory(
[FromRoute] Guid categoryId,
CancellationToken cancellationToken)
{
await _categoryService.DeleteCategoryAsync(categoryId, cancellationToken);

return Ok(categoryId);
}
}
Loading

0 comments on commit 31ce431

Please sign in to comment.