Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Fixed bugs for User #33

Merged
merged 3 commits into from
Jun 21, 2024
Merged
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
78 changes: 78 additions & 0 deletions src/MLS.Api/Controllers/UserController.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,78 @@
using MediatR;
using Microsoft.AspNetCore.Mvc;
using MLS.Application.Contracts.Persistence.IRepositories;
using MLS.Application.DTO.User;
using MLS.Application.Features.User.Commands.CreateUserCommand;
using MLS.Application.Features.User.Commands.DeleteUserCommand;
using MLS.Application.Features.User.Commands.UpdateUserCommand;
using MLS.Application.Features.User.Queries.GetAllUsers;
using MLS.Application.Features.User.Queries.GetUserDetails;

// For more information on enabling Web API for empty projects, visit https://go.microsoft.com/fwlink/?LinkID=397860

namespace MLS.Api.Controllers
{
[Route("api/[controller]")]
[ApiController]
public class UserController : ControllerBase
{
private readonly IMediator _mediator;
private readonly IUserRepository _userRepository;

public UserController(IMediator mediator, IUserRepository userRepository)
{
_mediator = mediator;
_userRepository = userRepository;
}

// GET: api/<UserController>
[HttpGet]
public async Task<List<UserDto>> Get()
{
var users = await _mediator.Send(new GetAllUsersQuery());
return users;
}

// GET api/<UserController>/5
[HttpGet("{id}")]
public async Task<ActionResult<UserDetailsDto>> Get(int id)
{
var user = await _mediator.Send(new GetUserDetailsQuery(id));
return Ok(user);
}

// POST api/<UserController>
[HttpPost]
[ProducesResponseType(201)]
[ProducesResponseType(400)]
public async Task<ActionResult> Post([FromBody] CreateUserCommand user)
{
var response = await _mediator.Send(user);
return CreatedAtAction(nameof(Get), new { id = response });
}

// PUT api/<UserController>/5
[HttpPut]
[ProducesResponseType(400)]
[ProducesResponseType(StatusCodes.Status204NoContent)]
[ProducesResponseType(StatusCodes.Status404NotFound)]
[ProducesDefaultResponseType]
public async Task<ActionResult> Put([FromBody] UpdateUserCommand user)
{
await _mediator.Send(user);
return NoContent();
}

// DELETE api/<UserController>/5
[HttpDelete("{id}")]
[ProducesResponseType(StatusCodes.Status204NoContent)]
[ProducesResponseType(StatusCodes.Status404NotFound)]
[ProducesDefaultResponseType]
public async Task<ActionResult> Delete(int id)
{
var command = new DeleteUserCommand() { Id = id };
await _mediator.Send(command);
return NoContent();
}
}
}
4 changes: 0 additions & 4 deletions src/MLS.Api/MLS.Api.csproj
Original file line number Diff line number Diff line change
Expand Up @@ -14,10 +14,6 @@
<PackageReference Include="Swashbuckle.AspNetCore" Version="6.6.2" />
</ItemGroup>

<ItemGroup>
<Folder Include="Controllers\" />
</ItemGroup>

<ItemGroup>
<ProjectReference Include="..\MLS.Infrastructure\MLS.Infrastructure.csproj" />
<ProjectReference Include="..\MLS.Persistence\MLS.Persistence.csproj" />
Expand Down
1 change: 0 additions & 1 deletion src/MSL.Application/DTO/User/UpdateUserDto.cs
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,6 @@ public class UpdateUserDto
public int Id { get; set; }
public string? Username { get; set; }
public string? Email { get; set; }
public string? PasswordHash { get; set; }
public string? FirstName { get; set; }
public string? LastName { get; set; }
public string? Phone { get; set; }
Expand Down
2 changes: 0 additions & 2 deletions src/MSL.Application/DTO/User/UserDtoValidator.cs
Original file line number Diff line number Diff line change
Expand Up @@ -60,8 +60,6 @@ public UpdateUserDtoValidator()
RuleFor(x => x.Email)
.NotEmpty().WithMessage("Email cannot be empty.")
.EmailAddress().WithMessage("Invalid email address.");
RuleFor(x => x.PasswordHash)
.NotEmpty().WithMessage("PasswordHash cannot be empty.");
RuleFor(x => x.FirstName)
.NotEmpty().WithMessage("FirstName cannot be empty.")
.MaximumLength(50).WithMessage("FirstName must be less than 50 characters.");
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@

