view announcements, users by page number

This commit is contained in:
Dimitar Byalkov
2023-03-29 22:46:46 +02:00
parent f858c47ff7
commit 5c0eb222a4
14 changed files with 141 additions and 21 deletions

View File

@@ -25,6 +25,10 @@ namespace StudentHouseDashboard.Managers
{
return userRepository.GetUserById(id);
}
public List<User> GetUsersByPage(int? p, int? c)
{
return userRepository.GetUsersByPage(p, c);
}
public bool CreateUser(string name, string password, UserRole role)
{
return userRepository.CreateUser(name, password, role);

View File

@@ -7,7 +7,7 @@ namespace StudentHouseDashboard.Models
{
public class Announcement : GenericMessage, IVotable
{
public Announcement(User author, string description, string title, DateTime publishDate, bool isImportant, bool isSticky) : base(author, description, title, publishDate)
public Announcement(int id, User author, string description, string title, DateTime publishDate, bool isImportant, bool isSticky) : base(id, author, description, title, publishDate)
{
IsImportant = isImportant;
IsSticky = isSticky;

View File

@@ -7,7 +7,7 @@ namespace StudentHouseDashboard.Models
{
public class Comment : GenericMessage, IVotable
{
public Comment(User author, string description, string title, DateTime publishDate) : base(author, description, title, publishDate)
public Comment(int id, User author, string description, string title, DateTime publishDate) : base(id, author, description, title, publishDate)
{
}

View File

@@ -7,7 +7,7 @@ namespace StudentHouseDashboard.Models
{
public class Complaint : GenericMessage
{
public Complaint(User author, string description, string title, DateTime publishDate, ComplaintStatus status, ComplaintSeverity severity) : base(author, description, title, publishDate)
public Complaint(int id, User author, string description, string title, DateTime publishDate, ComplaintStatus status, ComplaintSeverity severity) : base(id, author, description, title, publishDate)
{
Status = status;
Severity = severity;

View File

@@ -7,7 +7,7 @@ namespace StudentHouseDashboard.Models
{
public class Event : GenericMessage
{
public Event(User author, string description, string title, DateTime publishDate, DateTime startDate, DateTime endDate) : base(author, description, title, publishDate)
public Event(int id, User author, string description, string title, DateTime publishDate, DateTime startDate, DateTime endDate) : base(id, author, description, title, publishDate)
{
StartDate = startDate;
EndDate = endDate;

View File

@@ -13,14 +13,20 @@ namespace StudentHouseDashboard
private string title;
private DateTime publishDate;
protected GenericMessage(User author, string description, string title, DateTime publishDate)
protected GenericMessage(int id, User author, string description, string title, DateTime publishDate)
{
ID = id;
Author = author;
Description = description;
Title = title;
PublishDate = publishDate;
}
public int ID
{
get; private set;
}
public User Author
{
get => author;

View File

@@ -15,7 +15,7 @@ namespace StudentHouseDashboard.Models
public User(int id, string name, string password, UserRole role)
{
Id = id;
ID = id;
Name = name;
Password = password;
Role = role;
@@ -24,7 +24,7 @@ namespace StudentHouseDashboard.Models
{
}
public int Id
public int ID
{
get; private set;
}

View File

@@ -29,7 +29,7 @@ namespace StudentHouseDashboard.Repositories
}
public List<Announcement> GetAllAnnouncements()
{
var announcements = new List<Announcement>();
List<Announcement> announcements = new List<Announcement>();
UserManager userManager = new UserManager();
using (SqlConnection conn = CreateConnection())
{
@@ -40,7 +40,8 @@ namespace StudentHouseDashboard.Repositories
while (reader.Read())
{
// ID, Name, Password, Role
announcements.Add(new Announcement(userManager.GetUserById(Convert.ToInt32(reader["ID"])),
announcements.Add(new Announcement(Convert.ToInt32(reader["ID"]),
userManager.GetUserById(Convert.ToInt32(reader["Author"])),
reader["Description"].ToString(), reader["Title"].ToString(),
(DateTime)reader["PublishDate"], (bool)reader["IsImportant"],
(bool)reader["IsSticky"]));
@@ -49,5 +50,36 @@ namespace StudentHouseDashboard.Repositories
}
return announcements;
}
public List<Announcement> GetAnnouncementsByPage(int? p, int? c)
{
List<Announcement> announcements = new List<Announcement>();
UserManager userManager = new UserManager();
if (c == null)
{
c = 10;
}
if (p == null)
{
p = 0;
}
using (SqlConnection conn = CreateConnection())
{
string sql = "SELECT TOP(@count) * FROM Users WHERE ID > @start;";
SqlCommand sqlCommand = new SqlCommand(sql, conn);
sqlCommand.Parameters.AddWithValue("@start", p * c);
sqlCommand.Parameters.AddWithValue("@count", c);
var reader = sqlCommand.ExecuteReader();
while (reader.Read())
{
announcements.Add(new Announcement(Convert.ToInt32(reader["ID"]),
userManager.GetUserById(Convert.ToInt32(reader["Author"])),
reader["Description"].ToString(), reader["Title"].ToString(),
(DateTime)reader["PublishDate"], (bool)reader["IsImportant"],
(bool)reader["IsSticky"]));
}
}
return announcements;
}
}
}

View File

@@ -48,7 +48,7 @@ namespace StudentHouseDashboard.Repositories
(UserRole)reader["Role"])
);
}
conn.Close();
}
return users;
}
@@ -62,6 +62,7 @@ namespace StudentHouseDashboard.Repositories
var reader = cmd.ExecuteReader();
reader.Read();
// ID, Name, Password, Role
return new User(Convert.ToInt32(reader["ID"]),
reader["Name"].ToString(),
@@ -69,6 +70,33 @@ namespace StudentHouseDashboard.Repositories
(UserRole)reader["Role"]);
}
}
public List<User> GetUsersByPage(int? p, int? c)
{
List<User> users = new List<User>();
if (c == null)
{
c = 0;
}
if (p == null)
{
p = 0;
}
using (SqlConnection conn = CreateConnection())
{
string sql = "SELECT TOP(@count) * FROM Users WHERE ID > @start;";
SqlCommand sqlCommand = new SqlCommand(sql, conn);
sqlCommand.Parameters.AddWithValue("@start", p * c);
sqlCommand.Parameters.AddWithValue("@count", c);
var reader = sqlCommand.ExecuteReader();
while (reader.Read())
{
users.Add(new User(Convert.ToInt32(reader["ID"]), reader["Name"].ToString(),
reader["Password"].ToString(), (UserRole)reader["Role"]));
}
}
return users;
}
public bool CreateUser(string name, string password, UserRole role)
{
using (SqlConnection conn = CreateConnection())
@@ -79,6 +107,7 @@ namespace StudentHouseDashboard.Repositories
cmd.Parameters.AddWithValue("@pass", password);
cmd.Parameters.AddWithValue("@role", (int)role);
int writer = cmd.ExecuteNonQuery();
if (writer == 1)
{
return true;
@@ -99,6 +128,7 @@ namespace StudentHouseDashboard.Repositories
cmd.Parameters.AddWithValue("@role", (int)role);
cmd.Parameters.AddWithValue("@id", id);
int writer = cmd.ExecuteNonQuery();
if (writer == 1)
{
return true;
@@ -116,6 +146,7 @@ namespace StudentHouseDashboard.Repositories
SqlCommand cmd = new SqlCommand(sql, conn);
cmd.Parameters.AddWithValue("@id", id);
int writer = cmd.ExecuteNonQuery();
if (writer == 1)
{
return true;

View File

@@ -1,4 +1,23 @@
@page
@using StudentHouseDashboard.Models;
@model WebApp.Pages.AnnouncementModel
@{
User user = (User)ViewData["user"];
ViewData["Title"] = $"User {user.Name}";
}
<h1>@user.Name</h1>
<div class="container">
<div class="row">
<div class="col">
<p>Name: </p>
<p>Password: </p>
<p>Role: </p>
</div>
<div class="col">
<p>@user.Name</p>
<p>@user.Password</p>
<p>@user.Role.ToString()</p>
</div>
</div>
</div>

View File

@@ -1,12 +1,15 @@
using Microsoft.AspNetCore.Mvc;
using Microsoft.AspNetCore.Mvc.RazorPages;
using StudentHouseDashboard.Managers;
namespace WebApp.Pages
{
public class AnnouncementModel : PageModel
{
public void OnGet()
public void OnGet(int id)
{
UserManager userManager = new UserManager();
ViewData.Add("user", userManager.GetUserById(id));
}
}
}

View File

@@ -1,15 +1,33 @@
@page
@using StudentHouseDashboard.Models;
@model WebApp.Pages.AnnouncementsModel
@{
ViewData["Title"] = "Announcements";
List<User> users = (List<User>)ViewData["users"];
}
<div class="card" style="width: 18rem;">
<div class="card-body">
<h5 class="card-title">Card title</h5>
<h6 class="card-subtitle mb-2 text-muted">Card subtitle</h6>
<p class="card-text">Some quick example text to build on the card title and make up the bulk of the card's content.</p>
<a href="#" class="card-link">Card link</a>
<a href="#" class="card-link">Another link</a>
@foreach (User user in users)
{
<div class="card" style="display:inline-block; width: 18rem;">
<div class="card-body">
<h5 class="card-title">@user.Role.ToString()</h5>
<h6 class="card-subtitle mb-2 text-muted">@user.Name</h6>
<p class="card-text">@user.Password</p>
<a href="./Announcement?id=@user.ID" class="btn btn-primary">More details</a>
</div>
</div>
</div>
}
<nav aria-label="Page navigation example">
<ul class="pagination justify-content-center">
<li class="page-item disabled">
<a class="page-link" href="#" tabindex="-1">Previous</a>
</li>
<li class="page-item"><a class="page-link" href="#">1</a></li>
<li class="page-item"><a class="page-link" href="#">2</a></li>
<li class="page-item"><a class="page-link" href="#">3</a></li>
<li class="page-item">
<a class="page-link" href="#">Next</a>
</li>
</ul>
</nav>

View File

@@ -1,12 +1,19 @@
using Microsoft.AspNetCore.Mvc;
using Microsoft.AspNetCore.Mvc.RazorPages;
using StudentHouseDashboard.Managers;
namespace WebApp.Pages
{
public class AnnouncementsModel : PageModel
{
public void OnGet()
public AnnouncementManager AnnouncementManager { get; set; }
public UserManager UserManager { get; set; }
public void OnGet(int? p, int? c)
{
UserManager = new UserManager();
ViewData.Add("users", UserManager.GetUsersByPage(p, c));
ViewData.Add("page", p);
ViewData.Add("allCount", UserManager.GetAllUsers().Count());
}
}
}

View File

@@ -23,7 +23,7 @@ namespace WebApp.Pages
if (item.Name == MyUser.Name && BCrypt.Net.BCrypt.Verify(MyUser.Password, item.Password))
{
MyUser = item;
ViewData["confirm"] = $"Welcome, {MyUser.Name}! {MyUser.Id}, {MyUser.Password}, {MyUser.Role}";
ViewData["confirm"] = $"Welcome, {MyUser.Name}! {MyUser.ID}, {MyUser.Password}, {MyUser.Role}";
}
}