Skip to content
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
31 changes: 31 additions & 0 deletions BlogIT.DB/BL/EmailService.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,31 @@
using MimeKit;
using MailKit.Net.Smtp;
using System.Threading.Tasks;

namespace BlogIT.DB.BL
{
public class EmailService : IEmailService
{
public async Task SendEmailAsync(string email, string subject, string message)
{
var emailMessage = new MimeMessage();

emailMessage.From.Add(new MailboxAddress("Администрация сайта", "JIeHIH12345@mail.ru"));
emailMessage.To.Add(new MailboxAddress("", email));
emailMessage.Subject = subject;
emailMessage.Body = new TextPart(MimeKit.Text.TextFormat.Html)
{
Text = message
};

using (var client = new SmtpClient())
{
await client.ConnectAsync("smtp.mail.ru", 25, false);
await client.AuthenticateAsync("JIeHIH12345@mail.ru", "*********");
await client.SendAsync(emailMessage);

await client.DisconnectAsync(true);
}
}
}
}
9 changes: 9 additions & 0 deletions BlogIT.DB/BL/IEmailService.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
using System.Threading.Tasks;

namespace BlogIT.DB.BL
{
public interface IEmailService
{
Task SendEmailAsync(string email, string subject, string message);
}
}
19 changes: 11 additions & 8 deletions BlogIT.DB/BL/INewsService.cs
Original file line number Diff line number Diff line change
@@ -1,24 +1,27 @@

using BlogIT.DB.Interfaces;
using BlogIT.DB.Models;
using System.Collections;
using System.Collections.Generic;
using System.Linq;
using System.Threading.Tasks;

namespace BlogIT.DB.BL
{
public interface INewsService
{
IQueryable<News> ListAll();
List<Category> GetCategories();
int AddNews(News news);
News GetNewsById(int id);
void DeleteNewsById(int id);
void AddMessageChat(ChatMessage chatMessage);
IQueryable<ChatMessage> GetChatMessagesByPartyId(int partyId);
IQueryable<News> GetLastNews(int count);
IQueryable<Tag> GetTags(string tag);
IQueryable<News> GetTopNews(int count);
IQueryable<News> ListActualNews(bool includeChatMessage = false);
List<ChatMessage> GetChatMessagesByPartyId(int partyId);
List<News> GetLastNews(int count);
List<Tag> GetTags(string tag);
List<News> GetTopNews(int count);
void UpdateNews(News news);
void SetRating(Rating rating);
int GetCurrentUserRating(int newsId, string userId);
List<string> GetTopTags();
Task<IReadOnlyList<News>> ListNewsAsync(ISpecification<News> spec);
Task<int> CountNewsAsync(ISpecification<News> spec);
}
}
115 changes: 91 additions & 24 deletions BlogIT.DB/BL/NewsService.cs
Original file line number Diff line number Diff line change
@@ -1,10 +1,13 @@
using BlogIT.DB.DAL;
using BlogIT.DB.Interfaces;
using BlogIT.DB.Models;
using BlogIT.DB.Specifications;
using Microsoft.EntityFrameworkCore;
using System;
using System.Collections;
using System.Collections.Generic;
using System.Linq;
using System.Threading.Tasks;

namespace BlogIT.DB.BL
{
Expand Down Expand Up @@ -51,56 +54,48 @@ public News GetNewsById(int id)
.SingleOrDefault(p => p.Id == id);
}

public IQueryable<News> ListAll()
{
return _context.News.Where(x => !x.Deleted);
}

public void AddMessageChat(ChatMessage chatMessage)
{
_context.ChatMessages.Add(chatMessage);
_context.SaveChanges();
}

public IQueryable<ChatMessage> GetChatMessagesByPartyId(int newsId)
public List<ChatMessage> GetChatMessagesByPartyId(int newsId)
{
return _context.ChatMessages
.Where(c => c.NewsId == newsId)
.Include(i => i.User)
.ThenInclude(i => i.Avatar)
.Include(i => i.Like);
.Include(i => i.Like)
.ToList();
}

