Skip to content
This repository has been archived by the owner on Jul 8, 2024. It is now read-only.

FAQ & Home Page #1

Open
wants to merge 24 commits into
base: main
Choose a base branch
from
Open
Changes from 1 commit
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
Prev Previous commit
Next Next commit
news + resolved auth
  • Loading branch information
Eduard Cristea committed Jan 30, 2024
commit b5b24b5215ea933533363d7620f5b0263ab8b474
46 changes: 46 additions & 0 deletions Emulair.BusinessLogic/Implementation/News/Mappings/NewsMapper.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,46 @@
using AutoMapper;
using Emulair.BusinessLogic.Implementation.News.Models;
using Emulair.Entities.Entities;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;

namespace Emulair.BusinessLogic.Implementation.News.Mappings
{
public class NewsMapper : Profile
{
public NewsMapper()
{
CreateMap<News1, NewsModel>()
.ForMember(a => a.NewsId, a => a.MapFrom(s => s.NewsId))
.ForMember(a => a.AuthorId, a => a.MapFrom(s => s.AuthorId))
.ForMember(a => a.Description, a => a.MapFrom(s => s.Description))
.ForMember(a => a.PostDate, a => a.MapFrom(s => s.PostDate))
.ForMember(a => a.Title, a => a.MapFrom(s => s.Title));

CreateMap<News1, EditNewsModel>()
.ForMember(a => a.Description, a => a.MapFrom(s => s.Description))
.ForMember(a => a.Title, a => a.MapFrom(s => s.Title));

CreateMap<EditNewsModel, NewsModel>()
.ForMember(a => a.AuthorId, a => a.Ignore())
.ForMember(a => a.Description, a => a.MapFrom(s => s.Description))
.ForMember(a => a.PostDate, a => a.Ignore())
.ForMember(a => a.Title, a => a.MapFrom(s => s.Title))
.ForMember(a => a.Replies, a => a.Ignore())
.ForMember(a => a.NewComment, a => a.Ignore())
.ForMember(a => a.Comments, a => a.Ignore())
.ForMember(a => a.Images, a => a.Ignore());


CreateMap<NewsModel, News1>()
.ForMember(a => a.NewsId, a => a.MapFrom(s => s.NewsId))
.ForMember(a => a.AuthorId, a => a.MapFrom(s => s.AuthorId))
.ForMember(a => a.Description, a => a.MapFrom(s => s.Description))
.ForMember(a => a.PostDate, a => a.MapFrom(s => s.PostDate))
.ForMember(a => a.Title, a => a.MapFrom(s => s.Title));
}
}
}
15 changes: 15 additions & 0 deletions Emulair.BusinessLogic/Implementation/News/Models/EditNewsModel.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,15 @@
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;

namespace Emulair.BusinessLogic.Implementation.News.Models
{
public class EditNewsModel
{
public Guid NewsId { get; set; }
public string Title { get; set; }
public string Description { get; set; }
}
}
35 changes: 35 additions & 0 deletions Emulair.BusinessLogic/Implementation/News/Models/NewsModel.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,35 @@
using Emulair.Entities.Entities;
using Microsoft.AspNetCore.Http;
using System;
using System.Collections.Generic;
using System.ComponentModel.DataAnnotations;
using System.Linq;
using System.Text;
using System.Threading.Tasks;

namespace Emulair.BusinessLogic.Implementation.News.Models
{
public class NewsModel
{
public Guid NewsId { get; set; }
[Required(ErrorMessage = "Please enter the Title.")]
public string Title { get; set; } = null!;
[Required(ErrorMessage = "Please enter the Description.")]
public string Description { get; set; } = null!;
public Guid AuthorId { get; set; }
public string Author { get; set; }

public DateTime PostDate { get; set; }
public List<byte[]> Images { get; set; }
public List<Comment> Comments { get; set; }
public Comment NewComment { get; set; }
public List<Comment> Replies { get; set; }
public List<IFormFile> formFiles { get; set; }
public NewsModel()
{
Images = new List<byte[]>();
Comments = new List<Comment>();
Replies = new List<Comment>();
}
}
}
200 changes: 200 additions & 0 deletions Emulair.BusinessLogic/Implementation/News/NewsService.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,200 @@
using AutoMapper;
using Emulair.BusinessLogic.Base;
using Emulair.BusinessLogic.Implementation.News.Models;
using Emulair.BusinessLogic.Implementation.News.Validations;
using Emulair.Common.Exceptions;
using Emulair.Common.Extensions;
using Emulair.DataAccess;
using Emulair.Entities.Entities;
using EmulairWEB.Models;
using Microsoft.EntityFrameworkCore;
using Microsoft.Extensions.Configuration;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;

