Complaints working, web comments response page, filtering for announcements

This commit is contained in:
Dimitar Byalkov
2023-06-09 04:57:28 +02:00
parent d8e185757d
commit d81450ed21
20 changed files with 336 additions and 48 deletions

View File

@@ -0,0 +1,35 @@
@page
@using Models;
@model WebApp.Pages.AddCommentModel
@{
ViewData["Title"] = "Add comment";
GenericMessage parentMessage = (GenericMessage)ViewData["parent"];
}
<h1>@ViewData["Title"]</h1>
<h2>Responding to:</h2>
<div class="card" style="display:inline-flex; width: 18rem;">
<div class="card-body">
<h5 class="card-title">
@parentMessage.Title
</h5>
<h6 class="card-subtitle mb-2 text-muted">@parentMessage.Author.Name</h6>
<p class="card-text">@parentMessage.Description.PadRight(100).Substring(0,100).Trim()</p>
</div>
</div>
<form method="post">
<div class="mb-3">
<label asp-for="Comment.Title" class="form-label">Title: </label>
<input asp-for="Comment.Title" class="form-control" />
</div>
<div class="mb-3">
<label asp-for="Comment.Description" class="form-label">Description: </label>
<textarea asp-for="Comment.Description" class="form-control" rows="5"></textarea>
</div>
<input type="hidden" asp-for="Type" />
<input type="hidden" asp-for="ParentId" />
<input type="hidden" asp-for="PreviousPage" />
<input type="submit" value="Create" class="btn btn-primary" />
</form>

View File

@@ -0,0 +1,82 @@
using Logic;
using Microsoft.AspNetCore.Identity;
using Microsoft.AspNetCore.Mvc;
using Microsoft.AspNetCore.Mvc.RazorPages;
using Models;
using System.Security.Claims;
namespace WebApp.Pages
{
public class AddCommentModel : PageModel
{
[BindProperty]
public Comment Comment { get; set; }
[BindProperty]
public string Type { get; set; }
[BindProperty]
public int ParentId { get; set; }
[BindProperty]
public string PreviousPage { get; set; }
private readonly IAnnouncementRepository _announcementRepository;
private readonly ICommentRepository _commentRepository;
private readonly IComplaintRepository _complaintRepository;
private readonly IUserRepository _userRepository;
public AddCommentModel(ICommentRepository commentRepository, IAnnouncementRepository announcementRepository, IComplaintRepository complaintRepository, IUserRepository userRepository)
{
_announcementRepository = announcementRepository;
_commentRepository = commentRepository;
_complaintRepository = complaintRepository;
_userRepository = userRepository;
}
public void OnGet(string t, int id)
{
AnnouncementManager announcementManager = new AnnouncementManager(_announcementRepository);
CommentManager commentManager = new CommentManager(_commentRepository);
ComplaintManager complaintManager = new ComplaintManager(_complaintRepository);
Type = t;
ParentId = id;
PreviousPage = Request.Headers["Referer"].ToString();
switch (Type)
{
case "announcement":
ViewData["parent"] = announcementManager.GetAnnouncementById(ParentId);
break;
case "comment":
ViewData["parent"] = commentManager.GetCommentById(ParentId);
break;
case "complaint":
ViewData["parent"] = complaintManager.GetComplaintById(ParentId);
break;
default:
break;
}
}
public RedirectResult OnPost()
{
CommentManager commentManager = new CommentManager(_commentRepository);
UserManager userManager = new UserManager(_userRepository);
User currentUser = userManager.GetUserById(int.Parse(User.FindFirstValue("id")));
switch (Type)
{
case "announcement":
commentManager.CreateCommentOnAnnouncement(currentUser, Comment.Description, Comment.Title, DateTime.Now, ParentId);
break;
case "comment":
commentManager.CreateResponseOnComment(currentUser, Comment.Description, Comment.Title, DateTime.Now, ParentId);
break;
case "complaint":
commentManager.CreateCommentOnComplaint(currentUser, Comment.Description, Comment.Title, DateTime.Now, ParentId);
break;
default:
break;
}
return Redirect(PreviousPage);
}
}
}

View File

