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

Commit

Permalink
Edit member in club board
Browse files Browse the repository at this point in the history
  • Loading branch information
thientm27 committed Aug 23, 2023
1 parent cc7b7ab commit 31df1a9
Show file tree
Hide file tree
Showing 6 changed files with 333 additions and 3 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -9,10 +9,15 @@
<h1>Edit</h1>

<h4>ClubBoard</h4>
<form method="post" asp-page-handler="Change" asp-route-boardId="@Model.ClubBoard.Id">
<div class="form-group">
<input type="submit" value="Change Member" class="btn btn-primary"/>
</div>
</form>
<hr/>
<div class="row">
<div class="col-md-4">
<form method="post">
<form method="post" asp-page-handler="Update">
<div asp-validation-summary="ModelOnly" class="text-danger"></div>
<input type="hidden" asp-for="ClubBoard.Id"/>
<div class="form-group">
Expand Down Expand Up @@ -102,6 +107,17 @@
}
</tbody>
</table>
<div>
<ul class="pagination">
@for (var i = 1; i <= Model.TotalPages1; i++)
{
<li class="page-item @(i == Model.PageIndex1 ? "active" : "")">
<a asp-page="./Create2" asp-route-PageIndex1="@i" class="page-link">@i</a>
</li>
}
</ul>
</div>


<div>
<a asp-page="./Index" asp-route-clubId="@Model.ClubBoard.ClubId">Back to List</a>
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@
using Microsoft.AspNetCore.Mvc.Rendering;
using ClubMemberShip.Repo.Models;
using ClubMemberShip.Service;
using ClubMemberShip.Web.Pages.PageUser.StudentActivity;

namespace ClubMemberShip.Web.Pages.PageUser.ClubBoardManage
{
Expand All @@ -11,23 +12,31 @@ public class EditModel : PageModel
private IClubBoardService _clubBoardService;
private IClubServices _clubServices;
private IMemberRoleService _memberRoleService;
private IStudentServices _studentServices;
public IList<Student> NotAddStudent { get; set; } = default!;
[BindProperty(SupportsGet = true)] public int PageIndex1 { get; set; } = 1;
public int PageSize1 { get; set; } = 3;
public int TotalPages1;

public EditModel(IClubBoardService clubBoardService, IClubServices clubServices,
IMemberRoleService memberRoleService)
IMemberRoleService memberRoleService, IStudentServices studentServices)
{
_clubBoardService = clubBoardService;
_clubServices = clubServices;
_memberRoleService = memberRoleService;
_studentServices = studentServices;
}

[BindProperty] public ClubBoard ClubBoard { get; set; } = default!;

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

if (id == null)
{
return NotFound();
Expand All @@ -49,10 +58,30 @@ public IActionResult OnGet(int? id)
return Page();
}

public IActionResult OnPost()
public IActionResult OnPostUpdate()
{
_clubBoardService.Update(ClubBoard);
return RedirectToPage("./Index", new { clubId = ClubBoard.ClubId });
}

public IActionResult OnPostChange(int boardId)
{
var clubboard = _clubBoardService.GetById(boardId);
HttpContext.Session.SetObjectAsJson("ClubBoard", clubboard);

var data = _memberRoleService.GetAllMemberOfBoard(boardId);
var listId = data.Select(o => o.Id);
var addedStd = new AddedStudentObject();

foreach (var o in listId)
{
addedStd.List.Add(o);
}


HttpContext.Session.SetObjectAsJson("AddedStudent", addedStd);

return RedirectToPage("./Edit2");
}
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,133 @@
@page
@model ClubMemberShip.Web.Pages.PageUser.ClubBoardManage.Edit2

@{
ViewData["Title"] = "Edit2";
}

<h1>Change member of your club board</h1>

