Skip to content

Commit

Permalink
Merge branch 'master' into BE_MK_PDF_TableStyle
Browse files Browse the repository at this point in the history
  • Loading branch information
DarkenHouk authored Jul 25, 2022
2 parents 1d9c4cb + 94fa189 commit 1096051
Show file tree
Hide file tree
Showing 55 changed files with 1,932 additions and 1,211 deletions.
2 changes: 2 additions & 0 deletions Application/Application.csproj
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,9 @@
<PackageReference Include="Azure.Storage.Blobs" Version="12.12.0" />
<PackageReference Include="FluentValidation" Version="11.0.1" />
<PackageReference Include="FluentValidation.AspNetCore" Version="11.0.1" />
<PackageReference Include="MailKit" Version="3.3.0" />
<PackageReference Include="Microsoft.Extensions.DependencyInjection.Abstractions" Version="6.0.0" />
<PackageReference Include="MimeKit" Version="3.3.0" />
<PackageReference Include="NLog" Version="5.0.0" />
<PackageReference Include="Syncfusion.Pdf.Net.Core" Version="20.2.0.39" />
<PackageReference Include="System.Drawing.Common" Version="6.0.0" />
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -29,7 +29,7 @@ public static void AddApplicationServices(this IServiceCollection services)
services.AddScoped<IArticleService, ArticleService>();
services.AddScoped<ICommentService, CommentService>();
services.AddScoped<IUserProfilePictureService, UserProfilePictureService>();
services.AddScoped<IImageParser, ImageParser>();
services.AddScoped<IImageService, ImageService>();
services.AddScoped<IAnimalPhotoService, AnimalPhotoService>();
services.AddScoped<IFeedbackService, FeedbackService>();

Expand All @@ -39,6 +39,9 @@ public static void AddApplicationServices(this IServiceCollection services)

services.AddScoped<ICreateTableForPDF<Appointment>, CreateTableForAnimalMedCardPDF>();
services.AddScoped<IGenerateFullPDF<AnimalParameters>, AnimalMedCardPDFGenerator>();

services.AddScoped<IEmailService, EmailService>();

}
}
}
63 changes: 17 additions & 46 deletions Application/Services/ArticleService.cs
Original file line number Diff line number Diff line change
@@ -1,70 +1,50 @@
using System.Drawing;
using System.Linq.Expressions;
using Azure;
using Core.Entities;
using Core.Entities;
using Core.Exceptions;
using Core.Interfaces;
using Core.Interfaces.Repositories;
using Core.Interfaces.Services;
using Core.Paginator;
using Core.Paginator.Parameters;
using Microsoft.EntityFrameworkCore;
using Microsoft.Extensions.Configuration;
using System.Linq.Expressions;

namespace Application.Services;