@@ -30,6 +30,7 @@
<p>@Html.Raw(announcement.Description.Replace(Environment.NewLine, "<br />"))</p>
<br/>
<h3>Comments</h3>
<a href="./AddComment?t=announcement&id=@announcement.ID" class="btn btn-primary">Add comment</a>
@if (announcement.Comments.Count() == 0)
{
<p>No comments found</p>
@@ -55,9 +56,7 @@ else
<h5 class="card-title">@comment.Author.Name @Html.Raw((comment.Author.Role == UserRole.ADMIN || comment.Author.Role == UserRole.MANAGER) ? $"<b>({CultureInfo.CurrentCulture.TextInfo.ToTitleCase(comment.Author.Role.ToString().ToLower())})</b>" : "")</h5>
<h6>@comment.PublishDate.ToString("g")</h6>
<p class="card-text">@Html.Raw(comment.Description.Replace(Environment.NewLine, "<br />"))</p>
<a class="btn btn-outline-success" href="#">Like</a>
<a class="btn btn-outline-danger" href="#">Dislike</a>
<a class="btn btn-outline-primary" href="#">Reply</a>
<a class="btn btn-outline-primary" href="./AddComment?t=comment&id=@comment.ID">Reply</a>
</div>
@if (comment.Responses.Count != 0)
{

View File

@@ -19,7 +19,7 @@ namespace WebApp.Pages
public void OnGet(int id)
{
AnnouncementManager announcementManager = new AnnouncementManager(_announcementRepository);
ViewData.Add("announcement", announcementManager.GetAllAnnouncements().First(x => x.ID == id));
ViewData.Add("announcement", announcementManager.GetAnnouncementById(id));
}
}
}

View File

@@ -11,18 +11,29 @@
currentPage = Convert.ToInt32(ViewData["page"]);
}
}
<a href="./CreateAnnouncement" class="btn btn-primary">Create new announcement</a>
<form method="get">
<div class="form-actions no-color">
<p>
<input type="hidden" name="handler" value="search" />
<input type="text" name="s" />
<input type="submit" asp-page-handler="Search" value="Search" class="btn btn-default" />
</p>
</div>
</form>
<div id="functions">
<a href="./CreateAnnouncement" class="btn btn-primary">Create new announcement</a>
<form class="card" method="get">
<div class="card-body">
<input type="hidden" name="handler" value="filter" />
<input type="text" name="s" />
<div class="form-check form-check-inline">
<label class="form-check-label" for="asc">Sort by ascending order</label>
<input class="form-check-input" type="radio" name="asc" value="true" />
</div>
<div class="form-check form-check-inline">
<label class="form-check-label" for="des">Sort by descending order</label>
<input class="form-check-input" type="radio" name="des" value="true" />
</div>
<div class="form-check form-check-inline">
<label class="form-check-label" for="imp">Only important</label>
<input class="form-check-input" type="checkbox" name="imp" value="true" />
</div>
<input type="submit" asp-page-handler="Filter" value="Filter" class="btn btn-default" role="button" />
</div>
</form>
</div>
<div class="mb-3 mt-3">
@foreach (Announcement announcement in announcements)

View File

@@ -34,10 +34,31 @@ namespace WebApp.Pages
ViewData.Add("count", c);
ViewData.Add("allCount", AnnouncementManager.GetAllAnnouncements().Count);
}
public void OnGetSearch(string s) // search
public void OnGetFilter(string s, bool asc, bool des, bool imp) // search, ascending, descending order, isImportant
{
AnnouncementManager = new AnnouncementManager(_announcementRepository);
ViewData.Add("announcements", AnnouncementManager.SearchAnnouncements(s));
List<Announcement> announcements = new List<Announcement>();
if (!string.IsNullOrEmpty(s))
{
announcements = AnnouncementManager.SearchAnnouncements(s);
}
else
{
announcements = AnnouncementManager.GetAllAnnouncements();
}
if (imp)
{
announcements = announcements.Where(x => x.IsImportant).ToList();
}
if (asc)
{
announcements = announcements.OrderBy(x => x.PublishDate).ToList();
}
else if (des)
{
announcements = announcements.OrderByDescending(x => x.PublishDate).ToList();
}
ViewData.Add("announcements", announcements);
}
}
}

View File