namespace MLS.Application.Features.Article.Commands.CreateArticleCommand
{
public abstract class CreateArticleCommand : IRequest<int>
public class CreateArticleCommand : IRequest<int>
{
public CreateArticleDto Article { get; set; } = new();
}
Expand Down
Original file line number Diff line number Diff line change
@@ -1,6 +1,5 @@
using AutoMapper;
using MediatR;
using MLS.Application.Contracts.Logging;
using MLS.Application.Contracts.Persistence.IRepositories;
using MLS.Application.DTO.Article;
using MLS.Application.Exceptions;
Expand All @@ -11,13 +10,11 @@ public class CreateArticleCommandHandler : IRequestHandler<CreateArticleCommand,
{
private readonly IMapper _mapper;
private readonly IArticleRepository _articleRepository;
private readonly IAppLogger<CreateArticleCommandHandler> _logger;

public CreateArticleCommandHandler(IMapper mapper, IArticleRepository articleRepository, IAppLogger<CreateArticleCommandHandler> logger)
public CreateArticleCommandHandler(IMapper mapper, IArticleRepository articleRepository)
{
_mapper = mapper;
_articleRepository = articleRepository;
_logger = logger;
}

public async Task<int> Handle(CreateArticleCommand request, CancellationToken cancellationToken)
Expand All @@ -28,7 +25,6 @@ public async Task<int> Handle(CreateArticleCommand request, CancellationToken ca

if (!validationResult.IsValid)
{
_logger.LogInformation($"Validation error request for {0} - {1}", nameof(Domain.Entities.Article), request.Article);
throw new BadRequestException("Invalid Article", validationResult);
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@

namespace MLS.Application.Features.Article.Commands.DeleteArticleCommand
{
public abstract class DeleteArticleCommand : IRequest<Unit>
public class DeleteArticleCommand : IRequest<Unit>
{
public int Id { get; set; }
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@

namespace MLS.Application.Features.Article.Commands.UpdateArticleCommand
{
public abstract class UpdateArticleCommand : IRequest<Unit>
public class UpdateArticleCommand : IRequest<Unit>
{
public UpdateArticleDto Article { get; set; } = null!;
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -3,5 +3,5 @@

namespace MLS.Application.Features.Article.Queries.GetAllArticles
{
public abstract record GetAllArticlesQuery : IRequest<List<ArticleDto>>;
public record GetAllArticlesQuery : IRequest<List<ArticleDto>>;
}
Original file line number Diff line number Diff line change
Expand Up @@ -3,5 +3,5 @@

namespace MLS.Application.Features.Article.Queries.GetArticleDetails
{
public abstract record GetArticleDetailsQuery(int Id) : IRequest<ArticleDetailsDto>;
public record GetArticleDetailsQuery(int Id) : IRequest<ArticleDetailsDto>;
}
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@

namespace MLS.Application.Features.Category.Commands.CreateCategoryCommand
{
public abstract class CreateCategoryCommand : IRequest<int>
public class CreateCategoryCommand : IRequest<int>
{
public CreateCategoryDto Category { get; set; } = new();
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@

namespace MLS.Application.Features.Category.Commands.DeleteCategoryCommand
{
public abstract class DeleteCategoryCommand : IRequest<Unit>
public class DeleteCategoryCommand : IRequest<Unit>
{
public int Id { get; set; }
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@

namespace MLS.Application.Features.Category.Commands.UpdateCategoryCommand
{
public abstract class UpdateCategoryCommand : IRequest<Unit>
public class UpdateCategoryCommand : IRequest<Unit>
{
public UpdateCategoryDto Category { get; set; } = null!;
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -3,5 +3,5 @@

namespace MLS.Application.Features.Category.Queries.GetAllCategories
{
public abstract record GetAllCategoriesQuery : IRequest<List<CategoryDto>>;
public record GetAllCategoriesQuery : IRequest<List<CategoryDto>>;
}
Original file line number Diff line number Diff line change
Expand Up @@ -3,5 +3,5 @@

namespace MLS.Application.Features.Category.Queries.GetCategoryDetails
{
public abstract record GetCategoryDetailsQuery(int Id) : IRequest<CategoryDetailsDto>;
public record GetCategoryDetailsQuery(int Id) : IRequest<CategoryDetailsDto>;
}
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@

namespace MLS.Application.Features.Comment.Commands.CreateCommentCommand
{
public abstract class CreateCommentCommand : IRequest<int>
public class CreateCommentCommand : IRequest<int>
{
public CreateCommentDto Comment { get; set; } = new();
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@

namespace MLS.Application.Features.Comment.Commands.DeleteCommentCommand
{
public abstract class DeleteCommentCommand : IRequest<Unit>
public class DeleteCommentCommand : IRequest<Unit>
{
public int Id { get; set; }
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@

namespace MLS.Application.Features.Comment.Commands.UpdateCommentCommand
{
public abstract class UpdateCommentCommand : IRequest<Unit>
public class UpdateCommentCommand : IRequest<Unit>
{
public UpdateCommentDto Comment { get; set; } = null!;
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -3,5 +3,5 @@

namespace MLS.Application.Features.Comment.Queries.GetAllComments
{
public abstract record GetAllCommentsQuery : IRequest<List<CommentDto>>;
public record GetAllCommentsQuery : IRequest<List<CommentDto>>;
}
Original file line number Diff line number Diff line change
Expand Up @@ -3,5 +3,5 @@

namespace MLS.Application.Features.Comment.Queries.GetCommentDetails
{
public abstract record GetCommentDetailsQuery(int Id) : IRequest<CommentDetailsDto>;
public record GetCommentDetailsQuery(int Id) : IRequest<CommentDetailsDto>;
}
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@

namespace MLS.Application.Features.Order.Commands.CreateOrderCommand
{
public abstract class CreateOrderCommand : IRequest<int>
public class CreateOrderCommand : IRequest<int>
{
public CreateOrderDto Order { get; set; } = new();
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@

namespace MLS.Application.Features.Order.Commands.DeleteOrderCommand
{
public abstract class DeleteOrderCommand : IRequest<Unit>
public class DeleteOrderCommand : IRequest<Unit>
{
public int Id { get; set; }
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@

namespace MLS.Application.Features.Order.Commands.UpdateOrderCommand
{
public abstract class UpdateOrderCommand : IRequest<Unit>
public class UpdateOrderCommand : IRequest<Unit>
{
public UpdateOrderDto Order { get; set; } = null!;
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -3,5 +3,5 @@

namespace MLS.Application.Features.Order.Queries.GetAllOrders
{
public abstract record GetAllOrdersQuery : IRequest<List<OrderDto>>;
public record GetAllOrdersQuery : IRequest<List<OrderDto>>;
}
Original file line number Diff line number Diff line change
Expand Up @@ -3,5 +3,5 @@

namespace MLS.Application.Features.Order.Queries.GetOrderDetails
{
public abstract record GetOrderDetailsQuery(int Id) : IRequest<OrderDetailsDto>;
public record GetOrderDetailsQuery(int Id) : IRequest<OrderDetailsDto>;
}
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@

namespace MLS.Application.Features.OrderDetail.Commands.CreateOrderDetailCommand
{
public abstract class CreateOrderDetailCommand : IRequest<int>
public class CreateOrderDetailCommand : IRequest<int>
{
public CreateOrderDetailDto OrderDetail { get; set; } = new();
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@

namespace MLS.Application.Features.OrderDetail.Commands.DeleteOrderDetailCommand
{
public abstract class DeleteOrderDetailCommand : IRequest<Unit>
public class DeleteOrderDetailCommand : IRequest<Unit>
{
public int Id { get; set; }
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@

namespace MLS.Application.Features.OrderDetail.Commands.UpdateOrderDetailCommand
{
public abstract class UpdateOrderDetailCommand : IRequest<Unit>
public class UpdateOrderDetailCommand : IRequest<Unit>
{
public UpdateOrderDetailDto OrderDetail { get; set; } = null!;
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -3,5 +3,5 @@

namespace MLS.Application.Features.OrderDetail.Queries.GetAllOrderDetails
{
public abstract record GetAllOrderDetailsQuery : IRequest<List<OrderDetailDto>>;
public record GetAllOrderDetailsQuery : IRequest<List<OrderDetailDto>>;
}
Original file line number Diff line number Diff line change
Expand Up @@ -3,5 +3,5 @@

namespace MLS.Application.Features.OrderDetail.Queries.GetOrderDetailDetails
{
public abstract record GetOrderDetailDetailsQuery(int Id) : IRequest<OrderDetailDetailsDto>;
public record GetOrderDetailDetailsQuery(int Id) : IRequest<OrderDetailDetailsDto>;
}
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@

namespace MLS.Application.Features.Payment.Commands.CreatePaymentCommand
{
public abstract class CreatePaymentCommand : IRequest<int>
public class CreatePaymentCommand : IRequest<int>
{
public CreatePaymentDto Payment { get; set; } = new();
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@

namespace MLS.Application.Features.Payment.Commands.DeletePaymentCommand
{
public abstract class DeletePaymentCommand : IRequest<Unit>
public class DeletePaymentCommand : IRequest<Unit>
{
public int Id { get; set; }
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -3,5 +3,5 @@

namespace MLS.Application.Features.Payment.Queries.GetAllPayments
{
public abstract record GetAllPaymentsQuery : IRequest<List<PaymentDto>>;
public record GetAllPaymentsQuery : IRequest<List<PaymentDto>>;
}
Original file line number Diff line number Diff line change
Expand Up @@ -3,5 +3,5 @@

namespace MLS.Application.Features.Payment.Queries.GetPaymentDetails
{
public abstract record GetPaymentDetailsQuery(int Id) : IRequest<PaymentDetailsDto>;
public record GetPaymentDetailsQuery(int Id) : IRequest<PaymentDetailsDto>;
}
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@

namespace MLS.Application.Features.Product.Commands.CreateProductCommand
{
public abstract class CreateProductCommand : IRequest<int>
public class CreateProductCommand : IRequest<int>
{
public CreateProductDto Product { get; set; } = new();
}
Expand Down
Loading
Loading