namespace Emulair.BusinessLogic.Implementation.News
{
public class NewsService : BaseService
{
private readonly CreateNewsValidator CreateNewsValidator;
private readonly EditNewsValidator EditNewsValidator;
private readonly AddCommentValidator AddCommentValidator;
public NewsService(ServiceDependencies serviceDependencies, IConfiguration configuration) : base(serviceDependencies)
{

this.CreateNewsValidator = new CreateNewsValidator(serviceDependencies.UnitOfWork);
this.EditNewsValidator = new EditNewsValidator(serviceDependencies.UnitOfWork);
this.AddCommentValidator = new AddCommentValidator();
}
public List<NewsModel> DisplayNews()
{
var newsList = new List<NewsModel>();
var news = UnitOfWork.News.Get().Where(n => n.IsDeleted == false).ToList();
var comments = UnitOfWork.Comments.Get().ToList();
foreach (var n in news)
{
var newsImages = UnitOfWork.NewsImages
.Where(ne => ne.NewsId == n.NewsId)
.Include(ne => ne.Image)
.ToList();

var imageIds = newsImages.Select(ne => ne.ImageId).ToList();

var listImages = UnitOfWork.Images
.Where(image => imageIds.Contains(image.ImageId))
.Select(image => image.Content)
.ToList();

var newsComments = new List<Comment>();
foreach (var c in comments)
{
newsComments.Add(c);
}

var newNews = Mapper.Map<News1, NewsModel>(n);
newNews.Comments = newsComments;
newNews.Images = listImages;
newNews.Author = UnitOfWork.Users.Find(newNews.AuthorId).FirstName + " " + UnitOfWork.Users.Find(newNews.AuthorId).LastName;
newsList.Add(newNews);
}
return newsList;
}

public List<NewsModel> GetLastestNews(int count)
{
var lastNews = new List<NewsModel>();
lastNews = UnitOfWork.News.Get().Where(n => n.IsDeleted == false).OrderByDescending(n => n.PostDate).Select(n => Mapper.Map<News1, NewsModel>(n)).Take(count).ToList();
return lastNews;
}
public void CreateNews(NewsModel newsModel, List<byte[]> imageBytesList)
{

newsModel.NewsId = Guid.NewGuid();
CreateNewsValidator.Validate(newsModel).ThenThrow(newsModel);

var news = new News1
{
NewsId = newsModel.NewsId,
AuthorId = CurrentUser.Id,
Description = newsModel.Description,
PostDate = DateTime.Now,
Title = newsModel.Title
};

UnitOfWork.News.Insert(news);
foreach (var imageBytes in imageBytesList)
{
if (imageBytes.Length > 0)
{
var image = new Image { ImageId = Guid.NewGuid(), Content = imageBytes };
UnitOfWork.Images.Insert(image);
var newsImage = new NewsImage { NewsImageId = Guid.NewGuid(), NewsId = news.NewsId, ImageId = image.ImageId };
UnitOfWork.NewsImages.Insert(newsImage);
}
}

UnitOfWork.SaveChanges();

}
public NewsModel GetNewsById(Guid newsId)
{
if (newsId == null)
{
throw new NotFoundErrorException();
}
var news = UnitOfWork.News.Find(newsId);
if (news == null)
{
throw new NotFoundErrorException();
}
var comments = UnitOfWork.Comments.Get().Where(c => c.NewsId == newsId).ToList();
var newsModel = Mapper.Map<News1, NewsModel>(news);
newsModel.Comments = comments;
newsModel.Author = UnitOfWork.Users.Find(newsModel.AuthorId).FirstName + " " + UnitOfWork.Users.Find(newsModel.AuthorId).LastName;
var existingImages = GetNewsImages(newsId);
newsModel.Images = existingImages;
return newsModel;
}

public News1 EditNews(NewsModel newsModel, List<byte[]> imageBytesList)
{
EditNewsValidator.Validate(newsModel).ThenThrow(newsModel);
var news = UnitOfWork.News.Get()
.FirstOrDefault(n => n.NewsId == newsModel.NewsId)
;
if (news == null)
{
throw new NotFoundErrorException();
}
news.Title = newsModel.Title;
news.Description = newsModel.Description;

if (imageBytesList != null && imageBytesList.Any())
{
var existingImages = UnitOfWork.NewsImages.Where(pi => pi.NewsId == news.NewsId).ToList();

foreach (var existingImage in existingImages)
{
UnitOfWork.NewsImages.Delete(existingImage);
}

foreach (var imageBytes in imageBytesList)
{
if (imageBytes.Length > 0)
{
var image = new Image { ImageId = Guid.NewGuid(), Content = imageBytes };
UnitOfWork.Images.Insert(image);
var newsImage = new NewsImage { NewsImageId = Guid.NewGuid(), NewsId = news.NewsId, ImageId = image.ImageId };
UnitOfWork.NewsImages.Insert(newsImage);
}
}
}
UnitOfWork.News.Update(news);
UnitOfWork.SaveChanges();
return news;
}

public void DeleteNews(Guid id)
{
var news = UnitOfWork.News.Find(id);
if (news != null)
{
news.IsDeleted = true;
UnitOfWork.News.Update(news);
}
else
{
throw new NotFoundErrorException();
}

UnitOfWork.SaveChanges();
}

public List<byte[]> GetNewsImages(Guid newsId)
{
var images = UnitOfWork.NewsImages.Where(ni => ni.NewsId == newsId)
.Select(ni => ni.Image.Content)
.ToList();


return images;
}

public void AddCommentToNews(Guid newsId, string comment)
{
var com = new Comment();
com.NewsId = newsId;
com.AuthorId = CurrentUser.Id;
com.CommentId = Guid.NewGuid();
com.ParentCommentId = com.CommentId;
com.PostDate = DateTime.Now;
com.Message = comment;
AddCommentValidator.Validate(com).ThenThrow(com);
UnitOfWork.Comments.Insert(com);
UnitOfWork.SaveChanges();
}
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,21 @@
using Emulair.Entities.Entities;
using FluentValidation;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;

namespace Emulair.BusinessLogic.Implementation.News.Validations
{
public class AddCommentValidator : AbstractValidator<Comment>
{
public AddCommentValidator()
{
RuleFor(r => r.Message)
.NotEmpty().WithMessage("Camp obligatoriu!")
.Length(5, 500).WithMessage("Comment need to have atleast 5 letters and maximum 500");
}

}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,35 @@
using Emulair.BusinessLogic.Implementation.News.Models;
using Emulair.DataAccess;
using FluentValidation;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;

namespace Emulair.BusinessLogic.Implementation.News.Validations
{
public class CreateNewsValidator : AbstractValidator<NewsModel>
{
private UnitOfWork _unitOfWork;
public CreateNewsValidator(UnitOfWork unitOfWork)
{
_unitOfWork = unitOfWork;
RuleFor(r => r.Title)
.NotEmpty().WithMessage("Camp obligatoriu!")
.Must(NotAlreadyExist).WithMessage("There is already an existing news with this title!")
.Length(3, 100).WithMessage("Title must have atleast 3 letters and 100 maximum!");
RuleFor(r => r.Description)
.NotEmpty().WithMessage("Camp obligatoriu!")
.Length(3, 5000).WithMessage("Description must have atleast 3 letters and maximum 5000");

}
public bool NotAlreadyExist(string Title)
{
var mail = _unitOfWork.News.Where(u => u.Title == Title).FirstOrDefault();
if (mail == null)
return true;
return false;
}
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,27 @@
using Emulair.BusinessLogic.Implementation.News.Models;
using Emulair.DataAccess;
using FluentValidation;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;

namespace Emulair.BusinessLogic.Implementation.News.Validations
{
public class EditNewsValidator : AbstractValidator<NewsModel>
{
private UnitOfWork _unitOfWork;
public EditNewsValidator(UnitOfWork unitOfWork)
{
_unitOfWork = unitOfWork;
RuleFor(r => r.Title)
.NotEmpty().WithMessage("Camp obligatoriu!")
.Length(3, 100).WithMessage("Title must have atleast 3 letters and 100 maximum!");
RuleFor(r => r.Description)
.NotEmpty().WithMessage("Camp obligatoriu!")
.Length(3, 5000).WithMessage("Description must have atleast 3 letters and maximum 5000");

}
}
}
1 change: 1 addition & 0 deletions Emulair.Common/Emulair.Common.csproj
Original file line number Diff line number Diff line change
@@ -12,6 +12,7 @@

<ItemGroup>
<PackageReference Include="FluentValidation" Version="11.8.1" />
<PackageReference Include="Microsoft.AspNetCore.Http.Features" Version="5.0.17" />
</ItemGroup>

</Project>
Loading