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

Commit

Permalink
Add/Remove student to an activity in session
Browse files Browse the repository at this point in the history
  • Loading branch information
thientm27 committed Aug 22, 2023
1 parent c3027a9 commit 0fde5a7
Show file tree
Hide file tree
Showing 4 changed files with 91 additions and 23 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@
@model ClubMemberShip.Web.Pages.PageUser.StudentActivity.AddMemberToActivityModel

@{
Layout = "Shared/_UserLayout";
ViewData["Title"] = "AddMemberToActivity";
}

Expand Down Expand Up @@ -58,7 +59,9 @@
@Html.DisplayFor(modelItem => item.Major.Name)
</td>
<td>
<a asp-page="./AddMemberToActivity" asp-page-handler="Add" asp-route-id="@item.Id">Add</a>
<form asp-page="./AddMemberToActivity" method="post" asp-page-handler="Add" asp-route-id="@item.Id">
<button type="submit">Add</button>
</form>
</td>
</tr>
}
Expand Down Expand Up @@ -121,19 +124,14 @@
@Html.DisplayFor(modelItem => item.Major.Name)
</td>
<td>
<a asp-page="./AddMemberToActivity" asp-page-handler="Remove" asp-route-id="@item.Id">Remove</a>
<form asp-page="./AddMemberToActivity" method="post" asp-page-handler="Remove" asp-route-id="@item.Id">
<button type="submit">Remove</button>
</form>
</td>
</tr>
}
</tbody>
</table>
<div>
<ul class="pagination">
@for (var i = 1; i <= Model.TotalPages2; i++)
{
<li class="page-item @(i == Model.PageIndex2 ? "active" : "")">
<a asp-page="./AddMemberToActivity" asp-route-PageIndex2="@i" class="page-link">@i</a>
</li>
}
</ul>
</div>
<form asp-page="./AddMemberToActivity" method="post" asp-page-handler="Submit">
<button type="submit">Submit</button>
</form>
Original file line number Diff line number Diff line change
Expand Up @@ -19,9 +19,6 @@ public AddMemberToActivityModel(IStudentServices studentServices)
[BindProperty(SupportsGet = true)] public int PageIndex1 { get; set; } = 1;
public int PageSize1 { get; set; } = 3;
public int TotalPages1;
[BindProperty(SupportsGet = true)] public int PageIndex2 { get; set; } = 1;
public int PageSize2 { get; set; } = 3;
public int TotalPages2;

public IActionResult OnGet()
{
Expand All @@ -31,17 +28,77 @@ public IActionResult OnGet()
return RedirectToPage("/Login");
}

var activity = HttpContext.Session.GetObjectFromJson<ClubActivity>("ClubActivity");
AddedStudent = new List<Student>();
var sessionData = HttpContext.Session.GetObjectFromJson<AddedStudentObject>("AddedStudent") ??
new AddedStudentObject();

var data = _studentServices.GetPagination(PageIndex1 - 1, PageSize1);
TotalPages1 = data.TotalPagesCount;
NotAddStudent = (HttpContext.Session.GetObjectFromJson<IList<Student>>("NotAddStudent") ??
data.Items.ToList());
var ignoreList = sessionData.List;
ignoreList.Add(studentLogin.Id);
foreach (var o in ignoreList)
{
AddedStudent.Add(_studentServices.GetStudentById(o)!);
}

AddedStudent = HttpContext.Session.GetObjectFromJson<IList<Student>>("NotAddStudent") ??
new List<Student>();

var data = _studentServices.GetPaginationIgnoreId(PageIndex1 - 1, PageSize1, ignoreList);
TotalPages1 = data.TotalPagesCount;
NotAddStudent = data.Items.ToList();

return Page();
}

