more tasks done

This commit is contained in:
Egor 2022-08-20 15:40:32 +03:00
parent 99f21ec181
commit b9814fd408
18 changed files with 486 additions and 15 deletions

View file

@ -63,8 +63,8 @@ namespace EFTask
while (true)
{
var playerToDescribe = Prompt.Select("Select a player for additional information", players, 10,
textSelector: p => p == null ? "Back" : $"{p.Id}. {p.Name}, {p.Age}");
if (playerToDescribe == null) return;
textSelector: p => p is null ? "Back" : $"{p.Id}. {p.Name}, {p.Age}");
if (playerToDescribe is null) return;
Console.WriteLine("\nFootball player\n\n"
+ $"Id: {playerToDescribe.Id}\n"
+ $"Name: {playerToDescribe.Name}\n"
@ -93,8 +93,8 @@ namespace EFTask
var players = fc.Players.ToHashSet<FootballPlayer?>();
players.Add(null);
var player = Prompt.Select("Select a player to update", players, 10,
textSelector: p => p == null ? "Back" : $"{p.Id}. {p.Name}, {p.Age}");
if (player == null) return;
textSelector: p => p is null ? "Back" : $"{p.Id}. {p.Name}, {p.Age}");
if (player is null) return;
player.Name = Prompt.Input<string>("Enter the player's new name", player.Name);
player.Age = Prompt.Input<int>("Enter the player's new age", player.Age);
fc.SaveChanges();
@ -119,8 +119,8 @@ namespace EFTask
while (true)
{
var teamToDescribe = Prompt.Select("Select a team for additional information", teams, 10,
textSelector: t => t == null ? "Back" : $"{t.Id}. {t.Name}");
if (teamToDescribe == null) return;
textSelector: t => t is null ? "Back" : $"{t.Id}. {t.Name}");
if (teamToDescribe is null) return;
Console.WriteLine("\nFootball team\n\n"
+ $"Id: {teamToDescribe.Id}\n"
+ $"Name: {teamToDescribe.Name}\n"
@ -147,8 +147,8 @@ namespace EFTask
var teams = fc.Teams.ToHashSet<FootballTeam?>();
teams.Add(null);
var team = Prompt.Select("Select a team to update", teams, 10,
textSelector: t => t == null ? "Back" : $"{t.Id}. {t.Name}");
if (team == null) return;
textSelector: t => t is null ? "Back" : $"{t.Id}. {t.Name}");
if (team is null) return;
team.Name = Prompt.Input<string>("Enter the team's new name", team.Name);
fc.SaveChanges();
Console.WriteLine($"Successfully updated a team with an id {team.Id}");
@ -172,8 +172,8 @@ namespace EFTask
while (true)
{
var contractToDescribe = Prompt.Select("Select a team for additional information", contracts, 10,
textSelector: c => c == null ? "Back" : $"{c.Id}. {c.Player.Name} - {c.Team.Name}");
if (contractToDescribe == null) return;
textSelector: c => c is null ? "Back" : $"{c.Id}. {c.Player.Name} - {c.Team.Name}");
if (contractToDescribe is null) return;
Console.WriteLine("\nFootball contract\n\n"
+ $"Id: {contractToDescribe.Id}\n"
+ $"Player:\n"
@ -193,7 +193,7 @@ namespace EFTask
protected void InsertContract(FootballContext fc)
{
Console.WriteLine("Inserting a new football contract...");
if (fc.Players.Count() == 0)
if (!fc.Players.Any())
{
Console.WriteLine("No players were found, insert a new player to make a contract");
Console.WriteLine("Cancelling operation...");
@ -223,21 +223,21 @@ namespace EFTask
var contracts = fc.Contracts.ToHashSet<FootbalContract?>();
contracts.Add(null);
var contract = Prompt.Select("Select a contract to update", contracts, 10,
textSelector: c => c == null ? "Back" : $"{c.Id}. {c.Player.Name} - {c.Team.Name}");
if (contract == null) return;
textSelector: c => c is null ? "Back" : $"{c.Id}. {c.Player.Name} - {c.Team.Name}");
if (contract is null) return;
var players = fc.Players.Where(p => p.Id != contract.PlayerId).ToHashSet<FootballPlayer?>();
players.Add(null);
var player = Prompt.Select("Select a new player", players, 10,
textSelector: p => p == null ? "Do not change" : $"{p.Id}. {p.Name}, {p.Age}");
if (player != null)
if (player is not null)
contract.Player = player;
var teams = fc.Teams.Where(t => t.Id != contract.TeamId).ToHashSet<FootballTeam?>();
teams.Add(null);
var team = Prompt.Select("Select a new team", teams, 10,
textSelector: t => t == null ? "Do not change" : $"{t.Id}. {t.Name}");
if (team != null)
if (team is not null)
contract.Team = team;
var salary = Prompt.Input<decimal>("Input the player's salary", contract.Salary, contract.Salary.ToString());

View file

@ -0,0 +1,25 @@
**/.classpath
**/.dockerignore
**/.env
**/.git
**/.gitignore
**/.project
**/.settings
**/.toolstarget
**/.vs
**/.vscode
**/*.*proj.user
**/*.dbmdl
**/*.jfm
**/azds.yaml
**/bin
**/charts
**/docker-compose*
**/Dockerfile*
**/node_modules
**/npm-debug.log
**/obj
**/secrets.dev.yaml
**/values.dev.yaml
LICENSE
README.md

View file

@ -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}") = "WebAPIEventsTask", "WebAPIEventsTask\WebAPIEventsTask.csproj", "{E9A9B7F9-A548-45CD-B9BA-C7BC45A3F98C}"
EndProject
Global
GlobalSection(SolutionConfigurationPlatforms) = preSolution
Debug|Any CPU = Debug|Any CPU
Release|Any CPU = Release|Any CPU
EndGlobalSection
GlobalSection(ProjectConfigurationPlatforms) = postSolution
{E9A9B7F9-A548-45CD-B9BA-C7BC45A3F98C}.Debug|Any CPU.ActiveCfg = Debug|Any CPU
{E9A9B7F9-A548-45CD-B9BA-C7BC45A3F98C}.Debug|Any CPU.Build.0 = Debug|Any CPU
{E9A9B7F9-A548-45CD-B9BA-C7BC45A3F98C}.Release|Any CPU.ActiveCfg = Release|Any CPU
{E9A9B7F9-A548-45CD-B9BA-C7BC45A3F98C}.Release|Any CPU.Build.0 = Release|Any CPU
EndGlobalSection
GlobalSection(SolutionProperties) = preSolution
HideSolutionNode = FALSE
EndGlobalSection
GlobalSection(ExtensibilityGlobals) = postSolution
SolutionGuid = {E7CB29CF-AE33-41EE-B3B0-C4B696514324}
EndGlobalSection
EndGlobal

View file

@ -0,0 +1,52 @@
using Microsoft.AspNetCore.Mvc;
using RabbitMQ.Client;
using RabbitMQ.Client.Events;
using System.Text;
namespace WebAPIEventsTask.Controllers
{
[ApiController]
[Route("api")]
public class EventController : ControllerBase
{
private readonly ILogger<EventController> _logger;
private readonly IConfiguration _configuration;
private static IModel rmqChannel;
private static readonly string QUEUE_NAME = "test-queue";
static EventController()
{
var connectionFactory = new ConnectionFactory { HostName = "localhost" };
var connection = connectionFactory.CreateConnection();
rmqChannel = connection.CreateModel();
}
public EventController(ILogger<EventController> logger, IConfiguration configuration)
{
_logger = logger;
_configuration = configuration;
rmqChannel.QueueDeclare(QUEUE_NAME, false, false, false);
}
[HttpPost("send")]
public IActionResult SendMessage(string msg)
{
rmqChannel.BasicPublish("", QUEUE_NAME, null, Encoding.UTF8.GetBytes(msg));
return Ok();
}
[HttpGet("receive")]
public IActionResult ReceiveMessage()
{
var consumer = new EventingBasicConsumer(rmqChannel);
string? msg = null;
consumer.Received += (model, ea) =>
{
var body = ea.Body.ToArray();
msg = Encoding.UTF8.GetString(body);
};
rmqChannel.BasicConsume(QUEUE_NAME, true, consumer);
return msg == null ? NotFound() : Ok(msg);
}
}
}

View file

@ -0,0 +1,22 @@
#See https://aka.ms/containerfastmode to understand how Visual Studio uses this Dockerfile to build your images for faster debugging.
FROM mcr.microsoft.com/dotnet/aspnet:6.0 AS base
WORKDIR /app
EXPOSE 80
EXPOSE 443
FROM mcr.microsoft.com/dotnet/sdk:6.0 AS build
WORKDIR /src
COPY ["WebAPIEventsTask/WebAPIEventsTask.csproj", "WebAPIEventsTask/"]
RUN dotnet restore "WebAPIEventsTask/WebAPIEventsTask.csproj"
COPY . .
WORKDIR "/src/WebAPIEventsTask"
RUN dotnet build "WebAPIEventsTask.csproj" -c Release -o /app/build
FROM build AS publish
RUN dotnet publish "WebAPIEventsTask.csproj" -c Release -o /app/publish
FROM base AS final
WORKDIR /app
COPY --from=publish /app/publish .
ENTRYPOINT ["dotnet", "WebAPIEventsTask.dll"]

View file

@ -0,0 +1,25 @@
var builder = WebApplication.CreateBuilder(args);
// Add services to the container.
builder.Services.AddControllers();
// Learn more about configuring Swagger/OpenAPI at https://aka.ms/aspnetcore/swashbuckle
builder.Services.AddEndpointsApiExplorer();
builder.Services.AddSwaggerGen();
var app = builder.Build();
// Configure the HTTP request pipeline.
if (app.Environment.IsDevelopment())
{
app.UseSwagger();
app.UseSwaggerUI();
}
app.UseHttpsRedirection();
app.UseAuthorization();
app.MapControllers();
app.Run();

View file

@ -0,0 +1,38 @@
{
"$schema": "https://json.schemastore.org/launchsettings.json",
"iisSettings": {
"windowsAuthentication": false,
"anonymousAuthentication": true,
"iisExpress": {
"applicationUrl": "http://localhost:37939",
"sslPort": 44356
}
},
"profiles": {
"WebAPIEventsTask": {
"commandName": "Project",
"launchBrowser": true,
"launchUrl": "swagger",
"environmentVariables": {
"ASPNETCORE_ENVIRONMENT": "Development"
},
"applicationUrl": "https://localhost:7171;http://localhost:5171",
"dotnetRunMessages": true
},
"IIS Express": {
"commandName": "IISExpress",
"launchBrowser": true,
"launchUrl": "swagger",
"environmentVariables": {
"ASPNETCORE_ENVIRONMENT": "Development"
}
},
"Docker": {
"commandName": "Docker",
"launchBrowser": true,
"launchUrl": "{Scheme}://{ServiceHost}:{ServicePort}/swagger",
"publishAllPorts": true,
"useSSL": true
}
}
}

View file

@ -0,0 +1,7 @@
namespace WebAPIEventsTask
{
public class Startup
{
}
}

View file

@ -0,0 +1,17 @@
<Project Sdk="Microsoft.NET.Sdk.Web">
<PropertyGroup>
<TargetFramework>net6.0</TargetFramework>
<Nullable>enable</Nullable>
<ImplicitUsings>enable</ImplicitUsings>
<UserSecretsId>befb116a-f711-4f3f-9475-d96563d5378b</UserSecretsId>
<DockerDefaultTargetOS>Linux</DockerDefaultTargetOS>
</PropertyGroup>
<ItemGroup>
<PackageReference Include="Microsoft.VisualStudio.Azure.Containers.Tools.Targets" Version="1.15.1" />
<PackageReference Include="RabbitMQ.Client" Version="6.4.0" />
<PackageReference Include="Swashbuckle.AspNetCore" Version="6.2.3" />
</ItemGroup>
</Project>

View file

@ -0,0 +1,8 @@
{
"Logging": {
"LogLevel": {
"Default": "Information",
"Microsoft.AspNetCore": "Warning"
}
}
}

View file

@ -0,0 +1,12 @@
{
"Logging": {
"LogLevel": {
"Default": "Information",
"Microsoft.AspNetCore": "Warning"
}
},
"AllowedHosts": "*",
"ConnectionStrings": {
"RabbitMQ": "amqp://guest:guest@localhost:5672"
}
}

25
WebAPITask/WebAPITask.sln Normal file
View file

@ -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}") = "WebAPITask", "WebAPITask\WebAPITask.csproj", "{8CADF513-C715-491D-9DEC-2AF374AFD1C8}"
EndProject
Global
GlobalSection(SolutionConfigurationPlatforms) = preSolution
Debug|Any CPU = Debug|Any CPU
Release|Any CPU = Release|Any CPU
EndGlobalSection
GlobalSection(ProjectConfigurationPlatforms) = postSolution
{8CADF513-C715-491D-9DEC-2AF374AFD1C8}.Debug|Any CPU.ActiveCfg = Debug|Any CPU
{8CADF513-C715-491D-9DEC-2AF374AFD1C8}.Debug|Any CPU.Build.0 = Debug|Any CPU
{8CADF513-C715-491D-9DEC-2AF374AFD1C8}.Release|Any CPU.ActiveCfg = Release|Any CPU
{8CADF513-C715-491D-9DEC-2AF374AFD1C8}.Release|Any CPU.Build.0 = Release|Any CPU
EndGlobalSection
GlobalSection(SolutionProperties) = preSolution
HideSolutionNode = FALSE
EndGlobalSection
GlobalSection(ExtensibilityGlobals) = postSolution
SolutionGuid = {5F225663-CD71-4874-ADD4-0A3B8D190870}
EndGlobalSection
EndGlobal

