Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Кочергин, Заикин, Ковальчук ФТ-402 #35

Open
wants to merge 1 commit into
base: main
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
57,076 changes: 49,532 additions & 7,544 deletions BadNews/.db/news.txt

Large diffs are not rendered by default.

1 change: 1 addition & 0 deletions BadNews/BadNews.csproj
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@

<ItemGroup>
<PackageReference Include="Microsoft.AspNetCore.Mvc.Razor.RuntimeCompilation" Version="5.0.3" />
<PackageReference Include="Microsoft.AspNetCore.SignalR.Client" Version="5.0.4" />
<PackageReference Include="Newtonsoft.Json" Version="12.0.3" />
<PackageReference Include="Serilog.AspNetCore" Version="3.4.0" />
</ItemGroup>
Expand Down
71 changes: 71 additions & 0 deletions BadNews/BlazorComponents/Comments.razor
Original file line number Diff line number Diff line change
@@ -0,0 +1,71 @@
@using BadNews.Repositories.Comments
@using Microsoft.AspNetCore.SignalR.Client
@inject CommentsRepository commentsRepository;
@inject NavigationManager navigationManager
@implements IAsyncDisposable
<div>
<label>
Пользователь: <input @bind="userInput"/>
</label>
<label>
Сообщение: <input @bind="messageInput"/>
</label>
<div>
<button @onclick="Send">Отправить</button>
</div>
@foreach (var comment in comments)
{
<li>@RenderComment(comment)</li>
}
</div>

@code {
private HubConnection hubConnection;
private List<Comment> comments = new();

private string userInput;
private string messageInput;

[Parameter]
public Guid ArticleId { get; set; }

public override Task SetParametersAsync(ParameterView parameters)
{
comments = commentsRepository.GetComments(ArticleId).ToList();
return base.SetParametersAsync(parameters);
}

protected override async Task OnInitializedAsync()
{
hubConnection = new HubConnectionBuilder()
.WithUrl(navigationManager.ToAbsoluteUri("/commentsHub"))
.Build();

hubConnection.On<string, string>("ReceiveComment", (user, message) =>
{
var comment = new Comment(user, message);
comments.Add(comment);
StateHasChanged();
});

await hubConnection.StartAsync().ConfigureAwait(false);
}

private async Task Send()
{
if (hubConnection is not null)
{
await hubConnection.SendAsync("SendComment", userInput, messageInput);
}
}

public async ValueTask DisposeAsync()
{
if (hubConnection is not null)
{
await hubConnection.DisposeAsync();
}
}

private string RenderComment(Comment comment) => $"{comment.User} говорит: {comment.Value}";
}
26 changes: 26 additions & 0 deletions BadNews/Controllers/CommentsController.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,26 @@
using System;
using System.Linq;
using BadNews.Models.Comments;
using BadNews.Repositories.Comments;
using Microsoft.AspNetCore.Mvc;

namespace BadNews.Controllers
{
[ApiController]
public class CommentsController : ControllerBase
{
private readonly CommentsRepository commentsRepository;

public CommentsController(CommentsRepository commentsRepository)
{
this.commentsRepository = commentsRepository;
}

// GET
[HttpGet("api/news/{id}/comments")]
public ActionResult<CommentsDto> GetCommentsForNews(Guid newsId)
{
return Ok(commentsRepository.GetComments(newsId));
}
}
}
13 changes: 13 additions & 0 deletions BadNews/Hubs/CommentsHub.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,13 @@
using System.Threading.Tasks;
using Microsoft.AspNetCore.SignalR;

namespace BadNews.Hubs
{
public class CommentsHub : Hub
{
public async Task SendComment(string user, string message)
{
await Clients.All.SendAsync("ReceiveComment", user, message).ConfigureAwait(false);
}
}
}
9 changes: 9 additions & 0 deletions BadNews/Models/Comments/CommentDto.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
namespace BadNews.Models.Comments
{
public class CommentDto
{
public string User { get; set; }

public string Value { get; set; }
}
}
13 changes: 13 additions & 0 deletions BadNews/Models/Comments/CommentsDto.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,13 @@
using System;
using System.Collections.Generic;
using BadNews.Repositories.Comments;

namespace BadNews.Models.Comments
{
public class CommentsDto
{
public Guid NewsId { get; set; }

public IReadOnlyCollection<CommentDto> Comments { get; set; }
}
}
21 changes: 9 additions & 12 deletions BadNews/Startup.cs
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,9 @@
using Microsoft.Extensions.Hosting;
using Serilog;
using System;
using BadNews.Hubs;
using BadNews.Models.Comments;
using BadNews.Repositories.Comments;