public IActionResult OnPostAdd(int? id)
{
var studentLogin = _studentServices.GetById(HttpContext.Session.GetString("User"));
if (studentLogin == null)
{
return RedirectToPage("/Login");
}

var studentAdded = _studentServices.GetStudentById((int)id);

Check warning on line 58 in Clup-MemberShip/ClubMemberShip.Present/Pages/PageUser/StudentActivity/AddMemberToActivity.cshtml.cs

View workflow job for this annotation

GitHub Actions / build

Nullable value type may be null.
if (studentAdded == null)
{
return NotFound();
}

var sessionData = HttpContext.Session.GetObjectFromJson<AddedStudentObject>("AddedStudent") ??
new AddedStudentObject();
sessionData.List.Add((int)id);
HttpContext.Session.SetObjectAsJson("AddedStudent", sessionData);

return OnGet();
}

public IActionResult OnPostRemove(int? id)
{
var studentLogin = _studentServices.GetById(HttpContext.Session.GetString("User"));
if (studentLogin == null)
{
return RedirectToPage("/Login");
}

var studentAdded = _studentServices.GetStudentById((int)id);

Check warning on line 80 in Clup-MemberShip/ClubMemberShip.Present/Pages/PageUser/StudentActivity/AddMemberToActivity.cshtml.cs

View workflow job for this annotation

GitHub Actions / build

Nullable value type may be null.
if (studentAdded == null)
{
return NotFound();
}

if (id == studentLogin.Id) // remove current
{
return OnGet();
}

var sessionData = HttpContext.Session.GetObjectFromJson<AddedStudentObject>("AddedStudent") ??
new AddedStudentObject();
sessionData.List.Remove((int)id);
HttpContext.Session.SetObjectAsJson("AddedStudent", sessionData);

return OnGet();
}
}

public class AddedStudentObject
{
public List<int> List { get; set; } = new();
}
}
4 changes: 3 additions & 1 deletion Clup-MemberShip/ClubMemberShip.Service/IStudentServices.cs
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
using ClubMemberShip.Repo.Models;
using ClubMemberShip.Repo.Utils;
using ClubMemberShip.Service.Service;

namespace ClubMemberShip.Service
Expand All @@ -7,12 +8,13 @@ public interface IStudentServices : IGenericService<Student>
{
public List<Major>? GetMajors();
public List<Grade>? GetGrades();
public Student? GetStudentById(int id);
public List<Student> GetClubOfStudent(int id);
public Membership? GetMemberShipById(int id);
public Membership? GetMemberShipByClubIdAndStudentId(int studentId, int clubId);
public bool CheckRegisterToClub(int studentId, int clubId);
public Membership? RegisterToClub(Membership membership, bool save = true);

public Pagination<Student> GetPaginationIgnoreId(int pageIndex, int pageSize, List<int> listIgnore);
public Result Register(Student student);
public int Login(string id );
}
Expand Down
11 changes: 11 additions & 0 deletions Clup-MemberShip/ClubMemberShip.Service/Service/StudentService.cs
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,11 @@ public StudentService(IUnitOfWork unitOfWork) : base(unitOfWork)
return UnitOfWork.GradeRepo.Get().ToList();
}

public Student? GetStudentById(int id)
{
return Get().FirstOrDefault(o => o.Id == id);
}

public List<Student> GetClubOfStudent(int id)
{
return UnitOfWork.StudentRepo.Get(filter: student => student.Id == id, includeProperties: "Major,Grade")
Expand Down Expand Up @@ -96,6 +101,12 @@ public bool CheckRegisterToClub(int studentId, int clubId)
return result;
}

public Pagination<Student> GetPaginationIgnoreId(int pageIndex, int pageSize, List<int> listIgnore)
{
var listEntities = UnitOfWork.StudentRepo.Get(filter: o => !listIgnore.Contains(o.Id));
return UnitOfWork.StudentRepo.ToPagination(listEntities, pageIndex, pageSize);
}

public List<Major>? GetMajors()
{
return UnitOfWork.MajorRepo.Get().ToList();
Expand Down

0 comments on commit 0fde5a7

Please sign in to comment.