View file

@ -0,0 +1,114 @@
using Microsoft.AspNetCore.Mvc;
using System.ComponentModel.DataAnnotations;
namespace WebAPITask.Controllers
{
[ApiController]
[Route("api/messages")]
public class EmailController : ControllerBase
{
private static readonly IDictionary<string, int> emailMessageStats = new Dictionary<string, int>();
private readonly ILogger<EmailController> _logger;
public EmailController(ILogger<EmailController> logger)
{
_logger = logger;
}
/// <summary>
/// Sends a message to specified email addresses
/// </summary>
/// <param name="request">A list of emails and a message to send</param>
/// <response code="200">The message was successfully sent</response>
/// <response code="400">If one of the emails isn't a valid email address</response>
[HttpPost("send")]
public IActionResult SendMessages(SendEmailsRequestModel request)
{
_logger.LogInformation("Sending the message: {Message}...", request.Message);
foreach (var email in request.Emails)
{
_logger.LogInformation("Trying to send the message to {email}", email);
if (emailMessageStats.ContainsKey(email)) emailMessageStats[email]++;
else emailMessageStats[email] = 1;
_logger.LogInformation("Message sent successfully");
}
_logger.LogInformation("The message was successfully sent to all targets");
return Ok();
}
/// <summary>
/// Gets how many messages has every email address received
/// </summary>
/// <returns>Received messages statistic for every email address</returns>
/// <response code="200">The statistics were successfully fetched</response>
/// <response code="204">No one has received any messages at all</response>
[HttpGet("stat")]
public IActionResult GetMessageCountAll()
{
_logger.LogInformation("Getting the overall recieved messages statistics...");
var messageStats = new List<EmailStatResponseModel>();
foreach (var (email, msgCount) in emailMessageStats)
{
messageStats.Add(new() { Email = email, Message = msgCount });
}
_logger.LogInformation("The statistics were successfully fetched");
return messageStats.Count > 0 ? Ok(messageStats) : NoContent();
}
/// <summary>
/// Gets how many messages has a specified email address received
/// </summary>
/// <param name="email">The email address in question</param>
/// <returns>Amount of messages that were sent to specified email</returns>
/// <response code="200">The statistics were successfully fetched</response>
/// <response code="400">The specified email isn't a valid email address</response>
/// <response code="404">The specified email hasn't recieved any messages</response>
[HttpGet("count/{email}")]
public IActionResult GetMessageCount([EmailAddress] string email)
{
_logger.LogInformation("Getting the recieved messages statistics for {email}", email);
var retrieved = emailMessageStats.TryGetValue(email, out var result);
if (retrieved)
{
_logger.LogInformation("The statistics were successfully fetched");
return Ok(result);
}
_logger.LogInformation("The statistics for {email} were not found", email);
return NotFound();
}
public class SendEmailsRequestModel
{
[Required, EnumerableEmails]
public IEnumerable<string> Emails { get; set; }
[Required]
public string Message { get; set; }
}
public record EmailStatResponseModel
{
[Required, EmailAddress]
public string Email { get; set; }
[Required]
public int Message { get; set; }
}
public class EnumerableEmailsAttribute : ValidationAttribute
{
public override bool IsValid(object? value)
{
if (value == null) return false;
if (value is not IEnumerable<string> emails) return false;
var emailAttribute = new EmailAddressAttribute();
foreach (var email in emails)
{
if (!emailAttribute.IsValid(email))
{
return false;
}
}
return true;
}
}
}
}

