Skip to content

Commit

Permalink
feat ✨ : add userid saving logic along with the order
Browse files Browse the repository at this point in the history
  • Loading branch information
pasindu-pr committed Apr 25, 2023
1 parent 507c981 commit f957f4f
Show file tree
Hide file tree
Showing 23 changed files with 318 additions and 5 deletions.
1 change: 1 addition & 0 deletions src/services/Florage.Authentication/Dtos/UserPublishDto.cs
Original file line number Diff line number Diff line change
Expand Up @@ -5,5 +5,6 @@ public class UserPublishDto
public string Id { get; set; } = string.Empty;
public string Email { get; set; } = string.Empty;
public string UserName { get; set; } = string.Empty;
public string PhoneNumber { get; set; } = string.Empty;
}
}
2 changes: 2 additions & 0 deletions src/services/Florage.Authentication/Dtos/UserRegisterDto.cs
Original file line number Diff line number Diff line change
Expand Up @@ -4,5 +4,7 @@ public class UserRegisterDto
{
public string Email { get; set; } = String.Empty;
public string Password { get; set; } = String.Empty;
public string UserName { get; set; } = String.Empty;
public string PhoneNumber { get; set; } = String.Empty;
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -55,7 +55,7 @@ public async Task<UserTokenResponse> LoginAsync(UserLoginDto userLoginDto)

public async Task<IdentityResult> RegisterAsync(UserRegisterDto userRegisterDto)
{
ApplicationUser user = new ApplicationUser(userRegisterDto.Email, userRegisterDto.Email);
ApplicationUser user = new ApplicationUser(userRegisterDto.UserName, userRegisterDto.Email);

IdentityResult identityResult = await _userManager.CreateAsync(user, userRegisterDto.Password);

Expand Down
7 changes: 7 additions & 0 deletions src/services/Florage.Notifications/Contracts/IEmailService.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
namespace Florage.Notifications.Contracts
{
public interface IEmailService
{
void SendOrderNotification(string userName, string orderId, float price);
}
}
7 changes: 7 additions & 0 deletions src/services/Florage.Notifications/Contracts/ISmsService.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
namespace Florage.Notifications.Contracts
{
public interface ISmsService
{
void SendOrderNotification(string userName, string orderId, float price);
}
}
34 changes: 34 additions & 0 deletions src/services/Florage.Notifications/Controllers/EmailController.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,34 @@
using Florage.Notifications.Contracts;
using Microsoft.AspNetCore.Mvc;

namespace Florage.Notifications.Controllers
{
[Route("api/notifications")]
[ApiController]
public class EmailController : ControllerBase
{
private readonly IEmailService _emailService;
private readonly ISmsService _smsService;

public EmailController(IEmailService emailService, ISmsService smsService)
{
_emailService = emailService;
_smsService = smsService;
}

[HttpGet("mail")]
public IActionResult SendEmail()
{
_emailService.SendOrderNotification("Pasindu", "123", 1000.00f);
return Ok();
}


[HttpGet("sms")]
public IActionResult SendSms()
{
_smsService.SendOrderNotification("Pasindu", "123", 1000.00f);
return Ok();
}
}
}
15 changes: 15 additions & 0 deletions src/services/Florage.Notifications/Florage.Notifications.csproj
Original file line number Diff line number Diff line change
@@ -0,0 +1,15 @@
<Project Sdk="Microsoft.NET.Sdk.Web">

<PropertyGroup>
<TargetFramework>net6.0</TargetFramework>
<Nullable>enable</Nullable>
<ImplicitUsings>enable</ImplicitUsings>
</PropertyGroup>

<ItemGroup>
<PackageReference Include="Azure.Communication.Email" Version="1.0.0" />
<PackageReference Include="Swashbuckle.AspNetCore" Version="6.2.3" />
<PackageReference Include="Vonage" Version="6.2.0" />
</ItemGroup>

</Project>
25 changes: 25 additions & 0 deletions src/services/Florage.Notifications/Florage.Notifications.sln
Original file line number Diff line number Diff line change
@@ -0,0 +1,25 @@

Microsoft Visual Studio Solution File, Format Version 12.00
# Visual Studio Version 17
VisualStudioVersion = 17.2.32630.192
MinimumVisualStudioVersion = 10.0.40219.1
Project("{FAE04EC0-301F-11D3-BF4B-00C04F79EFBC}") = "Florage.Notifications", "Florage.Notifications.csproj", "{872E8CDA-2ECB-498A-8F9F-B65503399939}"
EndProject
Global
GlobalSection(SolutionConfigurationPlatforms) = preSolution
Debug|Any CPU = Debug|Any CPU
Release|Any CPU = Release|Any CPU
EndGlobalSection
GlobalSection(ProjectConfigurationPlatforms) = postSolution
{872E8CDA-2ECB-498A-8F9F-B65503399939}.Debug|Any CPU.ActiveCfg = Debug|Any CPU
{872E8CDA-2ECB-498A-8F9F-B65503399939}.Debug|Any CPU.Build.0 = Debug|Any CPU
{872E8CDA-2ECB-498A-8F9F-B65503399939}.Release|Any CPU.ActiveCfg = Release|Any CPU
{872E8CDA-2ECB-498A-8F9F-B65503399939}.Release|Any CPU.Build.0 = Release|Any CPU
EndGlobalSection
GlobalSection(SolutionProperties) = preSolution
HideSolutionNode = FALSE
EndGlobalSection
GlobalSection(ExtensibilityGlobals) = postSolution
SolutionGuid = {A8E366EB-5BA9-4898-A24C-28489421035C}
EndGlobalSection
EndGlobal
26 changes: 26 additions & 0 deletions src/services/Florage.Notifications/Program.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,26 @@
using Florage.Notifications.Contracts;
using Florage.Notifications.Services;

var builder = WebApplication.CreateBuilder(args);

builder.Services.AddControllers();
builder.Services.AddEndpointsApiExplorer();
builder.Services.AddSwaggerGen();
builder.Services.AddScoped<IEmailService, EmailService>();
builder.Services.AddScoped<ISmsService, SmsService>();

var app = builder.Build();

if (app.Environment.IsDevelopment())
{
app.UseSwagger();
app.UseSwaggerUI();
}

app.UseHttpsRedirection();

app.UseAuthorization();

app.MapControllers();

app.Run();
31 changes: 31 additions & 0 deletions src/services/Florage.Notifications/Properties/launchSettings.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,31 @@
{
"$schema": "https://json.schemastore.org/launchsettings.json",
"iisSettings": {
"windowsAuthentication": false,
"anonymousAuthentication": true,
"iisExpress": {
"applicationUrl": "http://localhost:44537",
"sslPort": 44366
}
},
"profiles": {
"Florage.Notifications": {
"commandName": "Project",
"dotnetRunMessages": true,
"launchBrowser": true,
"launchUrl": "swagger",
"applicationUrl": "https://localhost:7202;http://localhost:5202",
"environmentVariables": {
"ASPNETCORE_ENVIRONMENT": "Development"
}
},
"IIS Express": {
"commandName": "IISExpress",
"launchBrowser": true,
"launchUrl": "swagger",
"environmentVariables": {
"ASPNETCORE_ENVIRONMENT": "Development"
}
}
}
}
46 changes: 46 additions & 0 deletions src/services/Florage.Notifications/Services/EmailService.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,46 @@
using Azure;
using Azure.Communication.Email;
using Florage.Notifications.Contracts;
using Florage.Notifications.Settings;

namespace Florage.Notifications.Services
{
public class EmailService: IEmailService
{

private readonly IConfiguration _configuration;
private readonly ILogger _logger;

public EmailService(IConfiguration configuration, ILogger<EmailService> logger)
{
_configuration = configuration;
_logger = logger;
}

public void SendOrderNotification(string userName, string orderId, float price)
{
EmailSettings mailSettings = _configuration.GetSection("EmailSettings").Get<EmailSettings>(); ;
var client = new EmailClient(mailSettings.ConnectionString);

try
{
var emailSendOperation = client.Send(
wait: WaitUntil.Completed,
senderAddress: "[email protected]",
recipientAddress: "[email protected]",
subject: "This is the subject",
htmlContent: "<html><body>This is the html body</body></html>");

_logger.LogInformation($"Email Sent. Status = {emailSendOperation.Value.Status}");

/// Get the OperationId so that it can be used for tracking the message for troubleshooting
string operationId = emailSendOperation.Id;
_logger.LogInformation($"Email operation id = {operationId}");
}
catch (RequestFailedException ex)
{
_logger.LogInformation($"Email send operation failed with error code: {ex.ErrorCode}, message: {ex.Message}");
}
}
}
}
33 changes: 33 additions & 0 deletions src/services/Florage.Notifications/Services/SmsService.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,33 @@
using Florage.Notifications.Contracts;
using Vonage;
using Vonage.Request;

namespace Florage.Notifications.Services
{
public class SmsService : ISmsService
{
private readonly VonageClient _client;
private readonly IConfiguration _configuration;
private Credentials credentials = Credentials.FromApiKeyAndSecret(
"5330883a",
"cteW8eTUQEQrNzb9"
);

public SmsService(IConfiguration configuration)
{
_client = new VonageClient(credentials);
_configuration = configuration;
}

public void SendOrderNotification(string userName, string orderId, float price)
{

var response = _client.SmsClient.SendAnSms(new Vonage.Messaging.SendSmsRequest()
{
To = "94773279388",
From = "Florage Store",
Text = "A text message sent using the Vonage SMS API"
});
}
}
}
7 changes: 7 additions & 0 deletions src/services/Florage.Notifications/Settings/EmailSettings.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
namespace Florage.Notifications.Settings
{
public class EmailSettings
{
public string ConnectionString { get; set; } = string.Empty;
}
}
9 changes: 9 additions & 0 deletions src/services/Florage.Notifications/Settings/SmsSettings.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
namespace Florage.Notifications.Settings
{
public class SmsSettings
{
public string ApiKey { get; set; } = string.Empty;
public string ApiSecret { get; set; } = string.Empty;
public
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
{
"Logging": {
"LogLevel": {
"Default": "Information",
"Microsoft.AspNetCore": "Warning"
}
}
}
12 changes: 12 additions & 0 deletions src/services/Florage.Notifications/appsettings.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,12 @@
{
"Logging": {
"LogLevel": {
"Default": "Information",
"Microsoft.AspNetCore": "Warning"
}
},
"EmailSettings": {
"ConnectionString": "endpoint=https://cs-florage-comminucation-server.communication.azure.com/;accesskey=s+0+kPh9OQE2nz8sKszeRueHdLZUrMBDVu7Zr0yA9Lpt3xrGckTH0zZDtDmJwZaQSBt/lyNkWaXCVPeYf49cPg=="
},
"AllowedHosts": "*"
}
1 change: 1 addition & 0 deletions src/services/Florage.Orders/Contracts/IUserService.cs
Original file line number Diff line number Diff line change
Expand Up @@ -6,5 +6,6 @@ namespace Florage.Orders.Contracts
public interface IUserService
{
public Task<User> CreateUser(CreateUserDto createUserDto);
public Task<User> GetUserById(string userId);
}
}
1 change: 1 addition & 0 deletions src/services/Florage.Orders/Program.cs
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,7 @@
builder.Services.AddScoped<IProductService, ProductsService>();
builder.Services.AddScoped<IUserService, UserService>();
builder.Services.AddScoped<IOrderPublishingService, OrderPublishingService>();
builder.Services.AddHttpContextAccessor();

PersistanceConfigurations.AddMongoDb(builder.Services);
AsyncMessagingConfigurations.AddRabbitMq(builder.Services, builder.Configuration);
Expand Down
19 changes: 15 additions & 4 deletions src/services/Florage.Orders/Services/OrderService.cs
Original file line number Diff line number Diff line change
Expand Up @@ -13,25 +13,35 @@ public class OrderService : IOrderService
private readonly IRepository<Product> _productsRepository;
private readonly IMapper _mapper;
private readonly IOrderPublishingService _orderPublishingService;
private readonly IHttpContextAccessor _httpContextAccessor;
private readonly IUserService _userService;

public OrderService(IRepository<Order> repository,
IRepository<Product> productsRepository,
IMapper mapper,
IOrderPublishingService orderPublishingService)
IOrderPublishingService orderPublishingService,
IHttpContextAccessor httpContextAccessor,
IUserService userService)
{
_repository = repository;
_productsRepository = productsRepository;
_repository.SetCollectionName(Constants.OrdersCollectionName);
_productsRepository.SetCollectionName(Constants.ProductsCollectionName);
_mapper = mapper;
_orderPublishingService = orderPublishingService;
}

_httpContextAccessor = httpContextAccessor;
_userService = userService;
}
public async Task CreateAsync(CreateOrderDto orderDto)
{
List<OrderProduct> orderProducts = new List<OrderProduct>();
float totalPrice = 0;


string userId = _httpContextAccessor.HttpContext.User.Identity.Name;

User user = await _userService.GetUserById(userId);

foreach (var orderProductDto in orderDto.Products)
{
Product product = await _productsRepository.GetByIdAsync(orderProductDto.Product);
Expand All @@ -48,7 +58,8 @@ public async Task CreateAsync(CreateOrderDto orderDto)
Order order = new Order
{
Products = orderProducts,
TotalPrice = totalPrice
TotalPrice = totalPrice,
UserId = user.Id,
};

Order createdOrder = await _repository.CreateAsync(order);
Expand Down
6 changes: 6 additions & 0 deletions src/services/Florage.Orders/Services/UserService.cs
Original file line number Diff line number Diff line change
Expand Up @@ -24,5 +24,11 @@ public async Task<User> CreateUser(CreateUserDto createUserDto)
User insertedUser = await _repository.CreateAsync(user);
return insertedUser;
}

public async Task<User> GetUserById(string id)
{
User user = await _repository.GetByIdAsync(id);
return user;
}
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,20 @@
using Florage.Payments.Dtos;
using MassTransit;

namespace Florage.Payments.AsyncMessagingServices.Publishers
{
public class PaymentPublishingService
{
private readonly IPublishEndpoint _publishEndpoint;

public PaymentPublishingService(IPublishEndpoint publishEndpoint)
{
_publishEndpoint = publishEndpoint;
}

public async Task PublishPaymentCreatedEvent(PublishPaymentCreatedDto paymentCreatedEvent)
{
await _publishEndpoint.Publish(paymentCreatedEvent);
}
}
}
Loading

0 comments on commit f957f4f

Please sign in to comment.