public class ArticleService : IArticleService
{
private readonly IArticleRepository _articleRepository;
private readonly ILoggerManager _loggerManager;
private readonly IImageParser _imageParser;
private readonly IImageService _imageService;

public ArticleService(
IArticleRepository articleRepository,
ILoggerManager loggerManager,
IImageParser imageParser
IImageService imageService
)
{
_articleRepository = articleRepository;
_loggerManager = loggerManager;
_imageParser = imageParser;
_imageService = imageService;
}

public async Task CreateArticleAsync(Article article)
{
try
{
article.Body = await _imageParser.UploadImages(article.Body!);
await _articleRepository.InsertAsync(article);
await _articleRepository.SaveChangesAsync();
}
catch (DbUpdateException)
{
_loggerManager.LogWarn($"user with id {article.AuthorId} not found");
throw new NotFoundException($"user with id {article.AuthorId} not found");
}
catch (RequestFailedException)
{
_loggerManager.LogWarn("Error while uploading files to the blob");
throw new BadRequestException("Error while uploading files to the blob");
}

article.Body = _imageService.TrimArticleImages(article.Body!);
await _imageService.ClearUnusedImagesAsync(article.Body, article.AuthorId);
await _articleRepository.InsertAsync(article);
await _articleRepository.SaveChangesAsync();

_loggerManager.LogInfo($"Created new article with title {article.Title}");
}

public async Task UpdateArticleAsync(Article article)
{
var updatingArticle = await GetByIdAsync(article.Id);
updatingArticle.Title = article.Title;
try
{
updatingArticle.Body = await _imageParser.UploadImages(article.Body);
}
catch (RequestFailedException)
{
_loggerManager.LogWarn("Error while uploading files to the blob");
throw new BadRequestException("Error while uploading files to the blob");
}

await _imageService.ClearUnusedImagesAsync(article.Body!, updatingArticle.AuthorId);
await _imageService.ClearOutdatedImagesAsync(article.Body!, updatingArticle.Body!);
updatingArticle.Body = _imageService.TrimArticleImages(article.Body!);

updatingArticle.Published = article.Published;
updatingArticle.Edited = true;

Expand All @@ -75,25 +55,16 @@ public async Task UpdateArticleAsync(Article article)
public async Task DeleteArticleAsync(int articleId)
{
var articleToRemove = await GetByIdAsync(articleId);
articleToRemove.Body = await _imageService.DeleteImagesAsync(articleToRemove.Body!);

try
{
articleToRemove.Body = await _imageParser.DeleteImages(articleToRemove.Body);
}
catch (RequestFailedException)
{
_loggerManager.LogWarn("Error while deleting files from the blob");
throw new BadRequestException("Error while deleting files from the blob");
}

_articleRepository.Delete(articleToRemove);
await _articleRepository.SaveChangesAsync();
_loggerManager.LogInfo($"Deleted article with id {articleId}");
}

public async Task<Article> GetByIdAsync(int articleId)
{
var article = await _articleRepository.GetById(articleId, includeProperties:"Author");
var article = await _articleRepository.GetById(articleId, includeProperties: "Author");
if (article is null)
{
_loggerManager.LogWarn($"Article with id {articleId} does not exist");
Expand Down
100 changes: 100 additions & 0 deletions Application/Services/EmailService.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,100 @@
using Core.Entities;
using Core.Interfaces;
using Core.Interfaces.Services;
using MailKit.Net.Smtp;
using MailKit.Security;
using Microsoft.Extensions.Configuration;
using MimeKit;

namespace Application.Services
{
public class EmailService : IEmailService
{
readonly string address;
readonly string secret;
readonly string protocol = "smtp.gmail.com";
const int port = 587;

IConfiguration _configuration;
ILoggerManager _logger;

public EmailService(IConfiguration configuration, ILoggerManager logger)
{
_configuration = configuration;
_logger = logger;
address = _configuration.GetSection("Mailbox").GetSection("Address").Value;
secret = _configuration.GetSection("Mailbox").GetSection("Secret").Value;
}

public async Task NotifyUsers(Mailing message)
{
var createdMessage = new MimeMessage();
var recipients = new List<MailboxAddress>();

foreach (var recipient in message.Recipients)
recipients.Add(MailboxAddress.Parse(recipient));

createdMessage.From.Add(new MailboxAddress("Vet clinic", address));
createdMessage.To.AddRange(recipients);

createdMessage.Subject = message.Subject;

createdMessage.Body = new TextPart("html")
{
Text = message.Body
};


SmtpClient client = new SmtpClient();

try
{
await client.ConnectAsync(protocol, port, SecureSocketOptions.StartTls);
await client.AuthenticateAsync(address, "oxoelgyyqeyvyxzo");
await client.SendAsync(createdMessage);
}
catch (Exception ex)
{
_logger.LogError($"Error happened during sending email. Error: {ex.Message}");
}
finally
{
client.Disconnect(true);
client.Dispose();
}
_logger.LogInfo("Email was successfully sended");
}

public async Task Send(EmailMessage message)
{
var createdMessage = new MimeMessage();
createdMessage.From.Add(new MailboxAddress("Vet clinic", address));
createdMessage.To.Add(MailboxAddress.Parse(message.Recipient));

createdMessage.Subject = message.Subject;

createdMessage.Body = new TextPart("plain")
{
Text = message.Body
};
SmtpClient client = new SmtpClient();

try
{
await client.ConnectAsync(protocol, port, SecureSocketOptions.StartTls);
await client.AuthenticateAsync(address, secret);
await client.SendAsync(createdMessage);
}
catch (Exception ex)
{
_logger.LogError($"Error happened during sending emails. Error: {ex.Message}");
}
finally
{
client.Disconnect(true);
client.Dispose();
}
_logger.LogInfo("Emails were successfully sended");
}
}
}
18 changes: 8 additions & 10 deletions Application/Services/FeedbackService.cs
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,8 @@
using Core.Interfaces;
using Core.Interfaces.Repositories;
using Core.Interfaces.Services;
using Core.Paginator;
using Core.Paginator.Parameters;
using Microsoft.EntityFrameworkCore;
using System.Linq.Expressions;

Expand Down Expand Up @@ -44,19 +46,15 @@ public async Task AddFeedback(Feedback feedback)
_logger.LogInfo($"The new feedback with ID = {feedback.Id} was added");
}

public async Task<IEnumerable<Feedback>> GetAllFeedbacks(
string? filterParam,
int? takeCount,
int skipCount = 0)
public async Task<PagedList<Feedback>> GetAllFeedbacks(
FeedbackParameters parameters)
{
_logger.LogInfo("All feedbacks were received");

return await _repository.QueryAsync(
asNoTracking: true,
filter: GetFilterQuery(filterParam),
take: takeCount,
skip: skipCount,
include: query =>
return await _repository.GetPaged(
parameters: parameters,
filter: GetFilterQuery(parameters.FilterParam),
includeProperties: query =>
query.Include(feedback => feedback.User));
}
}
Expand Down
Loading

0 comments on commit 1096051

Please sign in to comment.