public IQueryable<News> GetLastNews(int count)
public List<News> GetLastNews(int count)
{
return _context.News
.Where(x => x.DateTime <= DateTime.Now && !x.Deleted)
.OrderByDescending(s => s.DateTime)
.Take(count)
.Include(i => i.Category)
.Include(i => i.ChatMessages);
.Include(i => i.ChatMessages)
.ToList();
}

public IQueryable<News> GetTopNews(int count)
public List<News> GetTopNews(int count)
{
return _context.News
.OrderByDescending(p => p.RateAverage)
.ThenByDescending(p => p.RateCount)
.ThenByDescending(p => p.DateTime)
.Take(count)
.Include(i => i.Category);
}

public IQueryable<News> ListActualNews(bool includeChatMessage = false)
{
IQueryable<News> news = ListAll().Where(p => p.DateTime <= DateTime.Now);
if (includeChatMessage)
{
news = news.Include(i => i.ChatMessages);
}
return news;
.Include(i => i.Category)
.ToList();
}

public void UpdateNews(News news)
{
News updateNews = _context.News.Find(news.Id);
News updateNews = _context.News.Include(p => p.NewsTag).FirstOrDefault(p => p.Id == news.Id);

if (updateNews != null)
{
Expand All @@ -110,7 +105,7 @@ public void UpdateNews(News news)
updateNews.NewsText = news.NewsText;
updateNews.CategoryId = news.CategoryId;

if(updateNews.Tags != news.Tags)
if (updateNews.Tags != news.Tags)
{
updateNews.Tags = news.Tags;
UpdateNewsTags(updateNews);
Expand Down Expand Up @@ -148,9 +143,81 @@ private void UpdateNewsTags(News news)
};
}

public IQueryable<Tag> GetTags(string tag)
public List<Tag> GetTags(string tag)
{
return _context.Tags
.Where(p => p.Title.Contains(tag))
.Take(10)
.ToList();
}

public void SetRating(Rating rating)
{
Rating updateRating = _context.Ratings.FirstOrDefault(p => p.NewsId == rating.NewsId && p.UserId == rating.UserId);

if (updateRating != null)
{
updateRating.Rate = rating.Rate;
_context.Ratings.Update(updateRating);
}
else
{
_context.Ratings.Add(rating);
}
_context.SaveChanges();

UpdateNewsRating(rating.NewsId);

}

private void UpdateNewsRating(int newsId)
{

News news = _context.News.Include(p => p.Ratings).SingleOrDefault(p => p.Id == newsId);

if (news != null)
{
news.RateCount = news.Ratings.Count;
news.RateAverage = news.RateCount == 0 ? 0 : news.Ratings.Sum(m => m.Rate)/ news.RateCount;
_context.News.Update(news);
_context.SaveChanges();

}
}

public int GetCurrentUserRating(int newsId, string userId)
{
if(!String.IsNullOrEmpty(userId))
{
Rating rating = _context.Ratings.FirstOrDefault(p => p.NewsId == newsId && p.UserId == userId);

if (rating != null)
{
return rating.Rate;
}
}

return 0;
}

public List<string> GetTopTags()
{
return _context.Tags.Where(p => p.NewsTag.Count > 0).OrderByDescending(p => p.NewsTag.Count).Take(10).Select(p => p.Title).ToList();
}

public async Task<IReadOnlyList<News>> ListNewsAsync(ISpecification<News> spec)
{
return await ApplySpecification(spec).ToListAsync();
}

public async Task<int> CountNewsAsync(ISpecification<News> spec)
{
return await ApplySpecification(spec).CountAsync();
}

private IQueryable<News> ApplySpecification(ISpecification<News> spec)
{
return _context.Tags.Where(p => p.Title.Contains(tag)).Take(10);
return SpecificationEvaluator<News>.GetQuery(_context.News.AsQueryable(), spec);
}
}
}
1 change: 1 addition & 0 deletions BlogIT.DB/BlogIT.DB.csproj
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,7 @@
</ItemGroup>

