ты втираешь мне какую то дичь
This commit is contained in:
parent
f81beec0f4
commit
4665f4a409
@ -0,0 +1,64 @@
|
||||
using Microsoft.Extensions.Logging;
|
||||
using System;
|
||||
using System.Collections.Generic;
|
||||
using System.Linq;
|
||||
using System.Text;
|
||||
using System.Threading.Tasks;
|
||||
using VeterinaryContracts.BindingModels;
|
||||
using VeterinaryContracts.BusinessLogicContracts;
|
||||
|
||||
namespace VeterinaryBusinessLogic.MailWorker
|
||||
{
|
||||
public abstract class AbstractMailWorker
|
||||
{
|
||||
protected string _mailLogin = string.Empty;
|
||||
protected string _mailPassword = string.Empty;
|
||||
protected string _smtpClientHost = string.Empty;
|
||||
protected int _smtpClientPort;
|
||||
protected string _popHost = string.Empty;
|
||||
protected int _popPort;
|
||||
private readonly IDoctorLogic _doctorLogic;
|
||||
private readonly ILogger _logger;
|
||||
|
||||
public AbstractMailWorker(ILogger<AbstractMailWorker> logger, IDoctorLogic doctorLogic)
|
||||
{
|
||||
_logger = logger;
|
||||
_doctorLogic = doctorLogic;
|
||||
}
|
||||
|
||||
public void MailConfig(MailConfigBindingModel config)
|
||||
{
|
||||
_mailLogin = config.MailLogin;
|
||||
_mailPassword = config.MailPassword;
|
||||
_smtpClientHost = config.SmtpClientHost;
|
||||
_smtpClientPort = config.SmtpClientPort;
|
||||
_popHost = config.PopHost;
|
||||
_popPort = config.PopPort;
|
||||
_logger.LogDebug("Config: {login}, {password}, {clientHost}, {clientPOrt}, {popHost}, {popPort}", _mailLogin, _mailPassword, _smtpClientHost, _smtpClientPort, _popHost, _popPort);
|
||||
}
|
||||
|
||||
public async void MailSendAsync(MailSendInfoBindingModel info)
|
||||
{
|
||||
if (string.IsNullOrEmpty(_mailLogin) || string.IsNullOrEmpty(_mailPassword))
|
||||
{
|
||||
return;
|
||||
}
|
||||
|
||||
if (string.IsNullOrEmpty(_smtpClientHost) || _smtpClientPort == 0)
|
||||
{
|
||||
return;
|
||||
}
|
||||
|
||||
if (string.IsNullOrEmpty(info.MailAddress) || string.IsNullOrEmpty(info.Subject) || string.IsNullOrEmpty(info.Text))
|
||||
{
|
||||
return;
|
||||
}
|
||||
|
||||
_logger.LogDebug("Send Mail: {To}, {Subject}", info.MailAddress, info.Subject);
|
||||
|
||||
await SendMailAsync(info);
|
||||
}
|
||||
|
||||
protected abstract Task SendMailAsync(MailSendInfoBindingModel info);
|
||||
}
|
||||
}
|
@ -0,0 +1,50 @@
|
||||
using Microsoft.Extensions.Logging;
|
||||
using System;
|
||||
using System.Collections.Generic;
|
||||
using System.Linq;
|
||||
using System.Net.Mail;
|
||||
using System.Net.Mime;
|
||||
using System.Net;
|
||||
using System.Text;
|
||||
using System.Threading.Tasks;
|
||||
using VeterinaryContracts.BindingModels;
|
||||
using VeterinaryContracts.BusinessLogicContracts;
|
||||
|
||||
namespace VeterinaryBusinessLogic.MailWorker
|
||||
{
|
||||
public class MailKitWorker : AbstractMailWorker
|
||||
{
|
||||
public MailKitWorker(ILogger<MailKitWorker> logger, IDoctorLogic doctorLogic) : base(logger, doctorLogic) { }
|
||||
|
||||
protected override async Task SendMailAsync(MailSendInfoBindingModel info)
|
||||
{
|
||||
using var objMailMessage = new MailMessage();
|
||||
using var objSmtpClient = new SmtpClient(_smtpClientHost, _smtpClientPort);
|
||||
|
||||
try
|
||||
{
|
||||
objMailMessage.From = new MailAddress(_mailLogin);
|
||||
objMailMessage.To.Add(new MailAddress(info.MailAddress));
|
||||
objMailMessage.Subject = info.Subject;
|
||||
objMailMessage.Body = info.Text;
|
||||
objMailMessage.SubjectEncoding = Encoding.UTF8;
|
||||
objMailMessage.BodyEncoding = Encoding.UTF8;
|
||||
Attachment attachment = new Attachment("C:\\ReportsCourseWork\\pdffile.pdf", new ContentType(MediaTypeNames.Application.Pdf));
|
||||
objMailMessage.Attachments.Add(attachment);
|
||||
|
||||
objSmtpClient.UseDefaultCredentials = false;
|
||||
objSmtpClient.EnableSsl = true;
|
||||
objSmtpClient.DeliveryMethod = SmtpDeliveryMethod.Network;
|
||||
objSmtpClient.Credentials = new NetworkCredential(_mailLogin, _mailPassword);
|
||||
|
||||
await Task.Run(() => objSmtpClient.Send(objMailMessage));
|
||||
}
|
||||
catch (Exception)
|
||||
{
|
||||
throw;
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
}
|
||||
}
|
@ -0,0 +1,18 @@
|
||||
using System;
|
||||
using System.Collections.Generic;
|
||||
using System.Linq;
|
||||
using System.Text;
|
||||
using System.Threading.Tasks;
|
||||
|
||||
namespace VeterinaryContracts.BindingModels
|
||||
{
|
||||
public class MailConfigBindingModel
|
||||
{
|
||||
public string MailLogin { get; set; } = string.Empty;
|
||||
public string MailPassword { get; set; } = string.Empty;
|
||||
public string SmtpClientHost { get; set; } = string.Empty;
|
||||
public int SmtpClientPort { get; set; }
|
||||
public string PopHost { get; set; } = string.Empty;
|
||||
public int PopPort { get; set; }
|
||||
}
|
||||
}
|
@ -0,0 +1,15 @@
|
||||
using System;
|
||||
using System.Collections.Generic;
|
||||
using System.Linq;
|
||||
using System.Text;
|
||||
using System.Threading.Tasks;
|
||||
|
||||
namespace VeterinaryContracts.BindingModels
|
||||
{
|
||||
public class MailSendInfoBindingModel
|
||||
{
|
||||
public string MailAddress { get; set; } = string.Empty;
|
||||
public string Subject { get; set; } = string.Empty;
|
||||
public string Text { get; set; } = string.Empty;
|
||||
}
|
||||
}
|
@ -26,9 +26,7 @@ namespace VeterinaryRestApi.Controllers
|
||||
var elem = _drug.ReadElement(new DrugSearchModel { Id = drugId });
|
||||
if (elem == null)
|
||||
return null;
|
||||
var huinya = Tuple.Create(elem, elem.DrugMedications.Select(x => x.Value.MedicationName).ToList());
|
||||
return huinya;
|
||||
//return Tuple.Create(elem, elem.DrugMedications.Select(x => x.Value.MedicationName).ToList());
|
||||
return Tuple.Create(elem, elem.DrugMedications.Select(x => x.Value.MedicationName).ToList());
|
||||
}
|
||||
catch (Exception ex)
|
||||
{
|
||||
|
@ -0,0 +1,89 @@
|
||||
using Microsoft.AspNetCore.Mvc;
|
||||
using VeterinaryBusinessLogic.BusinessLogic;
|
||||
using VeterinaryBusinessLogic.MailWorker;
|
||||
//using VeterinaryBusinessLogic.MailWorker;
|
||||
using VeterinaryContracts.BindingModels;
|
||||
using VeterinaryContracts.BusinessLogicContracts;
|
||||
using VeterinaryDatabaseImplement.Implements;
|
||||
|
||||
namespace VeterinaryRestApi.Controllers
|
||||
{
|
||||
[Route("api/[controller]/[action]")]
|
||||
[ApiController]
|
||||
public class ReportController : Controller
|
||||
{
|
||||
private readonly IReportLogicDoctor _reportDoctor;
|
||||
private readonly AbstractMailWorker _mailWorker;
|
||||
public ReportController(ILogger<ReportController> logger, IReportLogicDoctor reportDoctor, AbstractMailWorker mailWorker)
|
||||
{
|
||||
_reportDoctor = reportDoctor;
|
||||
_mailWorker = mailWorker;
|
||||
}
|
||||
[Microsoft.AspNetCore.Mvc.HttpGet]
|
||||
public IActionResult Index(ReportLogicDoctor reportDoctor)
|
||||
{
|
||||
return View();
|
||||
}
|
||||
[HttpPost]
|
||||
public void CreatePurchaseListWordFile(ReportPurchaseMedicationBindingModel model)
|
||||
{
|
||||
try
|
||||
{
|
||||
_reportDoctor.SavePurchasesToWordFile(model);
|
||||
}
|
||||
catch (Exception ex)
|
||||
{
|
||||
throw;
|
||||
}
|
||||
}
|
||||
[HttpPost]
|
||||
public void CreatePurchaseListExcelFile(ReportPurchaseMedicationBindingModel model)
|
||||
{
|
||||
try
|
||||
{
|
||||
_reportDoctor.SavePurchasesToExcelFile(model);
|
||||
}
|
||||
catch (Exception ex)
|
||||
{
|
||||
throw;
|
||||
}
|
||||
}
|
||||
[HttpGet]
|
||||
public List<VisitsGuidesViewModel> GetVisitsGuidesReport(string dateFrom, string dateTo, int pharmacistId)
|
||||
{
|
||||
try
|
||||
{
|
||||
DateTime DateFrom = DateTime.Parse(dateFrom);
|
||||
DateTime DateTo = DateTime.Parse(dateTo);
|
||||
VisitsGuidesBindingModel model = new();
|
||||
model.DateFrom = DateFrom;
|
||||
model.DateTo = DateTo;
|
||||
model.DoctorId = pharmacistId;
|
||||
return _reportDoctor.GetMedicineVisitsAndGuidances(model);
|
||||
}
|
||||
catch (Exception ex)
|
||||
{
|
||||
throw;
|
||||
}
|
||||
}
|
||||
|
||||
[HttpPost]
|
||||
public void SendVisitsGuidesReportToEmail(VisitsGuidesBindingModel model)
|
||||
{
|
||||
try
|
||||
{
|
||||
_reportDoctor.SaveMedicinesToPdfFile(model);
|
||||
_mailWorker.MailSendAsync(new MailSendInfoBindingModel
|
||||
{
|
||||
MailAddress = model.Email!,
|
||||
Subject = "Отчет по медикаментам",
|
||||
Text = "Лови"
|
||||
});
|
||||
}
|
||||
catch (Exception ex)
|
||||
{
|
||||
throw;
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
@ -1,8 +1,10 @@
|
||||
using VeterinaryBusinessLogic.BusinessLogic;
|
||||
using VeterinaryContracts.BindingModels;
|
||||
using VeterinaryContracts.BusinessLogicContracts;
|
||||
using VeterinaryContracts.StorageContracts;
|
||||
using VeterinaryDatabaseImplement.Implements;
|
||||
using Microsoft.OpenApi.Models;
|
||||
using VeterinaryBusinessLogic.MailWorker;
|
||||
|
||||
var builder = WebApplication.CreateBuilder(args);
|
||||
|
||||
@ -29,9 +31,29 @@ builder.Services.AddTransient<IPurchaseLogic, PurchaseLogic>();
|
||||
builder.Services.AddControllers();
|
||||
// Learn more about configuring Swagger/OpenAPI at https://aka.ms/aspnetcore/swashbuckle
|
||||
builder.Services.AddEndpointsApiExplorer();
|
||||
builder.Services.AddSwaggerGen();
|
||||
|
||||
builder.Services.AddSwaggerGen(c =>
|
||||
{
|
||||
c.SwaggerDoc("v1", new OpenApiInfo { Title = "VeterinaryViewRestApi", Version = "v1" });
|
||||
});
|
||||
var app = builder.Build();
|
||||
var mailSender = app.Services.GetService<AbstractMailWorker>();
|
||||
mailSender?.MailConfig(new MailConfigBindingModel
|
||||
{
|
||||
MailLogin = builder.Configuration?.GetSection("MailLogin")?.Value?.ToString()
|
||||
?? string.Empty,
|
||||
MailPassword =
|
||||
builder.Configuration?.GetSection("MailPassword")?.Value?.ToString() ??
|
||||
string.Empty,
|
||||
SmtpClientHost =
|
||||
builder.Configuration?.GetSection("SmtpClientHost")?.Value?.ToString() ??
|
||||
string.Empty,
|
||||
SmtpClientPort =
|
||||
Convert.ToInt32(builder.Configuration?.GetSection("SmtpClientPort")?.Value?.ToString()),
|
||||
PopHost = builder.Configuration?.GetSection("PopHost")?.Value?.ToString() ??
|
||||
string.Empty,
|
||||
PopPort = Convert.ToInt32(builder.Configuration?.GetSection("PopPort")?.Value?.ToString())
|
||||
});
|
||||
|
||||
|
||||
// Configure the HTTP request pipeline.
|
||||
if (app.Environment.IsDevelopment())
|
||||
|
@ -5,5 +5,11 @@
|
||||
"Microsoft.AspNetCore": "Warning"
|
||||
}
|
||||
},
|
||||
"AllowedHosts": "*"
|
||||
"AllowedHosts": "*",
|
||||
"SmtpClientHost": "smtp.gmail.com",
|
||||
"SmtpClientPort": "587",
|
||||
"PopHost": "pop.gmail.com",
|
||||
"PopPort": "995",
|
||||
"MailLogin": "anasirov48@gmail.com",
|
||||
"MailPassword": "ozxp vjof uinv fcmj"
|
||||
}
|
||||
|
@ -474,6 +474,55 @@ namespace VeterinaryShowDoctorApp.Controllers
|
||||
ViewBag.Medications = APIDoctor.GetRequest<List<MedicationViewModel>>($"api/medication/getmedications?doctorid={APIDoctor.Doctor.Id}");
|
||||
return View();
|
||||
}
|
||||
|
||||
[HttpPost]
|
||||
public void PurchaseMedicationReport(List<int> medications, string type)
|
||||
{
|
||||
if (APIDoctor.Doctor == null)
|
||||
{
|
||||
throw new Exception("Вы как суда попали? Суда вход только авторизованным");
|
||||
}
|
||||
|
||||
if (medications.Count <= 0)
|
||||
{
|
||||
throw new Exception("Количество должно быть больше 0");
|
||||
}
|
||||
|
||||
if (string.IsNullOrEmpty(type))
|
||||
{
|
||||
throw new Exception("Неверный тип отчета");
|
||||
}
|
||||
|
||||
if (type == "docx")
|
||||
{
|
||||
APIDoctor.PostRequest("api/report/createpurchaselistwordfile", new ReportPurchaseMedicationBindingModel
|
||||
{
|
||||
Medications = medications,
|
||||
FileName = "C:\\ReportsCourseWork\\wordfile.docx"
|
||||
});
|
||||
Response.Redirect("GetWordFile");
|
||||
}
|
||||
else
|
||||
{
|
||||
APIDoctor.PostRequest("api/report/createpurchaselistexcelfile", new ReportPurchaseMedicationBindingModel
|
||||
{
|
||||
Medications = medications,
|
||||
FileName = "C:\\ReportsCourseWork\\excelfile.xlsx"
|
||||
});
|
||||
Response.Redirect("GetExcelFile");
|
||||
}
|
||||
}
|
||||
|
||||
[HttpGet]
|
||||
public IActionResult GetWordFile()
|
||||
{
|
||||
return new PhysicalFileResult("C:\\ReportsCourseWork\\wordfile.docx", "application/vnd.openxmlformats-officedocument.wordprocessingml.document");
|
||||
}
|
||||
|
||||
public IActionResult GetExcelFile()
|
||||
{
|
||||
return new PhysicalFileResult("C:\\ReportsCourseWork\\excelfile.xlsx", "application/vnd.openxmlformats-officedocument.spreadsheetml.sheet");
|
||||
}
|
||||
[HttpGet]
|
||||
public IActionResult Report()
|
||||
{
|
||||
|
@ -19,9 +19,18 @@
|
||||
</select>
|
||||
</div>
|
||||
</div>
|
||||
<div class="row">
|
||||
<div class="col-8"></div>
|
||||
<div class="col-2"><input type="submit" value="Word" class="btn btn-primary" /></div>
|
||||
<div class="col-1"><input type="submit" value="Excel" class="btn btn-primary" /></div>
|
||||
<div class="file-format">
|
||||
<label class="form-label">Выберите формат файла:</label>
|
||||
<div class="form-check">
|
||||
<input class="form-check-input" type="radio" name="type" value="docx" id="docx">
|
||||
<label class="form-check-label" for="docx">Word-файл</label>
|
||||
</div>
|
||||
<div class="form-check">
|
||||
<input class="form-check-input" type="radio" name="type" value="xlsx" id="xlsx" checked>
|
||||
<label class="form-check-label" for="xlsx">Excel-файл</label>
|
||||
</div>
|
||||
</div>
|
||||
<div class="d-flex justify-content-center">
|
||||
<button type="submit" class="btn btn-block btn-outline-dark w-100">Создать</button>
|
||||
</div>
|
||||
</form>
|
||||
|
Loading…
Reference in New Issue
Block a user