Skip to content

Commit

Permalink
Merge pull request #64 from akazad13/feature/issue-63-implement-logger
Browse files Browse the repository at this point in the history
Issue #63: Add logger
  • Loading branch information
akazad13 authored Jan 23, 2025
2 parents 8bf7029 + 35518a5 commit f22edbc
Show file tree
Hide file tree
Showing 19 changed files with 584 additions and 168 deletions.
168 changes: 168 additions & 0 deletions src/Shopizy.Api/Common/LoggerMessages/LoggerMessages.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,168 @@
namespace Shopizy.Api.Common.LoggerMessages;

public static partial class LoggerMessages
{
[LoggerMessage(
EventId = 1000,
Level = LogLevel.Error,
Message = "An error occurred while fetching category."
)]
public static partial void CategoryFetchError(this ILogger logger, Exception ex);

[LoggerMessage(
EventId = 1001,
Level = LogLevel.Error,
Message = "An error occurred while creating category."
)]
public static partial void CategoryCreationError(this ILogger logger, Exception ex);

[LoggerMessage(
EventId = 1002,
Level = LogLevel.Error,
Message = "An error occur while updating category."
)]
public static partial void CategoryUpdateError(this ILogger logger, Exception ex);

[LoggerMessage(
EventId = 1003,
Level = LogLevel.Error,
Message = "An error occurred while deleting category."
)]
public static partial void CategoryDeleteError(this ILogger logger, Exception ex);

[LoggerMessage(
EventId = 1004,
Level = LogLevel.Error,
Message = "An error occurred while fetching cart."
)]
public static partial void CartFetchError(this ILogger logger, Exception ex);

[LoggerMessage(
EventId = 1005,
Level = LogLevel.Error,
Message = "An error occurred while creating cart."
)]
public static partial void CartCreationError(this ILogger logger, Exception ex);

[LoggerMessage(
EventId = 1006,
Level = LogLevel.Error,
Message = "An error occur while updating cart."
)]
public static partial void CartUpdateError(this ILogger logger, Exception ex);

[LoggerMessage(
EventId = 1007,
Level = LogLevel.Error,
Message = "An error occurred while removing item from cart."
)]
public static partial void RemoveItemFromCartError(this ILogger logger, Exception ex);

[LoggerMessage(
EventId = 1008,
Level = LogLevel.Error,
Message = "An error occurred while fetching order."
)]
public static partial void OrderFetchError(this ILogger logger, Exception ex);

[LoggerMessage(
EventId = 1009,
Level = LogLevel.Error,
Message = "An error occurred while creating order."
)]
public static partial void OrderCreationError(this ILogger logger, Exception ex);

[LoggerMessage(
EventId = 1010,
Level = LogLevel.Error,
Message = "An error occur while cancelling cart."
)]
public static partial void CancelOrderError(this ILogger logger, Exception ex);

[LoggerMessage(
EventId = 1011,
Level = LogLevel.Error,
Message = "An error occurred while fetching product."
)]
public static partial void ProductFetchError(this ILogger logger, Exception ex);

[LoggerMessage(
EventId = 1012,
Level = LogLevel.Error,
Message = "An error occurred while creating product."
)]
public static partial void ProductCreationError(this ILogger logger, Exception ex);

[LoggerMessage(
EventId = 1013,
Level = LogLevel.Error,
Message = "An error occur while updating product."
)]
public static partial void ProductUpdateError(this ILogger logger, Exception ex);

[LoggerMessage(
EventId = 1014,
Level = LogLevel.Error,
Message = "An error occurred while deleting product."
)]
public static partial void ProductDeleteError(this ILogger logger, Exception ex);

[LoggerMessage(
EventId = 1015,
Level = LogLevel.Error,
Message = "An error occurred while adding product image."
)]
public static partial void ProductImageAdditionError(this ILogger logger, Exception ex);

[LoggerMessage(
EventId = 1016,
Level = LogLevel.Error,
Message = "An error occurred while deleting product image."
)]
public static partial void ProductImageDeleteError(this ILogger logger, Exception ex);

[LoggerMessage(
EventId = 1017,
Level = LogLevel.Error,
Message = "An error occurred while fetching user."
)]
public static partial void UserFetchError(this ILogger logger, Exception ex);

[LoggerMessage(
EventId = 1018,
Level = LogLevel.Error,
Message = "An error occurred while updating user."
)]
public static partial void UserUpdateError(this ILogger logger, Exception ex);

[LoggerMessage(
EventId = 1019,
Level = LogLevel.Error,
Message = "An error occur while updating user address."
)]
public static partial void UserAddressUpdateError(this ILogger logger, Exception ex);

[LoggerMessage(
EventId = 1020,
Level = LogLevel.Error,
Message = "An error occur while updating user password."
)]
public static partial void UserPasswordUpdateError(this ILogger logger, Exception ex);

[LoggerMessage(
EventId = 1021,
Level = LogLevel.Error,
Message = "An error occur while payment."
)]
public static partial void PaymentError(this ILogger logger, Exception ex);

[LoggerMessage(
EventId = 1022,
Level = LogLevel.Error,
Message = "An error occur while registering user."
)]
public static partial void UserRegisterError(this ILogger logger, Exception ex);

[LoggerMessage(EventId = 1023, Level = LogLevel.Error, Message = "An error occur while login.")]
public static partial void UserLoginError(this ILogger logger, Exception ex);
}
6 changes: 3 additions & 3 deletions src/Shopizy.Api/Common/Mapping/AuthenticationMappingConfig.cs
Original file line number Diff line number Diff line change
@@ -1,8 +1,8 @@
using Ardalis.GuardClauses;
using Mapster;
using Shopizy.Application.Authentication.Commands.Register;
using Shopizy.Application.Authentication.Common;
using Shopizy.Application.Authentication.Queries.login;
using Shopizy.Application.Auth.Commands.Register;
using Shopizy.Application.Auth.Common;
using Shopizy.Application.Auth.Queries.login;
using Shopizy.Contracts.Authentication;

