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;