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

@@ -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;