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

4 day #1

Open
wants to merge 2 commits into
base: day-3
Choose a base branch
from
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
6 changes: 6 additions & 0 deletions Market.sln
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,8 @@
Microsoft Visual Studio Solution File, Format Version 12.00
Project("{FAE04EC0-301F-11D3-BF4B-00C04F79EFBC}") = "Market", "Market\Market.csproj", "{49C3DCB0-2273-433A-AB53-1A7F702F661B}"
EndProject
Project("{FAE04EC0-301F-11D3-BF4B-00C04F79EFBC}") = "MarketTests", "MarketTests\MarketTests.csproj", "{D423A6DA-5ABE-4CAC-AF04-A4C455B402AE}"
EndProject
Global
GlobalSection(SolutionConfigurationPlatforms) = preSolution
Debug|Any CPU = Debug|Any CPU
Expand All @@ -12,5 +14,9 @@ Global
{49C3DCB0-2273-433A-AB53-1A7F702F661B}.Debug|Any CPU.Build.0 = Debug|Any CPU
{49C3DCB0-2273-433A-AB53-1A7F702F661B}.Release|Any CPU.ActiveCfg = Release|Any CPU
{49C3DCB0-2273-433A-AB53-1A7F702F661B}.Release|Any CPU.Build.0 = Release|Any CPU
{D423A6DA-5ABE-4CAC-AF04-A4C455B402AE}.Debug|Any CPU.ActiveCfg = Debug|Any CPU
{D423A6DA-5ABE-4CAC-AF04-A4C455B402AE}.Debug|Any CPU.Build.0 = Debug|Any CPU
{D423A6DA-5ABE-4CAC-AF04-A4C455B402AE}.Release|Any CPU.ActiveCfg = Release|Any CPU
{D423A6DA-5ABE-4CAC-AF04-A4C455B402AE}.Release|Any CPU.Build.0 = Release|Any CPU
EndGlobalSection
EndGlobal
7 changes: 7 additions & 0 deletions Market.sln.DotSettings.user
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
<wpf:ResourceDictionary xml:space="preserve" xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml" xmlns:s="clr-namespace:System;assembly=mscorlib" xmlns:ss="urn:shemas-jetbrains-com:settings-storage-xaml" xmlns:wpf="http://schemas.microsoft.com/winfx/2006/xaml/presentation">
<s:String x:Key="/Default/Environment/UnitTesting/UnitTestSessionStore/Sessions/=c292903f_002D0ff6_002D4ca8_002Da94a_002D9514eb3d2a7e/@EntryIndexedValue">&lt;SessionState ContinuousTestingMode="0" IsActive="True" Name="Test1" xmlns="urn:schemas-jetbrains-com:jetbrains-ut-session"&gt;&#xD;
&lt;Solution /&gt;&#xD;
&lt;/SessionState&gt;</s:String>
<s:String x:Key="/Default/Environment/UnitTesting/UnitTestSessionStore/Sessions/=e7e5b869_002D76a2_002D4fad_002D98a7_002Df3d52ab623fc/@EntryIndexedValue">&lt;SessionState ContinuousTestingMode="0" Name="All tests from &amp;lt;MarketTests&amp;gt;" xmlns="urn:schemas-jetbrains-com:jetbrains-ut-session"&gt;&#xD;
&lt;Project Location="D:\crash\crash-course-nsk-april-2024\MarketTests" Presentation="&amp;lt;MarketTests&amp;gt;" /&gt;&#xD;
&lt;/SessionState&gt;</s:String></wpf:ResourceDictionary>
8 changes: 4 additions & 4 deletions Market/Controllers/CartsControllers.cs
Original file line number Diff line number Diff line change
Expand Up @@ -7,14 +7,14 @@ namespace Market.Controllers;