<ItemGroup>
<PackageReference Include="MailKit" Version="2.5.0" />
<PackageReference Include="Microsoft.AspNetCore.Identity" Version="2.2.0" />
<PackageReference Include="Microsoft.AspNetCore.Identity.EntityFrameworkCore" Version="3.1.0" />
<PackageReference Include="Microsoft.EntityFrameworkCore" Version="3.1.0" />
Expand Down
25 changes: 24 additions & 1 deletion BlogIT.DB/DAL/BlogITContext.cs
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,7 @@ public BlogITContext(DbContextOptions<BlogITContext> options)
public DbSet<Tag> Tags { get; set; }
public DbSet<NewsTag> NewsTags { get; set; }
public DbSet<Like> Likes { get; set; }
public DbSet<Rating> Ratings { get; set; }

protected override void OnModelCreating(ModelBuilder modelBuilder)
{
Expand All @@ -33,6 +34,7 @@ protected override void OnModelCreating(ModelBuilder modelBuilder)
modelBuilder.ApplyConfiguration(new TagsConfiguration());
modelBuilder.ApplyConfiguration(new NewsTagsConfiguration());
modelBuilder.ApplyConfiguration(new LikeConfiguration());
modelBuilder.ApplyConfiguration(new RatingConfiguration());

Initialization(modelBuilder);
}
Expand Down Expand Up @@ -94,6 +96,9 @@ public void Configure(EntityTypeBuilder<User> builder)
{
builder.Property(p => p.Sex).IsRequired();
builder.Property(p => p.Birthday).IsRequired();
builder.Property(p => p.Description).IsRequired(false).HasMaxLength(255);
builder.Property(p => p.ShortDescription).IsRequired(false).HasMaxLength(1024);
builder.Property(p => p.DateOfRegistration).IsRequired();
builder.HasOne(p => p.Avatar).WithOne().HasForeignKey<User>(p => p.AvatarId);
}
}
Expand All @@ -118,6 +123,8 @@ public void Configure(EntityTypeBuilder<News> builder)
builder.Property(p => p.Tags).IsRequired().HasMaxLength(1024);
builder.Property(p => p.NewsText).IsRequired().HasMaxLength(10240);
builder.Property(p => p.Deleted).IsRequired().HasDefaultValue(false);
builder.Property(p => p.RateCount).IsRequired().HasDefaultValue(0);
builder.Property(p => p.RateAverage).IsRequired().HasDefaultValue(0);
builder.HasOne(p => p.Category)
.WithMany(t => t.News)
.HasForeignKey(p => p.CategoryId);
Expand Down Expand Up @@ -186,6 +193,22 @@ public void Configure(EntityTypeBuilder<Like> builder)
.WithMany(t => t.Like)
.HasForeignKey(p => p.ChatMessageId);
}

}
}

public class RatingConfiguration : IEntityTypeConfiguration<Rating>
{
public void Configure(EntityTypeBuilder<Rating> builder)
{
builder.HasKey(p => p.Id);
builder.Property(p => p.Rate).IsRequired().HasDefaultValue(0);
builder.HasOne(p => p.User)
.WithMany(t => t.Ratings)
.HasForeignKey(p => p.UserId);
builder.HasOne(p => p.News)
.WithMany(t => t.Ratings)
.HasForeignKey(p => p.NewsId);
}
}

}
21 changes: 21 additions & 0 deletions BlogIT.DB/Interfaces/ISpecification.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,21 @@
using System;
using System.Collections.Generic;
using System.Linq.Expressions;
using System.Text;

namespace BlogIT.DB.Interfaces
{
public interface ISpecification<T>
{
Expression<Func<T, bool>> Criteria { get; }
List<Expression<Func<T, object>>> Includes { get; }
List<string> IncludeStrings { get; }
Expression<Func<T, object>> OrderBy { get; }
Expression<Func<T, object>> OrderByDescending { get; }
Expression<Func<T, object>> GroupBy { get; }

int Take { get; }
int Skip { get; }
bool IsPagingEnabled { get; }
}
}
Loading