namespace Shopizy.Api.Common.Mapping;
Expand Down
Original file line number Diff line number Diff line change
@@ -1,19 +1,23 @@
using ErrorOr;
using MapsterMapper;
using MediatR;
using Microsoft.AspNetCore.Mvc;
using Shopizy.Application.Authentication.Commands.Register;
using Shopizy.Application.Authentication.Queries.login;
using Shopizy.Api.Common.LoggerMessages;
using Shopizy.Application.Auth.Commands.Register;
using Shopizy.Application.Auth.Queries.login;
using Shopizy.Contracts.Authentication;
using Shopizy.Contracts.Common;
using Swashbuckle.AspNetCore.Annotations;

namespace Shopizy.Api.Controllers;

[Route("api/v1.0/auth")]
public class AuthenticationController(ISender mediator, IMapper mapper) : ApiController
public class AuthController(ISender mediator, IMapper mapper, ILogger<AuthController> logger)
: ApiController
{
private readonly ISender _mediator = mediator;
private readonly IMapper _mapper = mapper;
private readonly ILogger<AuthController> _logger = logger;

[HttpPost("register")]
[SwaggerResponse(StatusCodes.Status200OK, null, typeof(SuccessResult))]
Expand All @@ -23,13 +27,21 @@ public class AuthenticationController(ISender mediator, IMapper mapper) : ApiCon
[SwaggerResponse(StatusCodes.Status500InternalServerError, null, typeof(ErrorResult))]
public async Task<IActionResult> RegisterAsync(RegisterRequest request)
{
var command = _mapper.Map<RegisterCommand>(request);
var result = await _mediator.Send(command);
try
{
var command = _mapper.Map<RegisterCommand>(request);
var result = await _mediator.Send(command);

return result.Match(
success => Ok(SuccessResult.Success("Your account has been added. Please log in.")),
Problem
);
return result.Match(
success => Ok(SuccessResult.Success("Your account has been added. Please log in.")),
Problem
);
}
catch (Exception ex)
{
_logger.UserRegisterError(ex);
return Problem([Error.Unexpected(description: ex.Message)]);
}
}

[HttpPost("login")]
Expand All @@ -40,9 +52,20 @@ public async Task<IActionResult> RegisterAsync(RegisterRequest request)
[SwaggerResponse(StatusCodes.Status500InternalServerError, null, typeof(ErrorResult))]
public async Task<IActionResult> LoginAsync(LoginRequest request)
{
var query = _mapper.Map<LoginQuery>(request);
var authResult = await _mediator.Send(query);
try
{
var query = _mapper.Map<LoginQuery>(request);
var authResult = await _mediator.Send(query);

return authResult.Match(authResult => Ok(_mapper.Map<AuthResponse>(authResult)), Problem);
return authResult.Match(
authResult => Ok(_mapper.Map<AuthResponse>(authResult)),
Problem
);
}
catch (Exception ex)
{
_logger.UserLoginError(ex);
return Problem([Error.Unexpected(description: ex.Message)]);
}
}
}
74 changes: 55 additions & 19 deletions src/Shopizy.Api/Controllers/CartController.cs
Original file line number Diff line number Diff line change
@@ -1,6 +1,8 @@
using ErrorOr;
using MapsterMapper;
using MediatR;
using Microsoft.AspNetCore.Mvc;
using Shopizy.Api.Common.LoggerMessages;
using Shopizy.Application.Carts.Commands.AddProductToCart;
using Shopizy.Application.Carts.Commands.RemoveProductFromCart;
using Shopizy.Application.Carts.Commands.UpdateProductQuantity;
Expand All @@ -12,10 +14,12 @@
namespace Shopizy.Api.Controllers;