[ApiController]
[Route("customers/{customerId:guid}/cart")]
public class CartsControllers : ControllerBase
public sealed class CartsControllers : ControllerBase
{
public CartsControllers()
public CartsControllers(ICartsRepository cartsRepository)
{
CartsRepository = new CartsRepository();
CartsRepository = cartsRepository;
}

private CartsRepository CartsRepository { get; }
private ICartsRepository CartsRepository { get; }

[HttpGet]
public async Task<ActionResult<Cart>> GetCartAsync([FromRoute] Guid customerId)
Expand Down
8 changes: 4 additions & 4 deletions Market/Controllers/OrdersController.cs
Original file line number Diff line number Diff line change
Expand Up @@ -9,14 +9,14 @@ namespace Market.Controllers;

[ApiController]
[Route("orders")]
public class OrdersControllers : ControllerBase
public sealed class OrdersControllers : ControllerBase
{
public OrdersControllers()
public OrdersControllers(IOrdersRepository ordersRepository)
{
OrdersRepository = new OrdersRepository();
OrdersRepository = ordersRepository;
}

private OrdersRepository OrdersRepository { get; }
private IOrdersRepository OrdersRepository { get; }

[HttpPost]
public async Task<IActionResult> Create([FromBody] OrderDto order)
Expand Down
8 changes: 4 additions & 4 deletions Market/Controllers/ProductsController.cs
Original file line number Diff line number Diff line change
Expand Up @@ -16,13 +16,13 @@ public sealed class ProductsController : ControllerBase
{
private readonly MainValidator _validator;

public ProductsController()
public ProductsController(IProductsRepository productsRepository)
{
ProductsRepository = new ProductsRepository();
ProductsRepository = productsRepository;
_validator = new MainValidator();
}

private ProductsRepository ProductsRepository { get; }
private IProductsRepository ProductsRepository { get; }

[HttpGet("{productId:guid}")]
public async Task<ActionResult<ProductDto>> GetProductByIdAsync(Guid productId)
Expand Down Expand Up @@ -67,7 +67,7 @@ public async Task<ActionResult<List<ProductDto>>> GetSellerProductsAsync(
}

[HttpPost]
[CheckAuthFilter]
[ServiceFilter(typeof(CheckAuthFilter))]
public async Task<IActionResult> CreateProductAsync([FromBody] ProductDto product)
{
await _validator.Validate(product);
Expand Down
8 changes: 4 additions & 4 deletions Market/Controllers/UsersControllers.cs
Original file line number Diff line number Diff line change
Expand Up @@ -6,14 +6,14 @@ namespace Market.Controllers;

[ApiController]
[Route("users")]
public class UsersControllers : ControllerBase
public sealed class UsersControllers : ControllerBase
{
public UsersControllers()
public UsersControllers(IUserRepository userRepository)
{
UserRepository = new UserRepository();
UserRepository = userRepository;
}

private UserRepository UserRepository { get; }
private IUserRepository UserRepository { get; }

[HttpPost]
public async Task<ActionResult<Guid>> CreateUser([FromBody] CreateUserRequestDto userInfo)
Expand Down
6 changes: 3 additions & 3 deletions Market/DAL/Repositories/Carts/CartsRepository.cs
Original file line number Diff line number Diff line change
Expand Up @@ -4,13 +4,13 @@

namespace Market.DAL.Repositories.Carts;

internal sealed class CartsRepository
internal sealed class CartsRepository : ICartsRepository
{
private readonly RepositoryContext _context;

public CartsRepository()
public CartsRepository(RepositoryContext repositoryContext)
{
_context = new RepositoryContext();
_context = repositoryContext;
}

public Task<Cart?> GetCartAsync(Guid customerId) =>
Expand Down
15 changes: 15 additions & 0 deletions Market/DAL/Repositories/Carts/ICartsRepository.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,15 @@
using Market.Models;

namespace Market.DAL.Repositories.Carts;

public interface ICartsRepository
{
public Task<Cart?> GetCartAsync(Guid customerId);


public Task AddProductToCartAsync(Guid customerId, Guid productId);

public Task RemoveProductFromCartAsync(Guid customerId, Guid productId);

public Task ClearAll(Guid customerId);
}
12 changes: 12 additions & 0 deletions Market/DAL/Repositories/Orders/IOrdersRepository.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,12 @@
using Market.Models;

namespace Market.DAL.Repositories.Orders;

public interface IOrdersRepository
{
public Task CreateOrderAsync(Order order);

public Task ChangeStateForOrder(Guid orderId, OrderState newState);

public Task<IReadOnlyCollection<Order>> GetOrdersForSeller(Guid sellerId, bool onlyCreated);
}
6 changes: 3 additions & 3 deletions Market/DAL/Repositories/Orders/OrdersRepository.cs
Original file line number Diff line number Diff line change
Expand Up @@ -4,13 +4,13 @@

namespace Market.DAL.Repositories.Orders;

internal class OrdersRepository
internal class OrdersRepository : IOrdersRepository
{
private readonly RepositoryContext _context;

public OrdersRepository()
public OrdersRepository(RepositoryContext repositoryContext)
{
_context = new RepositoryContext();
_context = repositoryContext;
}

public async Task CreateOrderAsync(Order order)
Expand Down
22 changes: 22 additions & 0 deletions Market/DAL/Repositories/Products/IProductsRepository.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,22 @@
using Market.Enums;
using Market.Models;

namespace Market.DAL.Repositories.Products;

public interface IProductsRepository
{
public Task<IReadOnlyCollection<Product>> GetProductsAsync(
string? name = null,
Guid? sellerId = null,
ProductCategory? category = null,
int skip = 0,
int take = 50);

public Task<Product?> GetProductAsync(Guid productId);

public Task CreateProductAsync(Product product);

public Task UpdateProductAsync(Guid productId, ProductUpdateInfo updateInfo);

public Task DeleteProductAsync(Guid productId);
}
2 changes: 1 addition & 1 deletion Market/DAL/Repositories/Products/ProductUpdateInfo.cs
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@

namespace Market.DAL;

internal record ProductUpdateInfo
public record ProductUpdateInfo
{
public string? Name { get; set; }

Expand Down
6 changes: 3 additions & 3 deletions Market/DAL/Repositories/Products/ProductsRepository.cs
Original file line number Diff line number Diff line change
Expand Up @@ -5,13 +5,13 @@

namespace Market.DAL.Repositories.Products;

internal sealed class ProductsRepository
internal sealed class ProductsRepository : IProductsRepository
{
private readonly RepositoryContext _context;

public ProductsRepository()
public ProductsRepository(RepositoryContext repositoryContext)
{
_context = new RepositoryContext();
_context = repositoryContext;
}

public async Task<IReadOnlyCollection<Product>> GetProductsAsync(
Expand Down
12 changes: 12 additions & 0 deletions Market/DAL/Repositories/Users/IUserRepository.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,12 @@
using Market.Models;

namespace Market.DAL.Repositories.Users;

public interface IUserRepository
{
public Task<Guid> CreateUser(string name, string login, string password);

public Task<User?> GetUser(string login);

public Task SetSellerState(Guid userId, bool newState);
}
6 changes: 3 additions & 3 deletions Market/DAL/Repositories/Users/UserRepository.cs
Original file line number Diff line number Diff line change
Expand Up @@ -6,15 +6,15 @@

namespace Market.DAL.Repositories.Users;

internal sealed class UserRepository
internal sealed class UserRepository : IUserRepository
{
private static MD5 _md5 = MD5.Create();

private readonly RepositoryContext _context;

public UserRepository()
public UserRepository(RepositoryContext repositoryContext)
{
_context = new RepositoryContext();
_context = repositoryContext;
}

public async Task<Guid> CreateUser(string name, string login, string password)
Expand Down
13 changes: 9 additions & 4 deletions Market/Filters/CheckAuthFilter.cs
Original file line number Diff line number Diff line change
Expand Up @@ -7,13 +7,13 @@

namespace Market.Filters;

public class CheckAuthFilter : ActionFilterAttribute
internal class CheckAuthFilter : ActionFilterAttribute, IFilterFactory
{
private readonly UserAuthenticator _userAuthenticator;
private readonly IUserAuthenticator _userAuthenticator;

public CheckAuthFilter()
public CheckAuthFilter(IUserAuthenticator userAuthenticator)
{
_userAuthenticator = new UserAuthenticator();
_userAuthenticator = userAuthenticator;
}

public override async Task OnActionExecutionAsync(ActionExecutingContext context, ActionExecutionDelegate next)
Expand Down Expand Up @@ -70,4 +70,9 @@ private void AppendUserIdToClaims(Guid userId, ActionExecutingContext context)
claimIdentity.AddClaim(new Claim("user-id", userId.ToString()));
context.HttpContext.User.AddIdentity(claimIdentity);
}

public IFilterMetadata CreateInstance(IServiceProvider serviceProvider) =>
new CheckAuthFilter(serviceProvider.GetService<IUserAuthenticator>()!);

public bool IsReusable => true;
}
4 changes: 4 additions & 0 deletions Market/Market.csproj
Original file line number Diff line number Diff line change
Expand Up @@ -12,4 +12,8 @@
<PackageReference Include="Swashbuckle.AspNetCore" Version="6.5.0"/>
</ItemGroup>

<ItemGroup>
<Compile Remove="DAL\Repositories\UsersRepository.cs" />
</ItemGroup>

</Project>
26 changes: 26 additions & 0 deletions Market/Program.cs
Original file line number Diff line number Diff line change
@@ -1,4 +1,11 @@
using Market.Controllers;
using Market.DAL;
using Market.DAL.Repositories.Carts;
using Market.DAL.Repositories.Orders;
using Market.DAL.Repositories.Products;
using Market.DAL.Repositories.Users;
using Market.Filters;
using Market.Services;

var builder = WebApplication.CreateBuilder(args);

Expand All @@ -7,6 +14,24 @@
c.Filters.Add<ExceptionFilter>();
});



builder.Services
.AddDbContext<RepositoryContext>()
.AddScoped<IUserRepository, UserRepository>()
.AddScoped<ICartsRepository, CartsRepository>()
.AddScoped<IProductsRepository, ProductsRepository>()
.AddScoped<IOrdersRepository, OrdersRepository>();

builder.Services
.AddScoped<UsersControllers>()
.AddScoped<ProductsController>()
.AddScoped<OrdersControllers>()
.AddScoped<CartsControllers>();

builder.Services
.AddScoped<UserAuthenticator>();

builder.Services.AddEndpointsApiExplorer();
builder.Services.AddSwaggerGen();

Expand All @@ -21,4 +46,5 @@
app.UseHttpsRedirection();

app.MapControllers();

app.Run();
6 changes: 6 additions & 0 deletions Market/Services/IUserAuthenticator.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
namespace Market.Services;

public interface IUserAuthenticator
{
public Task<Guid?> AuthenticateUser(string login, string password);
}
8 changes: 4 additions & 4 deletions Market/Services/UserAuthenticator.cs
Original file line number Diff line number Diff line change
Expand Up @@ -3,13 +3,13 @@

namespace Market.Services;

public class UserAuthenticator
internal class UserAuthenticator : IUserAuthenticator
{
private readonly UserRepository _userRepository;
private readonly IUserRepository _userRepository;

public UserAuthenticator()
public UserAuthenticator(IUserRepository userRepository)
{
_userRepository = new UserRepository();
_userRepository = userRepository;
}
public async Task<Guid?> AuthenticateUser(string login, string password)
{
Expand Down
3 changes: 2 additions & 1 deletion Market/Validators/ProductDtoValidator.cs
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,7 @@ public ProductDtoValidator()
RuleFor(s => s.Name).Length(3, 100);
RuleFor(s => s.Description).Length(1, 1000)
.When(s => s.Description != null);
RuleFor(s => s.PriceInRubles).GreaterThan(0);
RuleFor(s => s.PriceInRubles).GreaterThan(0)
.WithMessage("Price must be more than zero");
}
}
Loading