@@ -0,0 +1,71 @@
@page
@using Models;
@using System.Globalization
@using System.Security.Claims;
@model WebApp.Pages.ComplaintModel
@{
Complaint complaint = (Complaint)ViewData["complaint"];
ViewData["Title"] = $"{complaint.Title}";
}
<h1 @if (complaint.Severity == ComplaintSeverity.URGENT)
{
@: style="color: red;"
}
else if (complaint.Severity == ComplaintSeverity.HIGH)
{
@: style="color: orange;"
}
@if (complaint.Severity == ComplaintSeverity.LOW)
{
@: style="color: darkgreen;"
}>
@complaint.Title
</h1>
<p>
Filed @complaint.PublishDate.ToString("g") by @complaint.Author.Name
</p>
<p>
@complaint.Status - @complaint.Severity
</p>
<hr/>
<p>@Html.Raw(complaint.Description.Replace(Environment.NewLine, "<br />"))</p>
<br/>
<h3>Comments</h3>
@if (complaint.Responses.Count() == 0)
{
<p>No comments found</p>
}
else
{
foreach (Comment comment in complaint.Responses)
{
DisplayComment(comment, 0);
}
}
<a href="./AddComment?t=complaint&id=@complaint.ID" class="btn btn-primary">Add reply</a>
@{
void DisplayComment(Comment comment, int level)
{
<div class="d-flex flex-row">
@for (int i = 0; i < level; i++)
{
<a class="me-3" href="#"></a>
}
<div class="card flex-fill">
<div class="card-body">
<h5 class="card-title">@comment.Author.Name @Html.Raw((comment.Author.Role == UserRole.ADMIN || comment.Author.Role == UserRole.MANAGER) ? $"<b>({CultureInfo.CurrentCulture.TextInfo.ToTitleCase(comment.Author.Role.ToString().ToLower())})</b>" : "")</h5>
<h6>@comment.PublishDate.ToString("g")</h6>
<p class="card-text">@Html.Raw(comment.Description.Replace(Environment.NewLine, "<br />"))</p>
</div>
@if (comment.Responses.Count != 0)
{
foreach (var response in comment.Responses)
{
DisplayComment(response, level + 1);
}
}
</div>
</div>
}
}

View File

@@ -0,0 +1,25 @@
using Microsoft.AspNetCore.Authorization;
using Microsoft.AspNetCore.Mvc;
using Microsoft.AspNetCore.Mvc.RazorPages;
using Logic;
using Models;
using Data;
namespace WebApp.Pages
{
[Authorize]
public class ComplaintModel : PageModel
{
private readonly IComplaintRepository _complaintRepository;
public ComplaintModel(IComplaintRepository complaintRepository)
{
_complaintRepository = complaintRepository;
}
public void OnGet(int id)
{
ComplaintManager complaintManager = new ComplaintManager(_complaintRepository);
ViewData.Add("complaint", complaintManager.GetComplaintById(id));
}
}
}

View File

@@ -14,7 +14,7 @@
</div>
<div class="mb-3">
<label asp-for="Complaint.Severity" class="form-label">Severity: </label>
<select asp-for="Complaint.Severity" asp-items="Html.GetEnumSelectList<ComplaintSeverity>()" />
<select asp-for="Complaint.Severity" asp-items="Html.GetEnumSelectList<ComplaintSeverity>()"></select>
</div>
<div class="mb-3">
<label asp-for="Complaint.Description" class="form-label">Description: </label>

View File

@@ -1,4 +1,3 @@
using Data;
using Logic;
using Microsoft.AspNetCore.Authorization;
using Microsoft.AspNetCore.Mvc;
@@ -13,13 +12,20 @@ namespace WebApp.Pages
{
[BindProperty]
public Complaint Complaint { get; set; }
private readonly IComplaintRepository _complaintRepository;
private readonly IUserRepository _userRepository;
public CreateComplaintModel(IComplaintRepository complaintRepository, IUserRepository userRepository)
{
_complaintRepository = complaintRepository;
_userRepository = userRepository;
}
public void OnGet()
{
}
public IActionResult OnPost()
{
ComplaintManager complaintManager = new ComplaintManager(new ComplaintRepository());
UserManager userManager = new UserManager(new UserRepository());
ComplaintManager complaintManager = new ComplaintManager(_complaintRepository);
UserManager userManager = new UserManager(_userRepository);
User user = userManager.GetUserById(int.Parse(User.FindFirstValue("id")));
complaintManager.CreateComplaint(Complaint.Title, Complaint.Description, user, DateTime.Now, ComplaintStatus.FILED, Complaint.Severity);
return RedirectToPage("Complaints");