[Route("api/v1.0/carts")]
public class CartController(ISender mediator, IMapper mapper) : ApiController
public class CartController(ISender mediator, IMapper mapper, ILogger<CartController> logger)
: ApiController
{
private readonly ISender _mediator = mediator;
private readonly IMapper _mapper = mapper;
private readonly ILogger<CartController> _logger = logger;

[HttpGet]
[SwaggerResponse(StatusCodes.Status200OK, null, typeof(CartResponse))]
Expand All @@ -24,10 +28,18 @@ public class CartController(ISender mediator, IMapper mapper) : ApiController
[SwaggerResponse(StatusCodes.Status500InternalServerError, null, typeof(ErrorResult))]
public async Task<IActionResult> GetCartAsync(Guid userId)
{
var query = _mapper.Map<GetCartQuery>(userId);
var result = await _mediator.Send(query);
try
{
var query = _mapper.Map<GetCartQuery>(userId);
var result = await _mediator.Send(query);

return result.Match(Product => Ok(_mapper.Map<CartResponse>(Product)), Problem);
return result.Match(Product => Ok(_mapper.Map<CartResponse>(Product)), Problem);
}
catch (Exception ex)
{
_logger.CartFetchError(ex);
return Problem([Error.Unexpected(description: ex.Message)]);
}
}

[HttpPatch("{cartId:guid}")]
Expand All @@ -41,10 +53,18 @@ public async Task<IActionResult> AddProductToCartAsync(
AddProductToCartRequest request
)
{
var command = _mapper.Map<AddProductToCartCommand>((cartId, request));
var result = await _mediator.Send(command);
try
{
var command = _mapper.Map<AddProductToCartCommand>((cartId, request));
var result = await _mediator.Send(command);

return result.Match(product => Ok(_mapper.Map<CartResponse>(product)), Problem);
return result.Match(product => Ok(_mapper.Map<CartResponse>(product)), Problem);
}
catch (Exception ex)
{
_logger.CartCreationError(ex);
return Problem([Error.Unexpected(description: ex.Message)]);
}
}

[HttpPatch("{cartId:guid}/items/{itemId:guid}")]
Expand All @@ -59,13 +79,21 @@ public async Task<IActionResult> UpdateProductQuantityAsync(
UpdateProductQuantityRequest request
)
{
var command = _mapper.Map<UpdateProductQuantityCommand>((cartId, itemId, request));
var result = await _mediator.Send(command);
try
{
var command = _mapper.Map<UpdateProductQuantityCommand>((cartId, itemId, request));
var result = await _mediator.Send(command);

return result.Match(
success => Ok(SuccessResult.Success("Successfully updated cart.")),
Problem
);
return result.Match(
success => Ok(SuccessResult.Success("Successfully updated cart.")),
Problem
);
}
catch (Exception ex)
{
_logger.CartUpdateError(ex);
return Problem([Error.Unexpected(description: ex.Message)]);
}
}

[HttpDelete("{cartId:guid}/items/{itemId:guid}")]
Expand All @@ -76,12 +104,20 @@ UpdateProductQuantityRequest request
[SwaggerResponse(StatusCodes.Status500InternalServerError, null, typeof(ErrorResult))]
public async Task<IActionResult> RemoveItemFromCartAsync(Guid cartId, Guid itemId)
{
var command = _mapper.Map<RemoveProductFromCartCommand>((cartId, itemId));
var result = await _mediator.Send(command);
try
{
var command = _mapper.Map<RemoveProductFromCartCommand>((cartId, itemId));
var result = await _mediator.Send(command);

return result.Match(
success => Ok(SuccessResult.Success("Successfully removed product from cart.")),
Problem
);
return result.Match(
success => Ok(SuccessResult.Success("Successfully removed product from cart.")),
Problem
);
}
catch (Exception ex)
{
_logger.RemoveItemFromCartError(ex);
return Problem([Error.Unexpected(description: ex.Message)]);
}
}
}
Loading

0 comments on commit f22edbc

Please sign in to comment.