<table class="table">
<thead>
<tr>
<th>
@Html.DisplayNameFor(model => model.NotAddStudent[0].Code)
</th>
<th>
@Html.DisplayNameFor(model => model.NotAddStudent[0].Name)
</th>
<th>
@Html.DisplayNameFor(model => model.NotAddStudent[0].Address)
</th>
<th>
@Html.DisplayNameFor(model => model.NotAddStudent[0].DateOfBirth)
</th>
<th>
@Html.DisplayNameFor(model => model.NotAddStudent[0].Grade)
</th>
<th>
@Html.DisplayNameFor(model => model.NotAddStudent[0].Major)
</th>
<th></th>
</tr>
</thead>
<tbody>
@foreach (var item in Model.NotAddStudent)
{
<tr>
<td>
@Html.DisplayFor(modelItem => item.Code)
</td>
<td>
@Html.DisplayFor(modelItem => item.Name)
</td>
<td>
@Html.DisplayFor(modelItem => item.Address)
</td>
<td>
@Html.DisplayFor(modelItem => item.DateOfBirth)
</td>
<td>
@Html.DisplayFor(modelItem => item.Grade.GradeYear.Year)
</td>
<td>
@Html.DisplayFor(modelItem => item.Major.Name)
</td>
<td>
<form asp-page="./Edit2" method="post" asp-page-handler="Add" asp-route-id="@item.Id">
<button type="submit">Add</button>
</form>
</td>
</tr>
}
</tbody>
</table>
<div>
<ul class="pagination">
@for (var i = 1; i <= Model.TotalPages1; i++)
{
<li class="page-item @(i == Model.PageIndex1 ? "active" : "")">
<a asp-page="./Edit2" asp-route-PageIndex1="@i" class="page-link">@i</a>
</li>
}
</ul>
</div>
<table class="table">
<thead>
<tr>
<th>
@Html.DisplayNameFor(model => model.NotAddStudent[0].Code)
</th>
<th>
@Html.DisplayNameFor(model => model.NotAddStudent[0].Name)
</th>
<th>
@Html.DisplayNameFor(model => model.NotAddStudent[0].Address)
</th>
<th>
@Html.DisplayNameFor(model => model.NotAddStudent[0].DateOfBirth)
</th>
<th>
@Html.DisplayNameFor(model => model.NotAddStudent[0].Grade)
</th>
<th>
@Html.DisplayNameFor(model => model.NotAddStudent[0].Major)
</th>
<th></th>
</tr>
</thead>
<tbody>
@foreach (var item in Model.AddedStudent)
{
<tr>
<td>
@Html.DisplayFor(modelItem => item.Code)
</td>
<td>
@Html.DisplayFor(modelItem => item.Name)
</td>
<td>
@Html.DisplayFor(modelItem => item.Address)
</td>
<td>
@Html.DisplayFor(modelItem => item.DateOfBirth)
</td>
<td>
@Html.DisplayFor(modelItem => item.Grade.GradeYear.Year)
</td>
<td>
@Html.DisplayFor(modelItem => item.Major.Name)
</td>
<td>
<form asp-page="./Edit2" method="post" asp-page-handler="Remove" asp-route-id="@item.Id">
<button type="submit">Remove</button>
</form>
</td>
</tr>
}
</tbody>
</table>

<form asp-page="./Edit2" method="post" asp-page-handler="Submit">
<button type="submit">Submit</button>
</form>
Original file line number Diff line number Diff line change
@@ -0,0 +1,133 @@
using Microsoft.AspNetCore.Mvc.RazorPages;
using ClubMemberShip.Repo.Models;
using ClubMemberShip.Service;
using ClubMemberShip.Web.Pages.PageUser.StudentActivity;
using Microsoft.AspNetCore.Mvc;

namespace ClubMemberShip.Web.Pages.PageUser.ClubBoardManage
{
public class Edit2 : PageModel
{
private readonly IStudentServices _studentServices;
private readonly IClubServices _clubServices;
private readonly IClubBoardService _clubBoardService;
private readonly IMemberRoleService _memberRoleService;


public Edit2(IStudentServices studentServices, IClubServices clubServices,
IClubBoardService clubBoardService, IMemberRoleService memberRoleService)
{
_studentServices = studentServices;
_clubServices = clubServices;
_clubBoardService = clubBoardService;
_memberRoleService = memberRoleService;
}


public IList<Student> NotAddStudent { get; set; } = default!;
public IList<Student> AddedStudent { get; set; } = default!;
[BindProperty(SupportsGet = true)] public int PageIndex1 { get; set; } = 1;
public int PageSize1 { get; set; } = 3;
public int TotalPages1;

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

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

var ignoreList = sessionData.List;
foreach (var o in ignoreList)
{
AddedStudent.Add(_studentServices.GetStudentById(o)!);
}

var clubBoard = HttpContext.Session.GetObjectFromJson<ClubBoard>("ClubBoard");
if (clubBoard == null)
{
return RedirectToPage("./Index", new { clubId = clubBoard.ClubId });
}

var data = _clubServices.GetStudentInClub(PageIndex1 - 1, PageSize1, clubBoard.ClubId, 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);
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);
if (studentAdded == null)
{
return NotFound();
}

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

return OnGet();
}

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

var addedStudent = HttpContext.Session.GetObjectFromJson<AddedStudentObject>("AddedStudent") ??
new AddedStudentObject();
var clubBoard = HttpContext.Session.GetObjectFromJson<ClubBoard>("ClubBoard");

if (clubBoard == null)
{
return RedirectToPage("./Create");
}


var originList = _memberRoleService.GetAllMemberOfBoard(clubBoard.Id);
var originIds = originList.Select(o => o.Id).ToList();
_memberRoleService.RemoveMultipleMember(clubBoard.ClubId, clubBoard.Id, originIds);
_memberRoleService.AddMultipleMember(clubBoard.ClubId, clubBoard.Id, addedStudent.List);
return RedirectToPage("./Index", new { clubId = clubBoard.ClubId });
}
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@ namespace ClubMemberShip.Service;
public interface IMemberRoleService : IGenericService<MemberRole>
{
public void AddMultipleMember(int clubId, int clubBoardId, List<int> studentId);
public void RemoveMultipleMember(int clubId, int clubBoardId, List<int> studentId);
public List<Student> GetAllMemberOfBoard(int boardId);
public Pagination<Student> GetPaginationAllMemberOfBoard(int pageIndex, int pageSize, int boardId);
}
Loading

0 comments on commit 31df1a9

Please sign in to comment.