namespace BadNews
{
Expand All @@ -30,6 +33,9 @@ public Startup(IWebHostEnvironment env, IConfiguration configuration)
// В этом методе добавляются сервисы в DI-контейнер
public void ConfigureServices(IServiceCollection services)
{
services.AddSingleton<CommentsRepository, CommentsRepository>();
services.AddSignalR();
services.AddServerSideBlazor();
services.AddSingleton<INewsRepository, NewsIndexedRepository>();
services.AddSingleton<INewsModelBuilder, NewsModelBuilder>();
services.AddSingleton<IValidationAttributeAdapterProvider, StopWordsAttributeAdapterProvider>();
Expand All @@ -55,18 +61,7 @@ public void Configure(IApplicationBuilder app)

app.UseHttpsRedirection();
app.UseResponseCompression();
app.UseStaticFiles(new StaticFileOptions()
{
OnPrepareResponse = options =>
{
options.Context.Response.GetTypedHeaders().CacheControl =
new Microsoft.Net.Http.Headers.CacheControlHeaderValue()
{
Public = false,
MaxAge = TimeSpan.FromDays(1)
};
}
});
app.UseStaticFiles();
app.UseSerilogRequestLogging();
app.UseStatusCodePagesWithReExecute("/StatusCode/{0}");

Expand All @@ -80,6 +75,8 @@ public void Configure(IApplicationBuilder app)
action = "StatusCode"
});
endpoints.MapControllerRoute("default", "{controller=News}/{action=Index}/{id?}");
endpoints.MapHub<CommentsHub>("/commentsHub");
endpoints.MapBlazorHub();
});
app.MapWhen(context => context.Request.IsElevated(), branchApp =>
{
Expand Down
8 changes: 7 additions & 1 deletion BadNews/Views/News/FullArticle.cshtml
Original file line number Diff line number Diff line change
@@ -1,4 +1,6 @@
@using BadNews.Elevation
@using BadNews.BlazorComponents
@using BadNews.Components
@using BadNews.Elevation
@model BadNews.Models.News.FullArticleModel

<main role="main" class="container">
Expand All @@ -20,6 +22,10 @@
</div>
</div>

<div id="comments">
@(await Html.RenderComponentAsync<Comments>(RenderMode.ServerPrerendered))
</div>

<aside class="col-md-4 news-sidebar">
<vc:weather></vc:weather>
<vc:archive-links></vc:archive-links>
Expand Down
35 changes: 18 additions & 17 deletions BadNews/Views/Shared/_Layout.cshtml
Original file line number Diff line number Diff line change
Expand Up @@ -13,25 +13,26 @@
<link rel="stylesheet" href="/css/site.css" asp-append-version="true" />
</head>
<body>
<div class="container">
<header class="news-header">
<a class="news-header-logo text-dark" href="/">Bad News</a>
@if (ViewContext.IsElevated())
{
<span class="text-primary ml-2">есть привилегии</span>
}
</header>
<div class="container">
<header class="news-header">
<a class="news-header-logo text-dark" href="/">Bad News</a>
@if (ViewContext.IsElevated())
{
<span class="text-primary ml-2">есть привилегии</span>
}
</header>

<div class="nav-scroller py-1 mb-2"></div>
<div class="nav-scroller py-1 mb-2"></div>

@RenderSection("Header", false)
</div>
@RenderSection("Header", false)
</div>

@RenderBody()

<script src="/lib/jquery/dist/jquery.min.js" asp-append-version="true"></script>
<script src="/lib/bootstrap/dist/js/bootstrap.bundle.js" asp-append-version="true"></script>
<script src="/js/site.js" asp-append-version="true"></script>
<partial name="_ValidationScriptsPartial" />
@RenderBody()
<base href="~/" />
<script src="/lib/jquery/dist/jquery.min.js" asp-append-version="true"></script>
<script src="/lib/bootstrap/dist/js/bootstrap.bundle.js" asp-append-version="true"></script>
<script src="/js/site.js" asp-append-version="true"></script>
<script src="_framework/blazor.server.js"></script>
<partial name="_ValidationScriptsPartial" />
</body>
</html>
5 changes: 5 additions & 0 deletions BadNews/_Imports.razor
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
@using Microsoft.AspNetCore.Authorization;
@using Microsoft.AspNetCore.Components.Authorization;
@using Microsoft.AspNetCore.Components.Forms;
@using Microsoft.AspNetCore.Components.Routing;
@using Microsoft.AspNetCore.Components.Web;