View file

@ -0,0 +1,38 @@
using System.Reflection;
var builder = WebApplication.CreateBuilder(args);
// Add services to the container.
builder.Services.AddControllers();
// Learn more about configuring Swagger/OpenAPI at https://aka.ms/aspnetcore/swashbuckle
builder.Services.AddEndpointsApiExplorer();
builder.Services.AddSwaggerGen(options =>
{
options.SwaggerDoc("v1", new Microsoft.OpenApi.Models.OpenApiInfo
{
Version = "v1",
Title = "Email API",
Description = "Demo ASP.NET Core Web API for managing emails"
});
var xml = $"{Assembly.GetExecutingAssembly().GetName().Name}.xml";
options.IncludeXmlComments(Path.Combine(AppContext.BaseDirectory, xml));
});
var app = builder.Build();
// Configure the HTTP request pipeline.
if (app.Environment.IsDevelopment())
{
app.UseSwagger();
app.UseSwaggerUI();
}
app.UseHttpsRedirection();
app.UseAuthorization();
app.MapControllers();
app.Run();

View file

@ -0,0 +1,31 @@
{
"$schema": "https://json.schemastore.org/launchsettings.json",
"iisSettings": {
"windowsAuthentication": false,
"anonymousAuthentication": true,
"iisExpress": {
"applicationUrl": "http://localhost:28324",
"sslPort": 44341
}
},
"profiles": {
"WebAPITask": {
"commandName": "Project",
"dotnetRunMessages": true,
"launchBrowser": true,
"launchUrl": "swagger",
"applicationUrl": "https://localhost:7126;http://localhost:5126",
"environmentVariables": {
"ASPNETCORE_ENVIRONMENT": "Development"
}
},
"IIS Express": {
"commandName": "IISExpress",
"launchBrowser": true,
"launchUrl": "swagger",
"environmentVariables": {
"ASPNETCORE_ENVIRONMENT": "Development"
}
}
}
}

View file

@ -0,0 +1,15 @@
<Project Sdk="Microsoft.NET.Sdk.Web">
<PropertyGroup>
<TargetFramework>net6.0</TargetFramework>
<Nullable>enable</Nullable>
<ImplicitUsings>enable</ImplicitUsings>
<GenerateDocumentationFile>true</GenerateDocumentationFile>
<NoWarn>$(NoWarn);1591</NoWarn>
</PropertyGroup>
<ItemGroup>
<PackageReference Include="Swashbuckle.AspNetCore" Version="6.2.3" />
</ItemGroup>
</Project>

View file

@ -0,0 +1,8 @@
{
"Logging": {
"LogLevel": {
"Default": "Information",
"Microsoft.AspNetCore": "Warning"
}
}
}

View file

@ -0,0 +1,9 @@
{
"Logging": {
"LogLevel": {
"Default": "Information",
"Microsoft.AspNetCore": "Warning"
}
},
"AllowedHosts": "*"
}