Skip to content

Commit

Permalink
Merge pull request #10 from Tohirjon-Odilov/samurai/update-source
Browse files Browse the repository at this point in the history
Samurai/update source
  • Loading branch information
Tohirjon-Odilov authored Mar 3, 2024
2 parents e17a44a + 2ef42fa commit 511f01b
Show file tree
Hide file tree
Showing 27 changed files with 577 additions and 47 deletions.
4 changes: 3 additions & 1 deletion Exam.StockManagement.API/Controllers/CategoryController.cs
Original file line number Diff line number Diff line change
@@ -1,9 +1,11 @@
using Microsoft.AspNetCore.Mvc;
using Microsoft.AspNetCore.Authorization;
using Microsoft.AspNetCore.Mvc;

namespace Exam.StockManagement.API.Controllers
{
[Route("api/[controller]/[action]")]
[ApiController]
[Authorize]
public class CategoryController : ControllerBase
{
[HttpPost]
Expand Down
37 changes: 33 additions & 4 deletions Exam.StockManagement.API/Controllers/Identity/AuthController.cs
Original file line number Diff line number Diff line change
@@ -1,25 +1,54 @@
using Exam.StockManagement.Application.Abstractions.IServices;
using Exam.StockManagement.Domain.Entities.DTOs;
using Exam.StockManagement.Domain.Exceptions;
using Microsoft.AspNetCore.Mvc;

namespace Exam.StockManagement.API.Controllers.Identity
{
[Route("api/[controller]")]
[Route("api/[controller]/[action]")]
[ApiController]
public class AuthController : ControllerBase
{
private readonly IEmailSenderService _emailSenderService;

Check failure on line 12 in Exam.StockManagement.API/Controllers/Identity/AuthController.cs

View workflow job for this annotation

GitHub Actions / build

The type or namespace name 'IEmailSenderService' could not be found (are you missing a using directive or an assembly reference?)

Check failure on line 12 in Exam.StockManagement.API/Controllers/Identity/AuthController.cs

View workflow job for this annotation

GitHub Actions / build

The type or namespace name 'IEmailSenderService' could not be found (are you missing a using directive or an assembly reference?)
private readonly IAuthService _authService;
private readonly IWebHostEnvironment _webHostEnvironment;

public AuthController(IAuthService authService)

public AuthController(IAuthService authService,
IEmailSenderService emailSenderService,

Check failure on line 18 in Exam.StockManagement.API/Controllers/Identity/AuthController.cs

View workflow job for this annotation

GitHub Actions / build

The type or namespace name 'IEmailSenderService' could not be found (are you missing a using directive or an assembly reference?)

Check failure on line 18 in Exam.StockManagement.API/Controllers/Identity/AuthController.cs

View workflow job for this annotation

GitHub Actions / build

The type or namespace name 'IEmailSenderService' could not be found (are you missing a using directive or an assembly reference?)
IWebHostEnvironment webHostEnvironment)
{
_emailSenderService = emailSenderService;
_authService = authService;
_webHostEnvironment = webHostEnvironment;
}

[HttpPost]
public async Task<IActionResult> SignUp(RequestSignUp model)
{
var result = await _authService.RegisterUser(model);
return Ok(result);
}

[HttpPost]
public async Task<ActionResult<ResponseLogin>> Login(RequestLogin model)
public async Task<IActionResult> Login(RequestLogin model)
{
var result = await _authService.GenerateToken(model);
var result = await _authService.UserExist(model);
if (result)
{
string path = Path.Combine(_webHostEnvironment.WebRootPath, "code.txt");

await _emailSenderService.SendEmailAsync(model.Email, path);
return Ok(result);
}
throw new NotFoundException();
}

[HttpPost]
public async Task<IActionResult> AcceptUser(CheckEmail model)

Check failure on line 48 in Exam.StockManagement.API/Controllers/Identity/AuthController.cs

View workflow job for this annotation

GitHub Actions / build

The type or namespace name 'CheckEmail' could not be found (are you missing a using directive or an assembly reference?)

Check failure on line 48 in Exam.StockManagement.API/Controllers/Identity/AuthController.cs

View workflow job for this annotation

GitHub Actions / build

The type or namespace name 'CheckEmail' could not be found (are you missing a using directive or an assembly reference?)
{
string path = Path.Combine(_webHostEnvironment.WebRootPath, "code.txt");
var result = await _authService.GenerateToken(model, path);
return Ok(result.Token);
}
}
Expand Down
4 changes: 3 additions & 1 deletion Exam.StockManagement.API/Controllers/ProductController.cs
Original file line number Diff line number Diff line change
@@ -1,9 +1,11 @@
using Microsoft.AspNetCore.Mvc;
using Microsoft.AspNetCore.Authorization;
using Microsoft.AspNetCore.Mvc;

namespace Exam.StockManagement.API.Controllers
{
[Route("api/[controller]/[action]")]
[ApiController]
[Authorize]
public class ProductController : ControllerBase
{
[HttpPost]
Expand Down
4 changes: 3 additions & 1 deletion Exam.StockManagement.API/Controllers/StatsController.cs
Original file line number Diff line number Diff line change
@@ -1,9 +1,11 @@
using Microsoft.AspNetCore.Mvc;
using Microsoft.AspNetCore.Authorization;
using Microsoft.AspNetCore.Mvc;

namespace Exam.StockManagement.API.Controllers
{
[Route("api/[controller]/[action]")]
[ApiController]
[Authorize]
public class StatsController : ControllerBase
{
[HttpPost]
Expand Down
12 changes: 1 addition & 11 deletions Exam.StockManagement.API/Controllers/UsersController.cs
Original file line number Diff line number Diff line change
@@ -1,6 +1,4 @@
using Exam.StockManagement.Application.Abstractions.IServices;
using Exam.StockManagement.Domain.Entities.DTOs;
using Exam.StockManagement.Domain.Entities.Models;
using Exam.StockManagement.Domain.Entities.ViewModels;
using Microsoft.AspNetCore.Authorization;
using Microsoft.AspNetCore.Mvc;
Expand All @@ -9,7 +7,6 @@ namespace Exam.StockManagement.API.Controllers
{
[Route("api/[controller]/[action]")]
[ApiController]
[Authorize]
public class UsersController : ControllerBase
{
private readonly IUserService _userService;
Expand All @@ -20,19 +17,12 @@ public UsersController(IUserService userService)
}

[HttpGet]
[Authorize]
public async Task<ActionResult<IEnumerable<UserViewModel>>> GetAllUsers()
{
var result = await _userService.GetAll();

return Ok(result);
}

[HttpPost]
public async Task<ActionResult<IEnumerable<User>>> CreateUser(UserDTO model)
{
var result = await _userService.Create(model);

return Ok(result);
}
}
}
4 changes: 4 additions & 0 deletions Exam.StockManagement.API/Exam.StockManagement.API.csproj
Original file line number Diff line number Diff line change
Expand Up @@ -22,4 +22,8 @@
<ProjectReference Include="..\Exam.StockManagement.Infrastructure\Exam.StockManagement.Infrastructure.csproj" />
</ItemGroup>

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

</Project>
7 changes: 7 additions & 0 deletions Exam.StockManagement.API/appsettings.json
Original file line number Diff line number Diff line change
Expand Up @@ -14,5 +14,12 @@
"ValidIssuer": "127.0.0.1",
"Secret": "TohirjonOdilovImtihonUchunTayyorlaganSecretKey03.02.2024",
"ExpireDate": "1000"
},
"EmailSettings": {
"MailServer": "smtp.gmail.com",
"MailPort": 587,
"SenderName": "Najot Ta'lim",
"Sender": "[email protected]",
"Password": "wcyz cueo vuiv tcku"
}
}
1 change: 1 addition & 0 deletions Exam.StockManagement.API/wwwroot/code.txt
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
9430
Original file line number Diff line number Diff line change
@@ -1,9 +1,13 @@
using Exam.StockManagement.Domain.Entities.DTOs;
using Exam.StockManagement.Domain.Entities.Models;

namespace Exam.StockManagement.Application.Abstractions.IServices
{
public interface IAuthService
{
public Task<ResponseLogin> GenerateToken(RequestLogin user);
public Task<bool> UserExist(RequestLogin user);
public Task<string> CorrectEmail(RegisterLogin user);
public Task<User> RegisterUser(RequestSignUp signUp);
}
}
Original file line number Diff line number Diff line change
@@ -1,12 +1,24 @@
using Exam.StockManagement.Domain.Entities.DTOs;
using Exam.StockManagement.Domain.Entities.Models;
using Exam.StockManagement.Domain.Entities.ViewModels;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Linq.Expressions;
using System.Text;
using System.Threading.Tasks;

namespace Exam.StockManagement.Application.Abstractions.IServices
{
public interface IProductService
{
public Task<Product> Create(ProductDTO userDTO);
public Task<Product> GetByName(string name);
public Task<Product> GetById(int Id);
public Task<Product> GetByEmail(string email);
public Task<Product> GetByLogin(string email);
public Task<IEnumerable<ProductViewModel>> GetAll();
public Task<bool> Delete(Expression<Func<Product, bool>> expression);
public Task<Product> Update(int Id, ProductDTO userDTO);
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@ namespace Exam.StockManagement.Application.Abstractions.IServices
{
public interface IUserService
{
public Task<User> Create(UserDTO userDTO);
public Task<User> Create(RequestSignUp signUp);
public Task<User> GetByName(string name);
public Task<User> GetById(int Id);
public Task<User> GetByEmail(string email);
Expand Down
Original file line number Diff line number Diff line change
@@ -1,5 +1,7 @@
using Exam.StockManagement.Application.Abstractions.IServices;
using Exam.StockManagement.Domain.Entities.DTOs;
using Exam.StockManagement.Domain.Entities.Models;
using Exam.StockManagement.Domain.Exceptions;
using Microsoft.Extensions.Configuration;
using Microsoft.IdentityModel.Tokens;
using System.Globalization;
Expand All @@ -20,14 +22,21 @@ public AuthService(IConfiguration conf, IUserService userService)
_userService = userService;
}

public async Task<string> CorrectEmail(RegisterLogin user)
{
var result = await _userService.GetByLogin(user.Login);
if (result.Code == user.Code)
{
return "Login successfully!";
}
throw new NotFoundException();
}

public async Task<ResponseLogin> GenerateToken(RequestLogin user)
{
if (user == null)
{
return new ResponseLogin()
{
Token = "User Not Found"
};
throw new NotFoundException();
}

if (await UserExist(user))
Expand Down Expand Up @@ -83,18 +92,26 @@ public async Task<ResponseLogin> GenerateToken(IEnumerable<Claim> additionalClai

}


public async Task<bool> UserExist(RequestLogin user)
{
if (user == null)
{
throw new NotFoundException();
}

var result = await _userService.GetByLogin(user.Login);

if (user.Login == result.Login && user.Password == result.Password)
{
return true;
}

return false;
}

public async Task<User> RegisterUser(RequestSignUp signUp)
{
var result = await _userService.Create(signUp);
return result;
}
}
}
22 changes: 14 additions & 8 deletions Exam.StockManagement.Application/Services/UserService.cs
Original file line number Diff line number Diff line change
Expand Up @@ -17,10 +17,15 @@ public UserService(IUserRepository userRepository)
_userRepository = userRepository;
}

public async Task<User> Create(UserDTO userDTO)
public async Task<User> Create(RequestSignUp requestSignUp)
{
User? hasLogin = await _userRepository.GetByAny(x => x.Login == userDTO.Login);
var hasEmail = await _userRepository.GetByAny(x => x.Email == userDTO.Email);
User? hasLogin = await _userRepository.GetByAny(x => x.Login == requestSignUp.Login);
var hasEmail = await _userRepository.GetByAny(x => x.Email == requestSignUp.Email);

if (requestSignUp.Password != requestSignUp.ConfirmPassword)
{
throw new PasswordNotMatchException();
}

if (hasLogin != null && hasEmail != null)
{
Expand All @@ -29,11 +34,12 @@ public async Task<User> Create(UserDTO userDTO)

User? user = new User()
{
Name = userDTO.Name,
Email = userDTO.Email,
Login = userDTO.Login,
Password = userDTO.Password,
Role = userDTO.Role,
Name = requestSignUp.Name,
Email = requestSignUp.Email,
Login = requestSignUp.Login,
Password = requestSignUp.Password,
Role = requestSignUp.Role

};

User? result = await _userRepository.Create(user);
Expand Down
4 changes: 2 additions & 2 deletions Exam.StockManagement.Domain/Entities/Common/Auditable.cs
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@ namespace Exam.StockManagement.Domain.Entities.Common
{
public abstract class Auditable : BaseEntity
{
public DateTime CreatedAt { get; set; }
public DateTime UpdatedAt { get; set; }
public DateTime CreatedAt { get; set; } = DateTime.UtcNow;
public DateTime UpdatedAt { get; set; } = DateTime.UtcNow;
}
}
7 changes: 2 additions & 5 deletions Exam.StockManagement.Domain/Entities/DTOs/RegisterLogin.cs
Original file line number Diff line number Diff line change
Expand Up @@ -2,10 +2,7 @@
{
public class RegisterLogin
{
public string? Name { get; set; }
public string? Email { get; set; }
public string Login { get; set; }
public string Password { get; set; }
public string ConiformPassword { get; set; }
public string? Login { get; set; }
public string? Code { get; set; }
}
}
2 changes: 1 addition & 1 deletion Exam.StockManagement.Domain/Entities/DTOs/RequestLogin.cs
Original file line number Diff line number Diff line change
Expand Up @@ -5,4 +5,4 @@ public class RequestLogin
public string Login { get; set; }
public string Password { get; set; }
}
}
}
12 changes: 12 additions & 0 deletions Exam.StockManagement.Domain/Entities/DTOs/RequestSignUp.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,12 @@
namespace Exam.StockManagement.Domain.Entities.DTOs
{
public class RequestSignUp
{
public string? Name { get; set; }
public string? Email { get; set; }
public string Login { get; set; }
public string Password { get; set; }
public string ConfirmPassword { get; set; }
public string Role { get; set; }
}
}
2 changes: 1 addition & 1 deletion Exam.StockManagement.Domain/Entities/DTOs/UserDTO.cs
Original file line number Diff line number Diff line change
Expand Up @@ -4,8 +4,8 @@ public class UserDTO
{
public string? Name { get; set; }
public string? Email { get; set; }
public string Password { get; set; }
public string Login { get; set; }

Check warning on line 7 in Exam.StockManagement.Domain/Entities/DTOs/UserDTO.cs

View workflow job for this annotation

GitHub Actions / build

Non-nullable property 'Login' must contain a non-null value when exiting constructor. Consider declaring the property as nullable.
public string Password { get; set; }

Check warning on line 8 in Exam.StockManagement.Domain/Entities/DTOs/UserDTO.cs

View workflow job for this annotation

GitHub Actions / build

Non-nullable property 'Password' must contain a non-null value when exiting constructor. Consider declaring the property as nullable.
public string Role { get; set; }
}
}
4 changes: 2 additions & 2 deletions Exam.StockManagement.Domain/Entities/Models/User.cs
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,7 @@ public class User : Auditable
public string? Email { get; set; }
public string Login { get; set; }

Check warning on line 9 in Exam.StockManagement.Domain/Entities/Models/User.cs

View workflow job for this annotation

GitHub Actions / build

Non-nullable property 'Login' must contain a non-null value when exiting constructor. Consider declaring the property as nullable.
public string Password { get; set; }

Check warning on line 10 in Exam.StockManagement.Domain/Entities/Models/User.cs

View workflow job for this annotation

GitHub Actions / build

Non-nullable property 'Password' must contain a non-null value when exiting constructor. Consider declaring the property as nullable.
public string Role { get; set; }
// salom
public string? Code { get; set; }
public string? Role { get; set; }
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,5 @@ public class UserViewModel
public string? Name { get; set; }
public string? Email { get; set; }
public string Role { get; set; }

Check warning on line 7 in Exam.StockManagement.Domain/Entities/ViewModels/UserViewModel.cs

View workflow job for this annotation

GitHub Actions / build

Non-nullable property 'Role' must contain a non-null value when exiting constructor. Consider declaring the property as nullable.

}
}
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
namespace Exam.StockManagement.Domain.Exceptions
{
public class NotFoundException() : Exception("Not Found");
public class NotFoundException() : Exception("Not found") { }
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
namespace Exam.StockManagement.Domain.Exceptions
{
public class PasswordNotMatchException : Exception
{
public PasswordNotMatchException() : base("Password not match") { }
}
}
Loading

0 comments on commit 511f01b

Please sign in to comment.